Skip to content

Commit ed5604c

Browse files
michaelsproulWoodpile37
authored andcommitted
Fix Arbitrary implementations (sigp#3867)
* Fix Arbitrary implementations * Remove remaining vestiges of arbitrary-fuzz * Remove FIXME * Clippy
1 parent f3373b6 commit ed5604c

Some content is hidden

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

66 files changed

+612
-149
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ eth2_hashing = { path = "crypto/eth2_hashing" }
102102
tree_hash = { path = "consensus/tree_hash" }
103103
tree_hash_derive = { path = "consensus/tree_hash_derive" }
104104
eth2_serde_utils = { path = "consensus/serde_utils" }
105+
arbitrary = { git = "https://github.com/michaelsproul/arbitrary", rev="a572fd8743012a4f1ada5ee5968b1b3619c427ba" }
105106

106107
[profile.maxperf]
107108
inherits = "release"

consensus/ssz_types/src/bitfield.rs

+2-2
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

+1-1
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

+1-1
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/src/aggregate_and_proof.rs

+13-2
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

+11-2
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

+1-1
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

+1-2
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

+11-2
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

+12-6
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: ExecPayload<T> = FullPayload<T>> {

consensus/types/src/beacon_block_header.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ use tree_hash_derive::TreeHash;
1010
/// A header of a `BeaconBlock`.
1111
///
1212
/// Spec v0.12.1
13-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1413
#[derive(
15-
Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
14+
arbitrary::Arbitrary,
15+
Debug,
16+
PartialEq,
17+
Eq,
18+
Hash,
19+
Clone,
20+
Serialize,
21+
Deserialize,
22+
Encode,
23+
Decode,
24+
TreeHash,
25+
TestRandom,
1626
)]
1727
pub struct BeaconBlockHeader {
1828
pub slot: Slot,

consensus/types/src/beacon_committee.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ impl<'a> BeaconCommittee<'a> {
1717
}
1818
}
1919

20-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
21-
#[derive(Default, Clone, Debug, PartialEq)]
20+
#[derive(arbitrary::Arbitrary, Default, Clone, Debug, PartialEq)]
2221
pub struct OwnedBeaconCommittee {
2322
pub slot: Slot,
2423
pub index: CommitteeIndex,

consensus/types/src/beacon_state.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ impl AllowNextEpoch {
145145
}
146146
}
147147

148-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
149-
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
148+
#[derive(PartialEq, Eq, Hash, Clone, Copy, arbitrary::Arbitrary)]
150149
pub struct BeaconStateHash(Hash256);
151150

152151
impl fmt::Debug for BeaconStateHash {
@@ -188,18 +187,19 @@ impl From<BeaconStateHash> for Hash256 {
188187
TreeHash,
189188
TestRandom,
190189
CompareFields,
190+
arbitrary::Arbitrary
191191
),
192192
serde(bound = "T: EthSpec", deny_unknown_fields),
193+
arbitrary(bound = "T: EthSpec"),
193194
derivative(Clone),
194-
cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))
195195
),
196196
cast_error(ty = "Error", expr = "Error::IncorrectStateVariant"),
197197
partial_getter_error(ty = "Error", expr = "Error::IncorrectStateVariant")
198198
)]
199-
#[derive(Debug, PartialEq, Serialize, Deserialize, Encode, TreeHash)]
199+
#[derive(Debug, PartialEq, Serialize, Deserialize, Encode, TreeHash, arbitrary::Arbitrary)]
200200
#[serde(untagged)]
201201
#[serde(bound = "T: EthSpec")]
202-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
202+
#[arbitrary(bound = "T: EthSpec")]
203203
#[tree_hash(enum_behaviour = "transparent")]
204204
#[ssz(enum_behaviour = "transparent")]
205205
pub struct BeaconState<T>

consensus/types/src/beacon_state/committee_cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> V
336336
active
337337
}
338338

339-
#[cfg(feature = "arbitrary-fuzz")]
340339
impl arbitrary::Arbitrary<'_> for CommitteeCache {
341340
fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
342341
Ok(Self::default())

consensus/types/src/beacon_state/exit_cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl ExitCache {
6161
}
6262
}
6363

64-
#[cfg(feature = "arbitrary-fuzz")]
6564
impl arbitrary::Arbitrary<'_> for ExitCache {
6665
fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
6766
Ok(Self::default())

consensus/types/src/beacon_state/pubkey_cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ impl PubkeyCache {
4242
}
4343
}
4444

45-
#[cfg(feature = "arbitrary-fuzz")]
4645
impl arbitrary::Arbitrary<'_> for PubkeyCache {
4746
fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
4847
Ok(Self::default())

