chore(codecs): add proptest roundtrip to all main_codec (#803)

This commit is contained in:
joshieDo
2023-01-11 14:06:01 +08:00
committed by GitHub
parent 593a94e426
commit aabbe4923b
13 changed files with 90 additions and 21 deletions

6
Cargo.lock generated
View File

@ -3620,9 +3620,12 @@ dependencies = [
name = "reth-codecs"
version = "0.1.0"
dependencies = [
"arbitrary",
"bytes",
"codecs-derive",
"modular-bitfield",
"proptest",
"proptest-derive",
"revm-interpreter",
"serde",
"test-fuzz",
@ -3668,6 +3671,8 @@ dependencies = [
"page_size",
"parity-scale-codec",
"postcard",
"proptest",
"proptest-derive",
"rand 0.8.5",
"reth-codecs",
"reth-db",
@ -3992,6 +3997,7 @@ dependencies = [
"parity-scale-codec",
"plain_hasher",
"proptest",
"proptest-derive",
"rand 0.8.5",
"reth-codecs",
"reth-rlp",

View File

@ -145,7 +145,7 @@ mod tests {
fn parse_socket_addresses() {
for value in ["localhost:9000", ":9000", "9000", "localhost"] {
let socket_addr = parse_socket_address(value)
.expect(&format!("could not parse socket address: {}", value));
.unwrap_or_else(|_| panic!("could not parse socket address: {value}"));
assert!(socket_addr.ip().is_loopback());
assert_eq!(socket_addr.port(), 9000);

View File

@ -365,7 +365,7 @@ pub fn execute<DB: StateProvider>(
.map(|l| Log {
address: H160(l.address.0),
topics: l.topics.into_iter().map(|h| H256(h.0)).collect(),
data: l.data,
data: l.data.into(),
})
.collect();

View File

@ -184,7 +184,7 @@ pub fn fill_tx_env(tx_env: &mut TxEnv, transaction: &TransactionSignedEcRecovere
pub fn is_log_equal(revm_log: &revm::Log, reth_log: &reth_primitives::Log) -> bool {
revm_log.topics.len() == reth_log.topics.len() &&
revm_log.address.0 == reth_log.address.0 &&
revm_log.data == reth_log.data &&
revm_log.data == reth_log.data.0 &&
!revm_log
.topics
.iter()

View File

@ -58,19 +58,21 @@ triehash = "0.8"
plain_hasher = "0.2"
hash-db = "0.15"
# optional
# arbitrary utils
arbitrary = { version = "1.1.7", features = ["derive"], optional = true }
proptest = { version = "1.0", optional = true }
proptest-derive = { version = "0.3", optional = true }
[dev-dependencies]
arbitrary = { version = "1.1.7", features = ["derive"] }
serde_json = "1.0"
hex-literal = "0.3"
test-fuzz = "3.0.4"
rand = "0.8"
revm-interpreter = { git = "https://github.com/bluealloy/revm", rev = "a05fb262d87c78ee52d400e6c0f4708d4c527f32", features = ["serde", "arbitrary"] }
proptest = { version = "1.0" }
arbitrary = { version = "1.1.7", features = ["derive"] }
proptest = { version = "1.0" }
proptest-derive = "0.3"
# necessary so we don't hit a "undeclared 'std'":
# https://github.com/paradigmxyz/reth/pull/177#discussion_r1021172198
@ -78,4 +80,4 @@ secp256k1 = "0.24.2"
[features]
default = []
arbitrary = ["dep:arbitrary", "dep:proptest", "revm-interpreter/arbitrary"]
arbitrary = ["revm-interpreter/arbitrary", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"]

View File

@ -7,7 +7,7 @@ use impl_serde::impl_fixed_hash_serde;
use reth_codecs::{impl_hash_compact, Compact};
use reth_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
#[cfg(test)]
#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
arbitrary::{any_with, Arbitrary as PropTestArbitrary, ParamsFor},
strategy::{BoxedStrategy, Strategy},
@ -29,7 +29,7 @@ construct_fixed_hash! {
impl_hash_compact!(Bloom);
impl_fixed_hash_serde!(Bloom, BLOOM_BYTE_LENGTH);
#[cfg(test)]
#[cfg(any(test, feature = "arbitrary"))]
impl PropTestArbitrary for Bloom {
type Parameters = ParamsFor<u8>;
type Strategy = BoxedStrategy<Bloom>;

View File

@ -1,4 +1,4 @@
use crate::{Address, H256};
use crate::{Address, Bytes, H256};
use reth_codecs::{main_codec, Compact};
use reth_rlp::{RlpDecodable, RlpEncodable};
@ -11,5 +11,5 @@ pub struct Log {
/// Topics of the log. The number of logs depend on what `LOG` opcode is used.
pub topics: Vec<H256>,
/// Arbitrary length data.
pub data: bytes::Bytes,
pub data: Bytes,
}

View File

@ -74,7 +74,6 @@ mod tests {
proofs::{calculate_receipt_root, calculate_transaction_root},
Block, Bloom, Log, Receipt, TxType, H160, H256,
};
use bytes::Bytes;
use reth_rlp::Decodable;
#[test]
@ -89,7 +88,7 @@ mod tests {
#[test]
fn check_receipt_root() {
let logs = vec![Log { address: H160::zero(), topics: vec![], data: Bytes::default() }];
let logs = vec![Log { address: H160::zero(), topics: vec![], data: Default::default() }];
let bloom = Bloom(hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"));
let receipt = Receipt {
tx_type: TxType::EIP2930,

View File

@ -186,7 +186,7 @@ mod tests {
)
.unwrap(),
],
data: Bytes::from_str("0100ff").unwrap().0,
data: Bytes::from_str("0100ff").unwrap().0.into(),
}],
success: false,
};
@ -220,7 +220,7 @@ mod tests {
)
.unwrap(),
],
data: Bytes::from_str("0100ff").unwrap().0,
data: Bytes::from_str("0100ff").unwrap().0.into(),
}],
success: false,
};

View File

@ -1,7 +1,8 @@
use reth_codecs::Compact;
use reth_codecs::{derive_compact_arbitrary, Compact};
use serde::{Deserialize, Serialize};
/// Transaction Type
#[derive_compact_arbitrary]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
pub enum TxType {
/// Legacy transaction pre EIP-2929

View File

@ -18,7 +18,16 @@ bytes = "1.2.1"
codecs-derive = { version = "0.1.0", path = "./derive", default-features = false }
revm-interpreter = { git = "https://github.com/bluealloy/revm", rev = "a05fb262d87c78ee52d400e6c0f4708d4c527f32", features = ["serde"] }
# arbitrary utils
arbitrary = { version = "1.1.7", features = ["derive"], optional = true }
proptest = { version = "1.0", optional = true }
proptest-derive = { version = "0.3", optional = true }
[dev-dependencies]
serde = "1.0"
modular-bitfield = "0.11.2"
test-fuzz = "3.0.4"
test-fuzz = "3.0.4"
arbitrary = { version = "1.1.7", features = ["derive"] }
proptest = { version = "1.0" }
proptest-derive = "0.3"

View File

@ -1,5 +1,5 @@
use proc_macro::{self, TokenStream};
use quote::quote;
use quote::{format_ident, quote};
use syn::{parse_macro_input, DeriveInput};
mod compact;
@ -73,9 +73,53 @@ pub fn use_postcard(_args: TokenStream, input: TokenStream) -> TokenStream {
pub fn use_compact(_args: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
derive_compact_arbitrary(
_args,
quote! {
#[derive(Compact, serde::Serialize, serde::Deserialize)]
#ast
}
.into(),
)
}
#[proc_macro_attribute]
pub fn derive_compact_arbitrary(_args: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
// Avoid duplicate names
let prop_import = format_ident!("{}PropTestArbitratry", ast.ident);
let arb_import = format_ident!("{}Arbitratry", ast.ident);
let mod_tests = format_ident!("{}Tests", ast.ident);
let type_ident = ast.ident.clone();
quote! {
#[derive(Compact, serde::Serialize, serde::Deserialize)]
#[cfg(any(test, feature = "arbitrary"))]
use proptest_derive::Arbitrary as #prop_import;
#[cfg(any(test, feature = "arbitrary"))]
use arbitrary::Arbitrary as #arb_import;
#[cfg_attr(any(test, feature = "arbitrary"), derive(#prop_import, #arb_import))]
#ast
#[allow(non_snake_case)]
#[cfg(test)]
mod #mod_tests {
use super::Compact;
#[test]
fn proptest() {
proptest::proptest!(|(field: super::#type_ident)| {
let mut buf = vec![];
let len = field.clone().to_compact(&mut buf);
let (decoded, _) = super::#type_ident::from_compact(&buf, len);
assert!(field == decoded);
});
}
}
}
.into()
}

View File

@ -22,7 +22,6 @@ parity-scale-codec = { version = "3.2.1", features = ["bytes"] }
futures = "0.3.25"
tokio-stream = "0.1.11"
rand = "0.8.5"
arbitrary = { version = "1.1.7", features = ["derive"], optional = true }
secp256k1 = { version = "0.24.2", default-features = false, features = ["alloc", "recovery", "rand"], optional = true }
modular-bitfield = "0.11.2"
@ -32,6 +31,11 @@ page_size = "0.4.2"
thiserror = "1.0.37"
tempfile = { version = "3.3.0", optional = true }
# arbitrary utils
arbitrary = { version = "1.1.7", features = ["derive"], optional = true }
proptest = { version = "1.0", optional = true }
proptest-derive = { version = "0.3", optional = true }
[dev-dependencies]
tempfile = "3.3.0"
test-fuzz = "3.0.4"
@ -39,7 +43,6 @@ test-fuzz = "3.0.4"
criterion = "0.4.0"
iai = "0.1.1"
tokio = { version = "1.21.2", features = ["full"] }
arbitrary = { version = "1.1.7", features = ["derive"]}
reth-db = { path = ".", features = ["test-utils","bench"]}
# needed for test-fuzz to work properly, see https://github.com/paradigmxyz/reth/pull/177#discussion_r1021172198
@ -48,6 +51,11 @@ secp256k1 = "0.24.2"
reth-interfaces = { path = "../../interfaces", features=["bench"] }
async-trait = "0.1.58"
arbitrary = { version = "1.1.7", features = ["derive"] }
proptest = { version = "1.0" }
proptest-derive = "0.3"
[features]
default = ["mdbx"]
test-utils = ["tempfile"]