Skip to content

Commit 6579723

Browse files
authored
Merge pull request #31 from TheWaWaR/fix-publish
fix: prepare for cargo publish
2 parents ac62640 + 214c0bd commit 6579723

File tree

9 files changed

+53
-29
lines changed

9 files changed

+53
-29
lines changed

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ edition = "2018"
66
license = "MIT"
77
repository = "https://github.com/nervosnetwork/sparse-merkle-tree"
88
description = "Sparse merkle tree implement in rust"
9-
exclude = ["/fixtures", "/proptest-regressions"]
9+
include = [
10+
"/src",
11+
"/benches",
12+
"/build.rs",
13+
"/c/deps/ckb-c-stdlib",
14+
"/c/ckb_smt.h",
15+
]
1016

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

1319
[features]
1420
default = ["std"]
1521
std = []
22+
# SMT implemented in C
23+
smtc = []
1624

1725
[dependencies]
1826
cfg-if = "0.1"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ test-c-impl:
2020
cd c/rust-tests && cargo test
2121

2222
test-cxx-build:
23-
g++ -c c/rust-tests/src/tests/ckb_smt.c -I c -o smt.o && rm -rf smt.o
23+
g++ -c src/ckb_smt.c -I c -o smt.o && rm -rf smt.o

build.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
fn main() {
2-
println!("cargo:rerun-if-changed=c/ckb_smt.h");
2+
#[cfg(feature = "smtc")]
3+
{
4+
println!("cargo:rerun-if-changed=c/ckb_smt.h");
35

4-
cc::Build::new()
5-
.file("c/rust-tests/src/tests/ckb_smt.c")
6-
.static_flag(true)
7-
.flag("-O3")
8-
.flag("-fvisibility=hidden")
9-
.flag("-fdata-sections")
10-
.flag("-ffunction-sections")
11-
.include("c/rust-tests/src/tests")
12-
.include("c/")
13-
.include("c/deps/ckb-c-stdlib")
14-
.flag("-Wall")
15-
.flag("-Werror")
16-
.flag("-Wno-unused-parameter")
17-
.flag("-Wno-nonnull")
18-
.define("__SHARED_LIBRARY__", None)
19-
.define("CKB_STDLIB_NO_SYSCALL_IMPL", None)
20-
.compile("smt-c-impl");
6+
cc::Build::new()
7+
.file("src/ckb_smt.c")
8+
.static_flag(true)
9+
.flag("-O3")
10+
.flag("-fvisibility=hidden")
11+
.flag("-fdata-sections")
12+
.flag("-ffunction-sections")
13+
.include("src/")
14+
.include("c/")
15+
.include("c/deps/ckb-c-stdlib")
16+
.flag("-Wall")
17+
.flag("-Werror")
18+
.flag("-Wno-unused-parameter")
19+
.flag("-Wno-nonnull")
20+
.define("__SHARED_LIBRARY__", None)
21+
.define("CKB_STDLIB_NO_SYSCALL_IMPL", None)
22+
.compile("smt-c-impl");
23+
}
2124
}

c/rust-tests/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
1110
sparse-merkle-tree = { path = "../../" }
1211
blake2b-rs = "0.2"
1312
proptest = "0.9"
@@ -19,4 +18,5 @@ serde_json = "1.0"
1918
anyhow = "1.0"
2019

2120
[build-dependencies]
22-
cc = "1.0"
21+
cc = "1.0"
22+

c/rust-tests/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ fn main() {
22
println!("cargo:rerun-if-changed=../ckb_smt.h");
33

44
cc::Build::new()
5-
.file("src/tests/ckb_smt.c")
5+
.file("../../src/ckb_smt.c")
66
.static_flag(true)
77
.flag("-O3")
88
.flag("-fvisibility=hidden")
99
.flag("-fdata-sections")
1010
.flag("-ffunction-sections")
11-
.include("src/tests")
11+
.include("../../src")
1212
.include("..")
1313
.include("../deps/ckb-c-stdlib")
1414
.flag("-Wall")
File renamed without changes.
File renamed without changes.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#![cfg_attr(not(feature = "std"), no_std)]
6363

6464
pub mod blake2b;
65+
#[cfg(feature = "smtc")]
6566
pub mod ckb_smt;
6667
pub mod default_store;
6768
pub mod error;
@@ -73,6 +74,7 @@ mod tests;
7374
pub mod traits;
7475
pub mod tree;
7576

77+
#[cfg(feature = "smtc")]
7678
pub use ckb_smt::{SMTBuilder, SMT};
7779
pub use h256::H256;
7880
pub use merkle_proof::{CompiledMerkleProof, MerkleProof};

src/tests/smt.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
#[cfg(feature = "smtc")]
12
use std::convert::TryInto;
23

34
use crate::*;
45
use blake2b_rs::{Blake2b, Blake2bBuilder};
56
use default_store::DefaultStore;
7+
#[cfg(feature = "smtc")]
68
use hex::decode;
79
use proptest::prelude::*;
810
use traits::Hasher;
911

12+
#[cfg(feature = "smtc")]
1013
fn str_to_h256(src: &str) -> H256 {
1114
let src = decode(src).unwrap();
1215
assert!(src.len() == 32);
1316
let data: [u8; 32] = src.try_into().unwrap();
1417
H256::from(data)
1518
}
1619

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

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

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

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

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

139-
let compiled_proof_bin: Vec<u8> = compiled_proof.into();
140-
let smt_state = SMTBuilder::new();
141-
let smt_state = smt_state.insert(&key, &value).unwrap();
142-
let smt = smt_state.build().unwrap();
143-
smt.verify(tree.root(), &compiled_proof_bin).expect("verify with c");
147+
#[cfg(feature = "smtc")]
148+
{
149+
let compiled_proof_bin: Vec<u8> = compiled_proof.into();
150+
let smt_state = SMTBuilder::new();
151+
let smt_state = smt_state.insert(&key, &value).unwrap();
152+
let smt = smt_state.build().unwrap();
153+
smt.verify(tree.root(), &compiled_proof_bin).expect("verify with c");
154+
}
144155
}
145156
}
146157
}

0 commit comments

Comments
 (0)