-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add thin binary merkle tree wrapper implementing
Codec
(#1581)
* add our own version of binary-merkle-tree * update Cargo.lock * update version and add licence header
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "itp-binary-merkle-tree" | ||
version = "0.8.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"], package = "parity-scale-codec" } | ||
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] } | ||
|
||
binary-merkle-tree = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" } | ||
|
||
[features] | ||
std = [ | ||
"parity-scale-codec/std", | ||
"serde/std", | ||
"binary-merkle-tree/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2021 Integritee AG and Supercomputing Systems AG | ||
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Todo: I think we can upstream the codec change, then we can delete this crate. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
#[cfg(not(feature = "std"))] | ||
extern crate alloc; | ||
#[cfg(not(feature = "std"))] | ||
use alloc::vec::Vec; | ||
|
||
use parity_scale_codec::{Decode, Encode}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// re-export the original one implementing all the merkle/logic. | ||
pub use binary_merkle_tree::{merkle_proof, merkle_root, verify_proof, MerkleProof}; | ||
|
||
/// Custom Merkle proof that implements codec | ||
/// The difference to the original one is that implements the scale-codec and that the fields contain u32 instead of usize. | ||
#[derive(Debug, PartialEq, Eq, Decode, Encode, Deserialize, Serialize)] | ||
pub struct MerkleProofWithCodec<H, L> { | ||
/// Root hash of generated merkle tree. | ||
pub root: H, | ||
/// Proof items (does not contain the leaf hash, nor the root obviously). | ||
/// | ||
/// This vec contains all inner node hashes necessary to reconstruct the root hash given the | ||
/// leaf hash. | ||
pub proof: Vec<H>, | ||
/// Number of leaves in the original tree. | ||
/// | ||
/// This is needed to detect a case where we have an odd number of leaves that "get promoted" | ||
/// to upper layers. | ||
pub number_of_leaves: u64, | ||
/// Index of the leaf the proof is for (0-based). | ||
pub leaf_index: u64, | ||
/// Leaf content. | ||
pub leaf: L, | ||
} | ||
|
||
impl<H, L> From<MerkleProof<H, L>> for MerkleProofWithCodec<H, L> { | ||
fn from(source: MerkleProof<H, L>) -> Self { | ||
Self { | ||
root: source.root, | ||
proof: source.proof, | ||
// usize as u64 can't panic | ||
number_of_leaves: source.number_of_leaves as u64, | ||
leaf_index: source.leaf_index as u64, | ||
leaf: source.leaf, | ||
} | ||
} | ||
} |