From 16289750e80efe4bb25ed8c5faf2defd546e0df4 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 9 Mar 2021 18:18:24 -0700 Subject: [PATCH] Remove WalletWrite::transactionally --- zcash_client_backend/src/data_api.rs | 43 ---------------------- zcash_client_backend/src/data_api/chain.rs | 6 --- zcash_client_sqlite/src/lib.rs | 26 ++++++------- 3 files changed, 11 insertions(+), 64 deletions(-) diff --git a/zcash_client_backend/src/data_api.rs b/zcash_client_backend/src/data_api.rs index 4a7a59606..c96aa413f 100644 --- a/zcash_client_backend/src/data_api.rs +++ b/zcash_client_backend/src/data_api.rs @@ -205,15 +205,6 @@ pub struct SentTransaction<'a> { /// This trait encapsulates the write capabilities required to update stored /// wallet data. pub trait WalletWrite: WalletRead { - /// Perform one or more write operations of this trait transactionally. - /// Implementations of this method must ensure that all mutations to the - /// state of the data store made by the provided closure must be performed - /// atomically and modifications to state must be automatically rolled back - /// if the provided closure returns an error. - fn transactionally(&mut self, f: F) -> Result - where - F: FnOnce(&mut Self) -> Result; - fn insert_pruned_block( &mut self, block: &PrunedBlock, @@ -241,21 +232,6 @@ pub trait WalletWrite: WalletRead { /// /// There may be restrictions on how far it is possible to rewind. fn rewind_to_height(&mut self, block_height: BlockHeight) -> Result<(), Self::Error>; - - /// Mark the specified transaction as spent and record the nullifier. - fn mark_spent(&mut self, tx_ref: Self::TxRef, nf: &Nullifier) -> Result<(), Self::Error>; - - /// Remove all incremental witness data before the specified block height. - // TODO: this is a backend-specific optimization that probably shouldn't be part of - // the public API - fn prune_witnesses(&mut self, from_height: BlockHeight) -> Result<(), Self::Error>; - - /// Remove the spent marker from any received notes that had been spent in a - /// transaction constructed by the wallet, but which transaction had not been mined - /// by the specified block height. - // TODO: this is a backend-specific optimization that probably shouldn't be part of - // the public API - fn update_expired_notes(&mut self, from_height: BlockHeight) -> Result<(), Self::Error>; } /// This trait provides sequential access to raw blockchain data via a callback-oriented @@ -471,13 +447,6 @@ pub mod testing { } impl WalletWrite for MockWalletDB { - fn transactionally(&mut self, f: F) -> Result - where - F: FnOnce(&mut Self) -> Result, - { - f(self) - } - fn insert_pruned_block( &mut self, _block: &PrunedBlock, @@ -503,17 +472,5 @@ pub mod testing { fn rewind_to_height(&mut self, _block_height: BlockHeight) -> Result<(), Self::Error> { Ok(()) } - - fn mark_spent(&mut self, _tx_ref: Self::TxRef, _nf: &Nullifier) -> Result<(), Self::Error> { - Ok(()) - } - - fn prune_witnesses(&mut self, _from_height: BlockHeight) -> Result<(), Self::Error> { - Ok(()) - } - - fn update_expired_notes(&mut self, _from_height: BlockHeight) -> Result<(), Self::Error> { - Ok(()) - } } } diff --git a/zcash_client_backend/src/data_api/chain.rs b/zcash_client_backend/src/data_api/chain.rs index e6b554a5d..c8986487a 100644 --- a/zcash_client_backend/src/data_api/chain.rs +++ b/zcash_client_backend/src/data_api/chain.rs @@ -339,12 +339,6 @@ where &witnesses, )?; - // Prune the stored witnesses (we only expect rollbacks of at most 100 blocks). - data.prune_witnesses(current_height - 100)?; - - // Update now-expired transactions that didn't get mined. - data.update_expired_notes(current_height)?; - let spent_nf: Vec = txs .iter() .flat_map(|tx| tx.shielded_spends.iter().map(|spend| spend.nf)) diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 0ffa13f4b..a33632193 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -359,10 +359,10 @@ impl<'a, P: consensus::Parameters> WalletRead for DataConnStmtCache<'a, P> { } } -impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { - fn transactionally(&mut self, f: F) -> Result +impl<'a, P: consensus::Parameters> DataConnStmtCache<'a, P> { + fn transactionally(&mut self, f: F) -> Result where - F: FnOnce(&mut Self) -> Result, + F: FnOnce(&mut Self) -> Result, { self.wallet_db.conn.execute("BEGIN IMMEDIATE", NO_PARAMS)?; match f(self) { @@ -385,7 +385,9 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { } } } +} +impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { fn insert_pruned_block( &mut self, block: &PrunedBlock, @@ -427,6 +429,12 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { } } + // Prune the stored witnesses (we only expect rollbacks of at most 100 blocks). + wallet::prune_witnesses(up, block.block_height - 100)?; + + // Update now-expired transactions that didn't get mined. + wallet::update_expired_notes(up, block.block_height)?; + Ok(new_witnesses) }) } @@ -485,18 +493,6 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { fn rewind_to_height(&mut self, block_height: BlockHeight) -> Result<(), Self::Error> { wallet::rewind_to_height(self.wallet_db, block_height) } - - fn mark_spent(&mut self, tx_ref: Self::TxRef, nf: &Nullifier) -> Result<(), Self::Error> { - wallet::mark_spent(self, tx_ref, nf) - } - - fn prune_witnesses(&mut self, below_height: BlockHeight) -> Result<(), Self::Error> { - wallet::prune_witnesses(self, below_height) - } - - fn update_expired_notes(&mut self, height: BlockHeight) -> Result<(), Self::Error> { - wallet::update_expired_notes(self, height) - } } pub struct BlockDB(Connection);