Skip to content

Commit

Permalink
Add tests for decoding state/block with bad slot (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner authored May 14, 2021
1 parent 6d01530 commit 967d680
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
58 changes: 58 additions & 0 deletions consensus/types/src/beacon_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ mod tests {
use super::*;
use crate::test_utils::{test_ssz_tree_hash_pair_with, SeedableRng, TestRandom, XorShiftRng};
use crate::{ForkName, MainnetEthSpec};
use ssz::Encode;

type BeaconBlock = super::BeaconBlock<MainnetEthSpec>;
type BeaconBlockBase = super::BeaconBlockBase<MainnetEthSpec>;
Expand Down Expand Up @@ -389,4 +390,61 @@ mod tests {
BeaconBlock::from_ssz_bytes(bytes, spec)
});
}

#[test]
fn decode_base_and_altair() {
let rng = &mut XorShiftRng::from_seed([42; 16]);

let fork_slot = Slot::from_ssz_bytes(&[7, 6, 5, 4, 3, 2, 1, 0]).unwrap();

let base_slot = fork_slot.saturating_sub(1_u64);
let altair_slot = fork_slot;

let mut spec = MainnetEthSpec::default_spec();
spec.altair_fork_slot = Some(fork_slot);

// BeaconBlockBase
{
let good_base_block = BeaconBlock::Base(BeaconBlockBase {
slot: base_slot,
..<_>::random_for_test(rng)
});
// It's invalid to have a base block with a slot higher than the fork slot.
let bad_base_block = {
let mut bad = good_base_block.clone();
*bad.slot_mut() = altair_slot;
bad
};

assert_eq!(
BeaconBlock::from_ssz_bytes(&good_base_block.as_ssz_bytes(), &spec)
.expect("good base block can be decoded"),
good_base_block
);
BeaconBlock::from_ssz_bytes(&bad_base_block.as_ssz_bytes(), &spec)
.expect_err("bad base block cannot be decoded");
}

// BeaconBlockAltair
{
let good_altair_block = BeaconBlock::Altair(BeaconBlockAltair {
slot: altair_slot,
..<_>::random_for_test(rng)
});
// It's invalid to have an Altair block with a slot lower than the fork slot.
let bad_altair_block = {
let mut bad = good_altair_block.clone();
*bad.slot_mut() = base_slot;
bad
};

assert_eq!(
BeaconBlock::from_ssz_bytes(&good_altair_block.as_ssz_bytes(), &spec)
.expect("good altair block can be decoded"),
good_altair_block
);
BeaconBlock::from_ssz_bytes(&bad_altair_block.as_ssz_bytes(), &spec)
.expect_err("bad altair block cannot be decoded");
}
}
}
64 changes: 62 additions & 2 deletions consensus/types/src/beacon_state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::test_utils::*;
use beacon_chain::store::config::StoreConfig;
use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType};
use beacon_chain::types::{
BeaconState, BeaconStateError, ChainSpec, CloneConfig, Domain, Epoch, EthSpec, FixedVector,
Hash256, Keypair, MinimalEthSpec, RelativeEpoch, Slot,
test_utils::TestRandom, BeaconState, BeaconStateAltair, BeaconStateBase, BeaconStateError,
ChainSpec, CloneConfig, Domain, Epoch, EthSpec, FixedVector, Hash256, Keypair, MainnetEthSpec,
MinimalEthSpec, RelativeEpoch, Slot,
};
use ssz::{Decode, Encode};
use std::ops::Mul;
use swap_or_not_shuffle::compute_shuffled_index;

Expand Down Expand Up @@ -418,3 +420,61 @@ mod get_outstanding_deposit_len {
);
}
}

#[test]
fn decode_base_and_altair() {
let rng = &mut XorShiftRng::from_seed([42; 16]);

let fork_slot = Slot::from_ssz_bytes(&[7, 6, 5, 4, 3, 2, 1, 0]).unwrap();

let base_slot = fork_slot.saturating_sub(1_u64);
let altair_slot = fork_slot;

let mut spec = MainnetEthSpec::default_spec();
spec.altair_fork_slot = Some(fork_slot);

// BeaconStateBase
{
let good_base_block: BeaconState<MainnetEthSpec> = BeaconState::Base(BeaconStateBase {
slot: base_slot,
..<_>::random_for_test(rng)
});
// It's invalid to have a base block with a slot higher than the fork slot.
let bad_base_block = {
let mut bad = good_base_block.clone();
*bad.slot_mut() = altair_slot;
bad
};

assert_eq!(
BeaconState::from_ssz_bytes(&good_base_block.as_ssz_bytes(), &spec)
.expect("good base block can be decoded"),
good_base_block
);
<BeaconState<MainnetEthSpec>>::from_ssz_bytes(&bad_base_block.as_ssz_bytes(), &spec)
.expect_err("bad base block cannot be decoded");
}

// BeaconStateAltair
{
let good_altair_block: BeaconState<MainnetEthSpec> =
BeaconState::Altair(BeaconStateAltair {
slot: altair_slot,
..<_>::random_for_test(rng)
});
// It's invalid to have an Altair block with a slot lower than the fork slot.
let bad_altair_block = {
let mut bad = good_altair_block.clone();
*bad.slot_mut() = base_slot;
bad
};

assert_eq!(
BeaconState::from_ssz_bytes(&good_altair_block.as_ssz_bytes(), &spec)
.expect("good altair block can be decoded"),
good_altair_block
);
<BeaconState<MainnetEthSpec>>::from_ssz_bytes(&bad_altair_block.as_ssz_bytes(), &spec)
.expect_err("bad altair block cannot be decoded");
}
}

0 comments on commit 967d680

Please sign in to comment.