@@ -177,6 +177,23 @@ impl CardanoTransactionRepository {
177
177
}
178
178
}
179
179
180
+ /// Retrieve all the Block Range Roots in database up to the given end block number excluded.
181
+ pub async fn retrieve_block_range_roots_up_to (
182
+ & self ,
183
+ end_block_number : BlockNumber ,
184
+ ) -> StdResult < Box < dyn Iterator < Item = ( BlockRange , MKTreeNode ) > > > {
185
+ let provider = GetBlockRangeRootProvider :: new ( & self . connection ) ;
186
+ let filters = provider. get_up_to_block_number_condition ( end_block_number) ;
187
+ let block_range_roots = provider. find ( filters) ?;
188
+ let iterator = block_range_roots
189
+ . into_iter ( )
190
+ . map ( |record| -> ( BlockRange , MKTreeNode ) { record. into ( ) } )
191
+ . collect :: < Vec < _ > > ( ) // TODO: remove this collect when we should ba able return the iterator directly
192
+ . into_iter ( ) ;
193
+
194
+ Ok ( Box :: new ( iterator) )
195
+ }
196
+
180
197
#[ cfg( test) ]
181
198
pub ( crate ) async fn get_all ( & self ) -> StdResult < Vec < CardanoTransaction > > {
182
199
let provider = GetCardanoTransactionProvider :: new ( & self . connection ) ;
@@ -340,16 +357,8 @@ impl BlockRangeRootRetriever for CardanoTransactionRepository {
340
357
. get_highest_block_number_for_immutable_number ( up_to_beacon)
341
358
. await ?
342
359
. unwrap_or ( 0 ) ;
343
- let provider = GetBlockRangeRootProvider :: new ( & self . connection ) ;
344
- let filters = provider. get_up_to_block_number_condition ( block_number) ;
345
- let block_range_roots = provider. find ( filters) ?;
346
- let iterator = block_range_roots
347
- . into_iter ( )
348
- . map ( |record| -> ( BlockRange , MKTreeNode ) { record. into ( ) } )
349
- . collect :: < Vec < _ > > ( ) // TODO: remove this collect when we should ba able return the iterator directly
350
- . into_iter ( ) ;
351
360
352
- Ok ( Box :: new ( iterator ) )
361
+ self . retrieve_block_range_roots_up_to ( block_number ) . await
353
362
}
354
363
}
355
364
@@ -807,4 +816,73 @@ mod tests {
807
816
record
808
817
) ;
809
818
}
819
+
820
+ #[ tokio:: test]
821
+ async fn repository_retrieve_block_range_roots_up_to ( ) {
822
+ let connection = Arc :: new ( cardano_tx_db_connection ( ) . unwrap ( ) ) ;
823
+ let repository = CardanoTransactionRepository :: new ( connection) ;
824
+ let block_range_roots = vec ! [
825
+ (
826
+ BlockRange :: from_block_number( 15 ) ,
827
+ MKTreeNode :: from_hex( "AAAA" ) . unwrap( ) ,
828
+ ) ,
829
+ (
830
+ BlockRange :: from_block_number( 30 ) ,
831
+ MKTreeNode :: from_hex( "BBBB" ) . unwrap( ) ,
832
+ ) ,
833
+ (
834
+ BlockRange :: from_block_number( 45 ) ,
835
+ MKTreeNode :: from_hex( "CCCC" ) . unwrap( ) ,
836
+ ) ,
837
+ ] ;
838
+ repository
839
+ . store_block_range_roots ( block_range_roots. clone ( ) )
840
+ . await
841
+ . unwrap ( ) ;
842
+
843
+ // Retrieve with a block far higher than the highest block range - should return all
844
+ {
845
+ let retrieved_block_ranges = repository
846
+ . retrieve_block_range_roots_up_to ( 1000 )
847
+ . await
848
+ . unwrap ( ) ;
849
+ assert_eq ! (
850
+ block_range_roots,
851
+ retrieved_block_ranges. collect:: <Vec <_>>( )
852
+ ) ;
853
+ }
854
+ // Retrieve with a block bellow than the smallest block range - should return none
855
+ {
856
+ let retrieved_block_ranges = repository
857
+ . retrieve_block_range_roots_up_to ( 2 )
858
+ . await
859
+ . unwrap ( ) ;
860
+ assert_eq ! (
861
+ Vec :: <( BlockRange , MKTreeNode ) >:: new( ) ,
862
+ retrieved_block_ranges. collect:: <Vec <_>>( )
863
+ ) ;
864
+ }
865
+ // The given block is matched to the end (excluded) - should return the first of the three
866
+ {
867
+ let retrieved_block_ranges = repository
868
+ . retrieve_block_range_roots_up_to ( 45 )
869
+ . await
870
+ . unwrap ( ) ;
871
+ assert_eq ! (
872
+ vec![ block_range_roots[ 0 ] . clone( ) ] ,
873
+ retrieved_block_ranges. collect:: <Vec <_>>( )
874
+ ) ;
875
+ }
876
+ // Right after the end of the second block range - should return first two of the three
877
+ {
878
+ let retrieved_block_ranges = repository
879
+ . retrieve_block_range_roots_up_to ( 46 )
880
+ . await
881
+ . unwrap ( ) ;
882
+ assert_eq ! (
883
+ block_range_roots[ 0 ..=1 ] . to_vec( ) ,
884
+ retrieved_block_ranges. collect:: <Vec <_>>( )
885
+ ) ;
886
+ }
887
+ }
810
888
}
0 commit comments