@@ -68,15 +68,19 @@ impl CardanoTransactionRepository {
68
68
) -> StdResult < Vec < CardanoTransactionRecord > > {
69
69
// Get the highest block number for the given immutable number.
70
70
// This is a temporary fix that will be removed when the retrieval is based on block number instead of immutable number.
71
- let block_number = self
71
+
72
+ if let Some ( block_number) = self
72
73
. get_highest_block_number_for_immutable_number ( beacon)
73
74
. await ?
74
- . unwrap_or ( 0 ) ;
75
- let provider = GetCardanoTransactionProvider :: new ( & self . connection ) ;
76
- let filters = provider. get_transaction_between_blocks_condition ( 0 ..block_number + 1 ) ;
77
- let transactions = provider. find ( filters) ?;
75
+ {
76
+ let provider = GetCardanoTransactionProvider :: new ( & self . connection ) ;
77
+ let filters = provider. get_transaction_between_blocks_condition ( 0 ..block_number + 1 ) ;
78
+ let transactions = provider. find ( filters) ?;
78
79
79
- Ok ( transactions. collect ( ) )
80
+ Ok ( transactions. collect ( ) )
81
+ } else {
82
+ Ok ( vec ! [ ] )
83
+ }
80
84
}
81
85
82
86
/// Return the [CardanoTransactionRecord] for the given transaction hash.
@@ -324,6 +328,8 @@ impl BlockRangeRootRetriever for CardanoTransactionRepository {
324
328
325
329
#[ cfg( test) ]
326
330
mod tests {
331
+ use mithril_common:: test_utils:: CardanoTransactionsBuilder ;
332
+
327
333
use crate :: database:: provider:: GetBlockRangeRootProvider ;
328
334
use crate :: database:: test_helper:: cardano_tx_db_connection;
329
335
use crate :: sqlite:: GetAllProvider ;
@@ -470,23 +476,20 @@ mod tests {
470
476
let connection = Arc :: new ( cardano_tx_db_connection ( ) . unwrap ( ) ) ;
471
477
let repository = CardanoTransactionRepository :: new ( connection) ;
472
478
473
- // Build transactions with block numbers from 20 to 40 and immutable file numbers from 12 to 14
474
- let cardano_transactions: Vec < CardanoTransactionRecord > = ( 20 ..=40 )
475
- . map ( |i| CardanoTransactionRecord {
476
- transaction_hash : format ! ( "tx-hash-{i}" ) ,
477
- block_number : i,
478
- slot_number : i * 100 ,
479
- block_hash : format ! ( "block-hash-{i}" ) ,
480
- immutable_file_number : i / 10 + 10 ,
481
- } )
479
+ let cardano_transactions: Vec < CardanoTransactionRecord > = CardanoTransactionsBuilder :: new ( )
480
+ . per_immutable_file ( 10 )
481
+ . first_immutable_file ( 120 )
482
+ . build_transactions ( 40 )
483
+ . iter ( )
484
+ . map ( |transaction| CardanoTransactionRecord :: from ( transaction. clone ( ) ) )
482
485
. collect ( ) ;
483
486
484
487
repository
485
488
. create_transactions ( cardano_transactions. clone ( ) )
486
489
. await
487
490
. unwrap ( ) ;
488
491
489
- let transaction_result = repository. get_transactions_up_to ( 12 ) . await . unwrap ( ) ;
492
+ let transaction_result = repository. get_transactions_up_to ( 120 ) . await . unwrap ( ) ;
490
493
let transaction_up_to_immutable_file_number_12 = cardano_transactions[ 0 ..10 ] . to_vec ( ) ;
491
494
assert_eq ! (
492
495
transaction_up_to_immutable_file_number_12,
@@ -497,7 +500,28 @@ mod tests {
497
500
let transaction_all = cardano_transactions[ ..] . to_vec ( ) ;
498
501
assert_eq ! ( transaction_all, transaction_result) ;
499
502
500
- let transaction_result = repository. get_transactions_up_to ( 9 ) . await . unwrap ( ) ;
503
+ let transaction_result = repository. get_transactions_up_to ( 90 ) . await . unwrap ( ) ;
504
+ assert_eq ! ( Vec :: <CardanoTransactionRecord >:: new( ) , transaction_result) ;
505
+ }
506
+
507
+ #[ tokio:: test]
508
+ async fn get_transactions_up_to_return_empty_list_when_no_record_found_with_provided_immutable_file_number (
509
+ ) {
510
+ let connection = Arc :: new ( cardano_tx_db_connection ( ) . unwrap ( ) ) ;
511
+ let repository = CardanoTransactionRepository :: new ( connection) ;
512
+
513
+ repository
514
+ . create_transactions ( vec ! [ CardanoTransaction :: new(
515
+ "tx-hash-123" . to_string( ) ,
516
+ 0 ,
517
+ 50 ,
518
+ "block-hash-0" ,
519
+ 99 ,
520
+ ) ] )
521
+ . await
522
+ . unwrap ( ) ;
523
+
524
+ let transaction_result = repository. get_transactions_up_to ( 90 ) . await . unwrap ( ) ;
501
525
assert_eq ! ( Vec :: <CardanoTransactionRecord >:: new( ) , transaction_result) ;
502
526
}
503
527
@@ -525,7 +549,7 @@ mod tests {
525
549
}
526
550
527
551
#[ tokio:: test]
528
- async fn repository_store_transactions_with_existing_hash_doesnt_erase_existing_data ( ) {
552
+ async fn repository_store_transactions_doesnt_erase_existing_data ( ) {
529
553
let connection = Arc :: new ( cardano_tx_db_connection ( ) . unwrap ( ) ) ;
530
554
let repository = CardanoTransactionRepository :: new ( connection) ;
531
555
@@ -871,21 +895,18 @@ mod tests {
871
895
let connection = Arc :: new ( cardano_tx_db_connection ( ) . unwrap ( ) ) ;
872
896
let repository = CardanoTransactionRepository :: new ( connection) ;
873
897
874
- // Build transactions with block numbers from 20 to 50
875
- let cardano_transactions: Vec < CardanoTransactionRecord > = ( 20 ..=50 )
876
- . map ( |i| CardanoTransactionRecord {
877
- transaction_hash : format ! ( "tx-hash-{i}" ) ,
878
- block_number : i,
879
- slot_number : i * 100 ,
880
- block_hash : format ! ( "block-hash-{i}" ) ,
881
- immutable_file_number : 1 ,
882
- } )
898
+ let cardano_transactions: Vec < CardanoTransactionRecord > = CardanoTransactionsBuilder :: new ( )
899
+ . blocks_per_block_range ( 15 )
900
+ . build_transactions ( 53 )
901
+ . iter ( )
902
+ . map ( |transaction| CardanoTransactionRecord :: from ( transaction. clone ( ) ) )
883
903
. collect ( ) ;
884
904
885
905
repository
886
906
. create_transactions ( cardano_transactions. clone ( ) )
887
907
. await
888
908
. unwrap ( ) ;
909
+ // Use by 'prune_transaction' to get the block_range of the highest block number
889
910
repository
890
911
. create_block_range_roots ( vec ! [ (
891
912
BlockRange :: from_block_number( 45 ) ,
@@ -911,6 +932,12 @@ mod tests {
911
932
. await
912
933
. unwrap ( ) ;
913
934
assert_eq ! ( Vec :: <CardanoTransactionRecord >:: new( ) , transaction_result) ;
935
+
936
+ let transaction_result = repository
937
+ . get_transactions_in_range_blocks ( 25 ..1000 )
938
+ . await
939
+ . unwrap ( ) ;
940
+ assert_eq ! ( 28 , transaction_result. len( ) ) ;
914
941
}
915
942
916
943
#[ tokio:: test]
0 commit comments