Skip to content
This repository has been archived by the owner on Oct 9, 2022. It is now read-only.

Commit

Permalink
testcase and CI link (#140)
Browse files Browse the repository at this point in the history
* test case for ffi

* lint

* batch test
  • Loading branch information
xiaoch05 authored Jan 4, 2021
1 parent 641e7f6 commit fc57a4d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ jobs:

- name: Run api tests
run: cargo test -p api --release -vv

- name: Run ffi tests
run: cargo test -p ffi --release -vv

- name: Lint
run: cargo clippy --all -- -D warnings
7 changes: 5 additions & 2 deletions ffi/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,12 @@ mod test {

#[test]
fn test_receipt() {
super::receipt(
let result = super::receipt(
"https://ropsten.infura.io/v3/0bfb9acbb13c426097aabb1d81a9d016",
"0x3b82a55f5e752c23359d5c3c4c3360455ce0e485ed37e1faabe9ea10d5db3e7a",
"0xa2284090be23f1092ec861f6e7cd5a0b86e04de677e135a22129c51ea32c1f62",
);
assert_eq!(result.0, "0x0");
assert_eq!(result.1, "0xf90209f90206b873f871a00ceb849936bf7d67a07c693a6e4eef688704b166cb0c5abcbe7ee16dcf33e7f0a0fa0619f124286848a344d4c6716b30cc1e5130c8f7477b82608de322cb2f6646808080808080a0d6fc2005e1035d63817a9cc30b22e3ef4fafc4422d7871282469b5b74b639ebc8080808080808080b9018ef9018b30b90187f901840182667fb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000040000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000f87bf879940800ca66234e4691690485d9e45d8a8c9e6f9b52e1a09cca506f9028bbcc0d976db0eaf80dfe6a6d7cadd99024edd88f690e1eda5541b840734e494b4b4549000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003");
assert_eq!(result.2, "0xabe71afabf25dcae21b880074d25826f996649dd19881ebc4ab6ce0c3aacc226");
}
}
2 changes: 1 addition & 1 deletion ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod ffi;

pub use ffi::{
pub use crate::ffi::{
proof, epoch, receipt, import, start, stop,
};
4 changes: 2 additions & 2 deletions mmr/src/mmr_client_for_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl MmrClientTrait for MmrClientForMysql {
}

/// push elements to mmr
fn batch_push(&mut self, elems: &Vec<[u8; 32]>) -> Result<Vec<u64>> {
fn batch_push(&mut self, elems: Vec<[u8; 32]>) -> Result<Vec<u64>> {
let mut result = vec![];

let mut conn = self.db.get_conn()?;
Expand All @@ -67,7 +67,7 @@ impl MmrClientTrait for MmrClientForMysql {
let mut mmr = MMR::<[u8; 32], MergeHash, _>::new(self.get_mmr_size()?, store);

// let mut root_pos_list = vec![];
for &elem in elems {
for elem in elems {
//let elem = H256::from(elem)?;
let position = mmr.push(elem)?;
// let root = H256::hex(&mmr.get_root()?);
Expand Down
4 changes: 2 additions & 2 deletions mmr/src/mmr_client_for_rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ impl MmrClientTrait for MmrClientForRocksdb {
Ok(position)
}

fn batch_push(&mut self, elems: &Vec<[u8; 32]>) -> Result<Vec<u64>> {
fn batch_push(&mut self, elems: Vec<[u8; 32]>) -> Result<Vec<u64>> {
let mut result = vec![];

let store = RocksdbStore::with(self.db.clone());
let mut mmr = MMR::<[u8; 32], MergeHash, _>::new(self.get_mmr_size()?, store);
for &elem in elems {
for elem in elems {
//let elem = H256::from(elem)?;
let position = mmr.push(elem)?;
result.push(position);
Expand Down
6 changes: 3 additions & 3 deletions mmr/src/mmr_client_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::Instant;

pub trait MmrClientTrait {
fn push(&mut self, elem: &[u8; 32]) -> Result<u64>;
fn batch_push(&mut self, elems: &Vec<[u8; 32]>) -> Result<Vec<u64>>;
fn batch_push(&mut self, elems: Vec<[u8; 32]>) -> Result<Vec<u64>>;
fn get_mmr_size(&self) -> Result<u64>;
fn get_last_leaf_index(&self) -> Result<Option<u64>>;
fn get_elem(&self, pos: u64) -> Result<Option<String>>;
Expand Down Expand Up @@ -39,11 +39,11 @@ pub trait MmrClientTrait {
}
info!("Importing ethereum headers from {:?}", geth_dir);
ffi::import(geth_dir.to_str().unwrap(), from as i32, til_block as i32, BATCH, |hashes| {
if let Err(err) = self.batch_push(&hashes) {
if let Err(err) = self.batch_push(hashes) {
info!("batch push hashes failed, err {}", err);
return false;
}
return true;
true
});
let elapsed = start.elapsed();
info!("batch_push elapsed: {:?}", elapsed);
Expand Down
41 changes: 34 additions & 7 deletions mmr/tests/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use mmr::{
build_client,
Database,
MmrClientTrait,
MergeHash
MergeHash,
RocksdbStore,
RocksBatchStore
};
use std::sync::Arc;
use rocksdb::{Options, DB};
Expand Down Expand Up @@ -93,7 +95,7 @@ fn test_mmr_size_n_leaves() {
});
}

fn rocks_test_client(file: &str) -> (Box<dyn MmrClientTrait>, PathBuf) {
fn rocks_test_client(file: &str) -> (Box<dyn MmrClientTrait>, PathBuf, Arc<DB>) {
use std::fs;
let dbpath = env::temp_dir().join(file);
fs::remove_dir_all(&dbpath).unwrap_or_else(|err|{
Expand All @@ -102,13 +104,13 @@ fn rocks_test_client(file: &str) -> (Box<dyn MmrClientTrait>, PathBuf) {
let rocksdb = DB::open_default(&dbpath).unwrap();
let db = Arc::new(rocksdb);
let clientdb = Database::Rocksdb(db.clone());
return (build_client(&clientdb).unwrap(), dbpath);
return (build_client(&clientdb).unwrap(), dbpath, db);
}

#[test]
fn test_rocks_client_base() {
let db = {
let (mut client, db) = rocks_test_client("test_rocks_client.db");
let (mut client, dbpath, _db) = rocks_test_client("test_rocks_client.db");
let pos: Vec<u64> = (0..10).map(|h| {
client.push(&H256::from(HEADERS_N_ROOTS[h].0).unwrap()).unwrap()
}).collect();
Expand All @@ -129,15 +131,15 @@ fn test_rocks_client_base() {
let mmr_root = client.get_mmr_root(l).unwrap().unwrap();
assert_eq!(mmr_root, HEADERS_N_ROOTS[l as usize].1);
});
db
dbpath
};
assert!(DB::destroy(&Options::default(), &db).is_ok());
}

#[test]
fn test_rocks_client_proof() {
let db = {
let (mut client, db) = rocks_test_client("test_rocks_proof.db");
let (mut client, dbpath, _db) = rocks_test_client("test_rocks_proof.db");
let pos: Vec<u64> = (0..10).map(|h| {
client.push(&H256::from(HEADERS_N_ROOTS[h].0).unwrap()).unwrap()
}).collect();
Expand All @@ -155,8 +157,33 @@ fn test_rocks_client_proof() {
let verify = merkle_proof.verify(H256::from(&mmr_root).unwrap(), vec![(pos[l as usize], H256::from(&HEADERS_N_ROOTS[((l + 1) % 10) as usize].0).unwrap())]).unwrap();
assert_eq!(verify, false);
});
db
dbpath
};
assert!(DB::destroy(&Options::default(), &db).is_ok());
}

#[test]
fn test_rocks_batch_store() {
let db = {
let (mut _client, dbpath, db) = rocks_test_client("test_rocks_batch_store.db");
let bstore = RocksBatchStore::with(db.clone());
let mut mmr = cmmr::MMR::<_, MergeHash, _>::new(0, &bstore);
&HEADERS_N_ROOTS.iter().for_each(|h| {
mmr.push(H256::from(h.0).unwrap()).unwrap();
});
let mmr_size = mmr.mmr_size();

let store = RocksdbStore::with(db.clone());
let mut mmr_size_fromdb = store.get_mmr_size() as u64;
assert_eq!(mmr_size_fromdb, 0);
bstore.start_batch();
mmr.commit().unwrap();
assert_eq!(mmr_size_fromdb, 0);
bstore.commit_batch().unwrap();
mmr_size_fromdb = store.get_mmr_size() as u64;
assert_eq!(mmr_size_fromdb, mmr_size);
assert!(mmr_size_fromdb > 0);
dbpath
};
assert!(DB::destroy(&Options::default(), &db).is_ok());
}
2 changes: 1 addition & 1 deletion src/cmd/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn exec(path: String, to: u64, uri: Option<String>) -> Result<()> {
std::env::set_var("RUST_LOG", "info,darwinia_shadow");
env_logger::init();

if !path.trim_end_matches("/").ends_with("chaindata") {
if !path.trim_end_matches('/').ends_with("chaindata") {
error!("invalid geth data path(must endwith chaindata) {}", path);
return Err(anyhow::anyhow!("invalid geth data path").into());
}
Expand Down

0 comments on commit fc57a4d

Please sign in to comment.