Skip to content

Commit

Permalink
Merge pull request #31 from TheWaWaR/fix-publish
Browse files Browse the repository at this point in the history
fix: prepare for cargo publish
  • Loading branch information
TheWaWaR authored Sep 27, 2022
2 parents ac62640 + 214c0bd commit 6579723
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 29 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ edition = "2018"
license = "MIT"
repository = "https://github.com/nervosnetwork/sparse-merkle-tree"
description = "Sparse merkle tree implement in rust"
exclude = ["/fixtures", "/proptest-regressions"]
include = [
"/src",
"/benches",
"/build.rs",
"/c/deps/ckb-c-stdlib",
"/c/ckb_smt.h",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["std"]
std = []
# SMT implemented in C
smtc = []

[dependencies]
cfg-if = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ test-c-impl:
cd c/rust-tests && cargo test

test-cxx-build:
g++ -c c/rust-tests/src/tests/ckb_smt.c -I c -o smt.o && rm -rf smt.o
g++ -c src/ckb_smt.c -I c -o smt.o && rm -rf smt.o
39 changes: 21 additions & 18 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
fn main() {
println!("cargo:rerun-if-changed=c/ckb_smt.h");
#[cfg(feature = "smtc")]
{
println!("cargo:rerun-if-changed=c/ckb_smt.h");

cc::Build::new()
.file("c/rust-tests/src/tests/ckb_smt.c")
.static_flag(true)
.flag("-O3")
.flag("-fvisibility=hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.include("c/rust-tests/src/tests")
.include("c/")
.include("c/deps/ckb-c-stdlib")
.flag("-Wall")
.flag("-Werror")
.flag("-Wno-unused-parameter")
.flag("-Wno-nonnull")
.define("__SHARED_LIBRARY__", None)
.define("CKB_STDLIB_NO_SYSCALL_IMPL", None)
.compile("smt-c-impl");
cc::Build::new()
.file("src/ckb_smt.c")
.static_flag(true)
.flag("-O3")
.flag("-fvisibility=hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.include("src/")
.include("c/")
.include("c/deps/ckb-c-stdlib")
.flag("-Wall")
.flag("-Werror")
.flag("-Wno-unused-parameter")
.flag("-Wno-nonnull")
.define("__SHARED_LIBRARY__", None)
.define("CKB_STDLIB_NO_SYSCALL_IMPL", None)
.compile("smt-c-impl");
}
}
4 changes: 2 additions & 2 deletions c/rust-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

sparse-merkle-tree = { path = "../../" }
blake2b-rs = "0.2"
proptest = "0.9"
Expand All @@ -19,4 +18,5 @@ serde_json = "1.0"
anyhow = "1.0"

[build-dependencies]
cc = "1.0"
cc = "1.0"

4 changes: 2 additions & 2 deletions c/rust-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ fn main() {
println!("cargo:rerun-if-changed=../ckb_smt.h");

cc::Build::new()
.file("src/tests/ckb_smt.c")
.file("../../src/ckb_smt.c")
.static_flag(true)
.flag("-O3")
.flag("-fvisibility=hidden")
.flag("-fdata-sections")
.flag("-ffunction-sections")
.include("src/tests")
.include("../../src")
.include("..")
.include("../deps/ckb-c-stdlib")
.flag("-Wall")
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod blake2b;
#[cfg(feature = "smtc")]
pub mod ckb_smt;
pub mod default_store;
pub mod error;
Expand All @@ -73,6 +74,7 @@ mod tests;
pub mod traits;
pub mod tree;

#[cfg(feature = "smtc")]
pub use ckb_smt::{SMTBuilder, SMT};
pub use h256::H256;
pub use merkle_proof::{CompiledMerkleProof, MerkleProof};
Expand Down
21 changes: 16 additions & 5 deletions src/tests/smt.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#[cfg(feature = "smtc")]
use std::convert::TryInto;

use crate::*;
use blake2b_rs::{Blake2b, Blake2bBuilder};
use default_store::DefaultStore;
#[cfg(feature = "smtc")]
use hex::decode;
use proptest::prelude::*;
use traits::Hasher;

#[cfg(feature = "smtc")]
fn str_to_h256(src: &str) -> H256 {
let src = decode(src).unwrap();
assert!(src.len() == 32);
let data: [u8; 32] = src.try_into().unwrap();
H256::from(data)
}

#[cfg(feature = "smtc")]
fn str_to_vec(src: &str) -> Vec<u8> {
decode(src).unwrap()
}

#[cfg(feature = "smtc")]
#[test]
fn test_ckb_smt_verify1() {
let key = str_to_h256("381dc5391dab099da5e28acd1ad859a051cf18ace804d037f12819c6fbc0e18b");
Expand All @@ -33,6 +38,7 @@ fn test_ckb_smt_verify1() {
assert!(smt.verify(&root_hash, &proof).is_ok());
}

#[cfg(feature = "smtc")]
#[test]
fn test_ckb_smt_verify2() {
let key = str_to_h256("a9bb945be71f0bd2757d33d2465b6387383da42f321072e47472f0c9c7428a8a");
Expand All @@ -48,6 +54,7 @@ fn test_ckb_smt_verify2() {
assert!(smt.verify(&root_hash, &proof).is_ok());
}

#[cfg(feature = "smtc")]
#[test]
fn test_ckb_smt_verify3() {
let key = str_to_h256("e8c0265680a02b680b6cbc880348f062b825b28e237da7169aded4bcac0a04e5");
Expand All @@ -63,6 +70,7 @@ fn test_ckb_smt_verify3() {
assert!(smt.verify(&root_hash, &proof).is_ok());
}

#[cfg(feature = "smtc")]
#[test]
fn test_ckb_smt_verify_invalid() {
let key = str_to_h256("e8c0265680a02b680b6cbc880348f062b825b28e237da7169aded4bcac0a04e5");
Expand Down Expand Up @@ -136,11 +144,14 @@ proptest! {
.verify::<CkbBlake2bHasher>(tree.root(), vec![(key, value)])
.expect("verify compiled one proof"));

let compiled_proof_bin: Vec<u8> = compiled_proof.into();
let smt_state = SMTBuilder::new();
let smt_state = smt_state.insert(&key, &value).unwrap();
let smt = smt_state.build().unwrap();
smt.verify(tree.root(), &compiled_proof_bin).expect("verify with c");
#[cfg(feature = "smtc")]
{
let compiled_proof_bin: Vec<u8> = compiled_proof.into();
let smt_state = SMTBuilder::new();
let smt_state = smt_state.insert(&key, &value).unwrap();
let smt = smt_state.build().unwrap();
smt.verify(tree.root(), &compiled_proof_bin).expect("verify with c");
}
}
}
}

0 comments on commit 6579723

Please sign in to comment.