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

Commit

Permalink
beefy-mmr: remove primitives::Hasher and primitives::Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Sep 30, 2022
1 parent 471881f commit d42a2ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 43 deletions.
48 changes: 14 additions & 34 deletions frame/beefy-mmr/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,15 @@ use beefy_primitives::mmr::{BeefyAuthoritySet, BeefyNextAuthoritySet};
pub use sp_runtime::traits::Keccak256;
use sp_runtime::{app_crypto::sp_core, traits::Hash as HashT};

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

/// Generic hasher trait.
///
/// Implement the function to support custom way of hashing data.
/// The implementation must return a [Hash](type@Hash) type, so only 32-byte output hashes are
/// supported.
pub trait Hasher {
/// Hash given arbitrary-length piece of data.
fn hash(data: &[u8]) -> Hash;
}

/// Keccak256 hasher implementation.
impl Hasher for Keccak256 {
fn hash(data: &[u8]) -> Hash {
<Self as HashT>::hash(data).into()
}
}

/// 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<H, I, T>(leaves: I) -> Hash
pub fn merkle_root<H, Out, I, T>(leaves: I) -> Out
where
H: HashT,
<H as HashT>::Output: Into<Hash>,
H: HashT<Output = Out>,
Out: Default + AsRef<[u8]>,
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
Expand Down Expand Up @@ -166,7 +144,7 @@ impl<T> Visitor<T> for () {
pub fn merkle_proof<H, Out, I, T>(leaves: I, leaf_index: usize) -> MerkleProof<Out, T>
where
H: HashT<Output = Out>,
Out: Default + Copy + Into<Hash> + AsRef<[u8]>,
Out: Default + Copy + AsRef<[u8]>,
I: IntoIterator<Item = T>,
I::IntoIter: ExactSizeIterator,
T: AsRef<[u8]>,
Expand Down Expand Up @@ -374,7 +352,6 @@ sp_api::decl_runtime_apis! {
/// API useful for BEEFY light clients.
pub trait BeefyMmrApi<H>
where
H: From<Hash> + Into<Hash>,
BeefyAuthoritySet<H>: sp_api::Decode,
{
/// Return the currently active BEEFY authority set proof.
Expand All @@ -397,11 +374,11 @@ mod tests {
let data: Vec<[u8; 1]> = Default::default();

// when
let out = merkle_root::<Keccak256, _, _>(data);
let out = merkle_root::<Keccak256, _, _, _>(data);

// then
assert_eq!(
array_bytes::bytes2hex("", &out),
array_bytes::bytes2hex("", out.as_ref()),
"0000000000000000000000000000000000000000000000000000000000000000"
);
}
Expand All @@ -415,11 +392,11 @@ mod tests {
)];

// when
let out = merkle_root::<Keccak256, _, _>(data);
let out = merkle_root::<Keccak256, _, _, _>(data);

// then
assert_eq!(
array_bytes::bytes2hex("", &out),
array_bytes::bytes2hex("", out.as_ref()),
"aeb47a269393297f4b0a3c9c9cfd00c7a4195255274cf39d83dabc2fcc9ff3d7"
);
}
Expand All @@ -434,11 +411,11 @@ mod tests {
];

// when
let out = merkle_root::<Keccak256, _, _>(data);
let out = merkle_root::<Keccak256, _, _, _>(data);

// then
assert_eq!(
array_bytes::bytes2hex("", &out),
array_bytes::bytes2hex("", out.as_ref()),
"697ea2a8fe5b03468548a7a413424a6292ab44a82a6f5cc594c3fa7dda7ce402"
);
}
Expand All @@ -447,7 +424,10 @@ mod tests {
fn should_generate_root_complex() {
let _ = env_logger::try_init();
let test = |root, data| {
assert_eq!(array_bytes::bytes2hex("", &merkle_root::<Keccak256, _, _>(data)), root);
assert_eq!(
array_bytes::bytes2hex("", &merkle_root::<Keccak256, _, _, _>(data).as_ref()),
root
);
};

test(
Expand Down
10 changes: 2 additions & 8 deletions frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ impl<T: Config> LeafDataProvider for Pallet<T> {
impl<T> beefy_primitives::OnNewValidatorSet<<T as pallet_beefy::Config>::BeefyId> for Pallet<T>
where
T: pallet::Config,
<T as pallet_mmr::Config>::Hashing: beefy_merkle_tree::Hasher,
beefy_merkle_tree::Hash: Into<MerkleRootOf<T>>,
{
/// Compute and cache BEEFY authority sets based on updated BEEFY validator sets.
fn on_new_validator_set(
Expand All @@ -179,11 +177,7 @@ where
}
}

impl<T: Config> Pallet<T>
where
<T as pallet_mmr::Config>::Hashing: beefy_merkle_tree::Hasher,
beefy_merkle_tree::Hash: Into<MerkleRootOf<T>>,
{
impl<T: Config> Pallet<T> {
/// Return the currently active BEEFY authority set proof.
pub fn authority_set_proof() -> BeefyAuthoritySet<MerkleRootOf<T>> {
Pallet::<T>::beefy_authorities()
Expand All @@ -210,7 +204,7 @@ where
.map(T::BeefyAuthorityToMerkleLeaf::convert)
.collect::<Vec<_>>();
let len = beefy_addresses.len() as u32;
let root = beefy_merkle_tree::merkle_root::<<T as pallet_mmr::Config>::Hashing, _, _>(
let root = beefy_merkle_tree::merkle_root::<<T as pallet_mmr::Config>::Hashing, _, _, _>(
beefy_addresses,
)
.into();
Expand Down
3 changes: 2 additions & 1 deletion frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ 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();
beefy_merkle_tree::merkle_root::<<Test as pallet_mmr::Config>::Hashing, _, _>(
beefy_merkle_tree::merkle_root::<<Test as pallet_mmr::Config>::Hashing, _, _, _>(
col.into_iter().map(|pair| pair.encode()),
)
.as_ref()
.to_vec()
}
}
Expand Down

0 comments on commit d42a2ff

Please sign in to comment.