Skip to content

Commit 47a6de8

Browse files
authored
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
1 parent ba391c4 commit 47a6de8

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,6 +3178,15 @@ dependencies = [
31783178
"yasna 0.3.1",
31793179
]
31803180

3181+
[[package]]
3182+
name = "itp-binary-merkle-tree"
3183+
version = "0.8.0"
3184+
dependencies = [
3185+
"binary-merkle-tree",
3186+
"parity-scale-codec",
3187+
"serde 1.0.193",
3188+
]
3189+
31813190
[[package]]
31823191
name = "itp-component-container"
31833192
version = "0.8.0"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ members = [
1818
"core/rpc-server",
1919
"core/tls-websocket-server",
2020
"core-primitives/attestation-handler",
21+
"core-primitives/binary-merkle-tree",
2122
"core-primitives/import-queue",
2223
"core-primitives/component-container",
2324
"core-primitives/enclave-api",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "itp-binary-merkle-tree"
3+
version = "0.8.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"], package = "parity-scale-codec" }
8+
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
9+
10+
binary-merkle-tree = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
11+
12+
[features]
13+
std = [
14+
"parity-scale-codec/std",
15+
"serde/std",
16+
"binary-merkle-tree/std",
17+
]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2021 Integritee AG and Supercomputing Systems AG
3+
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
*/
18+
19+
// Todo: I think we can upstream the codec change, then we can delete this crate.
20+
21+
#![cfg_attr(not(feature = "std"), no_std)]
22+
23+
#[cfg(not(feature = "std"))]
24+
extern crate alloc;
25+
#[cfg(not(feature = "std"))]
26+
use alloc::vec::Vec;
27+
28+
use parity_scale_codec::{Decode, Encode};
29+
use serde::{Deserialize, Serialize};
30+
31+
// re-export the original one implementing all the merkle/logic.
32+
pub use binary_merkle_tree::{merkle_proof, merkle_root, verify_proof, MerkleProof};
33+
34+
/// Custom Merkle proof that implements codec
35+
/// The difference to the original one is that implements the scale-codec and that the fields contain u32 instead of usize.
36+
#[derive(Debug, PartialEq, Eq, Decode, Encode, Deserialize, Serialize)]
37+
pub struct MerkleProofWithCodec<H, L> {
38+
/// Root hash of generated merkle tree.
39+
pub root: H,
40+
/// Proof items (does not contain the leaf hash, nor the root obviously).
41+
///
42+
/// This vec contains all inner node hashes necessary to reconstruct the root hash given the
43+
/// leaf hash.
44+
pub proof: Vec<H>,
45+
/// Number of leaves in the original tree.
46+
///
47+
/// This is needed to detect a case where we have an odd number of leaves that "get promoted"
48+
/// to upper layers.
49+
pub number_of_leaves: u64,
50+
/// Index of the leaf the proof is for (0-based).
51+
pub leaf_index: u64,
52+
/// Leaf content.
53+
pub leaf: L,
54+
}
55+
56+
impl<H, L> From<MerkleProof<H, L>> for MerkleProofWithCodec<H, L> {
57+
fn from(source: MerkleProof<H, L>) -> Self {
58+
Self {
59+
root: source.root,
60+
proof: source.proof,
61+
// usize as u64 can't panic
62+
number_of_leaves: source.number_of_leaves as u64,
63+
leaf_index: source.leaf_index as u64,
64+
leaf: source.leaf,
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)