Ensure test_harness crate compiles under v0.5.0

This commit is contained in:
Paul Hauner 2019-03-17 19:19:52 +11:00
parent df3f8df7bd
commit 446ff0c27e
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 51 additions and 66 deletions

View File

@ -46,8 +46,8 @@ impl BeaconChainHarness {
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec);
let (genesis_state, keypairs) = state_builder.build();
let state_root = Hash256::from_slice(&genesis_state.hash_tree_root());
let genesis_block = BeaconBlock::genesis(state_root, &spec);
let mut genesis_block = BeaconBlock::empty(&spec);
genesis_block.state_root = Hash256::from_slice(&genesis_state.hash_tree_root());
// Create the Beacon Chain
let beacon_chain = Arc::new(
@ -127,8 +127,8 @@ impl BeaconChainHarness {
.get_crosslink_committees_at_slot(present_slot, &self.spec)
.unwrap()
.iter()
.fold(vec![], |mut acc, (committee, _slot)| {
acc.append(&mut committee.clone());
.fold(vec![], |mut acc, c| {
acc.append(&mut c.committee.clone());
acc
});
let attesting_validators: HashSet<usize> =
@ -233,6 +233,27 @@ impl BeaconChainHarness {
Some(Signature::new(message, domain, &validator.keypair.sk))
}
/// Returns the current `Fork` of the `beacon_chain`.
pub fn fork(&self) -> Fork {
self.beacon_chain.state.read().fork.clone()
}
/// Returns the current `epoch` of the `beacon_chain`.
pub fn epoch(&self) -> Epoch {
self.beacon_chain
.state
.read()
.slot
.epoch(self.spec.slots_per_epoch)
}
/// Returns the keypair for some validator index.
pub fn validator_keypair(&self, validator_index: usize) -> Option<&Keypair> {
self.validators
.get(validator_index)
.and_then(|v| Some(&v.keypair))
}
/// Submit a deposit to the `BeaconChain` and, if given a keypair, create a new
/// `ValidatorHarness` instance for this validator.
///

View File

@ -3,12 +3,11 @@
use crate::beacon_chain_harness::BeaconChainHarness;
use beacon_chain::CheckPoint;
use bls::get_withdrawal_credentials;
use log::{info, warn};
use ssz::SignedRoot;
use types::*;
use types::test_utils::{TestingAttesterSlashingBuilder, TestingProposerSlashingBuilder};
use types::test_utils::*;
use yaml_rust::Yaml;
mod config;
@ -222,27 +221,20 @@ impl TestCase {
}
/// Builds a `Deposit` this is valid for the given `BeaconChainHarness` at its next slot.
fn build_transfer(harness: &BeaconChainHarness, from: u64, to: u64, amount: u64) -> Transfer {
fn build_transfer(
harness: &BeaconChainHarness,
sender: u64,
recipient: u64,
amount: u64,
) -> Transfer {
let slot = harness.beacon_chain.state.read().slot + 1;
let mut transfer = Transfer {
from,
to,
amount,
fee: 0,
slot,
pubkey: harness.validators[from as usize].keypair.pk.clone(),
signature: Signature::empty_signature(),
};
let mut builder = TestingTransferBuilder::new(sender, recipient, amount, slot);
let message = transfer.signed_root();
let epoch = slot.epoch(harness.spec.slots_per_epoch);
let keypair = harness.validator_keypair(sender as usize).unwrap();
builder.sign(keypair.clone(), &harness.fork(), &harness.spec);
transfer.signature = harness
.validator_sign(from as usize, &message[..], epoch, Domain::Transfer)
.expect("Unable to sign Transfer");
transfer
builder.build()
}
/// Builds a `Deposit` this is valid for the given `BeaconChainHarness`.
@ -255,41 +247,12 @@ fn build_deposit(
index_offset: u64,
) -> (Deposit, Keypair) {
let keypair = Keypair::random();
let withdrawal_credentials = Hash256::from_slice(
&get_withdrawal_credentials(&keypair.pk, harness.spec.bls_withdrawal_prefix_byte)[..],
);
let proof_of_possession = DepositInput::create_proof_of_possession(
&keypair,
&withdrawal_credentials,
harness.spec.get_domain(
harness
.beacon_chain
.state
.read()
.current_epoch(&harness.spec),
Domain::Deposit,
&harness.beacon_chain.state.read().fork,
),
);
let index = harness.beacon_chain.state.read().deposit_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,
proof_of_possession,
},
},
};
let mut builder = TestingDepositBuilder::new(keypair.pk.clone(), amount);
builder.set_index(harness.beacon_chain.state.read().deposit_index + index_offset);
builder.sign(&keypair, harness.epoch(), &harness.fork(), &harness.spec);
(deposit, keypair)
(builder.build(), keypair)
}
/// Builds a `VoluntaryExit` this is valid for the given `BeaconChainHarness`.

View File

@ -180,9 +180,14 @@ impl TestingBeaconBlockBuilder {
) {
let keypair = Keypair::random();
let mut builder = TestingDepositBuilder::new(amount);
let mut builder = TestingDepositBuilder::new(keypair.pk.clone(), amount);
builder.set_index(index);
builder.sign(&keypair, state, spec);
builder.sign(
&keypair,
state.slot.epoch(spec.slots_per_epoch),
&state.fork,
spec,
);
self.block.body.deposits.push(builder.build())
}

View File

@ -10,9 +10,7 @@ pub struct TestingDepositBuilder {
impl TestingDepositBuilder {
/// Instantiates a new builder.
pub fn new(amount: u64) -> Self {
let keypair = Keypair::random();
pub fn new(pubkey: PublicKey, amount: u64) -> Self {
let deposit = Deposit {
proof: vec![],
index: 0,
@ -20,7 +18,7 @@ impl TestingDepositBuilder {
amount,
timestamp: 1,
deposit_input: DepositInput {
pubkey: keypair.pk,
pubkey,
withdrawal_credentials: Hash256::zero(),
proof_of_possession: Signature::empty_signature(),
},
@ -40,13 +38,11 @@ impl TestingDepositBuilder {
/// - `pubkey` to the signing pubkey.
/// - `withdrawal_credentials` to the signing pubkey.
/// - `proof_of_possesssion`
pub fn sign(&mut self, keypair: &Keypair, state: &BeaconState, spec: &ChainSpec) {
pub fn sign(&mut self, keypair: &Keypair, epoch: Epoch, fork: &Fork, spec: &ChainSpec) {
let withdrawal_credentials = Hash256::from_slice(
&get_withdrawal_credentials(&keypair.pk, spec.bls_withdrawal_prefix_byte)[..],
);
let epoch = state.current_epoch(spec);
self.deposit.deposit_data.deposit_input.pubkey = keypair.pk.clone();
self.deposit
.deposit_data
@ -57,7 +53,7 @@ impl TestingDepositBuilder {
.deposit
.deposit_data
.deposit_input
.create_proof_of_possession(&keypair.sk, epoch, &state.fork, spec);
.create_proof_of_possession(&keypair.sk, epoch, fork, spec);
}
/// Builds the deposit, consuming the builder.