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

Fix Arbitrary implementations #3867

Merged
merged 4 commits into from
Jan 12, 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
62 changes: 48 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ eth2_hashing = { path = "crypto/eth2_hashing" }
tree_hash = { path = "consensus/tree_hash" }
tree_hash_derive = { path = "consensus/tree_hash_derive" }
eth2_serde_utils = { path = "consensus/serde_utils" }
arbitrary = { git = "https://github.com/michaelsproul/arbitrary", rev="a572fd8743012a4f1ada5ee5968b1b3619c427ba" }

[profile.maxperf]
inherits = "release"
Expand Down
4 changes: 2 additions & 2 deletions consensus/ssz_types/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ impl<N: 'static + Unsigned> arbitrary::Arbitrary<'_> for Bitfield<Fixed<N>> {
let size = N::to_usize();
let mut vec = smallvec![0u8; size];
u.fill_buffer(&mut vec)?;
Ok(Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand All @@ -672,7 +672,7 @@ impl<N: 'static + Unsigned> arbitrary::Arbitrary<'_> for Bitfield<Variable<N>> {
let size = std::cmp::min(rand, max_size);
let mut vec = smallvec![0u8; size];
u.fill_buffer(&mut vec)?;
Ok(Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/ssz_types/src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar
for _ in 0..size {
vec.push(<T>::arbitrary(u)?);
}
Ok(Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/ssz_types/src/variable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar
for _ in 0..size {
vec.push(<T>::arbitrary(u)?);
}
Ok(Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
}
}

Expand Down
28 changes: 12 additions & 16 deletions consensus/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ harness = false
[dependencies]
serde-big-array = {version = "0.3.2", features = ["const-generics"]}
merkle_proof = { path = "../../consensus/merkle_proof" }
bls = { path = "../../crypto/bls" }
bls = { path = "../../crypto/bls", features = ["arbitrary"] }
compare_fields = { path = "../../common/compare_fields" }
compare_fields_derive = { path = "../../common/compare_fields_derive" }
eth2_interop_keypairs = { path = "../../common/eth2_interop_keypairs" }
ethereum-types = "0.12.1"
ethereum-types = { version = "0.12.1", features = ["arbitrary"] }
eth2_hashing = "0.3.0"
hex = "0.4.2"
int_to_bytes = { path = "../int_to_bytes" }
Expand All @@ -26,20 +26,22 @@ safe_arith = { path = "../safe_arith" }
serde = {version = "1.0.116" , features = ["rc"] }
serde_derive = "1.0.116"
slog = "2.5.2"
eth2_ssz = "0.4.1"
eth2_ssz = { version = "0.4.1", features = ["arbitrary"] }
eth2_ssz_derive = "0.3.1"
eth2_ssz_types = "0.2.2"
swap_or_not_shuffle = { path = "../swap_or_not_shuffle" }
eth2_ssz_types = { version = "0.2.2", features = ["arbitrary"] }
swap_or_not_shuffle = { path = "../swap_or_not_shuffle", features = ["arbitrary"] }
test_random_derive = { path = "../../common/test_random_derive" }
tree_hash = "0.4.1"
tree_hash = { version = "0.4.1", features = ["arbitrary"] }
tree_hash_derive = "0.4.0"
rand_xorshift = "0.3.0"
cached_tree_hash = { path = "../cached_tree_hash" }
serde_yaml = "0.8.13"
tempfile = "3.1.0"
derivative = "2.1.1"
rusqlite = { version = "0.25.3", features = ["bundled"], optional = true }
arbitrary = { version = "1.0", features = ["derive"], optional = true }
# The arbitrary dependency is enabled by default since Capella to avoid complexity introduced by
# `AbstractExecPayload`
arbitrary = { version = "1.0", features = ["derive"] }
eth2_serde_utils = "0.1.1"
regex = "1.5.5"
lazy_static = "1.4.0"
Expand All @@ -63,12 +65,6 @@ default = ["sqlite", "legacy-arith"]
# Allow saturating arithmetic on slots and epochs. Enabled by default, but deprecated.
legacy-arith = []
sqlite = ["rusqlite"]
arbitrary-fuzz = [
"arbitrary",
"ethereum-types/arbitrary",
"bls/arbitrary",
"eth2_ssz/arbitrary",
"eth2_ssz_types/arbitrary",
"swap_or_not_shuffle/arbitrary",
"tree_hash/arbitrary",
]
# The `arbitrary-fuzz` feature is a no-op provided for backwards compatibility.
# For simplicity `Arbitrary` is now derived regardless of the feature's presence.
arbitrary-fuzz = []
15 changes: 13 additions & 2 deletions consensus/types/src/aggregate_and_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ use tree_hash_derive::TreeHash;
/// A Validators aggregate attestation and selection proof.
///
/// Spec v0.12.1
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
#[derive(
arbitrary::Arbitrary,
Debug,
Clone,
PartialEq,
Serialize,
Deserialize,
Encode,
Decode,
TestRandom,
TreeHash,
)]
#[serde(bound = "T: EthSpec")]
#[arbitrary(bound = "T: EthSpec")]
pub struct AggregateAndProof<T: EthSpec> {
/// The index of the validator that created the attestation.
#[serde(with = "eth2_serde_utils::quoted_u64")]
Expand Down
13 changes: 11 additions & 2 deletions consensus/types/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ pub enum Error {
/// Details an attestation that can be slashable.
///
/// Spec v0.12.1
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(
Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom, Derivative,
arbitrary::Arbitrary,
Debug,
Clone,
Serialize,
Deserialize,
Encode,
Decode,
TreeHash,
TestRandom,
Derivative,
)]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
#[serde(bound = "T: EthSpec")]
#[arbitrary(bound = "T: EthSpec")]
pub struct Attestation<T: EthSpec> {
pub aggregation_bits: BitList<T::MaxValidatorsPerCommittee>,
pub data: AttestationData,
Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/attestation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use tree_hash_derive::TreeHash;
/// The data upon which an attestation is based.
///
/// Spec v0.12.1
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(
arbitrary::Arbitrary,
Debug,
Clone,
PartialEq,
Expand Down
3 changes: 1 addition & 2 deletions consensus/types/src/attestation_duty.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::*;
use serde_derive::{Deserialize, Serialize};

#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
#[derive(arbitrary::Arbitrary, Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
pub struct AttestationDuty {
/// The slot during which the attester must attest.
pub slot: Slot,
Expand Down
13 changes: 11 additions & 2 deletions consensus/types/src/attester_slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ use tree_hash_derive::TreeHash;
/// Two conflicting attestations.
///
/// Spec v0.12.1
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(
Derivative, Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
Derivative,
Debug,
Clone,
Serialize,
Deserialize,
Encode,
Decode,
TreeHash,
TestRandom,
arbitrary::Arbitrary,
)]
#[derivative(PartialEq, Eq, Hash(bound = "T: EthSpec"))]
#[serde(bound = "T: EthSpec")]
#[arbitrary(bound = "T: EthSpec")]
pub struct AttesterSlashing<T: EthSpec> {
pub attestation_1: IndexedAttestation<T>,
pub attestation_2: IndexedAttestation<T>,
Expand Down
18 changes: 12 additions & 6 deletions consensus/types/src/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ use tree_hash_derive::TreeHash;
TreeHash,
TestRandom,
Derivative,
arbitrary::Arbitrary
),
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: ExecPayload<T>")),
serde(bound = "T: EthSpec, Payload: ExecPayload<T>", deny_unknown_fields),
cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary)),
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")),
serde(
bound = "T: EthSpec, Payload: AbstractExecPayload<T>",
deny_unknown_fields
),
arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>"),
),
ref_attributes(
derive(Debug, PartialEq, TreeHash),
Expand All @@ -41,11 +45,13 @@ use tree_hash_derive::TreeHash;
map_ref_into(BeaconBlockBodyRef, BeaconBlock),
map_ref_mut_into(BeaconBlockBodyRefMut)
)]
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash, Derivative)]
#[derive(
Debug, Clone, Serialize, Deserialize, Encode, TreeHash, Derivative, arbitrary::Arbitrary,
)]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
#[serde(untagged)]
#[serde(bound = "T: EthSpec, Payload: ExecPayload<T>")]
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[serde(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
#[arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
#[tree_hash(enum_behaviour = "transparent")]
#[ssz(enum_behaviour = "transparent")]
pub struct BeaconBlock<T: EthSpec, Payload: AbstractExecPayload<T> = FullPayload<T>> {
Expand Down
17 changes: 11 additions & 6 deletions consensus/types/src/beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ use tree_hash_derive::TreeHash;
TreeHash,
TestRandom,
Derivative,
arbitrary::Arbitrary
),
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: ExecPayload<T>")),
serde(bound = "T: EthSpec, Payload: ExecPayload<T>", deny_unknown_fields),
cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")),
serde(
bound = "T: EthSpec, Payload: AbstractExecPayload<T>",
deny_unknown_fields
),
arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>"),
),
cast_error(ty = "Error", expr = "Error::IncorrectStateVariant"),
partial_getter_error(ty = "Error", expr = "Error::IncorrectStateVariant")
)]
#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
#[derive(Debug, Clone, Serialize, Deserialize, Derivative, arbitrary::Arbitrary)]
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
#[serde(untagged)]
#[serde(bound = "T: EthSpec, Payload: ExecPayload<T>")]
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[serde(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
#[arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
pub struct BeaconBlockBody<T: EthSpec, Payload: AbstractExecPayload<T> = FullPayload<T>> {
pub randao_reveal: Signature,
pub eth1_data: Eth1Data,
Expand Down Expand Up @@ -71,6 +75,7 @@ pub struct BeaconBlockBody<T: EthSpec, Payload: AbstractExecPayload<T> = FullPay
#[ssz(skip_serializing, skip_deserializing)]
#[tree_hash(skip_hashing)]
#[serde(skip)]
#[arbitrary(default)]
pub _phantom: PhantomData<Payload>,
}

Expand Down
Loading