Skip to content

Commit

Permalink
Add some more reasonable messages in case the state does not match
Browse files Browse the repository at this point in the history
  • Loading branch information
mbjorkqvist committed Sep 24, 2024
1 parent 18a5b26 commit 118b798
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
101 changes: 100 additions & 1 deletion rs/rosetta-api/icrc1/ledger/sm-tests/src/in_memory_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub trait InMemoryLedgerState {
fn validate_invariants(&self);
}

#[derive(Eq, PartialEq, Debug)]
pub struct InMemoryLedger<K, AccountId, Tokens>
where
K: Ord + Hash,
Expand All @@ -83,6 +82,106 @@ where
burns_without_spender: Option<BurnsWithoutSpender<AccountId>>,
}

impl<K, AccountId, Tokens: std::fmt::Debug> PartialEq for InMemoryLedger<K, AccountId, Tokens>
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<K, AccountId, Tokens> InMemoryLedgerState for InMemoryLedger<K, AccountId, Tokens>
where
K: Eq + PartialEq + Ord + for<'a> From<(&'a AccountId, &'a AccountId)> + Clone + Hash,
Expand Down
6 changes: 5 additions & 1 deletion rs/rosetta-api/icrc1/tests/golden_state_upgrade_downgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 118b798

Please sign in to comment.