Merge commit 'e5bed7ac380b6adb54b60a2a72a2a8f07f50d6c1' as 'bridges'

This commit is contained in:
Hernando Castano
2021-04-21 11:56:23 -04:00
339 changed files with 71658 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,34 @@
[package]
name = "storage-proof-fuzzer"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1" }
finality-grandpa = "0.12.3"
hash-db = "0.15.2"
honggfuzz = "0.5.54"
log = "0.4.0"
env_logger = "0.8.3"
# Bridge Dependencies
bp-header-chain = { path = "../../primitives/header-chain" }
bp-runtime = { path = "../../primitives/runtime" }
bp-test-utils = { path = "../../primitives/test-utils" }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
@@ -0,0 +1,32 @@
# Storage Proof Fuzzer
## How to run?
Install dependencies:
```
$ sudo apt install build-essential binutils-dev libunwind-dev
```
Install `cargo hfuzz` plugin:
```
$ cargo install honggfuzz
```
Run:
```
$ cargo hfuzz run storage-proof-fuzzer
```
Use `HFUZZ_RUN_ARGS` to customize execution:
```
# 1 second of timeout
# use 12 fuzzing thread
# be verbose
# stop after 1000000 fuzzing iteration
# exit upon crash
HFUZZ_RUN_ARGS="-t 1 -n 12 -v -N 1000000 --exit_upon_crash" cargo hfuzz run example
```
More details in the [official documentation](https://docs.rs/honggfuzz/0.5.52/honggfuzz/#about-honggfuzz).
@@ -0,0 +1,84 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Storage Proof Checker fuzzer.
#![warn(missing_docs)]
use honggfuzz::fuzz;
// Logic for checking Substrate storage proofs.
use sp_core::{Blake2Hasher, H256};
use sp_state_machine::{backend::Backend, prove_read, InMemoryBackend};
use sp_std::vec::Vec;
use sp_trie::StorageProof;
use std::collections::HashMap;
fn craft_known_storage_proof(input_vec: Vec<(Vec<u8>, Vec<u8>)>) -> (H256, StorageProof) {
let storage_proof_vec = vec![(
None,
input_vec.iter().map(|x| (x.0.clone(), Some(x.1.clone()))).collect(),
)];
log::info!("Storage proof vec {:?}", storage_proof_vec);
let backend = <InMemoryBackend<Blake2Hasher>>::from(storage_proof_vec);
let root = backend.storage_root(std::iter::empty()).0;
let vector_element_proof = StorageProof::new(
prove_read(backend, input_vec.iter().map(|x| x.0.as_slice()))
.unwrap()
.iter_nodes()
.collect(),
);
(root, vector_element_proof)
}
fn transform_into_unique(input_vec: Vec<(Vec<u8>, Vec<u8>)>) -> Vec<(Vec<u8>, Vec<u8>)> {
let mut output_hashmap = HashMap::new();
let mut output_vec = Vec::new();
for key_value_pair in input_vec.clone() {
output_hashmap.insert(key_value_pair.0, key_value_pair.1); //Only 1 value per key
}
for (key, val) in output_hashmap.iter() {
output_vec.push((key.clone(), val.clone()));
}
output_vec
}
fn run_fuzzer() {
fuzz!(|input_vec: Vec<(Vec<u8>, Vec<u8>)>| {
if input_vec.is_empty() {
return;
}
let unique_input_vec = transform_into_unique(input_vec);
let (root, craft_known_storage_proof) = craft_known_storage_proof(unique_input_vec.clone());
let checker = <bp_runtime::StorageProofChecker<Blake2Hasher>>::new(root, craft_known_storage_proof)
.expect("Valid proof passed; qed");
for key_value_pair in unique_input_vec {
log::info!("Reading value for pair {:?}", key_value_pair);
assert_eq!(
checker.read_value(&key_value_pair.0),
Ok(Some(key_value_pair.1.clone()))
);
}
})
}
fn main() {
env_logger::init();
loop {
run_fuzzer();
}
}