Skip to content

Commit

Permalink
refactoring: extract merkle tree as crate (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilb authored and doitian committed Jan 26, 2019
1 parent bc82ad9 commit a159cdf
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 509 deletions.
35 changes: 19 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ members = [
"util/logger",
"util/hash",
"util/merkle-tree",
"util/merkle-root",
"util/jsonrpc-types",
"util/crypto",
"util/dir",
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ bit-vec = "0.5.0"
crossbeam-channel = "0.3"
ckb-util = { path = "../util" }
fnv = "1.0.3"
merkle-root = {path = "../util/merkle-root"}
ckb-merkle-tree = {path = "../util/merkle-tree"}
faster-hex = "0.3"
2 changes: 1 addition & 1 deletion core/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::header::{Header, HeaderBuilder};
use crate::transaction::{ProposalShortId, Transaction};
use crate::uncle::{uncles_hash, UncleBlock};
use ckb_merkle_tree::merkle_root;
use fnv::FnvHashSet;
use merkle_root::merkle_root;
use numext_fixed_hash::H256;
use serde_derive::{Deserialize, Serialize};

Expand Down
3 changes: 1 addition & 2 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ hash = { path = "../util/hash"}
siphasher = "0.3.0"
rand = "0.6"
ckb-util = { path = "../util" }
merkle-tree = { path = "../util/merkle-tree"}
merkle-root = { path = "../util/merkle-root"}
ckb-merkle-tree = { path = "../util/merkle-tree"}
11 changes: 5 additions & 6 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use ckb_core::header::{BlockNumber, Header};
use ckb_core::script::Script;
use ckb_core::transaction::{CellInput, CellOutput, OutPoint, ProposalShortId, Transaction};
use ckb_core::uncle::UncleBlock;
use ckb_merkle_tree::build_merkle_proof;
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use merkle_root::H256Sha3;
use merkle_tree::Tree;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -385,16 +384,16 @@ impl<'a> FilteredBlock<'a> {
})
.collect::<Vec<_>>();

let tree = Tree::<H256Sha3>::new(
let proof = build_merkle_proof(
&block
.commit_transactions()
.iter()
.map(|tx| tx.hash())
.collect::<Vec<_>>(),
transactions_index,
);
let lemmas = tree
.get_proof(transactions_index)
.map(|proof| proof.lemmas)
let lemmas = proof
.map(|proof| proof.lemmas().to_vec())
.unwrap_or_else(Vec::new);

let header = FbsHeader::build(fbb, &block.header());
Expand Down
11 changes: 0 additions & 11 deletions util/merkle-root/Cargo.toml

This file was deleted.

22 changes: 0 additions & 22 deletions util/merkle-root/src/lib.rs

This file was deleted.

6 changes: 3 additions & 3 deletions util/merkle-tree/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "merkle-tree"
name = "ckb-merkle-tree"
version = "0.5.0-pre"
license = "MIT"
authors = ["Nervos Core Dev <[email protected]>"]
edition = "2018"

[dev-dependencies]
proptest = "0.8"
[dependencies]
merkle-cbt = { version="0.1", features = ["sha3"] }
6 changes: 0 additions & 6 deletions util/merkle-tree/src/hash.rs

This file was deleted.

18 changes: 12 additions & 6 deletions util/merkle-tree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
mod hash;
mod proof;
mod tree;
use merkle_cbt::{MerkleProof, MerkleTree, CBMT, H256};

pub use crate::hash::Merge;
pub use crate::proof::Proof;
pub use crate::tree::Tree;
pub fn merkle_root(leaves: &[H256]) -> H256 {
CBMT::build_merkle_root(leaves)
}

pub fn build_merkle_tree(leaves: Vec<H256>) -> MerkleTree<H256> {
CBMT::build_merkle_tree(leaves)
}

pub fn build_merkle_proof(leaves: &[H256], indices: &[usize]) -> Option<MerkleProof<H256>> {
CBMT::build_merkle_proof(leaves, indices)
}
Loading

0 comments on commit a159cdf

Please sign in to comment.