diff --git a/rs/rosetta-api/icrc1/ledger/sm-tests/src/in_memory_ledger.rs b/rs/rosetta-api/icrc1/ledger/sm-tests/src/in_memory_ledger.rs index cb1c91e27a0..2c15ab6c7f9 100644 --- a/rs/rosetta-api/icrc1/ledger/sm-tests/src/in_memory_ledger.rs +++ b/rs/rosetta-api/icrc1/ledger/sm-tests/src/in_memory_ledger.rs @@ -70,7 +70,6 @@ pub trait InMemoryLedgerState { fn validate_invariants(&self); } -#[derive(Eq, PartialEq, Debug)] pub struct InMemoryLedger where K: Ord + Hash, @@ -83,6 +82,106 @@ where burns_without_spender: Option>, } +impl PartialEq for InMemoryLedger +where + K: Ord + Hash + std::fmt::Debug, + AccountId: Hash + Eq + std::fmt::Debug, + Tokens: PartialEq + std::fmt::Debug, +{ + fn eq(&self, other: &Self) -> bool { + if self.balances.len() != other.balances.len() { + println!( + "Mismatch in number of balances: {} vs {}", + self.balances.len(), + other.balances.len() + ); + return false; + } + if self.allowances.len() != other.allowances.len() { + println!( + "Mismatch in number of allowances: {} vs {}", + self.allowances.len(), + other.allowances.len() + ); + return false; + } + if self.total_supply != other.total_supply { + println!( + "Mismatch in total supply: {:?} vs {:?}", + self.total_supply, other.total_supply + ); + return false; + } + if self.fee_collector != other.fee_collector { + println!( + "Mismatch in fee collector: {:?} vs {:?}", + self.fee_collector, other.fee_collector + ); + return false; + } + if self.burns_without_spender != other.burns_without_spender { + println!( + "Mismatch in burns without spender: {:?} vs {:?}", + self.burns_without_spender, other.burns_without_spender + ); + return false; + } + for (account_id, balance) in &self.balances { + if !self.balances.iter().all(|(key, value)| { + other.balances.get(key).map_or_else( + || { + println!( + "Mismatch in balance for account {:?}: {:?} vs None", + account_id, balance + ); + false + }, + |v| { + if *value != *v { + println!( + "Mismatch in balance for account {:?}: {:?} vs {:?}", + account_id, balance, v + ); + false + } else { + true + } + }, + ) + }) { + return false; + } + } + for (account_id, allowance) in &self.allowances { + if !self.allowances.iter().all(|(key, value)| { + other.allowances.get(key).map_or_else( + || { + println!( + "Mismatch in allowance for account {:?}: {:?} vs None", + account_id, allowance + ); + false + }, + |v| { + if *value != *v { + println!( + "Mismatch in allowance for account {:?}: {:?} vs {:?}", + account_id, allowance, v + ); + false + } else { + true + } + }, + ) + }) { + return false; + } + } + true + } +} + impl InMemoryLedgerState for InMemoryLedger where K: Eq + PartialEq + Ord + for<'a> From<(&'a AccountId, &'a AccountId)> + Clone + Hash, diff --git a/rs/rosetta-api/icrc1/tests/golden_state_upgrade_downgrade.rs b/rs/rosetta-api/icrc1/tests/golden_state_upgrade_downgrade.rs index b43497611b8..086323e2785 100644 --- a/rs/rosetta-api/icrc1/tests/golden_state_upgrade_downgrade.rs +++ b/rs/rosetta-api/icrc1/tests/golden_state_upgrade_downgrade.rs @@ -274,7 +274,11 @@ fn build_in_memory_ledger_and_compare_to_previous( in_memory_ledger_state.ingest_icrc1_ledger_blocks(&blocks); in_memory_ledger_state.verify_balances_and_allowances(state_machine, canister_id); if let Some(previous_ledger_state) = previous_ledger_state { - assert_eq!(previous_ledger_state, in_memory_ledger_state); + assert!( + previous_ledger_state == in_memory_ledger_state, + "In-memory ledger state does not match previous state for canister {:?}", + canister_id + ); } in_memory_ledger_state }