@@ -2,7 +2,7 @@ use candid::{Decode, Encode, Nat};
2
2
use ic_base_types:: { CanisterId , PrincipalId } ;
3
3
use ic_icp_index:: {
4
4
GetAccountIdentifierTransactionsArgs , GetAccountIdentifierTransactionsResponse ,
5
- GetAccountIdentifierTransactionsResult , Status , TransactionWithId ,
5
+ GetAccountIdentifierTransactionsResult , SettledTransaction , SettledTransactionWithId , Status ,
6
6
} ;
7
7
use ic_icrc1_index_ng:: GetAccountTransactionsArgs ;
8
8
use ic_ledger_canister_core:: archive:: ArchiveOptions ;
@@ -13,7 +13,7 @@ use ic_state_machine_tests::StateMachine;
13
13
use icp_ledger:: {
14
14
AccountIdentifier , GetBlocksArgs , QueryEncodedBlocksResponse , MAX_BLOCKS_PER_REQUEST ,
15
15
} ;
16
- use icp_ledger:: { FeatureFlags , LedgerCanisterInitPayload , Memo , Operation , Transaction } ;
16
+ use icp_ledger:: { FeatureFlags , LedgerCanisterInitPayload , Memo , Operation } ;
17
17
use icrc_ledger_types:: icrc1:: account:: Account ;
18
18
use icrc_ledger_types:: icrc1:: transfer:: { BlockIndex , TransferArg , TransferError } ;
19
19
use icrc_ledger_types:: icrc2:: approve:: { ApproveArgs , ApproveError } ;
@@ -323,6 +323,8 @@ fn get_account_identifier_transactions(
323
323
accountidentifier_txs
324
324
}
325
325
326
+ const SYNC_STEP_SECONDS : Duration = Duration :: from_secs ( 60 ) ;
327
+
326
328
// Helper function that calls tick on env until either
327
329
// the index canister has synced all the blocks up to the
328
330
// last one in the ledger or enough attempts passed and therefore
@@ -332,7 +334,7 @@ fn wait_until_sync_is_completed(env: &StateMachine, index_id: CanisterId, ledger
332
334
let mut num_blocks_synced = u64:: MAX ;
333
335
let mut chain_length = u64:: MAX ;
334
336
for _i in 0 ..MAX_ATTEMPTS {
335
- env. advance_time ( Duration :: from_secs ( 60 ) ) ;
337
+ env. advance_time ( SYNC_STEP_SECONDS ) ;
336
338
env. tick ( ) ;
337
339
num_blocks_synced = status ( env, index_id) . num_blocks_synced ;
338
340
chain_length = icp_get_blocks ( env, ledger_id) . len ( ) as u64 ;
@@ -344,22 +346,22 @@ fn wait_until_sync_is_completed(env: &StateMachine, index_id: CanisterId, ledger
344
346
}
345
347
346
348
#[ track_caller]
347
- fn assert_tx_eq ( tx1 : & Transaction , tx2 : & Transaction ) {
349
+ fn assert_tx_eq ( tx1 : & SettledTransaction , tx2 : & SettledTransaction ) {
348
350
assert_eq ! ( tx1. operation, tx2. operation) ;
349
351
assert_eq ! ( tx1. memo, tx2. memo) ;
350
- assert_eq ! ( tx1. operation , tx2. operation ) ;
351
- assert_eq ! ( tx1. operation , tx2. operation ) ;
352
+ assert_eq ! ( tx1. icrc1_memo , tx2. icrc1_memo ) ;
353
+ assert_eq ! ( tx1. timestamp , tx2. timestamp ) ;
352
354
}
353
355
354
356
// checks that two txs are equal minus the fields set by the ledger (e.g. timestamp)
355
357
#[ track_caller]
356
- fn assert_tx_with_id_eq ( tx1 : & TransactionWithId , tx2 : & TransactionWithId ) {
358
+ fn assert_tx_with_id_eq ( tx1 : & SettledTransactionWithId , tx2 : & SettledTransactionWithId ) {
357
359
assert_eq ! ( tx1. id, tx2. id, "id" ) ;
358
360
assert_tx_eq ( & tx1. transaction , & tx2. transaction ) ;
359
361
}
360
362
361
363
#[ track_caller]
362
- fn assert_txs_with_id_eq ( txs1 : Vec < TransactionWithId > , txs2 : Vec < TransactionWithId > ) {
364
+ fn assert_txs_with_id_eq ( txs1 : Vec < SettledTransactionWithId > , txs2 : Vec < SettledTransactionWithId > ) {
363
365
assert_eq ! (
364
366
txs1. len( ) ,
365
367
txs2. len( ) ,
@@ -442,6 +444,18 @@ fn test_archive_indexing() {
442
444
assert_ledger_index_parity ( env, ledger_id, index_id) ;
443
445
}
444
446
447
+ fn expected_block_timestamp ( phase : u32 , start_time : SystemTime ) -> TimeStamp {
448
+ TimeStamp :: from (
449
+ start_time
450
+ . checked_add (
451
+ SYNC_STEP_SECONDS
452
+ . checked_mul ( phase)
453
+ . expect ( "checked_mul should not overflow" ) ,
454
+ )
455
+ . expect ( "checked_add should not overflow" ) ,
456
+ )
457
+ }
458
+
445
459
#[ test]
446
460
fn test_get_account_identifier_transactions ( ) {
447
461
let mut initial_balances = HashMap :: new ( ) ;
@@ -454,22 +468,26 @@ fn test_get_account_identifier_transactions() {
454
468
let index_id = install_index ( env, ledger_id) ;
455
469
456
470
// List of the transactions that the test is going to add. This exists to make
457
- // the test easier to read
458
- let tx0 = TransactionWithId {
471
+ // the test easier to read. The transactions are executed in separate phases, where the block
472
+ // timestamp is a function of the phase.
473
+ let mut phase = 0u32 ;
474
+ let tx0 = SettledTransactionWithId {
459
475
id : 0u64 ,
460
- transaction : Transaction {
476
+ transaction : SettledTransaction {
461
477
operation : Operation :: Mint {
462
478
to : account ( 1 , 0 ) . into ( ) ,
463
479
amount : Tokens :: from_e8s ( 1_000_000_000_000_u64 ) ,
464
480
} ,
465
481
memo : Memo ( 0 ) ,
466
482
created_at_time : None ,
467
483
icrc1_memo : None ,
484
+ timestamp : expected_block_timestamp ( phase, env. time ( ) ) ,
468
485
} ,
469
486
} ;
470
- let tx1 = TransactionWithId {
487
+ phase = 1 ;
488
+ let tx1 = SettledTransactionWithId {
471
489
id : 1u64 ,
472
- transaction : Transaction {
490
+ transaction : SettledTransaction {
473
491
operation : Operation :: Transfer {
474
492
to : account ( 2 , 0 ) . into ( ) ,
475
493
from : account ( 1 , 0 ) . into ( ) ,
@@ -480,11 +498,13 @@ fn test_get_account_identifier_transactions() {
480
498
memo : Memo ( 0 ) ,
481
499
created_at_time : None ,
482
500
icrc1_memo : None ,
501
+ timestamp : expected_block_timestamp ( phase, env. time ( ) ) ,
483
502
} ,
484
503
} ;
485
- let tx2 = TransactionWithId {
504
+ phase = 2 ;
505
+ let tx2 = SettledTransactionWithId {
486
506
id : 2u64 ,
487
- transaction : Transaction {
507
+ transaction : SettledTransaction {
488
508
operation : Operation :: Transfer {
489
509
to : account ( 2 , 0 ) . into ( ) ,
490
510
from : account ( 1 , 0 ) . into ( ) ,
@@ -495,11 +515,12 @@ fn test_get_account_identifier_transactions() {
495
515
memo : Memo ( 0 ) ,
496
516
created_at_time : None ,
497
517
icrc1_memo : None ,
518
+ timestamp : expected_block_timestamp ( phase, env. time ( ) ) ,
498
519
} ,
499
520
} ;
500
- let tx3 = TransactionWithId {
521
+ let tx3 = SettledTransactionWithId {
501
522
id : 3u64 ,
502
- transaction : Transaction {
523
+ transaction : SettledTransaction {
503
524
operation : Operation :: Transfer {
504
525
to : account ( 1 , 1 ) . into ( ) ,
505
526
from : account ( 2 , 0 ) . into ( ) ,
@@ -510,17 +531,19 @@ fn test_get_account_identifier_transactions() {
510
531
memo : Memo ( 0 ) ,
511
532
created_at_time : None ,
512
533
icrc1_memo : None ,
534
+ timestamp : expected_block_timestamp ( phase, env. time ( ) ) ,
513
535
} ,
514
536
} ;
537
+ phase = 3 ;
515
538
let expires_at = env
516
539
. time ( )
517
540
. duration_since ( SystemTime :: UNIX_EPOCH )
518
541
. unwrap ( )
519
542
. as_nanos ( ) as u64
520
543
+ Duration :: from_secs ( 3600 ) . as_nanos ( ) as u64 ;
521
- let tx4 = TransactionWithId {
544
+ let tx4 = SettledTransactionWithId {
522
545
id : 4u64 ,
523
- transaction : Transaction {
546
+ transaction : SettledTransaction {
524
547
operation : Operation :: Approve {
525
548
from : account ( 1 , 0 ) . into ( ) ,
526
549
spender : account ( 4 , 4 ) . into ( ) ,
@@ -532,6 +555,7 @@ fn test_get_account_identifier_transactions() {
532
555
memo : Memo ( 0 ) ,
533
556
created_at_time : None ,
534
557
icrc1_memo : None ,
558
+ timestamp : expected_block_timestamp ( phase, env. time ( ) ) ,
535
559
} ,
536
560
} ;
537
561
@@ -643,16 +667,17 @@ fn test_get_account_transactions_start_length() {
643
667
) ;
644
668
}
645
669
let expected_txs: Vec < _ > = ( 0 ..10 )
646
- . map ( |i| TransactionWithId {
670
+ . map ( |i| SettledTransactionWithId {
647
671
id : i,
648
- transaction : Transaction {
672
+ transaction : SettledTransaction {
649
673
operation : Operation :: Mint {
650
674
to : account ( 1 , 0 ) . into ( ) ,
651
675
amount : Tokens :: from_e8s ( i * 10_000 ) ,
652
676
} ,
653
677
memo : Memo ( 0 ) ,
654
678
created_at_time : None ,
655
679
icrc1_memo : None ,
680
+ timestamp : TimeStamp :: from ( env. time ( ) ) ,
656
681
} ,
657
682
} )
658
683
. collect ( ) ;
@@ -731,7 +756,7 @@ fn test_get_account_identifier_transactions_pagination() {
731
756
}
732
757
733
758
let mut last_seen_txid = start;
734
- for TransactionWithId { id, transaction } in & res. transactions {
759
+ for SettledTransactionWithId { id, transaction } in & res. transactions {
735
760
// transactions ids must be unique and in descending order
736
761
if let Some ( last_seen_txid) = last_seen_txid {
737
762
assert ! ( * id < last_seen_txid) ;
@@ -740,14 +765,19 @@ fn test_get_account_identifier_transactions_pagination() {
740
765
741
766
// check the transaction itself
742
767
assert_tx_eq (
743
- & Transaction {
768
+ & SettledTransaction {
744
769
operation : Operation :: Mint {
745
770
to : account ( 1 , 0 ) . into ( ) ,
746
771
amount : Tokens :: from_e8s ( * id * 10_000 ) ,
747
772
} ,
748
773
memo : Memo ( 0 ) ,
749
774
created_at_time : None ,
750
775
icrc1_memo : None ,
776
+ timestamp : TimeStamp :: from (
777
+ env. time ( )
778
+ . checked_sub ( SYNC_STEP_SECONDS )
779
+ . expect ( "should not underflow" ) ,
780
+ ) ,
751
781
} ,
752
782
transaction,
753
783
) ;
0 commit comments