diff --git a/Cargo.lock b/Cargo.lock index 060a91ce78..7ab4f65fe1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4897,9 +4897,12 @@ dependencies = [ name = "namada_fuzz" version = "0.0.0" dependencies = [ + "data-encoding", "lazy_static", "libfuzzer-sys", "masp_primitives", + "namada_apps_lib", + "namada_core", "namada_node", "namada_tx", ] diff --git a/Makefile b/Makefile index 4f00cb37d3..64c0a9caaf 100644 --- a/Makefile +++ b/Makefile @@ -284,6 +284,9 @@ fuzz-txs-prepare-proposal: fuzz-txs-process-proposal: $(cargo) +$(nightly) fuzz run txs_process_proposal --dev +fuzz-txs-finalize-block: + $(cargo) +$(nightly) fuzz run txs_finalize_block --dev + build-doc: $(cargo) doc --no-deps diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index d2991b4253..5718758a65 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -8,8 +8,12 @@ edition = "2021" cargo-fuzz = true [dependencies] +namada_apps_lib = { path = "../crates/apps_lib", features = ["testing"] } +namada_core = { path = "../crates/core", features = ["testing"] } namada_node = { path = "../crates/node", features = ["testing"] } namada_tx = { path = "../crates/tx", features = ["arbitrary"] } + +data-encoding = { workspace = true } masp_primitives = { workspace = true, features = ["arbitrary"] } lazy_static.workspace = true @@ -35,3 +39,10 @@ path = "fuzz_targets/txs_process_proposal.rs" test = false doc = false bench = false + +[[bin]] +name = "txs_finalize_block" +path = "fuzz_targets/txs_finalize_block.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/txs_finalize_block.rs b/fuzz/fuzz_targets/txs_finalize_block.rs new file mode 100644 index 0000000000..d60277fa7c --- /dev/null +++ b/fuzz/fuzz_targets/txs_finalize_block.rs @@ -0,0 +1,70 @@ +#![no_main] +#![allow(clippy::disallowed_methods)] + +use data_encoding::HEXUPPER; +use libfuzzer_sys::fuzz_target; +use namada_apps_lib::wallet; +use namada_core::address::Address; +use namada_core::key::PublicKeyTmRawHash; +use namada_core::time::DateTimeUtc; +use namada_node::shell; +use namada_node::shell::test_utils::TestShell; +use namada_node::shims::abcipp_shim_types::shim::request::{ + FinalizeBlock, ProcessedTx, +}; +use namada_node::shims::abcipp_shim_types::shim::TxBytes; +use namada_tx::data::TxType; +use namada_tx::Tx; + +static mut SHELL: Option = None; + +fuzz_target!(|txs: Vec| { + let mut txs_bytes: Vec = Vec::with_capacity(txs.len()); + for tx in txs { + // Skip raw transactions, they should never be included by an honest + // prepare_proposal + if let TxType::Raw = tx.header().tx_type { + continue; + } + // Only use transactions that can be encoded + if let Ok(tx_bytes) = tx.try_to_bytes() { + txs_bytes.push(tx_bytes.into()); + } + } + + let shell = unsafe { + match SHELL.as_mut() { + Some(shell) => shell, + None => { + let (shell, _recv, _, _) = shell::test_utils::setup(); + SHELL = Some(shell); + SHELL.as_mut().unwrap() + } + } + }; + + let proposer_pk = wallet::defaults::validator_keypair().to_public(); + let proposer_address = Address::from(&proposer_pk); + let block_time = DateTimeUtc::now(); + let processing_results = + shell.process_txs(&txs_bytes, block_time, &proposer_address); + let mut txs = Vec::with_capacity(txs_bytes.len()); + for (result, tx) in + processing_results.into_iter().zip(txs_bytes.into_iter()) + { + txs.push(ProcessedTx { tx, result }); + } + + let proposer_address_bytes = HEXUPPER + .decode(proposer_pk.tm_raw_hash().as_bytes()) + .unwrap(); + let req = FinalizeBlock { + txs, + proposer_address: proposer_address_bytes, + ..Default::default() + }; + let _events = shell.finalize_block(req).unwrap(); + + // Commit the block + shell.commit(); +}); diff --git a/wasm/Makefile b/wasm/Makefile index 6c38626701..d4c5b0a52f 100644 --- a/wasm/Makefile +++ b/wasm/Makefile @@ -16,7 +16,7 @@ check: $(cargo) +$(nightly) check --workspace --target wasm32-unknown-unknown clippy: - $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings + $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings --check-cfg 'cfg(fuzzing)' clippy-fix: $(cargo) +$(nightly) clippy --fix -Z unstable-options --workspace --allow-dirty --allow-staged diff --git a/wasm_for_tests/Makefile b/wasm_for_tests/Makefile index 67a95dbbc1..71b418cc19 100644 --- a/wasm_for_tests/Makefile +++ b/wasm_for_tests/Makefile @@ -39,7 +39,7 @@ check: $(cargo) +$(nightly) check --workspace --target wasm32-unknown-unknown clippy: - $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings + $(cargo) +$(nightly) clippy --all-targets --workspace -- -D warnings --check-cfg 'cfg(fuzzing)' clippy-fix: $(cargo) +$(nightly) clippy --fix -Z unstable-options --workspace --allow-dirty --allow-staged