consensus/types/src/beacon_state/tree_hash_cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ impl OptionalTreeHashCacheInner {
600600
}
601601
}
602602

603-
#[cfg(feature = "arbitrary-fuzz")]
604603
impl<T: EthSpec> arbitrary::Arbitrary<'_> for BeaconTreeHashCache<T> {
605604
fn arbitrary(_u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
606605
Ok(Self::default())

consensus/types/src/bls_to_execution_change.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ use ssz_derive::{Decode, Encode};
66
use test_random_derive::TestRandom;
77
use tree_hash_derive::TreeHash;
88

9-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
109
#[derive(
11-
Debug, PartialEq, Hash, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
10+
arbitrary::Arbitrary,
11+
Debug,
12+
PartialEq,
13+
Hash,
14+
Clone,
15+
Serialize,
16+
Deserialize,
17+
Encode,
18+
Decode,
19+
TreeHash,
20+
TestRandom,
1221
)]
1322
pub struct BlsToExecutionChange {
1423
#[serde(with = "eth2_serde_utils::quoted_u64")]

consensus/types/src/chain_spec.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ pub enum Domain {
2929
/// Lighthouse's internal configuration struct.
3030
///
3131
/// Contains a mixture of "preset" and "config" values w.r.t to the EF definitions.
32-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
33-
#[derive(PartialEq, Debug, Clone)]
32+
#[derive(arbitrary::Arbitrary, PartialEq, Debug, Clone)]
3433
pub struct ChainSpec {
3534
/*
3635
* Config name

consensus/types/src/checkpoint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use tree_hash_derive::TreeHash;
88
/// Casper FFG checkpoint, used in attestations.
99
///
1010
/// Spec v0.12.1
11-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1211
#[derive(
12+
arbitrary::Arbitrary,
1313
Debug,
1414
Clone,
1515
Copy,

consensus/types/src/contribution_and_proof.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@ use test_random_derive::TestRandom;
99
use tree_hash_derive::TreeHash;
1010

1111
/// A Validators aggregate sync committee contribution and selection proof.
12-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
13-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
12+
#[derive(
13+
Debug,
14+
Clone,
15+
PartialEq,
16+
Serialize,
17+
Deserialize,
18+
Encode,
19+
Decode,
20+
TestRandom,
21+
TreeHash,
22+
arbitrary::Arbitrary,
23+
)]
1424
#[serde(bound = "T: EthSpec")]
25+
#[arbitrary(bound = "T: EthSpec")]
1526
pub struct ContributionAndProof<T: EthSpec> {
1627
/// The index of the validator that created the sync contribution.
1728
#[serde(with = "eth2_serde_utils::quoted_u64")]

consensus/types/src/deposit.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ pub const DEPOSIT_TREE_DEPTH: usize = 32;
1111
/// A deposit to potentially become a beacon chain validator.
1212
///
1313
/// Spec v0.12.1
14-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1514
#[derive(
16-
Debug, PartialEq, Hash, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
15+
arbitrary::Arbitrary,
16+
Debug,
17+
PartialEq,
18+
Hash,
19+
Clone,
20+
Serialize,
21+
Deserialize,
22+
Encode,
23+
Decode,
24+
TreeHash,
25+
TestRandom,
1726
)]
1827
pub struct Deposit {
1928
pub proof: FixedVector<Hash256, U33>,

consensus/types/src/deposit_data.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ use tree_hash_derive::TreeHash;
1010
/// The data supplied by the user to the deposit contract.
1111
///
1212
/// Spec v0.12.1
13-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1413
#[derive(
15-
Debug, PartialEq, Hash, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
14+
arbitrary::Arbitrary,
15+
Debug,
16+
PartialEq,
17+
Hash,
18+
Clone,
19+
Serialize,
20+
Deserialize,
21+
Encode,
22+
Decode,
23+
TreeHash,
24+
TestRandom,
1625
)]
1726
pub struct DepositData {
1827
pub pubkey: PublicKeyBytes,

consensus/types/src/deposit_message.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@ use tree_hash_derive::TreeHash;
1010
/// The data supplied by the user to the deposit contract.
1111
///
1212
/// Spec v0.12.1
13-
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
14-
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
13+
#[derive(
14+
arbitrary::Arbitrary,
15+
Debug,
16+
PartialEq,
17+
Clone,
18+
Serialize,
19+
Deserialize,
20+
Encode,
21+
Decode,
22+
TreeHash,
23+
TestRandom,
24+
)]
1525
pub struct DepositMessage {
1626
pub pubkey: PublicKeyBytes,
1727
pub withdrawal_credentials: Hash256,

0 commit comments

Comments
 (0)