Skip to content

Commit

Permalink
Merge pull request #845 from anoma/bat/ethbridge/fix-merkle-values
Browse files Browse the repository at this point in the history
 Removed MerkleValue type.
  • Loading branch information
batconjurer authored Dec 1, 2022
2 parents 0c20c3b + ceaae64 commit a48251f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 124 deletions.
56 changes: 34 additions & 22 deletions shared/src/ledger/queries/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ use crate::types::keccak::KeccakHash;
use crate::types::storage::MembershipProof::BridgePool;
#[cfg(all(feature = "wasm-runtime", feature = "ferveo-tpke"))]
use crate::types::storage::TxIndex;
use crate::types::storage::{
self, BlockResults, Epoch, MerkleValue, PrefixValue,
};
use crate::types::storage::{self, BlockResults, Epoch, PrefixValue};
#[cfg(all(feature = "wasm-runtime", feature = "ferveo-tpke"))]
use crate::types::transaction::TxResult;

Expand Down Expand Up @@ -254,7 +252,7 @@ where
.storage
.get_existence_proof(
&storage_key,
value.clone().into(),
value.clone(),
request.height,
)
.into_storage_result()?;
Expand Down Expand Up @@ -311,7 +309,7 @@ where
for PrefixValue { key, value } in &data {
let mut proof = ctx
.storage
.get_existence_proof(key, value.clone().into(), request.height)
.get_existence_proof(key, value.clone(), request.height)
.into_storage_result()?;
ops.append(&mut proof.ops);
}
Expand Down Expand Up @@ -455,16 +453,12 @@ where
);
// from the hashes of the transfers, get the actual values.
let mut missing_hashes = vec![];
let (keys, values): (Vec<_>, Vec<PendingTransfer>) = transfer_hashes
let (keys, values): (Vec<_>, Vec<_>) = transfer_hashes
.iter()
.filter_map(|hash| {
let key = get_key_from_hash(hash);
match ctx.storage.read(&key) {
Ok((Some(bytes), _)) => {
PendingTransfer::try_from_slice(&bytes[..])
.ok()
.map(|transfer| (key, transfer))
}
Ok((Some(bytes), _)) => Some((key, bytes)),
_ => {
missing_hashes.push(hash);
None
Expand All @@ -483,10 +477,7 @@ where
)));
}
// get the membership proof
match tree.get_sub_tree_existence_proof(
&keys,
values.into_iter().map(MerkleValue::from).collect(),
) {
match tree.get_sub_tree_existence_proof(&keys, values) {
Ok(BridgePool(proof)) => {
let data = EncodeCell::new(&RelayProof {
// TODO: use actual validators
Expand Down Expand Up @@ -671,7 +662,10 @@ mod test {
// write a transfer into the bridge pool
client
.storage
.write(&get_pending_key(&transfer), transfer.clone())
.write(
&get_pending_key(&transfer),
transfer.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// commit the changes and increase block height
Expand Down Expand Up @@ -709,7 +703,10 @@ mod test {
// write a transfer into the bridge pool
client
.storage
.write(&get_pending_key(&transfer), transfer.clone())
.write(
&get_pending_key(&transfer),
transfer.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// commit the changes and increase block height
Expand All @@ -725,7 +722,10 @@ mod test {
transfer2.transfer.amount = 1.into();
client
.storage
.write(&get_pending_key(&transfer2), transfer2.clone())
.write(
&get_pending_key(&transfer2),
transfer2.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// commit the changes and increase block height
Expand Down Expand Up @@ -763,7 +763,10 @@ mod test {
// write a transfer into the bridge pool
client
.storage
.write(&get_pending_key(&transfer), transfer.clone())
.write(
&get_pending_key(&transfer),
transfer.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// create a signed Merkle root for this pool
Expand All @@ -782,7 +785,10 @@ mod test {
transfer2.transfer.amount = 1.into();
client
.storage
.write(&get_pending_key(&transfer2), transfer2.clone())
.write(
&get_pending_key(&transfer2),
transfer2.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// add the signature for the pool at the previous block height
Expand Down Expand Up @@ -853,7 +859,10 @@ mod test {
// write a transfer into the bridge pool
client
.storage
.write(&get_pending_key(&transfer), transfer.clone())
.write(
&get_pending_key(&transfer),
transfer.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// create a signed Merkle root for this pool
Expand All @@ -872,7 +881,10 @@ mod test {
transfer2.transfer.amount = 1.into();
client
.storage
.write(&get_pending_key(&transfer2), transfer2.clone())
.write(
&get_pending_key(&transfer2),
transfer2.try_to_vec().expect("Test failed"),
)
.expect("Test failed");

// add the signature for the pool at the previous block height
Expand Down
30 changes: 15 additions & 15 deletions shared/src/ledger/storage/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use crate::types::address::{Address, InternalAddress};
use crate::types::hash::Hash;
use crate::types::keccak::KeccakHash;
use crate::types::storage::{
DbKeySeg, Error as StorageError, Key, MembershipProof, MerkleValue,
StringKey, TreeBytes,
DbKeySeg, Error as StorageError, Key, MembershipProof, StringKey, TreeBytes,
};

#[allow(missing_docs)]
Expand Down Expand Up @@ -56,6 +55,9 @@ pub enum Error {
/// Result for functions that may fail
type Result<T> = std::result::Result<T, Error>;

/// Type alias for bytes to be put into the Merkle storage
pub(super) type StorageBytes = Vec<u8>;

/// Type aliases for the different merkle trees and backing stores
pub type SmtStore = DefaultStore<SmtHash, Hash, 32>;
pub type AmtStore = DefaultStore<StringKey, TreeBytes, IBC_KEY_LIMIT>;
Expand Down Expand Up @@ -315,12 +317,14 @@ impl<H: StorageHasher + Default> MerkleTree<H> {
&mut self,
store_type: &StoreType,
key: &Key,
value: MerkleValue,
value: impl AsRef<[u8]>,
) -> Result<()> {
let sub_root = self.tree_mut(store_type).subtree_update(key, value)?;
let sub_root = self
.tree_mut(store_type)
.subtree_update(key, value.as_ref())?;
// update the base tree with the updated sub root without hashing
if *store_type != StoreType::Base {
let base_key = H::hash(&store_type.to_string());
let base_key = H::hash(store_type.to_string());
self.base.update(base_key.into(), sub_root)?;
}
Ok(())
Expand All @@ -333,21 +337,17 @@ impl<H: StorageHasher + Default> MerkleTree<H> {
}

/// Update the tree with the given key and value
pub fn update(
&mut self,
key: &Key,
value: impl Into<MerkleValue>,
) -> Result<()> {
pub fn update(&mut self, key: &Key, value: impl AsRef<[u8]>) -> Result<()> {
let (store_type, sub_key) = StoreType::sub_key(key)?;
self.update_tree(&store_type, &sub_key, value.into())
self.update_tree(&store_type, &sub_key, value)
}

/// Delete the value corresponding to the given key
pub fn delete(&mut self, key: &Key) -> Result<()> {
let (store_type, sub_key) = StoreType::sub_key(key)?;
let sub_root = self.tree_mut(&store_type).subtree_delete(&sub_key)?;
if store_type != StoreType::Base {
let base_key = H::hash(&store_type.to_string());
let base_key = H::hash(store_type.to_string());
self.base.update(base_key.into(), sub_root)?;
}
Ok(())
Expand Down Expand Up @@ -376,7 +376,7 @@ impl<H: StorageHasher + Default> MerkleTree<H> {
pub fn get_sub_tree_existence_proof(
&self,
keys: &[Key],
values: Vec<MerkleValue>,
values: Vec<StorageBytes>,
) -> Result<MembershipProof> {
let first_key = keys.iter().next().ok_or_else(|| {
Error::InvalidMerkleKey(
Expand Down Expand Up @@ -733,7 +733,7 @@ mod test {
let proof = match tree
.get_sub_tree_existence_proof(
std::array::from_ref(&ibc_key),
vec![ibc_val.clone().into()],
vec![ibc_val.clone()],
)
.unwrap()
{
Expand Down Expand Up @@ -792,7 +792,7 @@ mod test {
let proof = match tree
.get_sub_tree_existence_proof(
std::array::from_ref(&pos_key),
vec![pos_val.clone().into()],
vec![pos_val.clone()],
)
.unwrap()
{
Expand Down
12 changes: 5 additions & 7 deletions shared/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use crate::types::key::dkg_session_keys::DkgPublicKey;
use crate::types::storage::TxQueue;
use crate::types::storage::{
BlockHash, BlockHeight, BlockResults, Epoch, Epochs, Header, Key, KeySeg,
MembershipProof, MerkleValue, TxIndex, BLOCK_HASH_LENGTH,
MembershipProof, TxIndex, BLOCK_HASH_LENGTH,
};
use crate::types::time::DateTimeUtc;
use crate::types::token::Amount;
Expand Down Expand Up @@ -532,13 +532,11 @@ where
pub fn write(
&mut self,
key: &Key,
value: impl Into<MerkleValue>,
value: impl AsRef<[u8]>,
) -> Result<(u64, i64)> {
let value: MerkleValue = value.into();
tracing::debug!("storage write key {}", key,);
self.block.tree.update(key, value.clone())?;

let bytes = value.to_bytes();
let bytes = value.as_ref();
self.block.tree.update(key, bytes)?;
let gas = key.len() + bytes.len();
let size_diff =
self.db.write_subspace_val(self.block.height, key, bytes)?;
Expand Down Expand Up @@ -621,7 +619,7 @@ where
pub fn get_existence_proof(
&self,
key: &Key,
value: MerkleValue,
value: Vec<u8>,
height: BlockHeight,
) -> Result<Proof> {
if height >= self.get_block_height().0 {
Expand Down
Loading

0 comments on commit a48251f

Please sign in to comment.