From 8385bed868807c7700ec1fe7958d250257b8a372 Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 5 Jan 2023 16:36:12 +0800 Subject: [PATCH 1/7] move BeefyMmrApi to pallet-beefy-mmr --- Cargo.lock | 7 +- frame/beefy-mmr/Cargo.toml | 2 + frame/beefy-mmr/primitives/Cargo.toml | 12 ++- frame/beefy-mmr/primitives/src/lib.rs | 114 +++++++++++++++----------- frame/beefy-mmr/src/lib.rs | 14 ++++ 5 files changed, 90 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 68c144b3f177e..8e2e9c256868c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -530,10 +530,10 @@ version = "4.0.0-dev" dependencies = [ "array-bytes", "env_logger", + "hash-db", + "hash256-std-hasher", "log", - "sp-api", - "sp-beefy", - "sp-runtime", + "tiny-keccak", ] [[package]] @@ -5450,6 +5450,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "serde", + "sp-api", "sp-beefy", "sp-core", "sp-io", diff --git a/frame/beefy-mmr/Cargo.toml b/frame/beefy-mmr/Cargo.toml index f65270acdc3f8..ce6820f6f2336 100644 --- a/frame/beefy-mmr/Cargo.toml +++ b/frame/beefy-mmr/Cargo.toml @@ -25,6 +25,7 @@ sp-core = { version = "7.0.0", default-features = false, path = "../../primitive sp-io = { version = "7.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" } sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } +sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } [dev-dependencies] array-bytes = "4.1" @@ -49,5 +50,6 @@ std = [ "sp-io/std", "sp-runtime/std", "sp-std/std", + "sp-api/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/beefy-mmr/primitives/Cargo.toml b/frame/beefy-mmr/primitives/Cargo.toml index c087bc2f37cd3..de7e2d3922820 100644 --- a/frame/beefy-mmr/primitives/Cargo.toml +++ b/frame/beefy-mmr/primitives/Cargo.toml @@ -11,10 +11,9 @@ homepage = "https://substrate.io" [dependencies] array-bytes = { version = "4.1", optional = true } log = { version = "0.4", default-features = false, optional = true } - -beefy-primitives = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/beefy", package = "sp-beefy" } -sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" } -sp-runtime = { version = "7.0.0", default-features = false, path = "../../../primitives/runtime" } +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" @@ -24,7 +23,6 @@ env_logger = "0.9" debug = ["array-bytes", "log"] default = ["debug", "std"] std = [ - "beefy-primitives/std", - "sp-api/std", - "sp-runtime/std" + "hash-db/std", + "hash256-std-hasher/std", ] diff --git a/frame/beefy-mmr/primitives/src/lib.rs b/frame/beefy-mmr/primitives/src/lib.rs index e6f8acefb039a..45826926a7853 100644 --- a/frame/beefy-mmr/primitives/src/lib.rs +++ b/frame/beefy-mmr/primitives/src/lib.rs @@ -31,40 +31,71 @@ //! efficient by removing the need to track which side each intermediate hash is concatenated on. //! //! If the number of leaves is not even, last leaf (hash of) is promoted to the upper layer. +#[cfg(not(feature = "std"))] +extern crate alloc; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; -pub use sp_runtime::traits::Keccak256; -use sp_runtime::{app_crypto::sp_core, sp_std, traits::Hash as HashT}; -use sp_std::{vec, 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 { + ::hash(data) + } + } -use beefy_primitives::mmr::{BeefyAuthoritySet, BeefyNextAuthoritySet}; + 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. /// /// In case an empty list of leaves is passed the function returns a 0-filled hash. -pub fn merkle_root(leaves: I) -> H::Output +pub fn merkle_root(leaves: I) -> H::Out where - H: HashT, - H::Output: Default + AsRef<[u8]> + PartialOrd, + H: Hasher, + H::Out: Default + AsRef<[u8]> + PartialOrd, I: IntoIterator, I::Item: AsRef<[u8]>, { - let iter = leaves.into_iter().map(|l| ::hash(l.as_ref())); + let iter = leaves.into_iter().map(|l| ::hash(l.as_ref())); merkelize::(iter, &mut ()).into() } -fn merkelize(leaves: I, visitor: &mut V) -> H::Output +fn merkelize(leaves: I, visitor: &mut V) -> H::Out where - H: HashT, - H::Output: Default + AsRef<[u8]> + PartialOrd, - V: Visitor, - I: Iterator, + H: Hasher, + H::Out: Default + AsRef<[u8]> + PartialOrd, + V: Visitor, + I: Iterator, { let upper = Vec::with_capacity((leaves.size_hint().1.unwrap_or(0).saturating_add(1)) / 2); let mut next = match merkelize_row::(leaves, upper, visitor) { Ok(root) => return root, - Err(next) if next.is_empty() => return H::Output::default(), + Err(next) if next.is_empty() => return H::Out::default(), Err(next) => next, }; @@ -139,17 +170,17 @@ impl Visitor for () { /// # Panic /// /// The function will panic if given `leaf_index` is greater than the number of leaves. -pub fn merkle_proof(leaves: I, leaf_index: usize) -> MerkleProof +pub fn merkle_proof(leaves: I, leaf_index: usize) -> MerkleProof where - H: HashT, - H::Output: Default + Copy + AsRef<[u8]> + PartialOrd, + H: Hasher, + H::Out: Default + Copy + AsRef<[u8]> + PartialOrd, I: IntoIterator, I::IntoIter: ExactSizeIterator, T: AsRef<[u8]>, { let mut leaf = None; let iter = leaves.into_iter().enumerate().map(|(idx, l)| { - let hash = ::hash(l.as_ref()); + let hash = ::hash(l.as_ref()); if idx == leaf_index { leaf = Some(l); } @@ -234,28 +265,28 @@ impl<'a, H, T: AsRef<[u8]>> From<&'a T> for Leaf<'a, H> { /// /// The proof must not contain the root hash. pub fn verify_proof<'a, H, P, L>( - root: &'a H::Output, + root: &'a H::Out, proof: P, number_of_leaves: usize, leaf_index: usize, leaf: L, ) -> bool where - H: HashT, - H::Output: PartialEq + AsRef<[u8]> + PartialOrd, - P: IntoIterator, - L: Into>, + H: Hasher, + H::Out: PartialEq + AsRef<[u8]> + PartialOrd, + P: IntoIterator, + L: Into>, { if leaf_index >= number_of_leaves { return false } let leaf_hash = match leaf.into() { - Leaf::Value(content) => ::hash(content), + Leaf::Value(content) => ::hash(content), Leaf::Hash(hash) => hash, }; - let hash_len = ::LENGTH; + let hash_len = ::LENGTH; let mut combined = vec![0_u8; hash_len * 2]; let computed = proof.into_iter().fold(leaf_hash, |a, b| { if a < b { @@ -265,7 +296,7 @@ where combined[..hash_len].copy_from_slice(&b.as_ref()); combined[hash_len..].copy_from_slice(&a.as_ref()); } - let hash = ::hash(&combined); + let hash = ::hash(&combined); #[cfg(feature = "debug")] log::debug!( "[verify_proof]: (a, b) {:?}, {:?} => {:?} ({:?}) hash", @@ -287,20 +318,20 @@ where /// empty iterator) an `Err` with the inner nodes of upper layer is returned. fn merkelize_row( mut iter: I, - mut next: Vec, + mut next: Vec, visitor: &mut V, -) -> Result> +) -> Result> where - H: HashT, - H::Output: AsRef<[u8]> + PartialOrd, - V: Visitor, - I: Iterator, + H: Hasher, + H::Out: AsRef<[u8]> + PartialOrd + PartialOrd, + V: Visitor, + I: Iterator, { #[cfg(feature = "debug")] log::debug!("[merkelize_row]"); next.clear(); - let hash_len = ::LENGTH; + let hash_len = ::LENGTH; let mut index = 0; let mut combined = vec![0_u8; hash_len * 2]; loop { @@ -326,7 +357,7 @@ where combined[hash_len..].copy_from_slice(a.as_ref()); } - next.push(::hash(&combined)); + next.push(::hash(&combined)); }, // Odd number of items. Promote the item to the upper layer. (Some(a), None) if !next.is_empty() => { @@ -347,24 +378,9 @@ where } } -sp_api::decl_runtime_apis! { - /// API useful for BEEFY light clients. - pub trait BeefyMmrApi - where - BeefyAuthoritySet: sp_api::Decode, - { - /// Return the currently active BEEFY authority set proof. - fn authority_set_proof() -> BeefyAuthoritySet; - - /// Return the next/queued BEEFY authority set proof. - fn next_authority_set_proof() -> BeefyNextAuthoritySet; - } -} - #[cfg(test)] mod tests { use super::*; - use crate::sp_core::H256; #[test] fn should_generate_empty_root() { diff --git a/frame/beefy-mmr/src/lib.rs b/frame/beefy-mmr/src/lib.rs index 0b7fc22cd279b..1bd1528328c06 100644 --- a/frame/beefy-mmr/src/lib.rs +++ b/frame/beefy-mmr/src/lib.rs @@ -207,3 +207,17 @@ impl Pallet { BeefyAuthoritySet { id, len, root } } } + +sp_api::decl_runtime_apis! { + /// API useful for BEEFY light clients. + pub trait BeefyMmrApi + where + BeefyAuthoritySet: sp_api::Decode, + { + /// Return the currently active BEEFY authority set proof. + fn authority_set_proof() -> BeefyAuthoritySet; + + /// Return the next/queued BEEFY authority set proof. + fn next_authority_set_proof() -> BeefyNextAuthoritySet; + } +} From 3f1259788121971a40ad773f030d55ee171e57c7 Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 5 Jan 2023 19:17:11 +0800 Subject: [PATCH 2/7] fix test_utils use pallet-beefy-mmr BeefyMmrApi --- Cargo.lock | 2 +- frame/beefy-mmr/primitives/src/lib.rs | 2 ++ test-utils/runtime/Cargo.toml | 4 ++-- test-utils/runtime/src/lib.rs | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e2e9c256868c..838ff18fecb1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10640,7 +10640,6 @@ dependencies = [ name = "substrate-test-runtime" version = "2.0.0" dependencies = [ - "beefy-merkle-tree", "cfg-if", "frame-support", "frame-system", @@ -10649,6 +10648,7 @@ dependencies = [ "log", "memory-db", "pallet-babe", + "pallet-beefy-mmr", "pallet-timestamp", "parity-scale-codec", "sc-block-builder", diff --git a/frame/beefy-mmr/primitives/src/lib.rs b/frame/beefy-mmr/primitives/src/lib.rs index 45826926a7853..5893517ad0e2c 100644 --- a/frame/beefy-mmr/primitives/src/lib.rs +++ b/frame/beefy-mmr/primitives/src/lib.rs @@ -34,6 +34,8 @@ #[cfg(not(feature = "std"))] extern crate alloc; #[cfg(not(feature = "std"))] +use alloc::vec; +#[cfg(not(feature = "std"))] use alloc::vec::Vec; /// Supported hashing output size. diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index cff0227d03184..9e79319c19cb8 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] beefy-primitives = { version = "4.0.0-dev", default-features = false, path = "../../primitives/beefy", package = "sp-beefy" } -beefy-merkle-tree = { version = "4.0.0-dev", default-features = false, path = "../../frame/beefy-mmr/primitives" } +pallet-beefy-mmr = { version = "4.0.0-dev", default-features = false, path = "../../frame/beefy-mmr" } sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../primitives/application-crypto" } sp-consensus-aura = { version = "0.10.0-dev", default-features = false, path = "../../primitives/consensus/aura" } sp-consensus-babe = { version = "0.10.0-dev", default-features = false, path = "../../primitives/consensus/babe" } @@ -67,7 +67,7 @@ default = [ ] std = [ "beefy-primitives/std", - "beefy-merkle-tree/std", + "pallet-beefy-mmr/std", "sp-application-crypto/std", "sp-consensus-aura/std", "sp-consensus-babe/std", diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 8b64528e243bd..5fc7c5e8558ea 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -978,7 +978,7 @@ cfg_if! { } } - impl beefy_merkle_tree::BeefyMmrApi for Runtime { + impl pallet_beefy_mmr::BeefyMmrApi for Runtime { fn authority_set_proof() -> beefy_primitives::mmr::BeefyAuthoritySet { Default::default() } From 5daf41a8ebc62989aa74817e989a1eaa1e3464ed Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 9 Jan 2023 17:26:14 +0800 Subject: [PATCH 3/7] Move beefy-merkle-tree to utils and Rename to Merkle-tree --- Cargo.lock | 26 +++++++++---------- Cargo.toml | 2 +- frame/beefy-mmr/Cargo.toml | 4 +-- frame/beefy-mmr/src/lib.rs | 2 +- .../merkle-tree}/Cargo.toml | 2 +- .../merkle-tree}/src/lib.rs | 0 6 files changed, 18 insertions(+), 18 deletions(-) rename {frame/beefy-mmr/primitives => utils/merkle-tree}/Cargo.toml (96%) rename {frame/beefy-mmr/primitives => utils/merkle-tree}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 838ff18fecb1f..40b30acf640c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,18 +524,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "beefy-merkle-tree" -version = "4.0.0-dev" -dependencies = [ - "array-bytes", - "env_logger", - "hash-db", - "hash256-std-hasher", - "log", - "tiny-keccak", -] - [[package]] name = "bincode" version = "1.3.3" @@ -4319,6 +4307,18 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" +[[package]] +name = "merkle-tree" +version = "4.0.0-dev" +dependencies = [ + "array-bytes", + "env_logger", + "hash-db", + "hash256-std-hasher", + "log", + "tiny-keccak", +] + [[package]] name = "merlin" version = "2.0.1" @@ -5440,10 +5440,10 @@ name = "pallet-beefy-mmr" version = "4.0.0-dev" dependencies = [ "array-bytes", - "beefy-merkle-tree", "frame-support", "frame-system", "log", + "merkle-tree", "pallet-beefy", "pallet-mmr", "pallet-session", diff --git a/Cargo.toml b/Cargo.toml index 8f55d8e527ecd..4db51f4afc163 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,6 @@ members = [ "frame/balances", "frame/beefy", "frame/beefy-mmr", - "frame/beefy-mmr/primitives", "frame/benchmarking", "frame/bounties", "frame/child-bounties", @@ -246,6 +245,7 @@ members = [ "utils/frame/rpc/client", "utils/prometheus", "utils/wasm-builder", + "utils/merkle-tree", ] # The list of dependencies below (which can be both direct and indirect dependencies) are crates diff --git a/frame/beefy-mmr/Cargo.toml b/frame/beefy-mmr/Cargo.toml index ce6820f6f2336..400793b6aba1e 100644 --- a/frame/beefy-mmr/Cargo.toml +++ b/frame/beefy-mmr/Cargo.toml @@ -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 } -beefy-merkle-tree = { version = "4.0.0-dev", default-features = false, path = "./primitives" } +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" } @@ -35,7 +35,7 @@ sp-staking = { version = "4.0.0-dev", path = "../../primitives/staking" } default = ["std"] std = [ "array-bytes", - "beefy-merkle-tree/std", + "merkle-tree/std", "beefy-primitives/std", "codec/std", "frame-support/std", diff --git a/frame/beefy-mmr/src/lib.rs b/frame/beefy-mmr/src/lib.rs index 1bd1528328c06..4d2cc97bfe0cc 100644 --- a/frame/beefy-mmr/src/lib.rs +++ b/frame/beefy-mmr/src/lib.rs @@ -200,7 +200,7 @@ impl Pallet { .map(T::BeefyAuthorityToMerkleLeaf::convert) .collect::>(); let len = beefy_addresses.len() as u32; - let root = beefy_merkle_tree::merkle_root::<::Hashing, _>( + let root = merkle_tree::merkle_root::<::Hashing, _>( beefy_addresses, ) .into(); diff --git a/frame/beefy-mmr/primitives/Cargo.toml b/utils/merkle-tree/Cargo.toml similarity index 96% rename from frame/beefy-mmr/primitives/Cargo.toml rename to utils/merkle-tree/Cargo.toml index de7e2d3922820..6f82e0a3c0f2b 100644 --- a/frame/beefy-mmr/primitives/Cargo.toml +++ b/utils/merkle-tree/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "beefy-merkle-tree" +name = "merkle-tree" version = "4.0.0-dev" authors = ["Parity Technologies "] edition = "2021" diff --git a/frame/beefy-mmr/primitives/src/lib.rs b/utils/merkle-tree/src/lib.rs similarity index 100% rename from frame/beefy-mmr/primitives/src/lib.rs rename to utils/merkle-tree/src/lib.rs From 3f8979525bde957359cf21d73751fa40aaaaa0d0 Mon Sep 17 00:00:00 2001 From: Davirain Date: Mon, 9 Jan 2023 18:05:17 +0800 Subject: [PATCH 4/7] fix fmt and test --- frame/beefy-mmr/src/lib.rs | 7 +++---- frame/beefy-mmr/src/mock.rs | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/frame/beefy-mmr/src/lib.rs b/frame/beefy-mmr/src/lib.rs index 4d2cc97bfe0cc..f75519ea79654 100644 --- a/frame/beefy-mmr/src/lib.rs +++ b/frame/beefy-mmr/src/lib.rs @@ -200,10 +200,9 @@ impl Pallet { .map(T::BeefyAuthorityToMerkleLeaf::convert) .collect::>(); let len = beefy_addresses.len() as u32; - let root = merkle_tree::merkle_root::<::Hashing, _>( - beefy_addresses, - ) - .into(); + let root = + merkle_tree::merkle_root::<::Hashing, _>(beefy_addresses) + .into(); BeefyAuthoritySet { id, len, root } } } diff --git a/frame/beefy-mmr/src/mock.rs b/frame/beefy-mmr/src/mock.rs index 0a64ad3fc9976..99d84b565d595 100644 --- a/frame/beefy-mmr/src/mock.rs +++ b/frame/beefy-mmr/src/mock.rs @@ -147,7 +147,7 @@ impl BeefyDataProvider> for DummyDataProvider { fn extra_data() -> Vec { let mut col = vec![(15, vec![1, 2, 3]), (5, vec![4, 5, 6])]; col.sort(); - beefy_merkle_tree::merkle_root::<::Hashing, _>( + merkle_tree::merkle_root::<::Hashing, _>( col.into_iter().map(|pair| pair.encode()), ) .as_ref() From aa626dcad45defe18853de1188a0fa9eb04be40a Mon Sep 17 00:00:00 2001 From: Davirain Date: Tue, 10 Jan 2023 21:35:03 +0800 Subject: [PATCH 5/7] Update merkle-tree to binary-merkle-tree and Remove Keccak256 mod from merkle-tree --- Cargo.lock | 26 +++++++++++++------------- frame/beefy-mmr/Cargo.toml | 4 ++-- frame/beefy-mmr/src/lib.rs | 7 ++++--- frame/beefy-mmr/src/mock.rs | 2 +- utils/merkle-tree/Cargo.toml | 11 ++++------- utils/merkle-tree/src/lib.rs | 35 +++-------------------------------- 6 files changed, 27 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40b30acf640c4..ee4b39b6e4ada 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,6 +524,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "binary-merkle-tree" +version = "4.0.0-dev" +dependencies = [ + "array-bytes", + "env_logger", + "hash-db", + "log", + "sp-core", + "sp-runtime", +] + [[package]] name = "bincode" version = "1.3.3" @@ -4307,18 +4319,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" -[[package]] -name = "merkle-tree" -version = "4.0.0-dev" -dependencies = [ - "array-bytes", - "env_logger", - "hash-db", - "hash256-std-hasher", - "log", - "tiny-keccak", -] - [[package]] name = "merlin" version = "2.0.1" @@ -5440,10 +5440,10 @@ name = "pallet-beefy-mmr" version = "4.0.0-dev" dependencies = [ "array-bytes", + "binary-merkle-tree", "frame-support", "frame-system", "log", - "merkle-tree", "pallet-beefy", "pallet-mmr", "pallet-session", diff --git a/frame/beefy-mmr/Cargo.toml b/frame/beefy-mmr/Cargo.toml index 400793b6aba1e..8a240b3356aaa 100644 --- a/frame/beefy-mmr/Cargo.toml +++ b/frame/beefy-mmr/Cargo.toml @@ -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" } @@ -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", diff --git a/frame/beefy-mmr/src/lib.rs b/frame/beefy-mmr/src/lib.rs index f75519ea79654..e5506ecd01d6e 100644 --- a/frame/beefy-mmr/src/lib.rs +++ b/frame/beefy-mmr/src/lib.rs @@ -200,9 +200,10 @@ impl Pallet { .map(T::BeefyAuthorityToMerkleLeaf::convert) .collect::>(); let len = beefy_addresses.len() as u32; - let root = - merkle_tree::merkle_root::<::Hashing, _>(beefy_addresses) - .into(); + let root = binary_merkle_tree::merkle_root::<::Hashing, _>( + beefy_addresses, + ) + .into(); BeefyAuthoritySet { id, len, root } } } diff --git a/frame/beefy-mmr/src/mock.rs b/frame/beefy-mmr/src/mock.rs index 99d84b565d595..2de71cd5b320a 100644 --- a/frame/beefy-mmr/src/mock.rs +++ b/frame/beefy-mmr/src/mock.rs @@ -147,7 +147,7 @@ impl BeefyDataProvider> for DummyDataProvider { fn extra_data() -> Vec { let mut col = vec![(15, vec![1, 2, 3]), (5, vec![4, 5, 6])]; col.sort(); - merkle_tree::merkle_root::<::Hashing, _>( + binary_merkle_tree::merkle_root::<::Hashing, _>( col.into_iter().map(|pair| pair.encode()), ) .as_ref() diff --git a/utils/merkle-tree/Cargo.toml b/utils/merkle-tree/Cargo.toml index 6f82e0a3c0f2b..926f49fbb7ca5 100644 --- a/utils/merkle-tree/Cargo.toml +++ b/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 "] edition = "2021" @@ -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 = [] diff --git a/utils/merkle-tree/src/lib.rs b/utils/merkle-tree/src/lib.rs index 5893517ad0e2c..15db4a4e12e43 100644 --- a/utils/merkle-tree/src/lib.rs +++ b/utils/merkle-tree/src/lib.rs @@ -38,39 +38,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 { - ::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. @@ -175,7 +144,7 @@ impl Visitor for () { pub fn merkle_proof(leaves: I, leaf_index: usize) -> MerkleProof where H: Hasher, - H::Out: Default + Copy + AsRef<[u8]> + PartialOrd, + H::Out: Default + Copy + AsRef<[u8]> + PartialOrd, I: IntoIterator, I::IntoIter: ExactSizeIterator, T: AsRef<[u8]>, @@ -383,6 +352,8 @@ where #[cfg(test)] mod tests { use super::*; + use sp_core::H256; + use sp_runtime::traits::Keccak256; #[test] fn should_generate_empty_root() { From 1932e4a2d4b7cadfc0988e326a1225cc75a402cf Mon Sep 17 00:00:00 2001 From: Davirain Date: Thu, 19 Jan 2023 12:31:43 +0800 Subject: [PATCH 6/7] change merkle-tree name to binary-merkle-tree --- Cargo.toml | 2 +- frame/beefy-mmr/Cargo.toml | 2 +- utils/{merkle-tree => binary-merkle-tree}/Cargo.toml | 0 utils/{merkle-tree => binary-merkle-tree}/src/lib.rs | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename utils/{merkle-tree => binary-merkle-tree}/Cargo.toml (100%) rename utils/{merkle-tree => binary-merkle-tree}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 4db51f4afc163..744f7b7edb5b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -245,7 +245,7 @@ members = [ "utils/frame/rpc/client", "utils/prometheus", "utils/wasm-builder", - "utils/merkle-tree", + "utils/binary-merkle-tree", ] # The list of dependencies below (which can be both direct and indirect dependencies) are crates diff --git a/frame/beefy-mmr/Cargo.toml b/frame/beefy-mmr/Cargo.toml index 8a240b3356aaa..319a9b101774b 100644 --- a/frame/beefy-mmr/Cargo.toml +++ b/frame/beefy-mmr/Cargo.toml @@ -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 } -binary-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/binary-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" } diff --git a/utils/merkle-tree/Cargo.toml b/utils/binary-merkle-tree/Cargo.toml similarity index 100% rename from utils/merkle-tree/Cargo.toml rename to utils/binary-merkle-tree/Cargo.toml diff --git a/utils/merkle-tree/src/lib.rs b/utils/binary-merkle-tree/src/lib.rs similarity index 100% rename from utils/merkle-tree/src/lib.rs rename to utils/binary-merkle-tree/src/lib.rs From 7af6cdf51b4471aa770a33841fda1bcd7aa83b92 Mon Sep 17 00:00:00 2001 From: Davirain Date: Sat, 28 Jan 2023 09:23:15 +0800 Subject: [PATCH 7/7] mirr fix --- utils/binary-merkle-tree/Cargo.toml | 5 ++++- utils/binary-merkle-tree/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/utils/binary-merkle-tree/Cargo.toml b/utils/binary-merkle-tree/Cargo.toml index 926f49fbb7ca5..a59d27fb09bb9 100644 --- a/utils/binary-merkle-tree/Cargo.toml +++ b/utils/binary-merkle-tree/Cargo.toml @@ -22,4 +22,7 @@ sp-runtime = { version = "7.0.0", path = "../../primitives/runtime" } [features] debug = ["array-bytes", "log"] default = ["debug", "std"] -std = [] +std = [ + "log/std", + "hash-db/std" +] diff --git a/utils/binary-merkle-tree/src/lib.rs b/utils/binary-merkle-tree/src/lib.rs index 15db4a4e12e43..42f4e8a5bbf26 100644 --- a/utils/binary-merkle-tree/src/lib.rs +++ b/utils/binary-merkle-tree/src/lib.rs @@ -294,7 +294,7 @@ fn merkelize_row( ) -> Result> where H: Hasher, - H::Out: AsRef<[u8]> + PartialOrd + PartialOrd, + H::Out: AsRef<[u8]> + PartialOrd, V: Visitor, I: Iterator, {