Skip to content

Commit

Permalink
Expose replay protection methods from WlStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Oct 16, 2023
1 parent 914b6f5 commit a77cb5c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
4 changes: 1 addition & 3 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 1 addition & 2 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()))?;

Expand All @@ -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()))
}
Expand Down
28 changes: 28 additions & 0 deletions core/src/ledger/storage/wl_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::D, Self::H>);

/// Write the provided tx hash to storage.
fn write_tx_hash(
&mut self,
hash: Hash,
) -> crate::ledger::storage::write_log::Result<()>;
}

impl<D, H> WriteLogAndStorage for WlStorage<D, H>
Expand All @@ -117,6 +123,13 @@ where
fn split_borrow(&mut self) -> (&mut WriteLog, &Storage<Self::D, Self::H>) {
(&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<D, H> WriteLogAndStorage for TempWlStorage<'_, D, H>
Expand All @@ -142,6 +155,13 @@ where
fn split_borrow(&mut self) -> (&mut WriteLog, &Storage<Self::D, Self::H>) {
(&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<D, H> WlStorage<D, H>
Expand Down Expand Up @@ -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`].
Expand Down
4 changes: 2 additions & 2 deletions core/src/ledger/storage/write_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion shared/src/ledger/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a77cb5c

Please sign in to comment.