Skip to content

Commit

Permalink
add additional info in spending counters mismatch errors (#825)
Browse files Browse the repository at this point in the history
* add additional info in spending counters mismatch errors

* fix tests
  • Loading branch information
zeegomo committed Jun 22, 2022
1 parent 4944822 commit bb12c86
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
7 changes: 5 additions & 2 deletions chain-impl-mockchain/src/accounting/account/mod.rs
Expand Up @@ -31,8 +31,11 @@ pub enum LedgerError {
AlreadyExists,
#[error("Removed account is not empty")]
NonZero,
#[error("Spending credential invalid")]
SpendingCredentialInvalid,
#[error("Spending credential invalid, expected {} got {} in lane {}", .expected.unlaned_counter(), .actual.unlaned_counter(), .actual.lane())]
SpendingCredentialInvalid {
expected: SpendingCounter,
actual: SpendingCounter,
},
#[error("Value calculation failed")]
ValueError(#[from] ValueError),
}
Expand Down
5 changes: 4 additions & 1 deletion chain-impl-mockchain/src/accounting/account/spending.rs
Expand Up @@ -53,7 +53,10 @@ impl SpendingCounterIncreasing {
let actual_counter = self.nexts[counter.lane()];

if actual_counter != counter {
Err(LedgerError::SpendingCredentialInvalid)
Err(LedgerError::SpendingCredentialInvalid {
expected: actual_counter,
actual: counter,
})
} else {
self.nexts[counter.lane()] = actual_counter.increment();
Ok(())
Expand Down
1 change: 1 addition & 0 deletions chain-impl-mockchain/src/ledger/tests/macros.rs
Expand Up @@ -29,6 +29,7 @@ macro_rules! assert_err {
//
// succeed if Expression's value a Err(E) where E match the ExpectedErrorPattern,
// otherwise panic!() with some diagnostic
#[allow(unused_macros)]
macro_rules! assert_err_match {
($left: pat, $right: expr) => {
match &($right) {
Expand Down
17 changes: 10 additions & 7 deletions chain-impl-mockchain/src/ledger/tests/transaction_tests.rs
@@ -1,7 +1,7 @@
#![cfg(test)]

use crate::{
accounting::account::LedgerError::NonExistent,
accounting::account::{LedgerError::NonExistent, SpendingCounter},
date::BlockDate,
ledger::{
self,
Expand Down Expand Up @@ -129,12 +129,15 @@ pub fn duplicated_account_transaction() {

match result {
Err(err) => panic!("first transaction should be succesful but {}", err),
Ok(_) => {
assert_err_match!(
ledger::Error::Account(crate::account::LedgerError::SpendingCredentialInvalid),
test_ledger.apply_transaction(fragment2, BlockDate::first())
);
}
Ok(_) => match test_ledger.apply_transaction(fragment2, BlockDate::first()) {
Err(ledger::Error::Account(
crate::account::LedgerError::SpendingCredentialInvalid { expected, actual },
)) => {
assert_eq!(expected, SpendingCounter::zero().increment());
assert_eq!(actual, SpendingCounter::zero());
}
_ => panic!("duplicated transaction should fail spending counter validation"),
},
}
}

Expand Down

0 comments on commit bb12c86

Please sign in to comment.