From b3b54299567f3a8f9ee4d8de6654536a685fa3ff Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 5 Apr 2022 18:19:20 +0000 Subject: [PATCH] wallet: Add assertions during Orchard wallet bundle appends `Wallet::append_bundle_commitments` should never be called twice on the same bundle, as that breaks sequentiality requirements (which we already check for), so it is safe to assert that the inserted values do not overwrite any existing data. --- src/rust/src/wallet.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/rust/src/wallet.rs b/src/rust/src/wallet.rs index 8418d7324..08e87c5df 100644 --- a/src/rust/src/wallet.rs +++ b/src/rust/src/wallet.rs @@ -584,13 +584,16 @@ impl Wallet { let my_notes_for_tx = self.wallet_received_notes.get(txid); if my_notes_for_tx.is_some() { tracing::trace!("Tx is ours, marking as mined"); - self.wallet_note_positions.insert( - *txid, - NotePositions { - tx_height: block_height, - note_positions: BTreeMap::default(), - }, - ); + assert!(self + .wallet_note_positions + .insert( + *txid, + NotePositions { + tx_height: block_height, + note_positions: BTreeMap::default(), + }, + ) + .is_none()); } for (action_idx, action) in bundle.actions().iter().enumerate() { @@ -610,23 +613,28 @@ impl Wallet { { tracing::trace!("Witnessing Orchard note ({}, {})", txid, action_idx); let pos = self.witness_tree.witness().expect("tree is not empty"); - self.wallet_note_positions + assert!(self + .wallet_note_positions .get_mut(txid) .expect("We created this above") .note_positions - .insert(action_idx, pos); + .insert(action_idx, pos) + .is_none()); } // For nullifiers that are ours that we detect as spent by this action, // we will record that input as being mined. if let Some(outpoint) = self.nullifiers.get(action.nullifier()) { - self.mined_notes.insert( - *outpoint, - InPoint { - txid: *txid, - action_idx, - }, - ); + assert!(self + .mined_notes + .insert( + *outpoint, + InPoint { + txid: *txid, + action_idx, + }, + ) + .is_none()); } }