From b636fe24a686238c3b5daab9ad76a5351ce70186 Mon Sep 17 00:00:00 2001 From: Giacomo Pasini Date: Tue, 21 Jun 2022 12:19:39 +0200 Subject: [PATCH 1/2] add additional info in spending counters mismatch errors --- chain-impl-mockchain/src/accounting/account/mod.rs | 7 +++++-- chain-impl-mockchain/src/accounting/account/spending.rs | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/chain-impl-mockchain/src/accounting/account/mod.rs b/chain-impl-mockchain/src/accounting/account/mod.rs index 88c9361e2..002918198 100644 --- a/chain-impl-mockchain/src/accounting/account/mod.rs +++ b/chain-impl-mockchain/src/accounting/account/mod.rs @@ -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), } diff --git a/chain-impl-mockchain/src/accounting/account/spending.rs b/chain-impl-mockchain/src/accounting/account/spending.rs index 41f4eccf4..a200a3e63 100644 --- a/chain-impl-mockchain/src/accounting/account/spending.rs +++ b/chain-impl-mockchain/src/accounting/account/spending.rs @@ -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(()) From 60c5f96a8dad723609cf4440d166acf5fdc68b8c Mon Sep 17 00:00:00 2001 From: Giacomo Pasini Date: Wed, 22 Jun 2022 11:49:52 +0200 Subject: [PATCH 2/2] fix tests --- chain-impl-mockchain/src/ledger/tests/macros.rs | 1 + .../src/ledger/tests/transaction_tests.rs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/chain-impl-mockchain/src/ledger/tests/macros.rs b/chain-impl-mockchain/src/ledger/tests/macros.rs index 81b037023..508a56b6a 100644 --- a/chain-impl-mockchain/src/ledger/tests/macros.rs +++ b/chain-impl-mockchain/src/ledger/tests/macros.rs @@ -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) { diff --git a/chain-impl-mockchain/src/ledger/tests/transaction_tests.rs b/chain-impl-mockchain/src/ledger/tests/transaction_tests.rs index f5accbcf6..ffcf15b4d 100644 --- a/chain-impl-mockchain/src/ledger/tests/transaction_tests.rs +++ b/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, @@ -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"), + }, } }