Update test_harness for spec v0.4.0

This commit is contained in:
Paul Hauner 2019-03-07 14:29:21 +11:00
parent db3b6cba6d
commit 195cb16a41
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 50 additions and 27 deletions

View File

@ -3,9 +3,11 @@
use crate::beacon_chain_harness::BeaconChainHarness; use crate::beacon_chain_harness::BeaconChainHarness;
use beacon_chain::CheckPoint; use beacon_chain::CheckPoint;
use bls::create_proof_of_possession;
use log::{info, warn}; use log::{info, warn};
use ssz::TreeHash; use ssz::SignedRoot;
use types::*; use types::*;
use types::{ use types::{
attester_slashing::AttesterSlashingBuilder, proposer_slashing::ProposerSlashingBuilder, attester_slashing::AttesterSlashingBuilder, proposer_slashing::ProposerSlashingBuilder,
}; };
@ -83,12 +85,18 @@ impl TestCase {
// -1 slots because genesis counts as a slot. // -1 slots because genesis counts as a slot.
for slot_height in 0..slots - 1 { for slot_height in 0..slots - 1 {
// Used to ensure that deposits in the same slot have incremental deposit indices.
let mut deposit_index_offset = 0;
// Feed deposits to the BeaconChain. // Feed deposits to the BeaconChain.
if let Some(ref deposits) = self.config.deposits { if let Some(ref deposits) = self.config.deposits {
for (slot, deposit, keypair) in deposits { for (slot, amount) in deposits {
if *slot == slot_height { if *slot == slot_height {
info!("Including deposit at slot height {}.", slot_height); info!("Including deposit at slot height {}.", slot_height);
harness.add_deposit(deposit.clone(), Some(keypair.clone())); let (deposit, keypair) =
build_deposit(&harness, *amount, deposit_index_offset);
harness.add_deposit(deposit, Some(keypair.clone()));
deposit_index_offset += 1;
} }
} }
} }
@ -200,6 +208,41 @@ impl TestCase {
} }
} }
/// Builds a `Deposit` this is valid for the given `BeaconChainHarness`.
///
/// `index_offset` is used to ensure that `deposit.index == state.index` when adding multiple
/// deposits.
fn build_deposit(
harness: &BeaconChainHarness,
amount: u64,
index_offset: u64,
) -> (Deposit, Keypair) {
let keypair = Keypair::random();
let proof_of_possession = create_proof_of_possession(&keypair);
let index = harness.beacon_chain.state.read().deposit_index + index_offset;
info!("index: {}, index_offset: {}", index, index_offset);
let deposit = Deposit {
// Note: `branch` and `index` will need to be updated once the spec defines their
// validity.
branch: vec![],
index,
deposit_data: DepositData {
amount,
timestamp: 1,
deposit_input: DepositInput {
pubkey: keypair.pk.clone(),
withdrawal_credentials: Hash256::zero(),
proof_of_possession,
},
},
};
(deposit, keypair)
}
/// Builds a `VoluntaryExit` this is valid for the given `BeaconChainHarness`.
fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> VoluntaryExit { fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> VoluntaryExit {
let epoch = harness let epoch = harness
.beacon_chain .beacon_chain
@ -213,7 +256,7 @@ fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> VoluntaryEx
signature: Signature::empty_signature(), signature: Signature::empty_signature(),
}; };
let message = exit.hash_tree_root(); let message = exit.signed_root();
exit.signature = harness exit.signature = harness
.validator_sign(validator_index as usize, &message[..], epoch, Domain::Exit) .validator_sign(validator_index as usize, &message[..], epoch, Domain::Exit)

View File

@ -1,12 +1,12 @@
use super::yaml_helpers::{as_u64, as_usize, as_vec_u64}; use super::yaml_helpers::{as_u64, as_usize, as_vec_u64};
use bls::create_proof_of_possession;
use types::*; use types::*;
use yaml_rust::Yaml; use yaml_rust::Yaml;
pub type ValidatorIndex = u64; pub type ValidatorIndex = u64;
pub type ValidatorIndices = Vec<u64>; pub type ValidatorIndices = Vec<u64>;
pub type GweiAmount = u64;
pub type DepositTuple = (SlotHeight, Deposit, Keypair); pub type DepositTuple = (SlotHeight, GweiAmount);
pub type ExitTuple = (SlotHeight, ValidatorIndex); pub type ExitTuple = (SlotHeight, ValidatorIndex);
pub type ProposerSlashingTuple = (SlotHeight, ValidatorIndex); pub type ProposerSlashingTuple = (SlotHeight, ValidatorIndex);
pub type AttesterSlashingTuple = (SlotHeight, ValidatorIndices); pub type AttesterSlashingTuple = (SlotHeight, ValidatorIndices);
@ -101,30 +101,11 @@ fn parse_deposits(yaml: &Yaml) -> Option<Vec<DepositTuple>> {
let mut deposits = vec![]; let mut deposits = vec![];
for deposit in yaml["deposits"].as_vec()? { for deposit in yaml["deposits"].as_vec()? {
let keypair = Keypair::random();
let proof_of_possession = create_proof_of_possession(&keypair);
let slot = as_u64(deposit, "slot").expect("Incomplete deposit (slot)"); let slot = as_u64(deposit, "slot").expect("Incomplete deposit (slot)");
let amount = let amount =
as_u64(deposit, "amount").expect("Incomplete deposit (amount)") * 1_000_000_000; as_u64(deposit, "amount").expect("Incomplete deposit (amount)") * 1_000_000_000;
let deposit = Deposit { deposits.push((SlotHeight::from(slot), amount))
// Note: `branch` and `index` will need to be updated once the spec defines their
// validity.
branch: vec![],
index: 0,
deposit_data: DepositData {
amount,
timestamp: 1,
deposit_input: DepositInput {
pubkey: keypair.pk.clone(),
withdrawal_credentials: Hash256::zero(),
proof_of_possession,
},
},
};
deposits.push((SlotHeight::from(slot), deposit, keypair));
} }
Some(deposits) Some(deposits)

View File

@ -16,7 +16,6 @@ pub fn verify_deposit(
) -> Result<(), Error> { ) -> Result<(), Error> {
// TODO: verify serialized deposit data. // TODO: verify serialized deposit data.
// TODO: verify deposit index.
verify!( verify!(
deposit.index == state.deposit_index, deposit.index == state.deposit_index,
Invalid::BadIndex(state.deposit_index, deposit.index) Invalid::BadIndex(state.deposit_index, deposit.index)