Skip to content

Commit 611a0c7

Browse files
Arbitrary trait for eth2/types (#1040)
* Add the arbitrary type to eth2/types and their deps Signed-off-by: Kirk Baird <[email protected]> * Wrap arbitrary in a feature flag Signed-off-by: Kirk Baird <[email protected]> * Fix feature for types Signed-off-by: Kirk Baird <[email protected]> * Fix comment Signed-off-by: Kirk Baird <[email protected]> * Patch versioning Signed-off-by: Kirk Baird <[email protected]> * Allow expanded crate reference for arbitrary 0.4.3 Signed-off-by: Kirk Baird <[email protected]> * Add arbitrary to remaining types Signed-off-by: Kirk Baird <[email protected]> * use cmp::min Signed-off-by: Kirk Baird <[email protected]> * Derive Arbitrary trait for ValidatorStatus, TotalBalances and InclusionInfo * Add CI check for state processing arbitrary faetures Signed-off-by: Kirk Baird <[email protected]> * Fix indentation Signed-off-by: Kirk Baird <[email protected]> Co-authored-by: Mehdi Zerouali <[email protected]>
1 parent fcccf63 commit 611a0c7

Some content is hidden

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

70 files changed

+436
-292
lines changed

.github/workflows/test-suite.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ jobs:
8181
- uses: actions/checkout@v1
8282
- name: Lint code for quality and style with Clippy
8383
run: make lint
84+
arbitrary-check:
85+
runs-on: ubuntu-latest
86+
needs: cargo-fmt
87+
steps:
88+
- uses: actions/checkout@v1
89+
- name: Validate state_processing feature arbitrary-fuzz
90+
run: make arbitrary-fuzz

Cargo.lock

Lines changed: 103 additions & 276 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
@@ -60,3 +60,4 @@ eth2_ssz = { path = "eth2/utils/ssz" }
6060
eth2_ssz_derive = { path = "eth2/utils/ssz_derive" }
6161
eth2_ssz_types = { path = "eth2/utils/ssz_types" }
6262
eth2_hashing = { path = "eth2/utils/eth2_hashing" }
63+
web3 = { git = "https://github.com/tomusdrw/rust-web3" }

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ lint:
5858
make-ef-tests:
5959
make -C $(EF_TESTS)
6060

61+
# Verifies that state_processing feature arbitrary-fuzz will compile
62+
arbitrary-fuzz:
63+
cargo check --manifest-path=eth2/state_processing/Cargo.toml --features arbitrary-fuzz
64+
6165
# Performs a `cargo` clean and cleans the `ef_tests` directory.
6266
clean:
6367
cargo clean

eth2/state_processing/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ types = { path = "../types" }
3434
rayon = "1.2.0"
3535
eth2_hashing = { path = "../utils/eth2_hashing" }
3636
int_to_bytes = { path = "../utils/int_to_bytes" }
37+
arbitrary = { version = "0.4.3", features = ["derive"], optional = true }
3738

3839
[features]
3940
fake_crypto = ["bls/fake_crypto"]
41+
arbitrary-fuzz = [
42+
"arbitrary",
43+
"types/arbitrary-fuzz",
44+
"bls/arbitrary",
45+
"merkle_proof/arbitrary",
46+
"eth2_ssz/arbitrary",
47+
"eth2_ssz_types/arbitrary",
48+
"tree_hash/arbitrary",
49+
]

eth2/state_processing/src/per_block_processing.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ mod verify_deposit;
3333
mod verify_exit;
3434
mod verify_proposer_slashing;
3535

36+
#[cfg(feature = "arbitrary-fuzz")]
37+
use arbitrary::Arbitrary;
38+
3639
/// The strategy to be used when validating the block's signatures.
40+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
3741
#[derive(PartialEq, Clone, Copy)]
3842
pub enum BlockSignatureStrategy {
3943
/// Do not validate any signature. Use with caution.
@@ -45,6 +49,7 @@ pub enum BlockSignatureStrategy {
4549
}
4650

4751
/// The strategy to be used when validating the block's signatures.
52+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
4853
#[derive(PartialEq, Clone, Copy)]
4954
pub enum VerifySignatures {
5055
/// Validate all signatures encountered.

eth2/state_processing/src/per_epoch_processing/validator_statuses.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use crate::common::get_attesting_indices;
22
use safe_arith::SafeArith;
33
use types::*;
44

5+
#[cfg(feature = "arbitrary-fuzz")]
6+
use arbitrary::Arbitrary;
7+
58
/// Sets the boolean `var` on `self` to be true if it is true on `other`. Otherwise leaves `self`
69
/// as is.
710
macro_rules! set_self_if_other_is_true {
@@ -13,6 +16,7 @@ macro_rules! set_self_if_other_is_true {
1316
}
1417

1518
/// The information required to reward a block producer for including an attestation in a block.
19+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
1620
#[derive(Debug, Clone, Copy)]
1721
pub struct InclusionInfo {
1822
/// The distance between the attestation slot and the slot that attestation was included in a
@@ -44,6 +48,7 @@ impl InclusionInfo {
4448
}
4549

4650
/// Information required to reward some validator during the current and previous epoch.
51+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
4752
#[derive(Debug, Default, Clone)]
4853
pub struct ValidatorStatus {
4954
/// True if the validator has been slashed, ever.
@@ -108,7 +113,9 @@ impl ValidatorStatus {
108113

109114
/// The total effective balances for different sets of validators during the previous and current
110115
/// epochs.
116+
111117
#[derive(Clone, Debug)]
118+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
112119
pub struct TotalBalances {
113120
/// The effective balance increment from the spec.
114121
effective_balance_increment: u64,
@@ -165,6 +172,7 @@ impl TotalBalances {
165172

166173
/// Summarised information about validator participation in the _previous and _current_ epochs of
167174
/// some `BeaconState`.
175+
#[cfg_attr(feature = "arbitrary-fuzz", derive(Arbitrary))]
168176
#[derive(Debug, Clone)]
169177
pub struct ValidatorStatuses {
170178
/// Information about each individual validator from the state's validator registry.

eth2/types/Cargo.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ compare_fields_derive = { path = "../utils/compare_fields_derive" }
1515
dirs = "2.0.2"
1616
derivative = "1.0.3"
1717
eth2_interop_keypairs = { path = "../utils/eth2_interop_keypairs" }
18-
ethereum-types = "0.8.0"
18+
ethereum-types = "0.9.1"
1919
eth2_hashing = "0.1.0"
2020
hex = "0.3"
2121
int_to_bytes = { path = "../utils/int_to_bytes" }
@@ -38,8 +38,21 @@ rand_xorshift = "0.2.0"
3838
cached_tree_hash = { path = "../utils/cached_tree_hash" }
3939
serde_yaml = "0.8.11"
4040
tempfile = "3.1.0"
41+
arbitrary = { version = "0.4", features = ["derive"], optional = true }
4142

4243
[dev-dependencies]
4344
env_logger = "0.7.1"
4445
serde_json = "1.0.41"
4546
criterion = "0.3.0"
47+
48+
[features]
49+
arbitrary-fuzz = [
50+
"arbitrary",
51+
"ethereum-types/arbitrary",
52+
"bls/arbitrary",
53+
"eth2_ssz/arbitrary",
54+
"eth2_ssz_types/arbitrary",
55+
"merkle_proof/arbitrary",
56+
"swap_or_not_shuffle/arbitrary",
57+
"tree_hash/arbitrary",
58+
]

eth2/types/src/aggregate_and_proof.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tree_hash_derive::TreeHash;
1111
/// A Validators aggregate attestation and selection proof.
1212
///
1313
/// Spec v0.10.1
14+
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
1415
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
1516
#[serde(bound = "T: EthSpec")]
1617
pub struct AggregateAndProof<T: EthSpec> {

eth2/types/src/attestation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Error {
2020
/// Details an attestation that can be slashable.
2121
///
2222
/// Spec v0.11.1
23+
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
2324
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
2425
#[serde(bound = "T: EthSpec")]
2526
pub struct Attestation<T: EthSpec> {

0 commit comments

Comments
 (0)