From 32662a0a16ff6e862ad875fa582ba24ba9544592 Mon Sep 17 00:00:00 2001 From: hashmap Date: Thu, 9 Aug 2018 11:37:11 +0200 Subject: [PATCH] Update fuzz in core crate Fix compilation error in fuzz code after the wallet refactoring --- core/fuzz/Cargo.toml | 6 ++++-- core/fuzz/README.md | 9 ++++++++- core/fuzz/src/main.rs | 37 ++++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/core/fuzz/Cargo.toml b/core/fuzz/Cargo.toml index ef0503218..cf1c472ba 100644 --- a/core/fuzz/Cargo.toml +++ b/core/fuzz/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grin_core-fuzz" -version = "0.0.1" +version = "0.0.3" authors = ["Grin Developers "] publish = false @@ -10,7 +10,9 @@ cargo-fuzz = true [dependencies] grin_core = { path = ".."} grin_keychain = { path = "../../keychain"} -libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } +grin_wallet = { path = "../../wallet"} +[dependencies.libfuzzer-sys] +git = "https://github.com/rust-fuzz/libfuzzer-sys.git" # Prevent this from interfering with workspaces [workspace] diff --git a/core/fuzz/README.md b/core/fuzz/README.md index 6046ce661..06f0b883b 100644 --- a/core/fuzz/README.md +++ b/core/fuzz/README.md @@ -1,6 +1,7 @@ # Fuzz testing ## Installation +You have to use Rust nightly at the moment. Cargo-fuzz (https://github.com/rust-fuzz/cargo-fuzz) has been used. To install it: @@ -24,6 +25,7 @@ Fuzz test is basically infinite test, run it for some period of time then stop if no failures are found. To run the tests make sure youre in folder `core` otherwise you may get some misleading errors, then run one of the following tests: + ``` cargo fuzz run tx_read @@ -32,4 +34,9 @@ cargo fuzz run block_read cargo fuzz run compact_block_read ``` -Check `fuzz/Cargo.toml` for the full list of targets. + +Run +``` +cargo fuzz list +``` +or check `fuzz/Cargo.toml` for the full list of targets. diff --git a/core/fuzz/src/main.rs b/core/fuzz/src/main.rs index e41aed8df..3b1306127 100644 --- a/core/fuzz/src/main.rs +++ b/core/fuzz/src/main.rs @@ -1,11 +1,14 @@ extern crate grin_core; extern crate grin_keychain; +extern crate grin_wallet; -use grin_core::core::build::{input, output, transaction_with_offset, with_fee}; use grin_core::core::target::Difficulty; use grin_core::core::{Block, BlockHeader, CompactBlock, Transaction}; use grin_core::ser; -use grin_keychain::keychain::Keychain; +use grin_keychain::keychain::ExtKeychain; +use grin_keychain::Keychain; +use grin_wallet::libtx::build::{input, output, transaction_with_offset, with_fee}; +use grin_wallet::libtx::reward; use std::fs::{self, File}; use std::path::Path; @@ -18,12 +21,16 @@ fn main() { fn generate(target: &str, obj: W) -> Result<(), ser::Error> { let dir_path = Path::new("corpus").join(target); if !dir_path.is_dir() { - fs::create_dir(&dir_path).map_err(|e| ser::Error::IOErr(e))?; + fs::create_dir_all(&dir_path).map_err(|e| { + println!("fail: {}", e); + ser::Error::IOErr("can't create corpus directory".to_owned(), e.kind()) + })?; } let pattern_path = dir_path.join("pattern"); if !pattern_path.exists() { - let mut file = File::create(&pattern_path).map_err(|e| ser::Error::IOErr(e))?; + let mut file = File::create(&pattern_path) + .map_err(|e| ser::Error::IOErr("can't create a pattern file".to_owned(), e.kind()))?; ser::serialize(&mut file, &obj) } else { Ok(()) @@ -31,19 +38,19 @@ fn generate(target: &str, obj: W) -> Result<(), ser::Error> { } fn block() -> Block { - let keychain = Keychain::from_random_seed().unwrap(); + let keychain = ExtKeychain::from_random_seed().unwrap(); let key_id = keychain.derive_key_id(1).unwrap(); - let mut tx1 = tx(); - let mut tx2 = tx(); + let mut txs = Vec::new(); + for _ in 1..10 { + txs.push(tx()); + } - Block::new( - &BlockHeader::default(), - vec![&mut tx1, &mut tx2], - &keychain, - &key_id, - Difficulty::one(), - ).unwrap() + let header = BlockHeader::default(); + + let reward = reward::output(&keychain, &key_id, 0, header.height).unwrap(); + + Block::new(&header, txs, Difficulty::one(), reward).unwrap() } fn compact_block() -> CompactBlock { @@ -57,7 +64,7 @@ fn compact_block() -> CompactBlock { } fn tx() -> Transaction { - let keychain = Keychain::from_random_seed().unwrap(); + let keychain = ExtKeychain::from_random_seed().unwrap(); let key_id1 = keychain.derive_key_id(1).unwrap(); let key_id2 = keychain.derive_key_id(2).unwrap(); let key_id3 = keychain.derive_key_id(3).unwrap();