Skip to content

Commit

Permalink
Fix an overflow during balance calculation when waiting for compound …
Browse files Browse the repository at this point in the history
…transaction acceptance. (#393)

* Fix balance calculation overflow when waiting for compound transaction acceptance.

* account for fees in compound transactions
  • Loading branch information
aspect authored Jan 15, 2024
1 parent d9a403d commit fb5e304
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 13 additions & 13 deletions wallet/core/src/utxo/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UtxoEntryReference>, current_daa_score: u64) -> Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions wallet/core/src/utxo/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit fb5e304

Please sign in to comment.