Skip to content
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

Arbitrary implementations for sov-schema-db & sov-sequencer-registry #1094

Merged
merged 4 commits into from
Oct 23, 2023
Merged
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
5 changes: 5 additions & 0 deletions Cargo.lock

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

29 changes: 14 additions & 15 deletions examples/demo-rollup/provers/risc0/guest-celestia/Cargo.lock

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

8 changes: 7 additions & 1 deletion full-node/db/sov-schema-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ repository = { workspace = true }
readme = "README.md"

[dependencies]
# External dependencies
# External dependencies
anyhow = { workspace = true }
once_cell = { workspace = true }
prometheus = { workspace = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }
rocksdb = { workspace = true }
tracing = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
byteorder = { workspace = true }
tempfile = { workspace = true }

[features]
default = []
arbitrary = ["dep:proptest", "dep:proptest-derive"]
26 changes: 25 additions & 1 deletion full-node/db/sov-schema-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ impl DB {
}
}

#[derive(Debug)]
#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
#[derive(Debug, PartialEq, Eq, Hash)]
enum WriteOp {
Value { key: Vec<u8>, value: Vec<u8> },
Deletion { key: Vec<u8> },
Expand Down Expand Up @@ -321,6 +322,29 @@ impl SchemaBatch {
}
}

#[cfg(feature = "arbitrary")]
impl proptest::arbitrary::Arbitrary for SchemaBatch {
type Parameters = &'static [ColumnFamilyName];
type Strategy = proptest::strategy::BoxedStrategy<Self>;

fn arbitrary_with(columns: Self::Parameters) -> Self::Strategy {
use proptest::prelude::any;
use proptest::strategy::Strategy;

proptest::collection::vec(any::<Vec<WriteOp>>(), columns.len())
.prop_map::<SchemaBatch, _>(|vec_vec_write_ops| {
let mut rows = HashMap::new();
for (col, write_ops) in columns.iter().zip(vec_vec_write_ops.into_iter()) {
rows.insert(*col, write_ops);
}
SchemaBatch {
rows: Mutex::new(rows),
}
})
.boxed()
}
}

/// An error that occurred during (de)serialization of a [`Schema`]'s keys or
/// values.
#[derive(Error, Debug)]
Expand Down
49 changes: 0 additions & 49 deletions full-node/db/sov-schema-db/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,52 +159,3 @@ macro_rules! define_schema {
}
};
}

#[cfg(feature = "fuzzing")]
pub mod fuzzing {
use proptest::collection::vec;
use proptest::prelude::*;

use super::{KeyDecoder, KeyEncoder, Schema, ValueCodec};

/// Helper used in tests to assert a (key, value) pair for a certain [`Schema`] is able to convert
/// to bytes and convert back.
pub fn assert_encode_decode<S: Schema>(key: &S::Key, value: &S::Value) {
{
let encoded = key.encode_key().expect("Encoding key should work.");
let decoded = S::Key::decode_key(&encoded).expect("Decoding key should work.");
assert_eq!(*key, decoded);
}
{
let encoded = value.encode_value().expect("Encoding value should work.");
let decoded = S::Value::decode_value(&encoded).expect("Decoding value should work.");
assert_eq!(*value, decoded);
}
}

/// Helper used in tests and fuzzers to make sure a schema never panics when decoding random bytes.
#[allow(unused_must_use)]
pub fn assert_no_panic_decoding<S: Schema>(bytes: &[u8]) {
S::Key::decode_key(bytes);
S::Value::decode_value(bytes);
}

pub fn arb_small_vec_u8() -> impl Strategy<Value = Vec<u8>> {
vec(any::<u8>(), 0..2048)
}

#[macro_export]
macro_rules! test_no_panic_decoding {
($schema_type:ty) => {
use proptest::prelude::*;
use sov_schema_db::schema::fuzzing::{arb_small_vec_u8, assert_no_panic_decoding};

proptest! {
#[test]
fn test_no_panic_decoding(bytes in arb_small_vec_u8()) {
assert_no_panic_decoding::<$schema_type>(&bytes)
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ tempfile = { workspace = true }

[dependencies]
anyhow = { workspace = true }
arbitrary = { workspace = true, optional = true }
clap = { workspace = true, optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }
sov-bank = { path = "../sov-bank", version = "0.3" }
sov-modules-api = { path = "../../sov-modules-api", version = "0.3" }
sov-state = { path = "../../sov-state", version = "0.3" }
Expand All @@ -36,6 +39,7 @@ sov-zk-cycle-utils = { path = "../../../utils/zk-cycle-utils", version = "0.3",
[features]
bench = ["sov-zk-cycle-macros/bench", "risc0-zkvm", "risc0-zkvm-platform", "sov-zk-cycle-utils"]
default = []
arbitrary = ["dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
native = [
"serde_json",
"jsonrpsee",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use crate::SequencerRegistry;
derive(schemars::JsonSchema),
derive(CliWalletArg)
)]
#[cfg_attr(
feature = "arbitrary",
derive(arbitrary::Arbitrary, proptest_derive::Arbitrary)
)]
#[derive(Debug, PartialEq, Clone, borsh::BorshSerialize, borsh::BorshDeserialize)]
pub enum CallMessage {
/// Add a new sequencer to the sequencer registry.
Expand Down
21 changes: 10 additions & 11 deletions sov-rollup-starter/provers/risc0/guest-mock/Cargo.lock

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

Loading