Skip to content

Commit

Permalink
test(ICRC_Ledger): FI-1531: Also check expiration time when comparing…
Browse files Browse the repository at this point in the history
… allowances in InMemoryLedger (#2958)

In addition to comparing the amounts, also check the expiration time
when comparing allowances in the ledger and the `InMemoryLedger`.
  • Loading branch information
mbjorkqvist authored Dec 4, 2024
1 parent f7e6c06 commit de64fee
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rs/ledger_suite/tests/sm-tests/src/in_memory_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use icp_ledger::AccountIdentifier;
use icrc_ledger_types::icrc1::account::Account;
use std::collections::HashMap;
use std::hash::Hash;
use std::time::SystemTime;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -699,6 +700,14 @@ where
actual_balance
);
}
let mut expiration_in_future_count = 0;
let mut expiration_in_past_count = 0;
let mut no_expiration_count = 0;
let timestamp = env
.time()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64;
for (approval, allowance) in self.allowances.iter() {
let (from, spender): (AccountId, AccountId) = approval.clone().into();
assert!(
Expand All @@ -708,6 +717,18 @@ where
&spender
);
let actual_allowance = AccountId::get_allowance(env, ledger_id, from, spender);
match actual_allowance.expires_at {
None => {
no_expiration_count += 1;
}
Some(expires_at) => {
if expires_at > timestamp {
expiration_in_future_count += 1;
} else {
expiration_in_past_count += 1;
}
}
}
assert_eq!(
allowance.amount,
Tokens::try_from(actual_allowance.allowance.clone()).unwrap(),
Expand All @@ -718,7 +739,22 @@ where
allowance,
actual_allowance
);
assert_eq!(
allowance.expires_at.map(|t| t.as_nanos_since_unix_epoch()),
actual_allowance.expires_at,
"Mismatch in allowance expiration for approval from {:?} spender {:?}: {:?} ({:?} vs {:?}) at {:?}",
&from,
&spender,
approval,
allowance,
actual_allowance,
env.time()
);
}
println!(
"allowances with no expiration: {}, expiration in future: {}, expiration in past: {}",
no_expiration_count, expiration_in_future_count, expiration_in_past_count
);
}
}

Expand Down

0 comments on commit de64fee

Please sign in to comment.