From fb5e304f558810bcd70fb16e3afb97759a39c28c Mon Sep 17 00:00:00 2001 From: aspect Date: Mon, 15 Jan 2024 16:09:42 +0200 Subject: [PATCH] Fix an overflow during balance calculation when waiting for compound transaction acceptance. (#393) * Fix balance calculation overflow when waiting for compound transaction acceptance. * account for fees in compound transactions --- wallet/core/src/utxo/context.rs | 26 +++++++++++++------------- wallet/core/src/utxo/outgoing.rs | 4 ++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/wallet/core/src/utxo/context.rs b/wallet/core/src/utxo/context.rs index 996e399a54..ca0883bb24 100644 --- a/wallet/core/src/utxo/context.rs +++ b/wallet/core/src/utxo/context.rs @@ -498,23 +498,23 @@ impl UtxoContext { consumed += tx.aggregate_input_value(); } else { // compound tx has no payment value - // we skip them, accumulating only fees - // as fees are the only component that will - // reduce the final balance after the - // compound process - outgoing += tx.fees(); + outgoing += tx.fees() + tx.aggregate_output_value(); + consumed += tx.aggregate_input_value() } } } - Balance::new( - (mature + consumed) - outgoing, - pending, - outgoing, - context.mature.len(), - context.pending.len(), - context.stasis.len(), - ) + // TODO - remove this check once we are confident that + // this condition does not occur. This is a temporary + // log for a fixed bug, but we want to keep the check + // just in case. + if mature + consumed < outgoing { + log_error!("Error: outgoing transaction value exceeds available balance"); + } + + let mature = (mature + consumed).saturating_sub(outgoing); + + Balance::new(mature, pending, outgoing, context.mature.len(), context.pending.len(), context.stasis.len()) } pub(crate) async fn handle_utxo_added(&self, utxos: Vec, current_daa_score: u64) -> Result<()> { diff --git a/wallet/core/src/utxo/outgoing.rs b/wallet/core/src/utxo/outgoing.rs index 40eb89bdc5..220409a18c 100644 --- a/wallet/core/src/utxo/outgoing.rs +++ b/wallet/core/src/utxo/outgoing.rs @@ -57,6 +57,10 @@ impl OutgoingTransaction { self.inner.pending_transaction.aggregate_input_value() } + pub fn aggregate_output_value(&self) -> u64 { + self.inner.pending_transaction.aggregate_output_value() + } + pub fn pending_transaction(&self) -> &PendingTransaction { &self.inner.pending_transaction }