lighthouse/testing/ef_tests/tests/tests.rs
Paul Hauner 931daa40d7 Add fork choice EF tests (#2737)
## Issue Addressed

Resolves #2545

## Proposed Changes

Adds the long-overdue EF tests for fork choice. Although we had pretty good coverage via other implementations that closely followed our approach, it is nonetheless important for us to implement these tests too.

During testing I found that we were using a hard-coded `SAFE_SLOTS_TO_UPDATE_JUSTIFIED` value rather than one from the `ChainSpec`. This caused a failure during a minimal preset test. This doesn't represent a risk to mainnet or testnets, since the hard-coded value matched the mainnet preset.

## Failing Cases

There is one failing case which is presently marked as `SkippedKnownFailure`:

```
case 4 ("new_finalized_slot_is_justified_checkpoint_ancestor") from /home/paul/development/lighthouse/testing/ef_tests/consensus-spec-tests/tests/minimal/phase0/fork_choice/on_block/pyspec_tests/new_finalized_slot_is_justified_checkpoint_ancestor failed with NotEqual:
head check failed: Got Head { slot: Slot(40), root: 0x9183dbaed4191a862bd307d476e687277fc08469fc38618699863333487703e7 } | Expected Head { slot: Slot(24), root: 0x105b49b51bf7103c182aa58860b039550a89c05a4675992e2af703bd02c84570 }
```

This failure is due to #2741. It's not a particularly high priority issue at the moment, so we fix it after merging this PR.
2021-11-08 07:29:04 +00:00

419 lines
14 KiB
Rust

#![cfg(feature = "ef_tests")]
use ef_tests::*;
use types::*;
// Check that the hand-computed multiplications on EthSpec are correctly computed.
// This test lives here because one is most likely to muck these up during a spec update.
fn check_typenum_values<E: EthSpec>() {
assert_eq!(
E::MaxPendingAttestations::to_u64(),
E::MaxAttestations::to_u64() * E::SlotsPerEpoch::to_u64()
);
assert_eq!(
E::SlotsPerEth1VotingPeriod::to_u64(),
E::EpochsPerEth1VotingPeriod::to_u64() * E::SlotsPerEpoch::to_u64()
);
}
#[test]
fn derived_typenum_values() {
check_typenum_values::<MinimalEthSpec>();
check_typenum_values::<MainnetEthSpec>();
}
#[test]
fn shuffling() {
ShufflingHandler::<MinimalEthSpec>::default().run();
ShufflingHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn operations_deposit() {
OperationsHandler::<MinimalEthSpec, Deposit>::default().run();
OperationsHandler::<MainnetEthSpec, Deposit>::default().run();
}
#[test]
fn operations_exit() {
OperationsHandler::<MinimalEthSpec, SignedVoluntaryExit>::default().run();
OperationsHandler::<MainnetEthSpec, SignedVoluntaryExit>::default().run();
}
#[test]
fn operations_proposer_slashing() {
OperationsHandler::<MinimalEthSpec, ProposerSlashing>::default().run();
OperationsHandler::<MainnetEthSpec, ProposerSlashing>::default().run();
}
#[test]
fn operations_attester_slashing() {
OperationsHandler::<MinimalEthSpec, AttesterSlashing<_>>::default().run();
OperationsHandler::<MainnetEthSpec, AttesterSlashing<_>>::default().run();
}
#[test]
fn operations_attestation() {
OperationsHandler::<MinimalEthSpec, Attestation<_>>::default().run();
OperationsHandler::<MainnetEthSpec, Attestation<_>>::default().run();
}
#[test]
fn operations_block_header() {
OperationsHandler::<MinimalEthSpec, BeaconBlock<_>>::default().run();
OperationsHandler::<MainnetEthSpec, BeaconBlock<_>>::default().run();
}
#[test]
fn operations_sync_aggregate() {
OperationsHandler::<MinimalEthSpec, SyncAggregate<_>>::default().run();
OperationsHandler::<MainnetEthSpec, SyncAggregate<_>>::default().run();
}
#[test]
fn sanity_blocks() {
SanityBlocksHandler::<MinimalEthSpec>::default().run();
SanityBlocksHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn sanity_slots() {
SanitySlotsHandler::<MinimalEthSpec>::default().run();
SanitySlotsHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn random() {
RandomHandler::<MinimalEthSpec>::default().run();
RandomHandler::<MainnetEthSpec>::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_aggregate() {
BlsAggregateSigsHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_sign() {
BlsSignMsgHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_verify() {
BlsVerifyMsgHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_aggregate_verify() {
BlsAggregateVerifyHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_fast_aggregate_verify() {
BlsFastAggregateVerifyHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_eth_aggregate_pubkeys() {
BlsEthAggregatePubkeysHandler::default().run();
}
#[test]
#[cfg(not(feature = "fake_crypto"))]
fn bls_eth_fast_aggregate_verify() {
BlsEthFastAggregateVerifyHandler::default().run();
}
/// As for `ssz_static_test_no_run` (below), but also executes the function as a test.
#[cfg(feature = "fake_crypto")]
macro_rules! ssz_static_test {
($($args:tt)*) => {
ssz_static_test_no_run!(#[test] $($args)*);
};
}
/// Generate a function to run the SSZ static tests for a type.
///
/// Quite complex in order to support an optional #[test] attrib, generics, and the two EthSpecs.
#[cfg(feature = "fake_crypto")]
macro_rules! ssz_static_test_no_run {
// Top-level
($(#[$test:meta])? $test_name:ident, $typ:ident$(<$generics:tt>)?) => {
ssz_static_test_no_run!($(#[$test])? $test_name, SszStaticHandler, $typ$(<$generics>)?);
};
// Generic
($(#[$test:meta])? $test_name:ident, $handler:ident, $typ:ident<_>) => {
ssz_static_test_no_run!(
$(#[$test])?
$test_name,
$handler,
{
($typ<MinimalEthSpec>, MinimalEthSpec),
($typ<MainnetEthSpec>, MainnetEthSpec)
}
);
};
// Non-generic
($(#[$test:meta])? $test_name:ident, $handler:ident, $typ:ident) => {
ssz_static_test_no_run!(
$(#[$test])?
$test_name,
$handler,
{
($typ, MinimalEthSpec),
($typ, MainnetEthSpec)
}
);
};
// Base case
($(#[$test:meta])? $test_name:ident, $handler:ident, { $(($($typ:ty),+)),+ }) => {
$(#[$test])?
fn $test_name() {
$(
$handler::<$($typ),+>::default().run();
)+
}
};
}
#[cfg(feature = "fake_crypto")]
mod ssz_static {
use ef_tests::{Handler, SszStaticHandler, SszStaticTHCHandler, SszStaticWithSpecHandler};
use types::*;
ssz_static_test!(aggregate_and_proof, AggregateAndProof<_>);
ssz_static_test!(attestation, Attestation<_>);
ssz_static_test!(attestation_data, AttestationData);
ssz_static_test!(attester_slashing, AttesterSlashing<_>);
ssz_static_test!(beacon_block, SszStaticWithSpecHandler, BeaconBlock<_>);
ssz_static_test!(beacon_block_header, BeaconBlockHeader);
ssz_static_test!(beacon_state, SszStaticTHCHandler, BeaconState<_>);
ssz_static_test!(checkpoint, Checkpoint);
ssz_static_test!(deposit, Deposit);
ssz_static_test!(deposit_data, DepositData);
ssz_static_test!(deposit_message, DepositMessage);
// NOTE: Eth1Block intentionally omitted, see: https://github.com/sigp/lighthouse/issues/1835
ssz_static_test!(eth1_data, Eth1Data);
ssz_static_test!(fork, Fork);
ssz_static_test!(fork_data, ForkData);
ssz_static_test!(historical_batch, HistoricalBatch<_>);
ssz_static_test!(indexed_attestation, IndexedAttestation<_>);
// NOTE: LightClient* intentionally omitted
ssz_static_test!(pending_attestation, PendingAttestation<_>);
ssz_static_test!(proposer_slashing, ProposerSlashing);
ssz_static_test!(signed_aggregate_and_proof, SignedAggregateAndProof<_>);
ssz_static_test!(
signed_beacon_block,
SszStaticWithSpecHandler,
SignedBeaconBlock<_>
);
ssz_static_test!(signed_beacon_block_header, SignedBeaconBlockHeader);
ssz_static_test!(signed_voluntary_exit, SignedVoluntaryExit);
ssz_static_test!(signing_data, SigningData);
ssz_static_test!(validator, Validator);
ssz_static_test!(voluntary_exit, VoluntaryExit);
// BeaconBlockBody has no internal indicator of which fork it is for, so we test it separately.
#[test]
fn beacon_block_body() {
SszStaticHandler::<BeaconBlockBodyBase<MinimalEthSpec>, MinimalEthSpec>::base_only().run();
SszStaticHandler::<BeaconBlockBodyBase<MainnetEthSpec>, MainnetEthSpec>::base_only().run();
SszStaticHandler::<BeaconBlockBodyAltair<MinimalEthSpec>, MinimalEthSpec>::altair_only()
.run();
SszStaticHandler::<BeaconBlockBodyAltair<MainnetEthSpec>, MainnetEthSpec>::altair_only()
.run();
}
// Altair-only
#[test]
fn contribution_and_proof() {
SszStaticHandler::<ContributionAndProof<MinimalEthSpec>, MinimalEthSpec>::altair_only()
.run();
SszStaticHandler::<ContributionAndProof<MainnetEthSpec>, MainnetEthSpec>::altair_only()
.run();
}
#[test]
fn signed_contribution_and_proof() {
SszStaticHandler::<SignedContributionAndProof<MinimalEthSpec>, MinimalEthSpec>::altair_only().run();
SszStaticHandler::<SignedContributionAndProof<MainnetEthSpec>, MainnetEthSpec>::altair_only().run();
}
#[test]
fn sync_aggregate() {
SszStaticHandler::<SyncAggregate<MinimalEthSpec>, MinimalEthSpec>::altair_only().run();
SszStaticHandler::<SyncAggregate<MainnetEthSpec>, MainnetEthSpec>::altair_only().run();
}
#[test]
fn sync_committee() {
SszStaticHandler::<SyncCommittee<MinimalEthSpec>, MinimalEthSpec>::altair_only().run();
SszStaticHandler::<SyncCommittee<MainnetEthSpec>, MainnetEthSpec>::altair_only().run();
}
#[test]
fn sync_committee_contribution() {
SszStaticHandler::<SyncCommitteeContribution<MinimalEthSpec>, MinimalEthSpec>::altair_only(
)
.run();
SszStaticHandler::<SyncCommitteeContribution<MainnetEthSpec>, MainnetEthSpec>::altair_only(
)
.run();
}
#[test]
fn sync_committee_message() {
SszStaticHandler::<SyncCommitteeMessage, MinimalEthSpec>::altair_only().run();
SszStaticHandler::<SyncCommitteeMessage, MainnetEthSpec>::altair_only().run();
}
#[test]
fn sync_aggregator_selection_data() {
SszStaticHandler::<SyncAggregatorSelectionData, MinimalEthSpec>::altair_only().run();
SszStaticHandler::<SyncAggregatorSelectionData, MainnetEthSpec>::altair_only().run();
}
}
#[test]
fn ssz_generic() {
SszGenericHandler::<BasicVector>::default().run();
SszGenericHandler::<Bitlist>::default().run();
SszGenericHandler::<Bitvector>::default().run();
SszGenericHandler::<Boolean>::default().run();
SszGenericHandler::<Uints>::default().run();
SszGenericHandler::<Containers>::default().run();
}
#[test]
fn epoch_processing_justification_and_finalization() {
EpochProcessingHandler::<MinimalEthSpec, JustificationAndFinalization>::default().run();
EpochProcessingHandler::<MainnetEthSpec, JustificationAndFinalization>::default().run();
}
#[test]
fn epoch_processing_rewards_and_penalties() {
EpochProcessingHandler::<MinimalEthSpec, RewardsAndPenalties>::default().run();
EpochProcessingHandler::<MainnetEthSpec, RewardsAndPenalties>::default().run();
}
#[test]
fn epoch_processing_registry_updates() {
EpochProcessingHandler::<MinimalEthSpec, RegistryUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, RegistryUpdates>::default().run();
}
#[test]
fn epoch_processing_slashings() {
EpochProcessingHandler::<MinimalEthSpec, Slashings>::default().run();
EpochProcessingHandler::<MainnetEthSpec, Slashings>::default().run();
}
#[test]
fn epoch_processing_eth1_data_reset() {
EpochProcessingHandler::<MinimalEthSpec, Eth1DataReset>::default().run();
EpochProcessingHandler::<MainnetEthSpec, Eth1DataReset>::default().run();
}
#[test]
fn epoch_processing_effective_balance_updates() {
EpochProcessingHandler::<MinimalEthSpec, EffectiveBalanceUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, EffectiveBalanceUpdates>::default().run();
}
#[test]
fn epoch_processing_slashings_reset() {
EpochProcessingHandler::<MinimalEthSpec, SlashingsReset>::default().run();
EpochProcessingHandler::<MainnetEthSpec, SlashingsReset>::default().run();
}
#[test]
fn epoch_processing_randao_mixes_reset() {
EpochProcessingHandler::<MinimalEthSpec, RandaoMixesReset>::default().run();
EpochProcessingHandler::<MainnetEthSpec, RandaoMixesReset>::default().run();
}
#[test]
fn epoch_processing_historical_roots_update() {
EpochProcessingHandler::<MinimalEthSpec, HistoricalRootsUpdate>::default().run();
EpochProcessingHandler::<MainnetEthSpec, HistoricalRootsUpdate>::default().run();
}
#[test]
fn epoch_processing_participation_record_updates() {
EpochProcessingHandler::<MinimalEthSpec, ParticipationRecordUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, ParticipationRecordUpdates>::default().run();
}
#[test]
fn epoch_processing_sync_committee_updates() {
EpochProcessingHandler::<MinimalEthSpec, SyncCommitteeUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, SyncCommitteeUpdates>::default().run();
}
#[test]
fn epoch_processing_inactivity_updates() {
EpochProcessingHandler::<MinimalEthSpec, InactivityUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, InactivityUpdates>::default().run();
}
#[test]
fn epoch_processing_participation_flag_updates() {
EpochProcessingHandler::<MinimalEthSpec, ParticipationFlagUpdates>::default().run();
EpochProcessingHandler::<MainnetEthSpec, ParticipationFlagUpdates>::default().run();
}
#[test]
fn fork_upgrade() {
ForkHandler::<MinimalEthSpec>::default().run();
ForkHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn transition() {
TransitionHandler::<MinimalEthSpec>::default().run();
TransitionHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn finality() {
FinalityHandler::<MinimalEthSpec>::default().run();
FinalityHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn fork_choice_get_head() {
ForkChoiceGetHeadHandler::<MinimalEthSpec>::default().run();
ForkChoiceGetHeadHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn fork_choice_on_block() {
ForkChoiceOnBlockHandler::<MinimalEthSpec>::default().run();
ForkChoiceOnBlockHandler::<MainnetEthSpec>::default().run();
}
#[test]
fn genesis_initialization() {
GenesisInitializationHandler::<MinimalEthSpec>::default().run();
}
#[test]
fn genesis_validity() {
GenesisValidityHandler::<MinimalEthSpec>::default().run();
// Note: there are no genesis validity tests for mainnet
}
#[test]
fn rewards() {
for handler in &["basic", "leak", "random"] {
RewardsHandler::<MinimalEthSpec>::new(handler).run();
RewardsHandler::<MainnetEthSpec>::new(handler).run();
}
}