Add deposits, transfers and exits to benches

This commit is contained in:
Paul Hauner
2019-03-09 22:10:47 +11:00
parent 6250c81bb9
commit c1e386a0b1
6 changed files with 248 additions and 25 deletions

View File

@@ -1,6 +1,12 @@
mod test_random;
mod testing_attestation_builder;
mod testing_deposit_builder;
mod testing_transfer_builder;
mod testing_voluntary_exit_builder;
pub use rand::{prng::XorShiftRng, SeedableRng};
pub use test_random::TestRandom;
pub use testing_attestation_builder::TestingAttestationBuilder;
pub use testing_deposit_builder::TestingDepositBuilder;
pub use testing_transfer_builder::TestingTransferBuilder;
pub use testing_voluntary_exit_builder::TestingVoluntaryExitBuilder;

View File

@@ -0,0 +1,48 @@
use crate::*;
use bls::{create_proof_of_possession, get_withdrawal_credentials};
pub struct TestingDepositBuilder {
deposit: Deposit,
}
impl TestingDepositBuilder {
pub fn new(amount: u64) -> Self {
let keypair = Keypair::random();
let deposit = Deposit {
branch: vec![],
index: 0,
deposit_data: DepositData {
amount,
timestamp: 1,
deposit_input: DepositInput {
pubkey: keypair.pk,
withdrawal_credentials: Hash256::zero(),
proof_of_possession: Signature::empty_signature(),
},
},
};
Self { deposit }
}
pub fn set_index(&mut self, index: u64) {
self.deposit.index = index;
}
pub fn sign(&mut self, keypair: &Keypair, spec: &ChainSpec) {
self.deposit.deposit_data.deposit_input.pubkey = keypair.pk.clone();
self.deposit.deposit_data.deposit_input.proof_of_possession =
create_proof_of_possession(&keypair);
self.deposit
.deposit_data
.deposit_input
.withdrawal_credentials = Hash256::from_slice(
&get_withdrawal_credentials(&keypair.pk, spec.bls_withdrawal_prefix_byte)[..],
);
}
pub fn build(self) -> Deposit {
self.deposit
}
}

View File

@@ -0,0 +1,37 @@
use crate::*;
use ssz::SignedRoot;
pub struct TestingTransferBuilder {
transfer: Transfer,
}
impl TestingTransferBuilder {
pub fn new(from: u64, to: u64, amount: u64, slot: Slot) -> Self {
let keypair = Keypair::random();
let mut transfer = Transfer {
from,
to,
amount,
fee: 0,
slot,
pubkey: keypair.pk,
signature: Signature::empty_signature(),
};
Self { transfer }
}
pub fn sign(&mut self, keypair: Keypair, fork: &Fork, spec: &ChainSpec) {
self.transfer.pubkey = keypair.pk;
let message = self.transfer.signed_root();
let epoch = self.transfer.slot.epoch(spec.slots_per_epoch);
let domain = spec.get_domain(epoch, Domain::Transfer, fork);
self.transfer.signature = Signature::new(&message, domain, &keypair.sk);
}
pub fn build(self) -> Transfer {
self.transfer
}
}

View File

@@ -0,0 +1,29 @@
use crate::*;
use ssz::SignedRoot;
pub struct TestingVoluntaryExitBuilder {
exit: VoluntaryExit,
}
impl TestingVoluntaryExitBuilder {
pub fn new(epoch: Epoch, validator_index: u64) -> Self {
let exit = VoluntaryExit {
epoch,
validator_index,
signature: Signature::empty_signature(),
};
Self { exit }
}
pub fn sign(&mut self, secret_key: &SecretKey, fork: &Fork, spec: &ChainSpec) {
let message = self.exit.signed_root();
let domain = spec.get_domain(self.exit.epoch, Domain::Exit, fork);
self.exit.signature = Signature::new(&message, domain, secret_key);
}
pub fn build(self) -> VoluntaryExit {
self.exit
}
}