2018-10-02 07:35:03 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
use super::attestation_validation::helpers::{generate_attestation, insert_justified_block_hash};
|
|
|
|
use super::bls::Keypair;
|
|
|
|
use super::db::stores::{BeaconBlockStore, PoWChainStore, ValidatorStore};
|
|
|
|
use super::db::MemoryDB;
|
|
|
|
use super::ssz::SszStream;
|
2018-10-16 02:59:45 +00:00
|
|
|
use super::ssz_helpers::ssz_beacon_block::SszBeaconBlock;
|
2018-10-29 19:29:15 +00:00
|
|
|
use super::types::{AttestationRecord, AttesterMap, BeaconBlock, Hash256, ProposerMap};
|
2018-10-02 07:35:03 +00:00
|
|
|
use super::validation::block_validation::{
|
2018-10-29 19:29:15 +00:00
|
|
|
BeaconBlockValidationContext, SszBeaconBlockValidationError,
|
2018-10-02 07:35:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2018-10-16 02:59:45 +00:00
|
|
|
pub struct BeaconBlockTestParams {
|
2018-10-02 07:35:03 +00:00
|
|
|
pub total_validators: usize,
|
|
|
|
pub cycle_length: u8,
|
|
|
|
pub shard_count: u16,
|
|
|
|
pub shards_per_slot: u16,
|
|
|
|
pub validators_per_shard: usize,
|
|
|
|
pub block_slot: u64,
|
|
|
|
pub attestations_justified_slot: u64,
|
|
|
|
pub parent_proposer_index: usize,
|
|
|
|
pub validation_context_slot: u64,
|
|
|
|
pub validation_context_justified_slot: u64,
|
2018-10-09 01:14:59 +00:00
|
|
|
pub validation_context_justified_block_hash: Hash256,
|
2018-10-02 07:35:03 +00:00
|
|
|
pub validation_context_finalized_slot: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestStore {
|
|
|
|
pub db: Arc<MemoryDB>,
|
2018-10-16 02:59:45 +00:00
|
|
|
pub block: Arc<BeaconBlockStore<MemoryDB>>,
|
2018-10-02 07:35:03 +00:00
|
|
|
pub pow_chain: Arc<PoWChainStore<MemoryDB>>,
|
|
|
|
pub validator: Arc<ValidatorStore<MemoryDB>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestStore {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let db = Arc::new(MemoryDB::open());
|
2018-10-16 02:59:45 +00:00
|
|
|
let block = Arc::new(BeaconBlockStore::new(db.clone()));
|
2018-10-02 07:35:03 +00:00
|
|
|
let pow_chain = Arc::new(PoWChainStore::new(db.clone()));
|
|
|
|
let validator = Arc::new(ValidatorStore::new(db.clone()));
|
|
|
|
Self {
|
|
|
|
db,
|
|
|
|
block,
|
|
|
|
pow_chain,
|
|
|
|
validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParentHashes = Vec<Hash256>;
|
|
|
|
|
|
|
|
/// Setup for a block validation function, without actually executing the
|
|
|
|
/// block validation function.
|
2018-10-29 19:29:15 +00:00
|
|
|
pub fn setup_block_validation_scenario(
|
|
|
|
params: &BeaconBlockTestParams,
|
|
|
|
) -> (
|
|
|
|
BeaconBlock,
|
|
|
|
ParentHashes,
|
|
|
|
AttesterMap,
|
|
|
|
ProposerMap,
|
|
|
|
TestStore,
|
|
|
|
) {
|
2018-10-02 07:35:03 +00:00
|
|
|
let stores = TestStore::new();
|
|
|
|
|
|
|
|
let cycle_length = params.cycle_length;
|
|
|
|
let shards_per_slot = params.shards_per_slot;
|
|
|
|
let validators_per_shard = params.validators_per_shard;
|
|
|
|
let block_slot = params.block_slot;
|
|
|
|
let attestations_justified_slot = params.attestations_justified_slot;
|
|
|
|
|
2018-10-11 13:41:47 +00:00
|
|
|
let mut parent_hashes: Vec<Hash256> = (0..(cycle_length * 2))
|
2018-10-02 07:35:03 +00:00
|
|
|
.map(|i| Hash256::from(i as u64))
|
|
|
|
.collect();
|
|
|
|
let parent_hash = Hash256::from("parent_hash".as_bytes());
|
2018-10-16 02:59:45 +00:00
|
|
|
let ancestor_hashes = vec![parent_hash.clone(); 32];
|
2018-10-02 07:35:03 +00:00
|
|
|
let randao_reveal = Hash256::from("randao_reveal".as_bytes());
|
|
|
|
let justified_block_hash = Hash256::from("justified_hash".as_bytes());
|
|
|
|
let pow_chain_ref = Hash256::from("pow_chain".as_bytes());
|
|
|
|
let active_state_root = Hash256::from("active_state".as_bytes());
|
|
|
|
let crystallized_state_root = Hash256::from("cry_state".as_bytes());
|
|
|
|
let shard_block_hash = Hash256::from("shard_block_hash".as_bytes());
|
|
|
|
|
2018-10-11 13:41:47 +00:00
|
|
|
/*
|
|
|
|
* Store a valid PoW chain ref
|
|
|
|
*/
|
2018-10-29 19:29:15 +00:00
|
|
|
stores
|
|
|
|
.pow_chain
|
|
|
|
.put_block_hash(pow_chain_ref.as_ref())
|
|
|
|
.unwrap();
|
2018-10-02 07:35:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a minimum viable parent block and store it in the database.
|
|
|
|
*/
|
2018-10-16 02:59:45 +00:00
|
|
|
let mut parent_block = BeaconBlock::zero();
|
2018-10-02 07:35:03 +00:00
|
|
|
let parent_attestation = AttestationRecord::zero();
|
2018-10-16 02:59:45 +00:00
|
|
|
parent_block.slot = block_slot - 1;
|
2018-10-02 07:35:03 +00:00
|
|
|
parent_block.attestations.push(parent_attestation);
|
|
|
|
let parent_block_ssz = serialize_block(&parent_block);
|
2018-10-29 19:29:15 +00:00
|
|
|
stores
|
|
|
|
.block
|
|
|
|
.put_serialized_block(parent_hash.as_ref(), &parent_block_ssz)
|
|
|
|
.unwrap();
|
2018-10-02 07:35:03 +00:00
|
|
|
|
|
|
|
let proposer_map = {
|
|
|
|
let mut proposer_map = ProposerMap::new();
|
2018-10-16 02:59:45 +00:00
|
|
|
proposer_map.insert(parent_block.slot, params.parent_proposer_index);
|
2018-10-02 07:35:03 +00:00
|
|
|
proposer_map
|
|
|
|
};
|
|
|
|
|
|
|
|
let (attester_map, attestations, _keypairs) = {
|
|
|
|
let mut i = 0;
|
|
|
|
let attestation_slot = block_slot - 1;
|
|
|
|
let mut attester_map = AttesterMap::new();
|
|
|
|
let mut attestations = vec![];
|
|
|
|
let mut keypairs = vec![];
|
2018-10-11 13:41:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert the required justified_block_hash into parent_hashes
|
|
|
|
*/
|
|
|
|
insert_justified_block_hash(
|
|
|
|
&mut parent_hashes,
|
|
|
|
&justified_block_hash,
|
|
|
|
block_slot,
|
2018-10-29 19:29:15 +00:00
|
|
|
attestation_slot,
|
|
|
|
);
|
2018-10-02 07:35:03 +00:00
|
|
|
/*
|
|
|
|
* For each shard in this slot, generate an attestation.
|
|
|
|
*/
|
|
|
|
for shard in 0..shards_per_slot {
|
2018-10-29 19:29:15 +00:00
|
|
|
let mut signing_keys = vec![];
|
|
|
|
let mut attesters = vec![];
|
|
|
|
/*
|
|
|
|
* Generate a random keypair for each validator and clone it into the
|
|
|
|
* list of keypairs. Store it in the database.
|
|
|
|
*/
|
2018-10-02 07:35:03 +00:00
|
|
|
for _ in 0..validators_per_shard {
|
2018-10-29 19:29:15 +00:00
|
|
|
let keypair = Keypair::random();
|
|
|
|
keypairs.push(keypair.clone());
|
|
|
|
stores
|
|
|
|
.validator
|
|
|
|
.put_public_key_by_index(i, &keypair.pk)
|
|
|
|
.unwrap();
|
|
|
|
signing_keys.push(Some(keypair.sk.clone()));
|
|
|
|
attesters.push(i);
|
|
|
|
i += 1;
|
2018-10-02 07:35:03 +00:00
|
|
|
}
|
|
|
|
attester_map.insert((attestation_slot, shard), attesters);
|
|
|
|
|
|
|
|
let attestation = generate_attestation(
|
|
|
|
shard,
|
|
|
|
&shard_block_hash,
|
|
|
|
block_slot,
|
|
|
|
attestation_slot,
|
|
|
|
attestations_justified_slot,
|
|
|
|
&justified_block_hash,
|
|
|
|
cycle_length,
|
|
|
|
&parent_hashes,
|
2018-10-11 13:41:47 +00:00
|
|
|
&signing_keys[..],
|
2018-10-29 19:29:15 +00:00
|
|
|
&stores.block,
|
|
|
|
);
|
2018-10-02 07:35:03 +00:00
|
|
|
attestations.push(attestation);
|
|
|
|
}
|
|
|
|
(attester_map, attestations, keypairs)
|
|
|
|
};
|
|
|
|
|
2018-10-16 02:59:45 +00:00
|
|
|
let block = BeaconBlock {
|
|
|
|
slot: block_slot,
|
2018-10-02 07:35:03 +00:00
|
|
|
randao_reveal,
|
2018-10-16 02:59:45 +00:00
|
|
|
pow_chain_reference: pow_chain_ref,
|
|
|
|
ancestor_hashes,
|
2018-10-02 07:35:03 +00:00
|
|
|
active_state_root,
|
|
|
|
crystallized_state_root,
|
2018-10-16 02:59:45 +00:00
|
|
|
attestations,
|
|
|
|
specials: vec![],
|
2018-10-02 07:35:03 +00:00
|
|
|
};
|
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
(block, parent_hashes, attester_map, proposer_map, stores)
|
2018-10-02 07:35:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 02:59:45 +00:00
|
|
|
/// Helper function to take some BeaconBlock and SSZ serialize it.
|
|
|
|
pub fn serialize_block(b: &BeaconBlock) -> Vec<u8> {
|
2018-10-02 07:35:03 +00:00
|
|
|
let mut stream = SszStream::new();
|
|
|
|
stream.append(b);
|
|
|
|
stream.drain()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Setup and run a block validation scenario, given some parameters.
|
|
|
|
///
|
|
|
|
/// Returns the Result returned from the block validation function.
|
|
|
|
pub fn run_block_validation_scenario<F>(
|
2018-10-16 02:59:45 +00:00
|
|
|
params: &BeaconBlockTestParams,
|
2018-10-29 19:29:15 +00:00
|
|
|
mutator_func: F,
|
|
|
|
) -> Result<BeaconBlock, SszBeaconBlockValidationError>
|
|
|
|
where
|
|
|
|
F: FnOnce(BeaconBlock, AttesterMap, ProposerMap, TestStore)
|
|
|
|
-> (BeaconBlock, AttesterMap, ProposerMap, TestStore),
|
2018-10-02 07:35:03 +00:00
|
|
|
{
|
2018-10-29 19:29:15 +00:00
|
|
|
let (block, parent_hashes, attester_map, proposer_map, stores) =
|
|
|
|
setup_block_validation_scenario(¶ms);
|
2018-10-02 07:35:03 +00:00
|
|
|
|
2018-10-29 19:29:15 +00:00
|
|
|
let (block, attester_map, proposer_map, stores) =
|
|
|
|
mutator_func(block, attester_map, proposer_map, stores);
|
2018-10-02 07:35:03 +00:00
|
|
|
|
|
|
|
let ssz_bytes = serialize_block(&block);
|
2018-10-29 19:29:15 +00:00
|
|
|
let ssz_block = SszBeaconBlock::from_slice(&ssz_bytes[..]).unwrap();
|
2018-10-02 07:35:03 +00:00
|
|
|
|
2018-10-16 02:59:45 +00:00
|
|
|
let context = BeaconBlockValidationContext {
|
2018-10-02 07:35:03 +00:00
|
|
|
present_slot: params.validation_context_slot,
|
|
|
|
cycle_length: params.cycle_length,
|
|
|
|
last_justified_slot: params.validation_context_justified_slot,
|
2018-10-09 01:14:59 +00:00
|
|
|
last_justified_block_hash: params.validation_context_justified_block_hash,
|
2018-10-02 07:35:03 +00:00
|
|
|
last_finalized_slot: params.validation_context_finalized_slot,
|
2018-10-23 11:15:08 +00:00
|
|
|
recent_block_hashes: Arc::new(parent_hashes),
|
2018-10-02 07:35:03 +00:00
|
|
|
proposer_map: Arc::new(proposer_map),
|
|
|
|
attester_map: Arc::new(attester_map),
|
|
|
|
block_store: stores.block.clone(),
|
|
|
|
validator_store: stores.validator.clone(),
|
2018-10-29 19:29:15 +00:00
|
|
|
pow_store: stores.pow_chain.clone(),
|
2018-10-02 07:35:03 +00:00
|
|
|
};
|
2018-10-23 11:15:08 +00:00
|
|
|
let block_hash = Hash256::from(&ssz_block.block_hash()[..]);
|
2018-10-29 19:29:15 +00:00
|
|
|
let validation_result = context.validate_ssz_block(&ssz_block);
|
2018-10-02 07:35:03 +00:00
|
|
|
/*
|
|
|
|
* If validation returned a block, make sure it's the same block we supplied to it.
|
|
|
|
*
|
|
|
|
* I.e., there were no errors during the serialization -> deserialization process.
|
|
|
|
*/
|
2018-10-29 19:29:15 +00:00
|
|
|
if let Ok(returned_block) = &validation_result {
|
2018-10-02 07:35:03 +00:00
|
|
|
assert_eq!(*returned_block, block);
|
|
|
|
};
|
2018-10-29 19:29:15 +00:00
|
|
|
validation_result
|
2018-10-02 07:35:03 +00:00
|
|
|
}
|