Skip to content

Commit

Permalink
Add thin binary merkle tree wrapper implementing Codec (#1581)
Browse files Browse the repository at this point in the history
* add our own version of binary-merkle-tree

* update Cargo.lock

* update version and add licence header
  • Loading branch information
clangenb authored Feb 29, 2024
1 parent ba391c4 commit 47a6de8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3178,6 +3178,15 @@ dependencies = [
"yasna 0.3.1",
]

[[package]]
name = "itp-binary-merkle-tree"
version = "0.8.0"
dependencies = [
"binary-merkle-tree",
"parity-scale-codec",
"serde 1.0.193",
]

[[package]]
name = "itp-component-container"
version = "0.8.0"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"core/rpc-server",
"core/tls-websocket-server",
"core-primitives/attestation-handler",
"core-primitives/binary-merkle-tree",
"core-primitives/import-queue",
"core-primitives/component-container",
"core-primitives/enclave-api",
Expand Down
17 changes: 17 additions & 0 deletions core-primitives/binary-merkle-tree/Cargo.toml
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",
]
67 changes: 67 additions & 0 deletions core-primitives/binary-merkle-tree/src/lib.rs
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,
}
}
}

0 comments on commit 47a6de8

Please sign in to comment.