From a77cb5ca68187dc401c84a2b919e5afa503baedc Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Mon, 16 Oct 2023 15:45:15 +0200 Subject: [PATCH] Expose replay protection methods from `WlStorage` --- .../lib/node/ledger/shell/finalize_block.rs | 4 +-- apps/src/lib/node/ledger/shell/mod.rs | 3 +- core/src/ledger/storage/wl_storage.rs | 28 +++++++++++++++++++ core/src/ledger/storage/write_log.rs | 4 +-- shared/src/ledger/protocol/mod.rs | 1 - 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/apps/src/lib/node/ledger/shell/finalize_block.rs b/apps/src/lib/node/ledger/shell/finalize_block.rs index b0f2bc893c..2d14e3134d 100644 --- a/apps/src/lib/node/ledger/shell/finalize_block.rs +++ b/apps/src/lib/node/ledger/shell/finalize_block.rs @@ -8,6 +8,7 @@ use namada::ledger::events::EventType; use namada::ledger::gas::{GasMetering, TxGasMeter}; use namada::ledger::parameters::storage as params_storage; use namada::ledger::pos::{namada_proof_of_stake, staking_token_address}; +use namada::ledger::storage::wl_storage::WriteLogAndStorage; use namada::ledger::storage::EPOCH_SWITCH_BLOCKS_DELAY; use namada::ledger::storage_api::token::credit_tokens; use namada::ledger::storage_api::{pgf, StorageRead, StorageWrite}; @@ -518,7 +519,6 @@ where // hash to storage to prevent // replay self.wl_storage - .write_log .write_tx_hash(wrapper.header_hash()) .expect("Error while writing tx hash to storage"); } @@ -963,12 +963,10 @@ where // corresponding wrapper transaction to avoid replay of that in the process fn allow_tx_replay(&mut self, mut wrapper_tx: Tx) { self.wl_storage - .write_log .write_tx_hash(wrapper_tx.header_hash()) .expect("Error while deleting tx hash from storage"); self.wl_storage - .write_log .delete_tx_hash(wrapper_tx.update_header(TxType::Raw).header_hash()) .expect("Error while deleting tx hash from storage"); } diff --git a/apps/src/lib/node/ledger/shell/mod.rs b/apps/src/lib/node/ledger/shell/mod.rs index d81446baef..5315f16d8d 100644 --- a/apps/src/lib/node/ledger/shell/mod.rs +++ b/apps/src/lib/node/ledger/shell/mod.rs @@ -42,6 +42,7 @@ use namada::ledger::protocol::{ apply_wasm_tx, get_fee_unshielding_transaction, get_transfer_hash_from_storage, ShellParams, }; +use namada::ledger::storage::wl_storage::WriteLogAndStorage; use namada::ledger::storage::write_log::WriteLog; use namada::ledger::storage::{ DBIter, Sha256Hasher, Storage, StorageHasher, TempWlStorage, WlStorage, DB, @@ -944,7 +945,6 @@ where // Write wrapper hash to tx WAL temp_wl_storage - .write_log .write_tx_hash(wrapper_hash) .map_err(|e| Error::ReplayAttempt(e.to_string()))?; @@ -962,7 +962,6 @@ where // Write inner hash to tx WAL temp_wl_storage - .write_log .write_tx_hash(inner_tx_hash) .map_err(|e| Error::ReplayAttempt(e.to_string())) } diff --git a/core/src/ledger/storage/wl_storage.rs b/core/src/ledger/storage/wl_storage.rs index 4e102e766b..d18262c4f3 100644 --- a/core/src/ledger/storage/wl_storage.rs +++ b/core/src/ledger/storage/wl_storage.rs @@ -92,6 +92,12 @@ pub trait WriteLogAndStorage { /// reference to `WriteLog` when in need of both (avoids complain from the /// borrow checker) fn split_borrow(&mut self) -> (&mut WriteLog, &Storage); + + /// Write the provided tx hash to storage. + fn write_tx_hash( + &mut self, + hash: Hash, + ) -> crate::ledger::storage::write_log::Result<()>; } impl WriteLogAndStorage for WlStorage @@ -117,6 +123,13 @@ where fn split_borrow(&mut self) -> (&mut WriteLog, &Storage) { (&mut self.write_log, &self.storage) } + + fn write_tx_hash( + &mut self, + hash: Hash, + ) -> crate::ledger::storage::write_log::Result<()> { + self.write_log.write_tx_hash(hash) + } } impl WriteLogAndStorage for TempWlStorage<'_, D, H> @@ -142,6 +155,13 @@ where fn split_borrow(&mut self) -> (&mut WriteLog, &Storage) { (&mut self.write_log, (self.storage)) } + + fn write_tx_hash( + &mut self, + hash: Hash, + ) -> crate::ledger::storage::write_log::Result<()> { + self.write_log.write_tx_hash(hash) + } } impl WlStorage @@ -235,6 +255,14 @@ where } Ok(new_epoch) } + + /// Delete the provided transaction's hash from storage. + pub fn delete_tx_hash( + &mut self, + hash: Hash, + ) -> crate::ledger::storage::write_log::Result<()> { + self.write_log.delete_tx_hash(hash) + } } /// Prefix iterator for [`WlStorage`]. diff --git a/core/src/ledger/storage/write_log.rs b/core/src/ledger/storage/write_log.rs index 4364028171..e563374146 100644 --- a/core/src/ledger/storage/write_log.rs +++ b/core/src/ledger/storage/write_log.rs @@ -675,7 +675,7 @@ impl WriteLog { } /// Write the transaction hash - pub fn write_tx_hash(&mut self, hash: Hash) -> Result<()> { + pub(crate) fn write_tx_hash(&mut self, hash: Hash) -> Result<()> { if self .replay_protection .insert(hash, ReProtStorageModification::Write) @@ -692,7 +692,7 @@ impl WriteLog { } /// Remove the transaction hash - pub fn delete_tx_hash(&mut self, hash: Hash) -> Result<()> { + pub(crate) fn delete_tx_hash(&mut self, hash: Hash) -> Result<()> { match self .replay_protection .insert(hash, ReProtStorageModification::Delete) diff --git a/shared/src/ledger/protocol/mod.rs b/shared/src/ledger/protocol/mod.rs index b00d75ff1f..c1304bb01d 100644 --- a/shared/src/ledger/protocol/mod.rs +++ b/shared/src/ledger/protocol/mod.rs @@ -247,7 +247,6 @@ where // If wrapper was succesful, write inner tx hash to storage shell_params .wl_storage - .write_log_mut() .write_tx_hash(tx.update_header(TxType::Raw).header_hash()) .expect("Error while writing tx hash to storage"); changed_keys.insert(replay_protection::get_replay_protection_last_key(