Skip to content

Commit 56e6b35

Browse files
Fix Arbitrary implementations (#3867)
* Fix Arbitrary implementations * Remove remaining vestiges of arbitrary-fuzz * Remove FIXME * Clippy
1 parent 52c1055 commit 56e6b35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+610
-194
lines changed

Cargo.lock

Lines changed: 48 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ eth2_hashing = { path = "crypto/eth2_hashing" }
100100
tree_hash = { path = "consensus/tree_hash" }
101101
tree_hash_derive = { path = "consensus/tree_hash_derive" }
102102
eth2_serde_utils = { path = "consensus/serde_utils" }
103+
arbitrary = { git = "https://github.com/michaelsproul/arbitrary", rev="a572fd8743012a4f1ada5ee5968b1b3619c427ba" }
103104

104105
[profile.maxperf]
105106
inherits = "release"

consensus/ssz_types/src/bitfield.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl<N: 'static + Unsigned> arbitrary::Arbitrary<'_> for Bitfield<Fixed<N>> {
660660
let size = N::to_usize();
661661
let mut vec = smallvec![0u8; size];
662662
u.fill_buffer(&mut vec)?;
663-
Ok(Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
663+
Self::from_bytes(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
664664
}
665665
}
666666

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

consensus/ssz_types/src/fixed_vector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar
291291
for _ in 0..size {
292292
vec.push(<T>::arbitrary(u)?);
293293
}
294-
Ok(Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
294+
Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
295295
}
296296
}
297297

consensus/ssz_types/src/variable_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrar
273273
for _ in 0..size {
274274
vec.push(<T>::arbitrary(u)?);
275275
}
276-
Ok(Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)?)
276+
Self::new(vec).map_err(|_| arbitrary::Error::IncorrectFormat)
277277
}
278278
}
279279

consensus/types/Cargo.toml

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ harness = false
1111
[dependencies]
1212
serde-big-array = {version = "0.3.2", features = ["const-generics"]}
1313
merkle_proof = { path = "../../consensus/merkle_proof" }
14-
bls = { path = "../../crypto/bls" }
14+
bls = { path = "../../crypto/bls", features = ["arbitrary"] }
1515
compare_fields = { path = "../../common/compare_fields" }
1616
compare_fields_derive = { path = "../../common/compare_fields_derive" }
1717
eth2_interop_keypairs = { path = "../../common/eth2_interop_keypairs" }
18-
ethereum-types = "0.12.1"
18+
ethereum-types = { version = "0.12.1", features = ["arbitrary"] }
1919
eth2_hashing = "0.3.0"
2020
hex = "0.4.2"
2121
int_to_bytes = { path = "../int_to_bytes" }
@@ -26,20 +26,22 @@ safe_arith = { path = "../safe_arith" }
2626
serde = {version = "1.0.116" , features = ["rc"] }
2727
serde_derive = "1.0.116"
2828
slog = "2.5.2"
29-
eth2_ssz = "0.4.1"
29+
eth2_ssz = { version = "0.4.1", features = ["arbitrary"] }
3030
eth2_ssz_derive = "0.3.1"
31-
eth2_ssz_types = "0.2.2"
32-
swap_or_not_shuffle = { path = "../swap_or_not_shuffle" }
31+
eth2_ssz_types = { version = "0.2.2", features = ["arbitrary"] }
32+
swap_or_not_shuffle = { path = "../swap_or_not_shuffle", features = ["arbitrary"] }
3333
test_random_derive = { path = "../../common/test_random_derive" }
34-
tree_hash = "0.4.1"
34+
tree_hash = { version = "0.4.1", features = ["arbitrary"] }
3535
tree_hash_derive = "0.4.0"
3636
rand_xorshift = "0.3.0"
3737
cached_tree_hash = { path = "../cached_tree_hash" }
3838
serde_yaml = "0.8.13"
3939
tempfile = "3.1.0"
4040
derivative = "2.1.1"
4141
rusqlite = { version = "0.25.3", features = ["bundled"], optional = true }
42-
arbitrary = { version = "1.0", features = ["derive"], optional = true }
42+
# The arbitrary dependency is enabled by default since Capella to avoid complexity introduced by
43+
# `AbstractExecPayload`
44+
arbitrary = { version = "1.0", features = ["derive"] }
4345
eth2_serde_utils = "0.1.1"
4446
regex = "1.5.5"
4547
lazy_static = "1.4.0"
@@ -63,12 +65,6 @@ default = ["sqlite", "legacy-arith"]
6365
# Allow saturating arithmetic on slots and epochs. Enabled by default, but deprecated.
6466
legacy-arith = []
6567
sqlite = ["rusqlite"]
66-
arbitrary-fuzz = [
67-
"arbitrary",
68-
"ethereum-types/arbitrary",
69-
"bls/arbitrary",
70-
"eth2_ssz/arbitrary",
71-
"eth2_ssz_types/arbitrary",
72-
"swap_or_not_shuffle/arbitrary",
73-
"tree_hash/arbitrary",
74-
]
68+
# The `arbitrary-fuzz` feature is a no-op provided for backwards compatibility.
69+
# For simplicity `Arbitrary` is now derived regardless of the feature's presence.
70+
arbitrary-fuzz = []

