Add attester to beacon chain test harness

This commit is contained in:
Paul Hauner
2019-01-28 15:50:42 +11:00
parent e1698102e0
commit 5bbffcb053
24 changed files with 311 additions and 166 deletions

View File

@@ -131,7 +131,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> Attester<T, U, V,
self.store_produce(attestation_data);
self.signer
.bls_sign(&attestation_data.signable_message(PHASE_0_CUSTODY_BIT)[..])
.sign_attestation_message(&attestation_data.signable_message(PHASE_0_CUSTODY_BIT)[..])
}
/// Returns `true` if signing some attestation_data is safe (non-slashable).

View File

@@ -25,7 +25,7 @@ impl TestSigner {
}
impl Signer for TestSigner {
fn bls_sign(&self, message: &[u8]) -> Option<Signature> {
fn sign_attestation_message(&self, message: &[u8]) -> Option<Signature> {
Some(Signature::new(message, &self.keypair.sk))
}
}

View File

@@ -47,5 +47,5 @@ pub trait DutiesReader: Send + Sync {
/// Signs message using an internally-maintained private key.
pub trait Signer {
fn bls_sign(&self, message: &[u8]) -> Option<Signature>;
fn sign_attestation_message(&self, message: &[u8]) -> Option<Signature>;
}

View File

@@ -4,7 +4,7 @@ mod traits;
use slot_clock::SlotClock;
use ssz::ssz_encode;
use std::sync::Arc;
use types::{BeaconBlock, ChainSpec, Hash256, ProposalSignedData, PublicKey};
use types::{BeaconBlock, ChainSpec, PublicKey};
pub use self::traits::{
BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer,
@@ -139,7 +139,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
// TODO: add domain, etc to this message.
let message = ssz_encode(&producer_nonce);
match self.signer.bls_sign(&message) {
match self.signer.sign_randao_reveal(&message) {
None => return Ok(PollOutcome::SignerRejection(slot)),
Some(signature) => signature,
}
@@ -171,7 +171,10 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
fn sign_block(&mut self, mut block: BeaconBlock) -> Option<BeaconBlock> {
self.store_produce(&block);
match self.signer.bls_sign(&block.proposal_root(&self.spec)[..]) {
match self
.signer
.sign_block_proposal(&block.proposal_root(&self.spec)[..])
{
None => None,
Some(signature) => {
block.signature = signature;

View File

@@ -25,7 +25,11 @@ impl TestSigner {
}
impl Signer for TestSigner {
fn bls_sign(&self, message: &[u8]) -> Option<Signature> {
fn sign_block_proposal(&self, message: &[u8]) -> Option<Signature> {
Some(Signature::new(message, &self.keypair.sk))
}
fn sign_randao_reveal(&self, message: &[u8]) -> Option<Signature> {
Some(Signature::new(message, &self.keypair.sk))
}
}

View File

@@ -47,5 +47,6 @@ pub trait DutiesReader: Send + Sync {
/// Signs message using an internally-maintained private key.
pub trait Signer {
fn bls_sign(&self, message: &[u8]) -> Option<Signature>;
fn sign_block_proposal(&self, message: &[u8]) -> Option<Signature>;
fn sign_randao_reveal(&self, message: &[u8]) -> Option<Signature>;
}

View File

@@ -38,6 +38,20 @@ impl BeaconState {
let validator_count = self.validator_registry.len();
Some((slot as usize) % validator_count)
}
pub fn attestation_slot_and_shard_for_validator(
&self,
validator_index: usize,
spec: &ChainSpec,
) -> (u64, u64) {
// TODO: this is a stub; implement it properly.
let validator_index = validator_index as u64;
let slot = validator_index % spec.epoch_length;
let shard = validator_index % spec.shard_count;
(slot, shard)
}
}
fn merkle_root(_input: &[Hash256]) -> Hash256 {