Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rs/ledger_suite/icp/index/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,16 @@ fn get_account_identifier_transactions(
.max_results
.min(icp_ledger::max_blocks_per_request(&PrincipalId::from(msg_caller())) as u64)
.min(usize::MAX as u64) as usize;
// TODO: deal with the user setting start to u64::MAX
let start = arg.start.map_or(u64::MAX, |n| n);
let key = account_identifier_block_ids_key(arg.account_identifier, start);
let start = arg.start;
let scan_from = start.unwrap_or(u64::MAX);
let key = account_identifier_block_ids_key(arg.account_identifier, scan_from);
let mut settled_transactions = vec![];
let indices = with_account_identifier_block_ids(|account_identifier_block_ids| {
account_identifier_block_ids
.range(key..)
// old txs of the requested account_identifier and skip the start index
.take_while(|(k, _)| k.0 == key.0)
.filter(|(k, _)| k.1.0 < start)
.filter(|(k, _)| start.is_none_or(|s| k.1.0 < s))
.take(length)
.map(|(k, _)| k.1.0)
.collect::<Vec<BlockIndex>>()
Expand Down
41 changes: 41 additions & 0 deletions rs/ledger_suite/icp/index/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,47 @@ fn test_get_account_transactions_start_length() {
}
}

#[test]
fn test_get_account_identifier_transactions_without_start_matches_unbounded_start() {
let initial_balances = HashMap::new();
let env = &StateMachine::new();
let ledger_id = install_ledger(env, initial_balances, default_archive_options());
let index_id = install_index(env, ledger_id);

transfer(
env,
ledger_id,
Account {
owner: MINTER_PRINCIPAL.into(),
subaccount: None,
},
account(1, 0),
10_000,
);
transfer(
env,
ledger_id,
Account {
owner: MINTER_PRINCIPAL.into(),
subaccount: None,
},
account(1, 0),
20_000,
);
wait_until_sync_is_completed(env, index_id, ledger_id);

let without_start =
get_account_identifier_transactions(env, index_id, account(1, 0), None, 100);
let with_unbounded_start =
get_account_identifier_transactions(env, index_id, account(1, 0), Some(u64::MAX), 100);

assert_eq!(without_start.transactions.len(), 2);
assert_eq!(
without_start.transactions,
with_unbounded_start.transactions
);
}

#[test]
fn test_get_account_identifier_transactions_pagination() {
// 10_000 mint transactions to index for the same account_identifier
Expand Down
8 changes: 4 additions & 4 deletions rs/ledger_suite/icrc1/index-ng/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,18 +1268,18 @@ fn get_account_transactions(arg: GetAccountTransactionsArgs) -> GetAccountTransa
.expect("The length must be a u64!")
.min(with_state(|opts| opts.max_blocks_per_response))
.min(usize::MAX as u64) as usize;
// TODO: deal with the user setting start to u64::MAX
let start = arg
.start
.map_or(u64::MAX, |n| n.0.to_u64().expect("start must be a u64!"));
let key = account_block_ids_key(arg.account, start);
.map(|n| n.0.to_u64().expect("start must be a u64!"));
let scan_from = start.unwrap_or(u64::MAX);
let key = account_block_ids_key(arg.account, scan_from);
let mut transactions = vec![];
let indices = with_account_block_ids(|account_block_ids| {
account_block_ids
.range(key..)
// old txs of the requested account and skip the start index
.take_while(|(k, _)| k.0 == key.0)
.filter(|(k, _)| k.1.0 < start)
.filter(|(k, _)| start.is_none_or(|s| k.1.0 < s))
.take(length)
.map(|(k, _)| k.1.0)
.collect::<Vec<BlockIndex64>>()
Expand Down
29 changes: 29 additions & 0 deletions rs/ledger_suite/icrc1/index-ng/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,35 @@ fn test_get_account_transactions_start_length() {
}
}

#[test]
fn test_get_account_transactions_without_start_matches_unbounded_start() {
let initial_balances = vec![(account(1, 0), 1_000_000_000_000_u64)];
let env = &StateMachine::new();
let minter = minter_identity().sender().unwrap();
let ledger_id = install_ledger(
env,
initial_balances,
default_archive_options(),
None,
minter,
);
let index_id = install_index_ng(env, index_init_arg_without_interval(ledger_id));

transfer(env, ledger_id, account(1, 0), account(2, 0), 1_000);
transfer(env, ledger_id, account(1, 0), account(2, 0), 2_000);
wait_until_sync_is_completed(env, index_id, ledger_id);

let without_start = get_account_transactions(env, index_id, account(1, 0), None, 100);
let with_unbounded_start =
get_account_transactions(env, index_id, account(1, 0), Some(u64::MAX), 100);

assert_eq!(without_start.transactions.len(), 3);
assert_eq!(
without_start.transactions,
with_unbounded_start.transactions
);
}

#[test]
fn test_get_account_transactions_pagination() {
// 10_000 mint transactions to index for the same account.
Expand Down
Loading