consensus/types/src/aggregate_and_proof.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@ use tree_hash_derive::TreeHash;
1111
/// A Validators aggregate attestation and selection proof.
1212
///
1313
/// Spec v0.12.1
14-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
15-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
14+
#[derive(
15+
arbitrary::Arbitrary,
16+
Debug,
17+
Clone,
18+
PartialEq,
19+
Serialize,
20+
Deserialize,
21+
Encode,
22+
Decode,
23+
TestRandom,
24+
TreeHash,
25+
)]
1626
#[serde(bound = "T: EthSpec")]
27+
#[arbitrary(bound = "T: EthSpec")]
1728
pub struct AggregateAndProof<T: EthSpec> {
1829
/// The index of the validator that created the attestation.
1930
#[serde(with = "eth2_serde_utils::quoted_u64")]

consensus/types/src/attestation.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ pub enum Error {
2323
/// Details an attestation that can be slashable.
2424
///
2525
/// Spec v0.12.1
26-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
2726
#[derive(
28-
Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom, Derivative,
27+
arbitrary::Arbitrary,
28+
Debug,
29+
Clone,
30+
Serialize,
31+
Deserialize,
32+
Encode,
33+
Decode,
34+
TreeHash,
35+
TestRandom,
36+
Derivative,
2937
)]
3038
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
3139
#[serde(bound = "T: EthSpec")]
40+
#[arbitrary(bound = "T: EthSpec")]
3241
pub struct Attestation<T: EthSpec> {
3342
pub aggregation_bits: BitList<T::MaxValidatorsPerCommittee>,
3443
pub data: AttestationData,

consensus/types/src/attestation_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use tree_hash_derive::TreeHash;
1010
/// The data upon which an attestation is based.
1111
///
1212
/// Spec v0.12.1
13-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1413
#[derive(
14+
arbitrary::Arbitrary,
1515
Debug,
1616
Clone,
1717
PartialEq,

consensus/types/src/attestation_duty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::*;
22
use serde_derive::{Deserialize, Serialize};
33

4-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
5-
#[derive(Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
4+
#[derive(arbitrary::Arbitrary, Debug, PartialEq, Clone, Copy, Default, Serialize, Deserialize)]
65
pub struct AttestationDuty {
76
/// The slot during which the attester must attest.
87
pub slot: Slot,

consensus/types/src/attester_slashing.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ use tree_hash_derive::TreeHash;
99
/// Two conflicting attestations.
1010
///
1111
/// Spec v0.12.1
12-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1312
#[derive(
14-
Derivative, Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
13+
Derivative,
14+
Debug,
15+
Clone,
16+
Serialize,
17+
Deserialize,
18+
Encode,
19+
Decode,
20+
TreeHash,
21+
TestRandom,
22+
arbitrary::Arbitrary,
1523
)]
1624
#[derivative(PartialEq, Eq, Hash(bound = "T: EthSpec"))]
1725
#[serde(bound = "T: EthSpec")]
26+
#[arbitrary(bound = "T: EthSpec")]
1827
pub struct AttesterSlashing<T: EthSpec> {
1928
pub attestation_1: IndexedAttestation<T>,
2029
pub attestation_2: IndexedAttestation<T>,

consensus/types/src/beacon_block.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ use tree_hash_derive::TreeHash;
2929
TreeHash,
3030
TestRandom,
3131
Derivative,
32+
arbitrary::Arbitrary
3233
),
33-
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: ExecPayload<T>")),
34-
serde(bound = "T: EthSpec, Payload: ExecPayload<T>", deny_unknown_fields),
35-
cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary)),
34+
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")),
35+
serde(
36+
bound = "T: EthSpec, Payload: AbstractExecPayload<T>",
37+
deny_unknown_fields
38+
),
39+
arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>"),
3640
),
3741
ref_attributes(
3842
derive(Debug, PartialEq, TreeHash),
@@ -41,11 +45,13 @@ use tree_hash_derive::TreeHash;
4145
map_ref_into(BeaconBlockBodyRef, BeaconBlock),
4246
map_ref_mut_into(BeaconBlockBodyRefMut)
4347
)]
44-
#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash, Derivative)]
48+
#[derive(
49+
Debug, Clone, Serialize, Deserialize, Encode, TreeHash, Derivative, arbitrary::Arbitrary,
50+
)]
4551
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
4652
#[serde(untagged)]
47-
#[serde(bound = "T: EthSpec, Payload: ExecPayload<T>")]
48-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
53+
#[serde(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
54+
#[arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
4955
#[tree_hash(enum_behaviour = "transparent")]
5056
#[ssz(enum_behaviour = "transparent")]
5157
pub struct BeaconBlock<T: EthSpec, Payload: AbstractExecPayload<T> = FullPayload<T>> {

consensus/types/src/beacon_block_body.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ use tree_hash_derive::TreeHash;
2626
TreeHash,
2727
TestRandom,
2828
Derivative,
29+
arbitrary::Arbitrary
2930
),
30-
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: ExecPayload<T>")),
31-
serde(bound = "T: EthSpec, Payload: ExecPayload<T>", deny_unknown_fields),
32-
cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))
31+
derivative(PartialEq, Hash(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")),
32+
serde(
33+
bound = "T: EthSpec, Payload: AbstractExecPayload<T>",
34+
deny_unknown_fields
35+
),
36+
arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>"),
3337
),
3438
cast_error(ty = "Error", expr = "Error::IncorrectStateVariant"),
3539
partial_getter_error(ty = "Error", expr = "Error::IncorrectStateVariant")
3640
)]
37-
#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
41+
#[derive(Debug, Clone, Serialize, Deserialize, Derivative, arbitrary::Arbitrary)]
3842
#[derivative(PartialEq, Hash(bound = "T: EthSpec"))]
3943
#[serde(untagged)]
40-
#[serde(bound = "T: EthSpec, Payload: ExecPayload<T>")]
41-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
44+
#[serde(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
45+
#[arbitrary(bound = "T: EthSpec, Payload: AbstractExecPayload<T>")]
4246
pub struct BeaconBlockBody<T: EthSpec, Payload: AbstractExecPayload<T> = FullPayload<T>> {
4347
pub randao_reveal: Signature,
4448
pub eth1_data: Eth1Data,
@@ -71,6 +75,7 @@ pub struct BeaconBlockBody<T: EthSpec, Payload: AbstractExecPayload<T> = FullPay
7175
#[ssz(skip_serializing, skip_deserializing)]
7276
#[tree_hash(skip_hashing)]
7377
#[serde(skip)]
78+
#[arbitrary(default)]
7479
pub _phantom: PhantomData<Payload>,
7580
}
7681

0 commit comments

Comments
 (0)