Skip to content

Model testing #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ warp = "0.3.7"
flate2 = "1.0.35"
prometheus = "0.13.4"
ctor = "0.2"
quickcheck = "1.0.3"
rand = "0.8"

libc = { version = "0.2.161" }
lazy_static = "1.4.0"
Expand Down
4 changes: 3 additions & 1 deletion crates/eth-sparse-mpt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ hash-db = { version = "0.15.2", optional = true }
triehash = { version = "0.8.4", optional = true }
flate2 = { workspace = true, optional = true }

quickcheck = { workspace = true }
rand = { workspace = true }

[features]
benchmark-utils = ["dep:hash-db", "dep:triehash", "dep:flate2"]

Expand All @@ -69,4 +72,3 @@ harness = false
[[bench]]
name = "trie_do_bench"
harness = false

2 changes: 2 additions & 0 deletions crates/eth-sparse-mpt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use reth_provider::{
StateCommitmentProvider,
};

pub mod model_test;

#[cfg(any(test, feature = "benchmark-utils"))]
pub mod test_utils;
pub mod utils;
Expand Down
150 changes: 150 additions & 0 deletions crates/eth-sparse-mpt/src/model_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use crate::v1::sparse_mpt::{DeletionError as DeletionErrorV1, DiffTrie};
use crate::v2::trie::{DeletionError as DeletionErrorV2, Trie};

use alloy_primitives::{hex, keccak256, Bytes, FixedBytes};
use quickcheck::{quickcheck, Arbitrary, Gen};
use std::collections::HashMap;

#[derive(Clone, Debug)]
enum Op {
Insert(FixedKey, Vec<u8>),
Delete(FixedKey),
}

// helper trait to extend `choose` with exception handling
trait ChooseNonempty {
fn one_of<'a, T>(&'a mut self, entries: &'a [T]) -> &'a T;
}

impl ChooseNonempty for Gen {
fn one_of<'a, T>(&'a mut self, entries: &'a [T]) -> &'a T {
self.choose(entries).expect("empty list in choose nonempty")
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct FixedKey(FixedBytes<32>);

impl FixedKey {
fn as_slice(&self) -> &[u8; 32] {
&self.0
}

fn from_string(s: &str) -> Self {
Self(keccak256(s))
}

fn from_bytes(bytes: [u8; 32]) -> Self {
Self(FixedBytes::new(bytes))
}

fn into_bytes(self) -> Bytes {
Bytes::from(self.0)
}
}

impl From<Bytes> for FixedKey {
fn from(bytes: Bytes) -> FixedKey {
let fbytes = FixedBytes::from_slice(bytes.as_ref());
FixedKey(fbytes)
}
}

// We chose a small number of keys, to make sure our error cases handle key collisions,
// as well as shared prefixes, properties that would be very unlikely for random keys
impl Arbitrary for FixedKey {
fn arbitrary(g: &mut Gen) -> Self {
let keys = [
FixedKey::from_bytes(hex!(
"0000000000000000000000000000000000000000000000000000000000000000"
)),
FixedKey::from_bytes(hex!(
"0000000000000000000000000000000000000000000000000000000000000001"
)),
FixedKey::from_bytes(hex!(
"0000000000000000000000000000001000000000000000000000000000000001"
)),
FixedKey::from_string("0"),
FixedKey::from_string("1"),
FixedKey::from_string("2"),
FixedKey::from_string("3"),
FixedKey::from_string("4"),
FixedKey::from_string("5"),
FixedKey::from_string("6"),
FixedKey::from_string("7"),
];
*g.one_of(&keys)
}
}

impl Arbitrary for Op {
fn arbitrary(g: &mut Gen) -> Self {
// pick a random key to perform an operation on
let key = FixedKey::arbitrary(g);

if *g.one_of(&[true, false]) {
Op::Insert(key, "value".into())
} else {
Op::Delete(key)
}
}
}

/// This test fails, since the Trie is designed for fixed key sizes
#[ignore]
#[test]
fn crash_example_v2() {
let mut trie = Trie::new_empty();
trie.insert(b"00aeee", b"ok").unwrap();
trie.insert(b"00ae", b"ok").unwrap();
}

quickcheck! {
fn model_test_v1_map(ops: Vec<Op>) -> bool {
let mut model = HashMap::new();
let mut implementation = DiffTrie::new_empty();

for op in ops {
match op {
Op::Insert(key, value) => {
implementation.insert(key.into_bytes(), Bytes::from(value.clone())).unwrap();
model.insert(key, value);
}
Op::Delete(key) => {
match (implementation.delete(key.into_bytes()), model.remove(&key)) {
(Err(DeletionErrorV1::KeyNotFound), None) => (),
(Err(err), _) => panic!("Implementation error {err:?}"),
(Ok(_), Some(_)) => (),
(Ok(returned), None) => panic!("Implementation returned {returned:?} on delete"),
}
}
}
}
true
}
}

quickcheck! {
fn model_test_v2_map(ops: Vec<Op>) -> bool {
let mut model = HashMap::new();
let mut implementation = Trie::new_empty();

for op in ops {
match op {
Op::Insert(k, v) => {
implementation.insert(k.as_slice(), v.as_slice()).unwrap();
model.insert(k, v);
}
Op::Delete(k) => {
match (implementation.delete(k.as_slice()), model.remove(&k)) {
(Err(DeletionErrorV2::KeyNotFound), None) => (),
(Err(e), _) => panic!("Implementation error {e:?}"),
(Ok(_), Some(_)) => (),
(Ok(a), None) => panic!("Implementation returned {a:?} on delete"),
}
}
}
}
true
}
}
Loading