Skip to content

Commit

Permalink
fuzz: add txs_finalize_block target
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 16, 2024
1 parent 422e2af commit b18507a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
70 changes: 70 additions & 0 deletions fuzz/fuzz_targets/txs_finalize_block.rs
Original file line number Diff line number Diff line change
@@ -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<TestShell> = None;

fuzz_target!(|txs: Vec<Tx>| {
let mut txs_bytes: Vec<TxBytes> = 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();
});
2 changes: 1 addition & 1 deletion wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wasm_for_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b18507a

Please sign in to comment.