lighthouse/beacon_node/beacon_chain/src/test_utils.rs

345 lines
12 KiB
Rust
Raw Normal View History

2019-06-16 06:24:33 +00:00
use crate::{BeaconChain, BeaconChainTypes, BlockProcessingOutcome};
use lmd_ghost::LmdGhost;
2019-06-16 06:24:33 +00:00
use slot_clock::SlotClock;
use slot_clock::TestingSlotClock;
2019-06-20 00:59:19 +00:00
use state_processing::per_slot_processing;
2019-06-16 06:24:33 +00:00
use std::marker::PhantomData;
use std::sync::Arc;
use store::MemoryStore;
use store::Store;
2019-06-16 06:24:33 +00:00
use tree_hash::{SignedRoot, TreeHash};
use types::{
test_utils::TestingBeaconStateBuilder, AggregateSignature, Attestation,
AttestationDataAndCustodyBit, BeaconBlock, BeaconState, Bitfield, ChainSpec, Domain, EthSpec,
Hash256, Keypair, RelativeEpoch, SecretKey, Signature, Slot,
2019-06-16 06:24:33 +00:00
};
2019-06-26 03:06:08 +00:00
pub use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY};
2019-06-21 01:55:37 +00:00
/// Indicates how the `BeaconChainHarness` should produce blocks.
2019-06-20 00:59:19 +00:00
#[derive(Clone, Copy, Debug)]
2019-06-21 01:55:37 +00:00
pub enum BlockStrategy {
/// Produce blocks upon the canonical head (normal case).
OnCanonicalHead,
2019-06-21 01:55:37 +00:00
/// Ignore the canonical head and produce blocks upon the block at the given slot.
///
/// Useful for simulating forks.
2019-06-23 00:05:12 +00:00
ForkCanonicalChainAt {
/// The slot of the parent of the first block produced.
previous_slot: Slot,
/// The slot of the first block produced (must be higher than `previous_slot`.
first_slot: Slot,
},
}
2019-06-21 01:55:37 +00:00
/// Indicates how the `BeaconChainHarness` should produce attestations.
2019-06-21 08:54:37 +00:00
#[derive(Clone, Debug)]
pub enum AttestationStrategy {
2019-06-21 01:55:37 +00:00
/// All validators attest to whichever block the `BeaconChainHarness` has produced.
AllValidators,
2019-06-21 08:54:37 +00:00
/// Only the given validators should attest. All others should fail to produce attestations.
SomeValidators(Vec<usize>),
}
2019-06-21 01:55:37 +00:00
/// Used to make the `BeaconChainHarness` generic over some types.
2019-06-16 06:24:33 +00:00
pub struct CommonTypes<L, E>
where
L: LmdGhost<MemoryStore, E>,
E: EthSpec,
{
_phantom_l: PhantomData<L>,
_phantom_e: PhantomData<E>,
}
impl<L, E> BeaconChainTypes for CommonTypes<L, E>
where
L: LmdGhost<MemoryStore, E>,
E: EthSpec,
{
type Store = MemoryStore;
type SlotClock = TestingSlotClock;
type LmdGhost = L;
type EthSpec = E;
}
2019-06-21 01:55:37 +00:00
/// A testing harness which can instantiate a `BeaconChain` and populate it with blocks and
/// attestations.
2019-06-16 06:24:33 +00:00
pub struct BeaconChainHarness<L, E>
where
L: LmdGhost<MemoryStore, E>,
E: EthSpec,
{
2019-06-23 00:05:12 +00:00
pub chain: BeaconChain<CommonTypes<L, E>>,
2019-06-26 03:06:08 +00:00
pub keypairs: Vec<Keypair>,
pub spec: ChainSpec,
2019-06-16 06:24:33 +00:00
}
impl<L, E> BeaconChainHarness<L, E>
where
L: LmdGhost<MemoryStore, E>,
E: EthSpec,
{
2019-06-21 01:55:37 +00:00
/// Instantiate a new harness with `validator_count` initial validators.
2019-06-16 06:24:33 +00:00
pub fn new(validator_count: usize) -> Self {
let spec = E::default_spec();
let store = Arc::new(MemoryStore::open());
let state_builder =
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec);
let (genesis_state, keypairs) = state_builder.build();
let mut genesis_block = BeaconBlock::empty(&spec);
genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root());
// Slot clock
let slot_clock = TestingSlotClock::new(
spec.genesis_slot,
genesis_state.genesis_time,
spec.seconds_per_slot,
);
let chain = BeaconChain::from_genesis(
store,
slot_clock,
genesis_state,
genesis_block,
spec.clone(),
)
.expect("Terminate if beacon chain generation fails");
Self {
chain,
keypairs,
spec,
}
}
2019-06-21 01:55:37 +00:00
/// Advance the slot of the `BeaconChain`.
///
/// Does not produce blocks or attestations.
2019-06-20 00:59:19 +00:00
pub fn advance_slot(&self) {
2019-06-16 06:24:33 +00:00
self.chain.slot_clock.advance_slot();
self.chain.catchup_state().expect("should catchup state");
}
2019-06-23 00:33:35 +00:00
/// Extend the `BeaconChain` with some blocks and attestations. Returns the root of the
/// last-produced block (the head of the chain).
2019-06-21 01:55:37 +00:00
///
/// Chain will be extended by `num_blocks` blocks.
///
/// The `block_strategy` dictates where the new blocks will be placed.
///
/// The `attestation_strategy` dictates which validators will attest to the newly created
/// blocks.
pub fn extend_chain(
&self,
2019-06-21 01:55:37 +00:00
num_blocks: usize,
block_strategy: BlockStrategy,
attestation_strategy: AttestationStrategy,
2019-06-23 00:33:35 +00:00
) -> Hash256 {
2019-06-23 00:05:12 +00:00
let mut state = {
// Determine the slot for the first block (or skipped block).
let state_slot = match block_strategy {
BlockStrategy::OnCanonicalHead => self.chain.read_slot_clock().unwrap() - 1,
BlockStrategy::ForkCanonicalChainAt { previous_slot, .. } => previous_slot,
};
self.get_state_at_slot(state_slot)
2019-06-20 00:59:19 +00:00
};
2019-06-23 00:05:12 +00:00
// Determine the first slot where a block should be built.
2019-06-21 01:55:37 +00:00
let mut slot = match block_strategy {
BlockStrategy::OnCanonicalHead => self.chain.read_slot_clock().unwrap(),
2019-06-23 00:05:12 +00:00
BlockStrategy::ForkCanonicalChainAt { first_slot, .. } => first_slot,
2019-06-20 00:59:19 +00:00
};
2019-06-23 00:33:35 +00:00
let mut head_block_root = None;
2019-06-21 01:55:37 +00:00
for _ in 0..num_blocks {
2019-06-20 00:59:19 +00:00
while self.chain.read_slot_clock().expect("should have a slot") < slot {
self.advance_slot();
}
2019-06-21 01:55:37 +00:00
let (block, new_state) = self.build_block(state.clone(), slot, block_strategy);
2019-06-20 00:59:19 +00:00
let outcome = self
.chain
.process_block(block)
2019-06-20 08:46:03 +00:00
.expect("should not error during block processing");
2019-06-20 00:59:19 +00:00
2019-06-20 08:46:03 +00:00
if let BlockProcessingOutcome::Processed { block_root } = outcome {
2019-06-23 00:33:35 +00:00
head_block_root = Some(block_root);
self.add_attestations_to_op_pool(
2019-06-21 08:54:37 +00:00
&attestation_strategy,
&new_state,
block_root,
slot,
);
2019-06-20 08:46:03 +00:00
} else {
2019-06-23 00:05:12 +00:00
panic!("block should be successfully processed: {:?}", outcome);
2019-06-20 08:46:03 +00:00
}
2019-06-20 00:59:19 +00:00
state = new_state;
slot += 1;
}
2019-06-23 00:33:35 +00:00
head_block_root.expect("did not produce any blocks")
}
2019-06-23 00:05:12 +00:00
fn get_state_at_slot(&self, state_slot: Slot) -> BeaconState<E> {
let state_root = self
.chain
.rev_iter_state_roots(self.chain.current_state().slot - 1)
2019-06-23 00:05:12 +00:00
.find(|(_hash, slot)| *slot == state_slot)
.map(|(hash, _slot)| hash)
.expect("could not find state root");
self.chain
.store
.get(&state_root)
.expect("should read db")
.expect("should find state root")
}
2019-06-21 01:55:37 +00:00
/// Returns a newly created block, signed by the proposer for the given slot.
2019-06-20 00:59:19 +00:00
fn build_block(
&self,
mut state: BeaconState<E>,
slot: Slot,
2019-06-21 01:55:37 +00:00
block_strategy: BlockStrategy,
2019-06-20 00:59:19 +00:00
) -> (BeaconBlock, BeaconState<E>) {
if slot < state.slot {
panic!("produce slot cannot be prior to the state slot");
}
2019-06-20 08:46:03 +00:00
while state.slot < slot {
2019-06-20 00:59:19 +00:00
per_slot_processing(&mut state, &self.spec)
.expect("should be able to advance state to slot");
}
state.build_all_caches(&self.spec).unwrap();
2019-06-21 01:55:37 +00:00
let proposer_index = match block_strategy {
BlockStrategy::OnCanonicalHead => self
2019-06-16 06:24:33 +00:00
.chain
.block_proposer(slot)
.expect("should get block proposer from chain"),
_ => state
.get_beacon_proposer_index(slot, RelativeEpoch::Current, &self.spec)
.expect("should get block proposer from state"),
2019-06-16 06:24:33 +00:00
};
let sk = &self.keypairs[proposer_index].sk;
let fork = &state.fork.clone();
2019-06-16 06:24:33 +00:00
let randao_reveal = {
let epoch = slot.epoch(E::slots_per_epoch());
let message = epoch.tree_hash_root();
let domain = self.spec.get_domain(epoch, Domain::Randao, fork);
Signature::new(&message, domain, sk)
};
2019-06-20 00:59:19 +00:00
let (mut block, state) = self
2019-06-16 06:24:33 +00:00
.chain
.produce_block_on_state(state, slot, randao_reveal)
.expect("should produce block");
2019-06-16 06:24:33 +00:00
block.signature = {
let message = block.signed_root();
let epoch = block.slot.epoch(E::slots_per_epoch());
let domain = self.spec.get_domain(epoch, Domain::BeaconProposer, fork);
Signature::new(&message, domain, sk)
};
2019-06-20 00:59:19 +00:00
(block, state)
2019-06-16 06:24:33 +00:00
}
2019-06-21 01:55:37 +00:00
/// Adds attestations to the `BeaconChain` operations pool to be included in future blocks.
///
/// The `attestation_strategy` dictates which validators should attest.
fn add_attestations_to_op_pool(
&self,
2019-06-21 08:54:37 +00:00
attestation_strategy: &AttestationStrategy,
state: &BeaconState<E>,
head_block_root: Hash256,
head_block_slot: Slot,
) {
let spec = &self.spec;
let fork = &state.fork;
let attesting_validators: Vec<usize> = match attestation_strategy {
AttestationStrategy::AllValidators => (0..self.keypairs.len()).collect(),
2019-06-21 08:54:37 +00:00
AttestationStrategy::SomeValidators(vec) => vec.clone(),
};
state
.get_crosslink_committees_at_slot(state.slot)
.expect("should get committees")
.iter()
.for_each(|cc| {
let committee_size = cc.committee.len();
for (i, validator_index) in cc.committee.iter().enumerate() {
// Note: searching this array is worst-case `O(n)`. A hashset could be a better
// alternative.
if attesting_validators.contains(validator_index) {
let data = self
.chain
.produce_attestation_data_for_block(
cc.shard,
head_block_root,
head_block_slot,
state,
)
.expect("should produce attestation data");
let mut aggregation_bitfield = Bitfield::new();
aggregation_bitfield.set(i, true);
aggregation_bitfield.set(committee_size, false);
let mut custody_bitfield = Bitfield::new();
custody_bitfield.set(committee_size, false);
let signature = {
let message = AttestationDataAndCustodyBit {
data: data.clone(),
custody_bit: false,
}
.tree_hash_root();
let domain =
spec.get_domain(data.target_epoch, Domain::Attestation, fork);
let mut agg_sig = AggregateSignature::new();
agg_sig.add(&Signature::new(
&message,
domain,
self.get_sk(*validator_index),
));
agg_sig
};
let attestation = Attestation {
aggregation_bitfield,
data,
custody_bitfield,
signature,
};
self.chain
.process_attestation(attestation)
.expect("should process attestation");
}
}
});
}
2019-06-21 01:55:37 +00:00
/// Returns the secret key for the given validator index.
fn get_sk(&self, validator_index: usize) -> &SecretKey {
&self.keypairs[validator_index].sk
}
2019-06-16 06:24:33 +00:00
}