Skip to content

Commit

Permalink
fix to read signed root
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Dec 6, 2023
1 parent ff68f7b commit a912cc7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
34 changes: 23 additions & 11 deletions apps/src/lib/node/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod tests {

use itertools::Itertools;
use namada::core::ledger::masp_conversions::update_allowed_conversions;
use namada::eth_bridge::storage::proof::BridgePoolRootProof;
use namada::ledger::eth_bridge::storage::bridge_pool;
use namada::ledger::gas::STORAGE_ACCESS_GAS_PER_BYTE;
use namada::ledger::ibc::storage::ibc_key;
Expand All @@ -65,6 +66,7 @@ mod tests {
use namada::types::chain::ChainId;
use namada::types::ethereum_events::Uint;
use namada::types::hash::Hash;
use namada::types::keccak::KeccakHash;
use namada::types::storage::{BlockHash, BlockHeight, Key};
use namada::types::time::DurationSecs;
use namada::types::{address, storage, token};
Expand Down Expand Up @@ -489,8 +491,10 @@ mod tests {
let value_bytes = types::encode(&storage.block.height);
storage.write(&key, value_bytes)?;
}
let key = bridge_pool::get_nonce_key();
let bytes = types::encode(&Uint::default());
let key = bridge_pool::get_signed_root_key();
let root_proof =
BridgePoolRootProof::new((KeccakHash::default(), Uint::default()));
let bytes = types::encode(&root_proof);
storage.write(&key, bytes)?;

// Update and commit
Expand Down Expand Up @@ -586,10 +590,12 @@ mod tests {
None,
Some(5),
);
let bp_nonce_key = bridge_pool::get_nonce_key();
let signed_root_key = bridge_pool::get_signed_root_key();
let nonce = Uint::default();
let bytes = types::encode(&nonce);
storage.write(&bp_nonce_key, bytes).unwrap();
let root_proof =
BridgePoolRootProof::new((KeccakHash::default(), nonce));
let bytes = types::encode(&root_proof);
storage.write(&signed_root_key, bytes).unwrap();

storage
.begin_block(BlockHash::default(), BlockHeight(1))
Expand Down Expand Up @@ -617,8 +623,10 @@ mod tests {
.expect("write failed");

let nonce = nonce + 1;
let bytes = types::encode(&nonce);
storage.write(&bp_nonce_key, bytes).unwrap();
let root_proof =
BridgePoolRootProof::new((KeccakHash::default(), nonce));
let bytes = types::encode(&root_proof);
storage.write(&signed_root_key, bytes).unwrap();

storage.block.epoch = storage.block.epoch.next();
storage.block.pred_epochs.new_epoch(BlockHeight(6));
Expand All @@ -633,8 +641,10 @@ mod tests {
.expect("begin_block failed");

let nonce = nonce + 1;
let bytes = types::encode(&nonce);
storage.write(&bp_nonce_key, bytes).unwrap();
let root_proof =
BridgePoolRootProof::new((KeccakHash::default(), nonce));
let bytes = types::encode(&root_proof);
storage.write(&signed_root_key, bytes).unwrap();

storage.block.epoch = storage.block.epoch.next();
storage.block.pred_epochs.new_epoch(BlockHeight(11));
Expand All @@ -659,8 +669,10 @@ mod tests {
.expect("begin_block failed");

let nonce = nonce + 1;
let bytes = types::encode(&nonce);
storage.write(&bp_nonce_key, bytes).unwrap();
let root_proof =
BridgePoolRootProof::new((KeccakHash::default(), nonce));
let bytes = types::encode(&root_proof);
storage.write(&signed_root_key, bytes).unwrap();
storage.block.epoch = storage.block.epoch.next();
storage.block.pred_epochs.new_epoch(BlockHeight(12));
let batch = PersistentStorage::batch();
Expand Down
20 changes: 20 additions & 0 deletions apps/src/lib/node/ledger/storage/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use data_encoding::HEXLOWER;
use itertools::Either;
use namada::core::ledger::masp_conversions::ConversionState;
use namada::core::types::ethereum_structs;
use namada::eth_bridge::storage::proof::BridgePoolRootProof;
use namada::ledger::eth_bridge::storage::bridge_pool;
use namada::ledger::storage::merkle_tree::{
base_tree_key_prefix, subtree_key_prefix,
};
Expand All @@ -58,6 +60,7 @@ use namada::ledger::storage::{
types, BlockStateRead, BlockStateWrite, DBIter, DBWriteBatch, Error,
MerkleTreeStoresRead, Result, StoreType, DB,
};
use namada::types::ethereum_events::Uint;
use namada::types::internal::TxQueue;
use namada::types::storage::{
BlockHeight, BlockResults, Epoch, EthEventsQueue, Header, Key, KeySeg,
Expand Down Expand Up @@ -1446,6 +1449,23 @@ impl DB for RocksDB {
Ok(())
}

fn read_bridge_pool_signed_nonce(
&self,
height: BlockHeight,
last_height: BlockHeight,
) -> Result<Uint> {
let nonce_key = bridge_pool::get_signed_root_key();
let bytes = if height == BlockHeight(0) || height >= last_height {
self.read_subspace_val(&nonce_key)?
} else {
self.read_subspace_val_with_height(&nonce_key, height, last_height)?
};
let bytes = bytes.expect("Signed root should exist");
let bp_root_proof = BridgePoolRootProof::try_from_slice(&bytes)
.map_err(Error::BorshCodingError)?;
Ok(bp_root_proof.data.1)
}

fn write_replay_protection_entry(
&mut self,
batch: &mut Self::WriteBatch,
Expand Down
9 changes: 9 additions & 0 deletions core/src/ledger/storage/mockdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::{
};
use crate::ledger::masp_conversions::ConversionState;
use crate::ledger::storage::types::{self, KVBytes, PrefixIterator};
use crate::types::ethereum_events::Uint;
use crate::types::ethereum_structs;
use crate::types::hash::Hash;
use crate::types::internal::TxQueue;
Expand Down Expand Up @@ -588,6 +589,14 @@ impl DB for MockDB {
Ok(())
}

fn read_bridge_pool_signed_nonce(
&self,
_height: BlockHeight,
_last_height: BlockHeight,
) -> Result<Uint> {
Ok(Uint::default())
}

fn write_replay_protection_entry(
&mut self,
_batch: &mut Self::WriteBatch,
Expand Down
26 changes: 14 additions & 12 deletions core/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub use wl_storage::{
};

use super::gas::MEMORY_ACCESS_GAS_PER_BYTE;
use crate::ledger::eth_bridge::storage::bridge_pool::{
self, is_pending_transfer_key,
};
use crate::ledger::eth_bridge::storage::bridge_pool::is_pending_transfer_key;
use crate::ledger::gas::{
STORAGE_ACCESS_GAS_PER_BYTE, STORAGE_WRITE_GAS_PER_BYTE,
};
Expand Down Expand Up @@ -360,6 +358,13 @@ pub trait DB: std::fmt::Debug {
pruned_epoch: Epoch,
) -> Result<()>;

/// Read the signed nonce of Bridge Pool
fn read_bridge_pool_signed_nonce(
&self,
height: BlockHeight,
last_height: BlockHeight,
) -> Result<Uint>;

/// Write a replay protection entry
fn write_replay_protection_entry(
&mut self,
Expand Down Expand Up @@ -1209,11 +1214,10 @@ where

/// Get oldest epoch which has the valid signed nonce of the bridge pool
pub fn get_oldest_epoch_with_valid_nonce(&self) -> Result<Epoch> {
let nonce_key = bridge_pool::get_nonce_key();
let (bytes, _) = self.read(&nonce_key)?;
let bytes = bytes.expect("Bridge pool nonce should exits");
let current_nonce =
Uint::try_from_slice(&bytes).map_err(Error::BorshCodingError)?;
let last_height = self.get_last_block_height();
let current_nonce = self
.db
.read_bridge_pool_signed_nonce(last_height, last_height)?;
let (mut epoch, _) = self.get_last_epoch();
// We don't need to check the older epochs because their Merkle tree
// snapshots have been already removed
Expand All @@ -1228,10 +1232,8 @@ where
Some(h) => h,
None => continue,
};
let (bytes, _) = self.read_with_height(&nonce_key, height)?;
let bytes = bytes.expect("Bridge pool nonce should exits");
let nonce = Uint::try_from_slice(&bytes)
.map_err(Error::BorshCodingError)?;
let nonce =
self.db.read_bridge_pool_signed_nonce(height, last_height)?;
if nonce < current_nonce {
break;
}
Expand Down

0 comments on commit a912cc7

Please sign in to comment.