Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Update merkle-tree to binary-merkle-tree and Remove Keccak256 mod fro…
Browse files Browse the repository at this point in the history
…m merkle-tree
  • Loading branch information
DaviRain-Su committed Jan 10, 2023
1 parent 2b9030e commit 30d4ca9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frame/beefy-mmr/Cargo.toml
Expand Up @@ -14,7 +14,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", optional = true }
merkle-tree = { version = "4.0.0-dev", default-features = false, path = "../../utils/merkle-tree" }
binary-merkle-tree = { version = "4.0.0-dev", default-features = false, path = "../../utils/merkle-tree" }
beefy-primitives = { version = "4.0.0-dev", default-features = false, path = "../../primitives/beefy", package = "sp-beefy" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
Expand All @@ -35,7 +35,7 @@ sp-staking = { version = "4.0.0-dev", path = "../../primitives/staking" }
default = ["std"]
std = [
"array-bytes",
"merkle-tree/std",
"binary-merkle-tree/std",
"beefy-primitives/std",
"codec/std",
"frame-support/std",
Expand Down
7 changes: 4 additions & 3 deletions frame/beefy-mmr/src/lib.rs
Expand Up @@ -200,9 +200,10 @@ impl<T: Config> Pallet<T> {
.map(T::BeefyAuthorityToMerkleLeaf::convert)
.collect::<Vec<_>>();
let len = beefy_addresses.len() as u32;
let root =
merkle_tree::merkle_root::<<T as pallet_mmr::Config>::Hashing, _>(beefy_addresses)
.into();
let root = binary_merkle_tree::merkle_root::<<T as pallet_mmr::Config>::Hashing, _>(
beefy_addresses,
)
.into();
BeefyAuthoritySet { id, len, root }
}
}
Expand Down
2 changes: 1 addition & 1 deletion frame/beefy-mmr/src/mock.rs
Expand Up @@ -147,7 +147,7 @@ impl BeefyDataProvider<Vec<u8>> for DummyDataProvider {
fn extra_data() -> Vec<u8> {
let mut col = vec![(15, vec![1, 2, 3]), (5, vec![4, 5, 6])];
col.sort();
merkle_tree::merkle_root::<<Test as pallet_mmr::Config>::Hashing, _>(
binary_merkle_tree::merkle_root::<<Test as pallet_mmr::Config>::Hashing, _>(
col.into_iter().map(|pair| pair.encode()),
)
.as_ref()
Expand Down
11 changes: 4 additions & 7 deletions utils/merkle-tree/Cargo.toml
@@ -1,5 +1,5 @@
[package]
name = "merkle-tree"
name = "binary-merkle-tree"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
Expand All @@ -11,18 +11,15 @@ homepage = "https://substrate.io"
[dependencies]
array-bytes = { version = "4.1", optional = true }
log = { version = "0.4", default-features = false, optional = true }
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
hash-db = { version = "0.15.2", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }

[dev-dependencies]
array-bytes = "4.1"
env_logger = "0.9"
sp-core = { version = "7.0.0", path = "../../primitives/core" }
sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" }

[features]
debug = ["array-bytes", "log"]
default = ["debug", "std"]
std = [
"hash-db/std",
"hash256-std-hasher/std",
]
std = []
35 changes: 3 additions & 32 deletions utils/merkle-tree/src/lib.rs
Expand Up @@ -37,39 +37,8 @@ use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Supported hashing output size.
///
/// The size is restricted to 32 bytes to allow for a more optimised implementation.
pub type Hash = [u8; 32];
use hash_db::Hasher;

mod keccak256 {
use tiny_keccak::{Hasher as _, Keccak};
/// Keccak256 hasher implementation.
pub struct Keccak256;
impl Keccak256 {
/// Hash given data.
pub fn hash(data: &[u8]) -> super::Hash {
<Keccak256 as super::Hasher>::hash(data)
}
}

impl super::Hasher for Keccak256 {
type Out = super::Hash;
type StdHasher = hash256_std_hasher::Hash256StdHasher;
const LENGTH: usize = 32;

fn hash(data: &[u8]) -> super::Hash {
let mut keccak = Keccak::v256();
keccak.update(data);
let mut output = [0_u8; 32];
keccak.finalize(&mut output);
output
}
}
}
pub use keccak256::Keccak256;

/// Construct a root hash of a Binary Merkle Tree created from given leaves.
///
/// See crate-level docs for details about Merkle Tree construction.
Expand Down Expand Up @@ -381,6 +350,8 @@ where
#[cfg(test)]
mod tests {
use super::*;
use sp_core::H256;
use sp_runtime::traits::Keccak256;

#[test]
fn should_generate_empty_root() {
Expand Down Expand Up @@ -778,7 +749,7 @@ mod tests {
"0xA4cDc98593CE52d01Fe5Ca47CB3dA5320e0D7592",
"0xc26B34D375533fFc4c5276282Fa5D660F3d8cbcB",
];
let root: Hash = array_bytes::hex2array_unchecked(
let root: H256 = array_bytes::hex2array_unchecked(
"72b0acd7c302a84f1f6b6cefe0ba7194b7398afb440e1b44a9dbbe270394ca53",
)
.into();
Expand Down

0 comments on commit 30d4ca9

Please sign in to comment.