@@ -22,6 +22,9 @@ pub trait TransactionStore: Send + Sync {
22
22
/// Get the highest known transaction beacon
23
23
async fn get_highest_beacon ( & self ) -> StdResult < Option < ChainPoint > > ;
24
24
25
+ /// Get the highest stored block range root bounds
26
+ async fn get_highest_block_range ( & self ) -> StdResult < Option < BlockRange > > ;
27
+
25
28
/// Store list of transactions
26
29
async fn store_transactions ( & self , transactions : Vec < CardanoTransaction > ) -> StdResult < ( ) > ;
27
30
@@ -130,15 +133,15 @@ impl CardanoTransactionsImporter {
130
133
}
131
134
132
135
async fn import_block_ranges ( & self , until : BlockNumber ) -> StdResult < ( ) > {
133
- let block_ranges = match self
134
- . transaction_store
135
- . get_block_interval_without_block_range_root ( )
136
- . await ?
137
- // .map(|range| BlockRange::all_block_ranges_in(BlockRange::start(range.start)..range.end) )
138
- . map ( |range| BlockRange :: all_block_ranges_in ( BlockRange :: start ( range . start ) ..= ( until ) ) )
139
- {
140
- // Everything is already computed
141
- None => return Ok ( ( ) ) ,
136
+ let block_ranges = match self . transaction_store . get_highest_block_range ( ) . await ? . map (
137
+ |highest_stored_block_range| {
138
+ BlockRange :: all_block_ranges_in (
139
+ BlockRange :: start ( highest_stored_block_range . end ) ..= ( until ) ,
140
+ )
141
+ } ,
142
+ ) {
143
+ // No block range root stored yet, start from the beginning
144
+ None => BlockRange :: all_block_ranges_in ( 0 ..= ( until ) ) ,
142
145
// Not enough block to form at least one block range
143
146
Some ( ranges) if ranges. is_empty ( ) => return Ok ( ( ) ) ,
144
147
Some ( ranges) => ranges,
@@ -602,8 +605,7 @@ mod tests {
602
605
SqliteConnectionPool :: build_from_connection ( connection) ,
603
606
) ) ) ;
604
607
605
- // Transactions for all blocks in the (15..=25) interval, meaning that the last blocks in
606
- // the block range (15..30) don't have transactions
608
+ // For the block range (15..=29) we only have transactions in the 10 first blocks (15..=24)
607
609
let blocks = build_blocks ( BlockRange :: LENGTH , 10 ) ;
608
610
let transactions = into_transactions ( & blocks) ;
609
611
repository. store_transactions ( transactions) . await . unwrap ( ) ;
@@ -632,31 +634,29 @@ mod tests {
632
634
async fn block_range_root_retrieves_only_strictly_required_transactions ( ) {
633
635
fn transactions_for_block ( range : Range < BlockNumber > ) -> StdResult < Vec < CardanoTransaction > > {
634
636
Ok ( build_blocks ( range. start , range. count ( ) as BlockNumber )
635
- . iter ( )
636
- . flat_map ( |b| b. clone ( ) . into_transactions ( ) )
637
+ . into_iter ( )
638
+ . flat_map ( |b| b. into_transactions ( ) )
637
639
. collect ( ) )
638
640
}
641
+ const HIGHEST_BLOCK_RANGE_START : BlockNumber = BlockRange :: LENGTH ;
642
+ const UP_TO_BLOCK_NUMBER : BlockNumber = BlockRange :: LENGTH * 5 ;
639
643
640
644
let importer = {
641
645
let mut store_mock = MockTransactionStore :: new ( ) ;
642
646
store_mock
643
- . expect_get_block_interval_without_block_range_root ( )
644
- // Specification of the interval without block range root
645
- // Note: in reality the lower bound will always be a multiple of BlockRange::LENGTH
646
- // since it's computed from the `block_range_root` table
647
- . returning ( || Ok ( Some ( ( BlockRange :: LENGTH + 2 ) ..( BlockRange :: LENGTH * 5 ) ) ) )
647
+ . expect_get_highest_block_range ( )
648
+ . returning ( || {
649
+ Ok ( Some ( BlockRange :: from_block_number (
650
+ HIGHEST_BLOCK_RANGE_START ,
651
+ ) ) )
652
+ } )
648
653
. once ( ) ;
649
654
store_mock
650
655
. expect_get_transactions_in_range ( )
651
- // Lower bound should be the block number that start after the last known block range end
652
- //
653
- // if it's not a multiple of BlockRange::LENGTH, it should be the start block number
654
- // of the block range that contains the end of the last known block range.
655
- //
656
- // Upper bound should be the block number of the highest transaction in a db that can be
657
- // included in a block range
656
+ // Lower bound should be the end block number of the last known block range
657
+ // Upper bound should be the block number provided to `import_block_ranges`
658
658
. withf ( |range| {
659
- BlockRangesSequence :: new ( BlockRange :: LENGTH ..=( BlockRange :: LENGTH * 5 ) )
659
+ BlockRangesSequence :: new ( HIGHEST_BLOCK_RANGE_START ..=UP_TO_BLOCK_NUMBER )
660
660
. contains ( range)
661
661
} )
662
662
. returning ( transactions_for_block) ;
@@ -670,9 +670,8 @@ mod tests {
670
670
)
671
671
} ;
672
672
673
- // todo: update this test after reworking expect_get_block_interval_without_block_range_root
674
673
importer
675
- . import_block_ranges ( BlockRange :: LENGTH * 5 )
674
+ . import_block_ranges ( UP_TO_BLOCK_NUMBER )
676
675
. await
677
676
. expect ( "Transactions Importer should succeed" ) ;
678
677
}
0 commit comments