merged master
This commit is contained in:
+1
-1
@@ -30,7 +30,7 @@ Rust crates containing logic common across the Lighthouse project.
|
||||
- [`ssz`](utils/ssz/): an implementation of the SimpleSerialize
|
||||
serialization/deserialization protocol used by Eth 2.0.
|
||||
- [`ssz_derive`](utils/ssz_derive/): provides procedural macros for
|
||||
deriving SSZ `Encodable`, `Decodable`, and `TreeHash` methods.
|
||||
deriving SSZ `Encode`, `Decode`, and `TreeHash` methods.
|
||||
- [`swap_or_not_shuffle`](utils/swap_or_not_shuffle/): a list-shuffling
|
||||
method which is slow, but allows for a subset of indices to be shuffled.
|
||||
- [`test_random_derive`](utils/test_random_derive/): provides procedural
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
[package]
|
||||
name = "attester"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||
ssz = { path = "../../eth2/utils/ssz" }
|
||||
types = { path = "../../eth2/types" }
|
||||
@@ -1,257 +0,0 @@
|
||||
pub mod test_utils;
|
||||
mod traits;
|
||||
|
||||
use slot_clock::SlotClock;
|
||||
use ssz::TreeHash;
|
||||
use std::sync::Arc;
|
||||
use types::{AttestationData, AttestationDataAndCustodyBit, FreeAttestation, Signature, Slot};
|
||||
|
||||
pub use self::traits::{
|
||||
BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer,
|
||||
};
|
||||
|
||||
const PHASE_0_CUSTODY_BIT: bool = false;
|
||||
const DOMAIN_ATTESTATION: u64 = 1;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum PollOutcome {
|
||||
AttestationProduced(Slot),
|
||||
AttestationNotRequired(Slot),
|
||||
SlashableAttestationNotProduced(Slot),
|
||||
BeaconNodeUnableToProduceAttestation(Slot),
|
||||
ProducerDutiesUnknown(Slot),
|
||||
SlotAlreadyProcessed(Slot),
|
||||
SignerRejection(Slot),
|
||||
ValidatorIsUnknown(Slot),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
SlotClockError,
|
||||
SlotUnknowable,
|
||||
EpochMapPoisoned,
|
||||
SlotClockPoisoned,
|
||||
EpochLengthIsZero,
|
||||
BeaconNodeError(BeaconNodeError),
|
||||
}
|
||||
|
||||
/// A polling state machine which performs block production duties, based upon some epoch duties
|
||||
/// (`EpochDutiesMap`) and a concept of time (`SlotClock`).
|
||||
///
|
||||
/// Ensures that messages are not slashable.
|
||||
///
|
||||
/// Relies upon an external service to keep the `EpochDutiesMap` updated.
|
||||
pub struct Attester<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
||||
pub last_processed_slot: Option<Slot>,
|
||||
duties: Arc<V>,
|
||||
slot_clock: Arc<T>,
|
||||
beacon_node: Arc<U>,
|
||||
signer: Arc<W>,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> Attester<T, U, V, W> {
|
||||
/// Returns a new instance where `last_processed_slot == 0`.
|
||||
pub fn new(duties: Arc<V>, slot_clock: Arc<T>, beacon_node: Arc<U>, signer: Arc<W>) -> Self {
|
||||
Self {
|
||||
last_processed_slot: None,
|
||||
duties,
|
||||
slot_clock,
|
||||
beacon_node,
|
||||
signer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> Attester<T, U, V, W> {
|
||||
/// Poll the `BeaconNode` and produce an attestation if required.
|
||||
pub fn poll(&mut self) -> Result<PollOutcome, Error> {
|
||||
let slot = self
|
||||
.slot_clock
|
||||
.present_slot()
|
||||
.map_err(|_| Error::SlotClockError)?
|
||||
.ok_or(Error::SlotUnknowable)?;
|
||||
|
||||
if !self.is_processed_slot(slot) {
|
||||
self.last_processed_slot = Some(slot);
|
||||
|
||||
let shard = match self.duties.attestation_shard(slot) {
|
||||
Ok(Some(result)) => result,
|
||||
Ok(None) => return Ok(PollOutcome::AttestationNotRequired(slot)),
|
||||
Err(DutiesReaderError::UnknownEpoch) => {
|
||||
return Ok(PollOutcome::ProducerDutiesUnknown(slot));
|
||||
}
|
||||
Err(DutiesReaderError::UnknownValidator) => {
|
||||
return Ok(PollOutcome::ValidatorIsUnknown(slot));
|
||||
}
|
||||
Err(DutiesReaderError::EpochLengthIsZero) => return Err(Error::EpochLengthIsZero),
|
||||
Err(DutiesReaderError::Poisoned) => return Err(Error::EpochMapPoisoned),
|
||||
};
|
||||
|
||||
self.produce_attestation(slot, shard)
|
||||
} else {
|
||||
Ok(PollOutcome::SlotAlreadyProcessed(slot))
|
||||
}
|
||||
}
|
||||
|
||||
fn produce_attestation(&mut self, slot: Slot, shard: u64) -> Result<PollOutcome, Error> {
|
||||
let attestation_data = match self.beacon_node.produce_attestation_data(slot, shard)? {
|
||||
Some(attestation_data) => attestation_data,
|
||||
None => return Ok(PollOutcome::BeaconNodeUnableToProduceAttestation(slot)),
|
||||
};
|
||||
|
||||
if !self.safe_to_produce(&attestation_data) {
|
||||
return Ok(PollOutcome::SlashableAttestationNotProduced(slot));
|
||||
}
|
||||
|
||||
let signature = match self.sign_attestation_data(&attestation_data) {
|
||||
Some(signature) => signature,
|
||||
None => return Ok(PollOutcome::SignerRejection(slot)),
|
||||
};
|
||||
|
||||
let validator_index = match self.duties.validator_index() {
|
||||
Some(validator_index) => validator_index,
|
||||
None => return Ok(PollOutcome::ValidatorIsUnknown(slot)),
|
||||
};
|
||||
|
||||
let free_attestation = FreeAttestation {
|
||||
data: attestation_data,
|
||||
signature,
|
||||
validator_index,
|
||||
};
|
||||
|
||||
self.beacon_node.publish_attestation(free_attestation)?;
|
||||
Ok(PollOutcome::AttestationProduced(slot))
|
||||
}
|
||||
|
||||
fn is_processed_slot(&self, slot: Slot) -> bool {
|
||||
match self.last_processed_slot {
|
||||
Some(processed_slot) if slot <= processed_slot => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes a block, returning that block signed by the validators private key.
|
||||
///
|
||||
/// Important: this function will not check to ensure the block is not slashable. This must be
|
||||
/// done upstream.
|
||||
fn sign_attestation_data(&mut self, attestation_data: &AttestationData) -> Option<Signature> {
|
||||
self.store_produce(attestation_data);
|
||||
|
||||
let message = AttestationDataAndCustodyBit {
|
||||
data: attestation_data.clone(),
|
||||
custody_bit: PHASE_0_CUSTODY_BIT,
|
||||
}
|
||||
.hash_tree_root();
|
||||
|
||||
self.signer
|
||||
.sign_attestation_message(&message[..], DOMAIN_ATTESTATION)
|
||||
}
|
||||
|
||||
/// Returns `true` if signing some attestation_data is safe (non-slashable).
|
||||
///
|
||||
/// !!! UNSAFE !!!
|
||||
///
|
||||
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
||||
fn safe_to_produce(&self, _attestation_data: &AttestationData) -> bool {
|
||||
// TODO: ensure the producer doesn't produce slashable blocks.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
true
|
||||
}
|
||||
|
||||
/// Record that a block was produced so that slashable votes may not be made in the future.
|
||||
///
|
||||
/// !!! UNSAFE !!!
|
||||
///
|
||||
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
||||
fn store_produce(&mut self, _block: &AttestationData) {
|
||||
// TODO: record this block production to prevent future slashings.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BeaconNodeError> for Error {
|
||||
fn from(e: BeaconNodeError) -> Error {
|
||||
Error::BeaconNodeError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::test_utils::{EpochMap, LocalSigner, SimulatedBeaconNode};
|
||||
use super::*;
|
||||
use slot_clock::TestingSlotClock;
|
||||
use types::{
|
||||
test_utils::{SeedableRng, TestRandom, XorShiftRng},
|
||||
ChainSpec, Keypair,
|
||||
};
|
||||
|
||||
// TODO: implement more thorough testing.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
//
|
||||
// These tests should serve as a good example for future tests.
|
||||
|
||||
#[test]
|
||||
pub fn polling() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
|
||||
let spec = Arc::new(ChainSpec::foundation());
|
||||
let slot_clock = Arc::new(TestingSlotClock::new(0));
|
||||
let beacon_node = Arc::new(SimulatedBeaconNode::default());
|
||||
let signer = Arc::new(LocalSigner::new(Keypair::random()));
|
||||
|
||||
let mut duties = EpochMap::new(spec.slots_per_epoch);
|
||||
let attest_slot = Slot::new(100);
|
||||
let attest_epoch = attest_slot / spec.slots_per_epoch;
|
||||
let attest_shard = 12;
|
||||
duties.insert_attestation_shard(attest_slot, attest_shard);
|
||||
duties.set_validator_index(Some(2));
|
||||
let duties = Arc::new(duties);
|
||||
|
||||
let mut attester = Attester::new(
|
||||
duties.clone(),
|
||||
slot_clock.clone(),
|
||||
beacon_node.clone(),
|
||||
signer.clone(),
|
||||
);
|
||||
|
||||
// Configure responses from the BeaconNode.
|
||||
beacon_node.set_next_produce_result(Ok(Some(AttestationData::random_for_test(&mut rng))));
|
||||
beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidAttestation));
|
||||
|
||||
// One slot before attestation slot...
|
||||
slot_clock.set_slot(attest_slot.as_u64() - 1);
|
||||
assert_eq!(
|
||||
attester.poll(),
|
||||
Ok(PollOutcome::AttestationNotRequired(attest_slot - 1))
|
||||
);
|
||||
|
||||
// On the attest slot...
|
||||
slot_clock.set_slot(attest_slot.as_u64());
|
||||
assert_eq!(
|
||||
attester.poll(),
|
||||
Ok(PollOutcome::AttestationProduced(attest_slot))
|
||||
);
|
||||
|
||||
// Trying the same attest slot again...
|
||||
slot_clock.set_slot(attest_slot.as_u64());
|
||||
assert_eq!(
|
||||
attester.poll(),
|
||||
Ok(PollOutcome::SlotAlreadyProcessed(attest_slot))
|
||||
);
|
||||
|
||||
// One slot after the attest slot...
|
||||
slot_clock.set_slot(attest_slot.as_u64() + 1);
|
||||
assert_eq!(
|
||||
attester.poll(),
|
||||
Ok(PollOutcome::AttestationNotRequired(attest_slot + 1))
|
||||
);
|
||||
|
||||
// In an epoch without known duties...
|
||||
let slot = (attest_epoch + 1) * spec.slots_per_epoch;
|
||||
slot_clock.set_slot(slot.into());
|
||||
assert_eq!(
|
||||
attester.poll(),
|
||||
Ok(PollOutcome::ProducerDutiesUnknown(slot))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
use crate::{DutiesReader, DutiesReaderError};
|
||||
use std::collections::HashMap;
|
||||
use types::{Epoch, Slot};
|
||||
|
||||
pub struct EpochMap {
|
||||
slots_per_epoch: u64,
|
||||
validator_index: Option<u64>,
|
||||
map: HashMap<Epoch, (Slot, u64)>,
|
||||
}
|
||||
|
||||
impl EpochMap {
|
||||
pub fn new(slots_per_epoch: u64) -> Self {
|
||||
Self {
|
||||
slots_per_epoch,
|
||||
validator_index: None,
|
||||
map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_attestation_shard(&mut self, slot: Slot, shard: u64) {
|
||||
let epoch = slot.epoch(self.slots_per_epoch);
|
||||
self.map.insert(epoch, (slot, shard));
|
||||
}
|
||||
|
||||
pub fn set_validator_index(&mut self, index: Option<u64>) {
|
||||
self.validator_index = index;
|
||||
}
|
||||
}
|
||||
|
||||
impl DutiesReader for EpochMap {
|
||||
fn attestation_shard(&self, slot: Slot) -> Result<Option<u64>, DutiesReaderError> {
|
||||
let epoch = slot.epoch(self.slots_per_epoch);
|
||||
|
||||
match self.map.get(&epoch) {
|
||||
Some((attest_slot, attest_shard)) if *attest_slot == slot => Ok(Some(*attest_shard)),
|
||||
Some((attest_slot, _attest_shard)) if *attest_slot != slot => Ok(None),
|
||||
_ => Err(DutiesReaderError::UnknownEpoch),
|
||||
}
|
||||
}
|
||||
|
||||
fn validator_index(&self) -> Option<u64> {
|
||||
self.validator_index
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
use crate::traits::Signer;
|
||||
use std::sync::RwLock;
|
||||
use types::{Keypair, Signature};
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
pub struct LocalSigner {
|
||||
keypair: Keypair,
|
||||
should_sign: RwLock<bool>,
|
||||
}
|
||||
|
||||
impl LocalSigner {
|
||||
/// Produce a new LocalSigner with signing enabled by default.
|
||||
pub fn new(keypair: Keypair) -> Self {
|
||||
Self {
|
||||
keypair,
|
||||
should_sign: RwLock::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
/// If set to `false`, the service will refuse to sign all messages. Otherwise, all messages
|
||||
/// will be signed.
|
||||
pub fn enable_signing(&self, enabled: bool) {
|
||||
*self.should_sign.write().unwrap() = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
impl Signer for LocalSigner {
|
||||
fn sign_attestation_message(&self, message: &[u8], domain: u64) -> Option<Signature> {
|
||||
Some(Signature::new(message, domain, &self.keypair.sk))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
mod epoch_map;
|
||||
mod local_signer;
|
||||
mod simulated_beacon_node;
|
||||
|
||||
pub use self::epoch_map::EpochMap;
|
||||
pub use self::local_signer::LocalSigner;
|
||||
pub use self::simulated_beacon_node::SimulatedBeaconNode;
|
||||
@@ -1,44 +0,0 @@
|
||||
use crate::traits::{BeaconNode, BeaconNodeError, PublishOutcome};
|
||||
use std::sync::RwLock;
|
||||
use types::{AttestationData, FreeAttestation, Slot};
|
||||
|
||||
type ProduceResult = Result<Option<AttestationData>, BeaconNodeError>;
|
||||
type PublishResult = Result<PublishOutcome, BeaconNodeError>;
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
#[derive(Default)]
|
||||
pub struct SimulatedBeaconNode {
|
||||
pub produce_input: RwLock<Option<(Slot, u64)>>,
|
||||
pub produce_result: RwLock<Option<ProduceResult>>,
|
||||
|
||||
pub publish_input: RwLock<Option<FreeAttestation>>,
|
||||
pub publish_result: RwLock<Option<PublishResult>>,
|
||||
}
|
||||
|
||||
impl SimulatedBeaconNode {
|
||||
pub fn set_next_produce_result(&self, result: ProduceResult) {
|
||||
*self.produce_result.write().unwrap() = Some(result);
|
||||
}
|
||||
|
||||
pub fn set_next_publish_result(&self, result: PublishResult) {
|
||||
*self.publish_result.write().unwrap() = Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
impl BeaconNode for SimulatedBeaconNode {
|
||||
fn produce_attestation_data(&self, slot: Slot, shard: u64) -> ProduceResult {
|
||||
*self.produce_input.write().unwrap() = Some((slot, shard));
|
||||
match *self.produce_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: produce_result == None"),
|
||||
}
|
||||
}
|
||||
|
||||
fn publish_attestation(&self, free_attestation: FreeAttestation) -> PublishResult {
|
||||
*self.publish_input.write().unwrap() = Some(free_attestation.clone());
|
||||
match *self.publish_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: publish_result == None"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
use types::{AttestationData, FreeAttestation, Signature, Slot};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconNodeError {
|
||||
RemoteFailure(String),
|
||||
DecodeFailure,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum PublishOutcome {
|
||||
ValidAttestation,
|
||||
InvalidAttestation(String),
|
||||
}
|
||||
|
||||
/// Defines the methods required to produce and publish blocks on a Beacon Node.
|
||||
pub trait BeaconNode: Send + Sync {
|
||||
fn produce_attestation_data(
|
||||
&self,
|
||||
slot: Slot,
|
||||
shard: u64,
|
||||
) -> Result<Option<AttestationData>, BeaconNodeError>;
|
||||
|
||||
fn publish_attestation(
|
||||
&self,
|
||||
free_attestation: FreeAttestation,
|
||||
) -> Result<PublishOutcome, BeaconNodeError>;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum DutiesReaderError {
|
||||
UnknownValidator,
|
||||
UnknownEpoch,
|
||||
EpochLengthIsZero,
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
/// Informs a validator of their duties (e.g., block production).
|
||||
pub trait DutiesReader: Send + Sync {
|
||||
/// Returns `Some(shard)` if this slot is an attestation slot. Otherwise, returns `None.`
|
||||
fn attestation_shard(&self, slot: Slot) -> Result<Option<u64>, DutiesReaderError>;
|
||||
|
||||
/// Returns `Some(shard)` if this slot is an attestation slot. Otherwise, returns `None.`
|
||||
fn validator_index(&self) -> Option<u64>;
|
||||
}
|
||||
|
||||
/// Signs message using an internally-maintained private key.
|
||||
pub trait Signer {
|
||||
fn sign_attestation_message(&self, message: &[u8], domain: u64) -> Option<Signature>;
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "block_proposer"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
int_to_bytes = { path = "../utils/int_to_bytes" }
|
||||
slot_clock = { path = "../utils/slot_clock" }
|
||||
ssz = { path = "../utils/ssz" }
|
||||
types = { path = "../types" }
|
||||
@@ -1,303 +0,0 @@
|
||||
pub mod test_utils;
|
||||
mod traits;
|
||||
|
||||
use slot_clock::SlotClock;
|
||||
use ssz::{SignedRoot, TreeHash};
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, ChainSpec, Domain, Slot};
|
||||
|
||||
pub use self::traits::{
|
||||
BeaconNode, BeaconNodeError, DutiesReader, DutiesReaderError, PublishOutcome, Signer,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum PollOutcome {
|
||||
/// A new block was produced.
|
||||
BlockProduced(Slot),
|
||||
/// A block was not produced as it would have been slashable.
|
||||
SlashableBlockNotProduced(Slot),
|
||||
/// The validator duties did not require a block to be produced.
|
||||
BlockProductionNotRequired(Slot),
|
||||
/// The duties for the present epoch were not found.
|
||||
ProducerDutiesUnknown(Slot),
|
||||
/// The slot has already been processed, execution was skipped.
|
||||
SlotAlreadyProcessed(Slot),
|
||||
/// The Beacon Node was unable to produce a block at that slot.
|
||||
BeaconNodeUnableToProduceBlock(Slot),
|
||||
/// The signer failed to sign the message.
|
||||
SignerRejection(Slot),
|
||||
/// The public key for this validator is not an active validator.
|
||||
ValidatorIsUnknown(Slot),
|
||||
/// Unable to determine a `Fork` for signature domain generation.
|
||||
UnableToGetFork(Slot),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
SlotClockError,
|
||||
SlotUnknowable,
|
||||
EpochMapPoisoned,
|
||||
SlotClockPoisoned,
|
||||
EpochLengthIsZero,
|
||||
BeaconNodeError(BeaconNodeError),
|
||||
}
|
||||
|
||||
/// A polling state machine which performs block production duties, based upon some epoch duties
|
||||
/// (`EpochDutiesMap`) and a concept of time (`SlotClock`).
|
||||
///
|
||||
/// Ensures that messages are not slashable.
|
||||
///
|
||||
/// Relies upon an external service to keep the `EpochDutiesMap` updated.
|
||||
pub struct BlockProducer<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> {
|
||||
pub last_processed_slot: Option<Slot>,
|
||||
spec: Arc<ChainSpec>,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<T>,
|
||||
beacon_node: Arc<U>,
|
||||
signer: Arc<W>,
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U, V, W> {
|
||||
/// Returns a new instance where `last_processed_slot == 0`.
|
||||
pub fn new(
|
||||
spec: Arc<ChainSpec>,
|
||||
epoch_map: Arc<V>,
|
||||
slot_clock: Arc<T>,
|
||||
beacon_node: Arc<U>,
|
||||
signer: Arc<W>,
|
||||
) -> Self {
|
||||
Self {
|
||||
last_processed_slot: None,
|
||||
spec,
|
||||
epoch_map,
|
||||
slot_clock,
|
||||
beacon_node,
|
||||
signer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U, V, W> {
|
||||
/// "Poll" to see if the validator is required to take any action.
|
||||
///
|
||||
/// The slot clock will be read and any new actions undertaken.
|
||||
pub fn poll(&mut self) -> Result<PollOutcome, Error> {
|
||||
let slot = self
|
||||
.slot_clock
|
||||
.present_slot()
|
||||
.map_err(|_| Error::SlotClockError)?
|
||||
.ok_or(Error::SlotUnknowable)?;
|
||||
|
||||
// If this is a new slot.
|
||||
if !self.is_processed_slot(slot) {
|
||||
let is_block_production_slot = match self.epoch_map.is_block_production_slot(slot) {
|
||||
Ok(result) => result,
|
||||
Err(DutiesReaderError::UnknownEpoch) => {
|
||||
return Ok(PollOutcome::ProducerDutiesUnknown(slot));
|
||||
}
|
||||
Err(DutiesReaderError::UnknownValidator) => {
|
||||
return Ok(PollOutcome::ValidatorIsUnknown(slot));
|
||||
}
|
||||
Err(DutiesReaderError::EpochLengthIsZero) => return Err(Error::EpochLengthIsZero),
|
||||
Err(DutiesReaderError::Poisoned) => return Err(Error::EpochMapPoisoned),
|
||||
};
|
||||
|
||||
if is_block_production_slot {
|
||||
self.last_processed_slot = Some(slot);
|
||||
|
||||
self.produce_block(slot)
|
||||
} else {
|
||||
Ok(PollOutcome::BlockProductionNotRequired(slot))
|
||||
}
|
||||
} else {
|
||||
Ok(PollOutcome::SlotAlreadyProcessed(slot))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_processed_slot(&self, slot: Slot) -> bool {
|
||||
match self.last_processed_slot {
|
||||
Some(processed_slot) if processed_slot >= slot => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce a block at some slot.
|
||||
///
|
||||
/// Assumes that a block is required at this slot (does not check the duties).
|
||||
///
|
||||
/// Ensures the message is not slashable.
|
||||
///
|
||||
/// !!! UNSAFE !!!
|
||||
///
|
||||
/// The slash-protection code is not yet implemented. There is zero protection against
|
||||
/// slashing.
|
||||
fn produce_block(&mut self, slot: Slot) -> Result<PollOutcome, Error> {
|
||||
let fork = match self.epoch_map.fork() {
|
||||
Ok(fork) => fork,
|
||||
Err(_) => return Ok(PollOutcome::UnableToGetFork(slot)),
|
||||
};
|
||||
|
||||
let randao_reveal = {
|
||||
// TODO: add domain, etc to this message. Also ensure result matches `into_to_bytes32`.
|
||||
let message = slot.epoch(self.spec.slots_per_epoch).hash_tree_root();
|
||||
|
||||
match self.signer.sign_randao_reveal(
|
||||
&message,
|
||||
self.spec
|
||||
.get_domain(slot.epoch(self.spec.slots_per_epoch), Domain::Randao, &fork),
|
||||
) {
|
||||
None => return Ok(PollOutcome::SignerRejection(slot)),
|
||||
Some(signature) => signature,
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(block) = self
|
||||
.beacon_node
|
||||
.produce_beacon_block(slot, &randao_reveal)?
|
||||
{
|
||||
if self.safe_to_produce(&block) {
|
||||
let domain = self.spec.get_domain(
|
||||
slot.epoch(self.spec.slots_per_epoch),
|
||||
Domain::BeaconBlock,
|
||||
&fork,
|
||||
);
|
||||
if let Some(block) = self.sign_block(block, domain) {
|
||||
self.beacon_node.publish_beacon_block(block)?;
|
||||
Ok(PollOutcome::BlockProduced(slot))
|
||||
} else {
|
||||
Ok(PollOutcome::SignerRejection(slot))
|
||||
}
|
||||
} else {
|
||||
Ok(PollOutcome::SlashableBlockNotProduced(slot))
|
||||
}
|
||||
} else {
|
||||
Ok(PollOutcome::BeaconNodeUnableToProduceBlock(slot))
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes a block, returning that block signed by the validators private key.
|
||||
///
|
||||
/// Important: this function will not check to ensure the block is not slashable. This must be
|
||||
/// done upstream.
|
||||
fn sign_block(&mut self, mut block: BeaconBlock, domain: u64) -> Option<BeaconBlock> {
|
||||
self.store_produce(&block);
|
||||
|
||||
match self
|
||||
.signer
|
||||
.sign_block_proposal(&block.signed_root()[..], domain)
|
||||
{
|
||||
None => None,
|
||||
Some(signature) => {
|
||||
block.signature = signature;
|
||||
Some(block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if signing a block is safe (non-slashable).
|
||||
///
|
||||
/// !!! UNSAFE !!!
|
||||
///
|
||||
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
||||
fn safe_to_produce(&self, _block: &BeaconBlock) -> bool {
|
||||
// TODO: ensure the producer doesn't produce slashable blocks.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
true
|
||||
}
|
||||
|
||||
/// Record that a block was produced so that slashable votes may not be made in the future.
|
||||
///
|
||||
/// !!! UNSAFE !!!
|
||||
///
|
||||
/// Important: this function is presently stubbed-out. It provides ZERO SAFETY.
|
||||
fn store_produce(&mut self, _block: &BeaconBlock) {
|
||||
// TODO: record this block production to prevent future slashings.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BeaconNodeError> for Error {
|
||||
fn from(e: BeaconNodeError) -> Error {
|
||||
Error::BeaconNodeError(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::test_utils::{EpochMap, LocalSigner, SimulatedBeaconNode};
|
||||
use super::*;
|
||||
use slot_clock::TestingSlotClock;
|
||||
use types::{
|
||||
test_utils::{SeedableRng, TestRandom, XorShiftRng},
|
||||
Keypair,
|
||||
};
|
||||
|
||||
// TODO: implement more thorough testing.
|
||||
// https://github.com/sigp/lighthouse/issues/160
|
||||
//
|
||||
// These tests should serve as a good example for future tests.
|
||||
|
||||
#[test]
|
||||
pub fn polling() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
|
||||
let spec = Arc::new(ChainSpec::foundation());
|
||||
let slot_clock = Arc::new(TestingSlotClock::new(0));
|
||||
let beacon_node = Arc::new(SimulatedBeaconNode::default());
|
||||
let signer = Arc::new(LocalSigner::new(Keypair::random()));
|
||||
|
||||
let mut epoch_map = EpochMap::new(spec.slots_per_epoch);
|
||||
let produce_slot = Slot::new(100);
|
||||
let produce_epoch = produce_slot.epoch(spec.slots_per_epoch);
|
||||
epoch_map.map.insert(produce_epoch, produce_slot);
|
||||
let epoch_map = Arc::new(epoch_map);
|
||||
|
||||
let mut block_proposer = BlockProducer::new(
|
||||
spec.clone(),
|
||||
epoch_map.clone(),
|
||||
slot_clock.clone(),
|
||||
beacon_node.clone(),
|
||||
signer.clone(),
|
||||
);
|
||||
|
||||
// Configure responses from the BeaconNode.
|
||||
beacon_node.set_next_produce_result(Ok(Some(BeaconBlock::random_for_test(&mut rng))));
|
||||
beacon_node.set_next_publish_result(Ok(PublishOutcome::ValidBlock));
|
||||
|
||||
// One slot before production slot...
|
||||
slot_clock.set_slot(produce_slot.as_u64() - 1);
|
||||
assert_eq!(
|
||||
block_proposer.poll(),
|
||||
Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1))
|
||||
);
|
||||
|
||||
// On the produce slot...
|
||||
slot_clock.set_slot(produce_slot.as_u64());
|
||||
assert_eq!(
|
||||
block_proposer.poll(),
|
||||
Ok(PollOutcome::BlockProduced(produce_slot.into()))
|
||||
);
|
||||
|
||||
// Trying the same produce slot again...
|
||||
slot_clock.set_slot(produce_slot.as_u64());
|
||||
assert_eq!(
|
||||
block_proposer.poll(),
|
||||
Ok(PollOutcome::SlotAlreadyProcessed(produce_slot))
|
||||
);
|
||||
|
||||
// One slot after the produce slot...
|
||||
slot_clock.set_slot(produce_slot.as_u64() + 1);
|
||||
assert_eq!(
|
||||
block_proposer.poll(),
|
||||
Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1))
|
||||
);
|
||||
|
||||
// In an epoch without known duties...
|
||||
let slot = (produce_epoch.as_u64() + 1) * spec.slots_per_epoch;
|
||||
slot_clock.set_slot(slot);
|
||||
assert_eq!(
|
||||
block_proposer.poll(),
|
||||
Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot)))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
use crate::{DutiesReader, DutiesReaderError};
|
||||
use std::collections::HashMap;
|
||||
use types::{Epoch, Fork, Slot};
|
||||
|
||||
pub struct EpochMap {
|
||||
slots_per_epoch: u64,
|
||||
pub map: HashMap<Epoch, Slot>,
|
||||
}
|
||||
|
||||
impl EpochMap {
|
||||
pub fn new(slots_per_epoch: u64) -> Self {
|
||||
Self {
|
||||
slots_per_epoch,
|
||||
map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DutiesReader for EpochMap {
|
||||
fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError> {
|
||||
let epoch = slot.epoch(self.slots_per_epoch);
|
||||
match self.map.get(&epoch) {
|
||||
Some(s) if *s == slot => Ok(true),
|
||||
Some(s) if *s != slot => Ok(false),
|
||||
_ => Err(DutiesReaderError::UnknownEpoch),
|
||||
}
|
||||
}
|
||||
|
||||
fn fork(&self) -> Result<Fork, DutiesReaderError> {
|
||||
Ok(Fork {
|
||||
previous_version: [0; 4],
|
||||
current_version: [0; 4],
|
||||
epoch: Epoch::new(0),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
use crate::traits::Signer;
|
||||
use std::sync::RwLock;
|
||||
use types::{Keypair, Signature};
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
pub struct LocalSigner {
|
||||
keypair: Keypair,
|
||||
should_sign: RwLock<bool>,
|
||||
}
|
||||
|
||||
impl LocalSigner {
|
||||
/// Produce a new LocalSigner with signing enabled by default.
|
||||
pub fn new(keypair: Keypair) -> Self {
|
||||
Self {
|
||||
keypair,
|
||||
should_sign: RwLock::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
/// If set to `false`, the service will refuse to sign all messages. Otherwise, all messages
|
||||
/// will be signed.
|
||||
pub fn enable_signing(&self, enabled: bool) {
|
||||
*self.should_sign.write().unwrap() = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
impl Signer for LocalSigner {
|
||||
fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option<Signature> {
|
||||
Some(Signature::new(message, domain, &self.keypair.sk))
|
||||
}
|
||||
|
||||
fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option<Signature> {
|
||||
Some(Signature::new(message, domain, &self.keypair.sk))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
mod epoch_map;
|
||||
mod local_signer;
|
||||
mod simulated_beacon_node;
|
||||
|
||||
pub use self::epoch_map::EpochMap;
|
||||
pub use self::local_signer::LocalSigner;
|
||||
pub use self::simulated_beacon_node::SimulatedBeaconNode;
|
||||
@@ -1,48 +0,0 @@
|
||||
use crate::traits::{BeaconNode, BeaconNodeError, PublishOutcome};
|
||||
use std::sync::RwLock;
|
||||
use types::{BeaconBlock, Signature, Slot};
|
||||
|
||||
type ProduceResult = Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
type PublishResult = Result<PublishOutcome, BeaconNodeError>;
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
#[derive(Default)]
|
||||
pub struct SimulatedBeaconNode {
|
||||
pub produce_input: RwLock<Option<(Slot, Signature)>>,
|
||||
pub produce_result: RwLock<Option<ProduceResult>>,
|
||||
|
||||
pub publish_input: RwLock<Option<BeaconBlock>>,
|
||||
pub publish_result: RwLock<Option<PublishResult>>,
|
||||
}
|
||||
|
||||
impl SimulatedBeaconNode {
|
||||
/// Set the result to be returned when `produce_beacon_block` is called.
|
||||
pub fn set_next_produce_result(&self, result: ProduceResult) {
|
||||
*self.produce_result.write().unwrap() = Some(result);
|
||||
}
|
||||
|
||||
/// Set the result to be returned when `publish_beacon_block` is called.
|
||||
pub fn set_next_publish_result(&self, result: PublishResult) {
|
||||
*self.publish_result.write().unwrap() = Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
impl BeaconNode for SimulatedBeaconNode {
|
||||
/// Returns the value specified by the `set_next_produce_result`.
|
||||
fn produce_beacon_block(&self, slot: Slot, randao_reveal: &Signature) -> ProduceResult {
|
||||
*self.produce_input.write().unwrap() = Some((slot, randao_reveal.clone()));
|
||||
match *self.produce_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("SimulatedBeaconNode: produce_result == None"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value specified by the `set_next_publish_result`.
|
||||
fn publish_beacon_block(&self, block: BeaconBlock) -> PublishResult {
|
||||
*self.publish_input.write().unwrap() = Some(block);
|
||||
match *self.publish_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("SimulatedBeaconNode: publish_result == None"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
use types::{BeaconBlock, Fork, Signature, Slot};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum BeaconNodeError {
|
||||
RemoteFailure(String),
|
||||
DecodeFailure,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum PublishOutcome {
|
||||
ValidBlock,
|
||||
InvalidBlock(String),
|
||||
}
|
||||
|
||||
/// Defines the methods required to produce and publish blocks on a Beacon Node.
|
||||
pub trait BeaconNode: Send + Sync {
|
||||
/// Request that the node produces a block.
|
||||
///
|
||||
/// Returns Ok(None) if the Beacon Node is unable to produce at the given slot.
|
||||
fn produce_beacon_block(
|
||||
&self,
|
||||
slot: Slot,
|
||||
randao_reveal: &Signature,
|
||||
) -> Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
|
||||
/// Request that the node publishes a block.
|
||||
///
|
||||
/// Returns `true` if the publish was sucessful.
|
||||
fn publish_beacon_block(&self, block: BeaconBlock) -> Result<PublishOutcome, BeaconNodeError>;
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum DutiesReaderError {
|
||||
UnknownValidator,
|
||||
UnknownEpoch,
|
||||
EpochLengthIsZero,
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
/// Informs a validator of their duties (e.g., block production).
|
||||
pub trait DutiesReader: Send + Sync {
|
||||
fn is_block_production_slot(&self, slot: Slot) -> Result<bool, DutiesReaderError>;
|
||||
fn fork(&self) -> Result<Fork, DutiesReaderError>;
|
||||
}
|
||||
|
||||
/// Signs message using an internally-maintained private key.
|
||||
pub trait Signer {
|
||||
fn sign_block_proposal(&self, message: &[u8], domain: u64) -> Option<Signature>;
|
||||
fn sign_randao_reveal(&self, message: &[u8], domain: u64) -> Option<Signature>;
|
||||
}
|
||||
@@ -9,8 +9,9 @@ use db::{
|
||||
};
|
||||
use log::{debug, trace};
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, ChainSpec, Hash256, Slot, SlotHeight};
|
||||
use types::{BeaconBlock, BeaconState, ChainSpec, EthSpec, Hash256, Slot, SlotHeight};
|
||||
|
||||
//TODO: Pruning - Children
|
||||
//TODO: Handle Syncing
|
||||
@@ -33,7 +34,7 @@ fn power_of_2_below(x: u64) -> u64 {
|
||||
}
|
||||
|
||||
/// Stores the necessary data structures to run the optimised bitwise lmd ghost algorithm.
|
||||
pub struct BitwiseLMDGhost<T: ClientDB + Sized> {
|
||||
pub struct BitwiseLMDGhost<T: ClientDB + Sized, E> {
|
||||
/// A cache of known ancestors at given heights for a specific block.
|
||||
//TODO: Consider FnvHashMap
|
||||
cache: HashMap<CacheKey<u64>, Hash256>,
|
||||
@@ -50,9 +51,10 @@ pub struct BitwiseLMDGhost<T: ClientDB + Sized> {
|
||||
/// State storage access.
|
||||
state_store: Arc<BeaconStateStore<T>>,
|
||||
max_known_height: SlotHeight,
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<T> BitwiseLMDGhost<T>
|
||||
impl<T, E: EthSpec> BitwiseLMDGhost<T, E>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
{
|
||||
@@ -68,6 +70,7 @@ where
|
||||
max_known_height: SlotHeight::new(0),
|
||||
block_store,
|
||||
state_store,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +88,7 @@ where
|
||||
// build a hashmap of block_hash to weighted votes
|
||||
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
|
||||
// gets the current weighted votes
|
||||
let current_state = self
|
||||
let current_state: BeaconState<E> = self
|
||||
.state_store
|
||||
.get_deserialized(&state_root)?
|
||||
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
|
||||
@@ -240,7 +243,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ClientDB + Sized> ForkChoice for BitwiseLMDGhost<T> {
|
||||
impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for BitwiseLMDGhost<T, E> {
|
||||
fn add_block(
|
||||
&mut self,
|
||||
block: &BeaconBlock,
|
||||
|
||||
@@ -9,8 +9,9 @@ use db::{
|
||||
use log::{debug, trace};
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, ChainSpec, Hash256, Slot, SlotHeight};
|
||||
use types::{BeaconBlock, BeaconState, ChainSpec, EthSpec, Hash256, Slot, SlotHeight};
|
||||
|
||||
//TODO: Pruning - Children
|
||||
//TODO: Handle Syncing
|
||||
@@ -33,7 +34,7 @@ fn power_of_2_below(x: u64) -> u64 {
|
||||
}
|
||||
|
||||
/// Stores the necessary data structures to run the optimised lmd ghost algorithm.
|
||||
pub struct OptimizedLMDGhost<T: ClientDB + Sized> {
|
||||
pub struct OptimizedLMDGhost<T: ClientDB + Sized, E> {
|
||||
/// A cache of known ancestors at given heights for a specific block.
|
||||
//TODO: Consider FnvHashMap
|
||||
cache: HashMap<CacheKey<u64>, Hash256>,
|
||||
@@ -50,9 +51,10 @@ pub struct OptimizedLMDGhost<T: ClientDB + Sized> {
|
||||
/// State storage access.
|
||||
state_store: Arc<BeaconStateStore<T>>,
|
||||
max_known_height: SlotHeight,
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<T> OptimizedLMDGhost<T>
|
||||
impl<T, E: EthSpec> OptimizedLMDGhost<T, E>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
{
|
||||
@@ -68,6 +70,7 @@ where
|
||||
max_known_height: SlotHeight::new(0),
|
||||
block_store,
|
||||
state_store,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +88,7 @@ where
|
||||
// build a hashmap of block_hash to weighted votes
|
||||
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
|
||||
// gets the current weighted votes
|
||||
let current_state = self
|
||||
let current_state: BeaconState<E> = self
|
||||
.state_store
|
||||
.get_deserialized(&state_root)?
|
||||
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
|
||||
@@ -211,7 +214,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ClientDB + Sized> ForkChoice for OptimizedLMDGhost<T> {
|
||||
impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for OptimizedLMDGhost<T, E> {
|
||||
fn add_block(
|
||||
&mut self,
|
||||
block: &BeaconBlock,
|
||||
|
||||
@@ -7,12 +7,13 @@ use db::{
|
||||
};
|
||||
use log::{debug, trace};
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, ChainSpec, Hash256, Slot};
|
||||
use types::{BeaconBlock, BeaconState, ChainSpec, EthSpec, Hash256, Slot};
|
||||
|
||||
//TODO: Pruning and syncing
|
||||
|
||||
pub struct SlowLMDGhost<T: ClientDB + Sized> {
|
||||
pub struct SlowLMDGhost<T: ClientDB + Sized, E> {
|
||||
/// The latest attestation targets as a map of validator index to block hash.
|
||||
//TODO: Could this be a fixed size vec
|
||||
latest_attestation_targets: HashMap<u64, Hash256>,
|
||||
@@ -22,9 +23,10 @@ pub struct SlowLMDGhost<T: ClientDB + Sized> {
|
||||
block_store: Arc<BeaconBlockStore<T>>,
|
||||
/// State storage access.
|
||||
state_store: Arc<BeaconStateStore<T>>,
|
||||
_phantom: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<T> SlowLMDGhost<T>
|
||||
impl<T, E: EthSpec> SlowLMDGhost<T, E>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
{
|
||||
@@ -37,6 +39,7 @@ where
|
||||
children: HashMap::new(),
|
||||
block_store,
|
||||
state_store,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +57,7 @@ where
|
||||
// build a hashmap of block_hash to weighted votes
|
||||
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
|
||||
// gets the current weighted votes
|
||||
let current_state = self
|
||||
let current_state: BeaconState<E> = self
|
||||
.state_store
|
||||
.get_deserialized(&state_root)?
|
||||
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
|
||||
@@ -105,7 +108,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
|
||||
impl<T: ClientDB + Sized, E: EthSpec> ForkChoice for SlowLMDGhost<T, E> {
|
||||
/// Process when a block is added
|
||||
fn add_block(
|
||||
&mut self,
|
||||
|
||||
@@ -25,7 +25,9 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::{fs::File, io::prelude::*, path::PathBuf};
|
||||
use types::test_utils::TestingBeaconStateBuilder;
|
||||
use types::{BeaconBlock, BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Keypair, Slot};
|
||||
use types::{
|
||||
BeaconBlock, BeaconBlockBody, Eth1Data, EthSpec, FoundationEthSpec, Hash256, Keypair, Slot,
|
||||
};
|
||||
use yaml_rust::yaml;
|
||||
|
||||
// Note: We Assume the block Id's are hex-encoded.
|
||||
@@ -82,7 +84,7 @@ fn test_yaml_vectors(
|
||||
let test_cases = load_test_cases_from_yaml(yaml_file_path);
|
||||
|
||||
// default vars
|
||||
let spec = ChainSpec::foundation();
|
||||
let spec = FoundationEthSpec::spec();
|
||||
let zero_hash = Hash256::zero();
|
||||
let eth1_data = Eth1Data {
|
||||
deposit_root: zero_hash.clone(),
|
||||
@@ -227,23 +229,27 @@ fn setup_inital_state(
|
||||
|
||||
// the fork choice instantiation
|
||||
let fork_choice: Box<ForkChoice> = match fork_choice_algo {
|
||||
ForkChoiceAlgorithm::OptimizedLMDGhost => Box::new(OptimizedLMDGhost::new(
|
||||
block_store.clone(),
|
||||
state_store.clone(),
|
||||
)),
|
||||
ForkChoiceAlgorithm::BitwiseLMDGhost => Box::new(BitwiseLMDGhost::new(
|
||||
block_store.clone(),
|
||||
state_store.clone(),
|
||||
)),
|
||||
ForkChoiceAlgorithm::OptimizedLMDGhost => {
|
||||
let f: OptimizedLMDGhost<MemoryDB, FoundationEthSpec> =
|
||||
OptimizedLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
Box::new(f)
|
||||
}
|
||||
ForkChoiceAlgorithm::BitwiseLMDGhost => {
|
||||
let f: BitwiseLMDGhost<MemoryDB, FoundationEthSpec> =
|
||||
BitwiseLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
Box::new(f)
|
||||
}
|
||||
ForkChoiceAlgorithm::SlowLMDGhost => {
|
||||
Box::new(SlowLMDGhost::new(block_store.clone(), state_store.clone()))
|
||||
let f: SlowLMDGhost<MemoryDB, FoundationEthSpec> =
|
||||
SlowLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
Box::new(f)
|
||||
}
|
||||
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(block_store.clone())),
|
||||
};
|
||||
|
||||
let spec = ChainSpec::foundation();
|
||||
let spec = FoundationEthSpec::spec();
|
||||
|
||||
let mut state_builder =
|
||||
let mut state_builder: TestingBeaconStateBuilder<FoundationEthSpec> =
|
||||
TestingBeaconStateBuilder::from_single_keypair(num_validators, &Keypair::random(), &spec);
|
||||
state_builder.build_caches(&spec).unwrap();
|
||||
let (state, _keypairs) = state_builder.build();
|
||||
|
||||
+336
-301
@@ -13,10 +13,11 @@ use state_processing::per_block_processing::{
|
||||
verify_transfer_time_independent_only,
|
||||
};
|
||||
use std::collections::{btree_map::Entry, hash_map, BTreeMap, HashMap, HashSet};
|
||||
use std::marker::PhantomData;
|
||||
use types::chain_spec::Domain;
|
||||
use types::{
|
||||
Attestation, AttestationData, AttesterSlashing, BeaconState, ChainSpec, Deposit, Epoch,
|
||||
ProposerSlashing, Transfer, Validator, VoluntaryExit,
|
||||
EthSpec, ProposerSlashing, Transfer, Validator, VoluntaryExit,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -25,7 +26,7 @@ const VERIFY_DEPOSIT_PROOFS: bool = false;
|
||||
const VERIFY_DEPOSIT_PROOFS: bool = false; // TODO: enable this
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct OperationPool {
|
||||
pub struct OperationPool<T: EthSpec + Default> {
|
||||
/// Map from attestation ID (see below) to vectors of attestations.
|
||||
attestations: RwLock<HashMap<AttestationId, Vec<Attestation>>>,
|
||||
/// Map from deposit index to deposit data.
|
||||
@@ -42,6 +43,7 @@ pub struct OperationPool {
|
||||
voluntary_exits: RwLock<HashMap<u64, VoluntaryExit>>,
|
||||
/// Set of transfers.
|
||||
transfers: RwLock<HashSet<Transfer>>,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// Serialized `AttestationData` augmented with a domain to encode the fork info.
|
||||
@@ -52,14 +54,22 @@ struct AttestationId(Vec<u8>);
|
||||
const DOMAIN_BYTES_LEN: usize = 8;
|
||||
|
||||
impl AttestationId {
|
||||
fn from_data(attestation: &AttestationData, state: &BeaconState, spec: &ChainSpec) -> Self {
|
||||
fn from_data<T: EthSpec>(
|
||||
attestation: &AttestationData,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Self {
|
||||
let mut bytes = ssz_encode(attestation);
|
||||
let epoch = attestation.slot.epoch(spec.slots_per_epoch);
|
||||
bytes.extend_from_slice(&AttestationId::compute_domain_bytes(epoch, state, spec));
|
||||
AttestationId(bytes)
|
||||
}
|
||||
|
||||
fn compute_domain_bytes(epoch: Epoch, state: &BeaconState, spec: &ChainSpec) -> Vec<u8> {
|
||||
fn compute_domain_bytes<T: EthSpec>(
|
||||
epoch: Epoch,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Vec<u8> {
|
||||
int_to_bytes8(spec.get_domain(epoch, Domain::Attestation, &state.fork))
|
||||
}
|
||||
|
||||
@@ -75,7 +85,11 @@ impl AttestationId {
|
||||
/// receive for including it in a block.
|
||||
// TODO: this could be optimised with a map from validator index to whether that validator has
|
||||
// attested in each of the current and previous epochs. Currently quadractic in number of validators.
|
||||
fn attestation_score(attestation: &Attestation, state: &BeaconState, spec: &ChainSpec) -> usize {
|
||||
fn attestation_score<T: EthSpec>(
|
||||
attestation: &Attestation,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> usize {
|
||||
// Bitfield of validators whose attestations are new/fresh.
|
||||
let mut new_validators = attestation.aggregation_bitfield.clone();
|
||||
|
||||
@@ -113,7 +127,7 @@ pub enum DepositInsertStatus {
|
||||
Replaced(Box<Deposit>),
|
||||
}
|
||||
|
||||
impl OperationPool {
|
||||
impl<T: EthSpec> OperationPool<T> {
|
||||
/// Create a new operation pool.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
@@ -123,7 +137,7 @@ impl OperationPool {
|
||||
pub fn insert_attestation(
|
||||
&self,
|
||||
attestation: Attestation,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), AttestationValidationError> {
|
||||
// Check that attestation signatures are valid.
|
||||
@@ -161,15 +175,11 @@ impl OperationPool {
|
||||
|
||||
/// Total number of attestations in the pool, including attestations for the same data.
|
||||
pub fn num_attestations(&self) -> usize {
|
||||
self.attestations
|
||||
.read()
|
||||
.values()
|
||||
.map(|atts| atts.len())
|
||||
.sum()
|
||||
self.attestations.read().values().map(Vec::len).sum()
|
||||
}
|
||||
|
||||
/// Get a list of attestations for inclusion in a block.
|
||||
pub fn get_attestations(&self, state: &BeaconState, spec: &ChainSpec) -> Vec<Attestation> {
|
||||
pub fn get_attestations(&self, state: &BeaconState<T>, spec: &ChainSpec) -> Vec<Attestation> {
|
||||
// Attestations for the current fork, which may be from the current or previous epoch.
|
||||
let prev_epoch = state.previous_epoch(spec);
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
@@ -204,7 +214,7 @@ impl OperationPool {
|
||||
// TODO: we could probably prune other attestations here:
|
||||
// - ones that are completely covered by attestations included in the state
|
||||
// - maybe ones invalidated by the confirmation of one fork over another
|
||||
pub fn prune_attestations(&self, finalized_state: &BeaconState, spec: &ChainSpec) {
|
||||
pub fn prune_attestations(&self, finalized_state: &BeaconState<T>, spec: &ChainSpec) {
|
||||
self.attestations.write().retain(|_, attestations| {
|
||||
// All the attestations in this bucket have the same data, so we only need to
|
||||
// check the first one.
|
||||
@@ -220,7 +230,7 @@ impl OperationPool {
|
||||
pub fn insert_deposit(
|
||||
&self,
|
||||
deposit: Deposit,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<DepositInsertStatus, DepositValidationError> {
|
||||
use DepositInsertStatus::*;
|
||||
@@ -245,7 +255,7 @@ impl OperationPool {
|
||||
/// Get an ordered list of deposits for inclusion in a block.
|
||||
///
|
||||
/// Take at most the maximum number of deposits, beginning from the current deposit index.
|
||||
pub fn get_deposits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec<Deposit> {
|
||||
pub fn get_deposits(&self, state: &BeaconState<T>, spec: &ChainSpec) -> Vec<Deposit> {
|
||||
let start_idx = state.deposit_index;
|
||||
(start_idx..start_idx + spec.max_deposits)
|
||||
.map(|idx| self.deposits.read().get(&idx).cloned())
|
||||
@@ -255,7 +265,7 @@ impl OperationPool {
|
||||
}
|
||||
|
||||
/// Remove all deposits with index less than the deposit index of the latest finalised block.
|
||||
pub fn prune_deposits(&self, state: &BeaconState) -> BTreeMap<u64, Deposit> {
|
||||
pub fn prune_deposits(&self, state: &BeaconState<T>) -> BTreeMap<u64, Deposit> {
|
||||
let deposits_keep = self.deposits.write().split_off(&state.deposit_index);
|
||||
std::mem::replace(&mut self.deposits.write(), deposits_keep)
|
||||
}
|
||||
@@ -269,7 +279,7 @@ impl OperationPool {
|
||||
pub fn insert_proposer_slashing(
|
||||
&self,
|
||||
slashing: ProposerSlashing,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), ProposerSlashingValidationError> {
|
||||
// TODO: should maybe insert anyway if the proposer is unknown in the validator index,
|
||||
@@ -286,7 +296,7 @@ impl OperationPool {
|
||||
/// Depends on the fork field of the state, but not on the state's epoch.
|
||||
fn attester_slashing_id(
|
||||
slashing: &AttesterSlashing,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> (AttestationId, AttestationId) {
|
||||
(
|
||||
@@ -299,7 +309,7 @@ impl OperationPool {
|
||||
pub fn insert_attester_slashing(
|
||||
&self,
|
||||
slashing: AttesterSlashing,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), AttesterSlashingValidationError> {
|
||||
verify_attester_slashing(state, &slashing, true, spec)?;
|
||||
@@ -315,7 +325,7 @@ impl OperationPool {
|
||||
/// earlier in the block.
|
||||
pub fn get_slashings(
|
||||
&self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> (Vec<ProposerSlashing>, Vec<AttesterSlashing>) {
|
||||
let proposer_slashings = filter_limit_operations(
|
||||
@@ -370,7 +380,7 @@ impl OperationPool {
|
||||
}
|
||||
|
||||
/// Prune proposer slashings for all slashed or withdrawn validators.
|
||||
pub fn prune_proposer_slashings(&self, finalized_state: &BeaconState, spec: &ChainSpec) {
|
||||
pub fn prune_proposer_slashings(&self, finalized_state: &BeaconState<T>, spec: &ChainSpec) {
|
||||
prune_validator_hash_map(
|
||||
&mut self.proposer_slashings.write(),
|
||||
|validator| {
|
||||
@@ -383,7 +393,7 @@ impl OperationPool {
|
||||
|
||||
/// Prune attester slashings for all slashed or withdrawn validators, or attestations on another
|
||||
/// fork.
|
||||
pub fn prune_attester_slashings(&self, finalized_state: &BeaconState, spec: &ChainSpec) {
|
||||
pub fn prune_attester_slashings(&self, finalized_state: &BeaconState<T>, spec: &ChainSpec) {
|
||||
self.attester_slashings.write().retain(|id, slashing| {
|
||||
let fork_ok = &Self::attester_slashing_id(slashing, finalized_state, spec) == id;
|
||||
let curr_epoch = finalized_state.current_epoch(spec);
|
||||
@@ -402,7 +412,7 @@ impl OperationPool {
|
||||
pub fn insert_voluntary_exit(
|
||||
&self,
|
||||
exit: VoluntaryExit,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), ExitValidationError> {
|
||||
verify_exit_time_independent_only(state, &exit, spec)?;
|
||||
@@ -413,7 +423,11 @@ impl OperationPool {
|
||||
}
|
||||
|
||||
/// Get a list of voluntary exits for inclusion in a block.
|
||||
pub fn get_voluntary_exits(&self, state: &BeaconState, spec: &ChainSpec) -> Vec<VoluntaryExit> {
|
||||
pub fn get_voluntary_exits(
|
||||
&self,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Vec<VoluntaryExit> {
|
||||
filter_limit_operations(
|
||||
self.voluntary_exits.read().values(),
|
||||
|exit| verify_exit(state, exit, spec).is_ok(),
|
||||
@@ -422,7 +436,7 @@ impl OperationPool {
|
||||
}
|
||||
|
||||
/// Prune if validator has already exited at the last finalized state.
|
||||
pub fn prune_voluntary_exits(&self, finalized_state: &BeaconState, spec: &ChainSpec) {
|
||||
pub fn prune_voluntary_exits(&self, finalized_state: &BeaconState<T>, spec: &ChainSpec) {
|
||||
prune_validator_hash_map(
|
||||
&mut self.voluntary_exits.write(),
|
||||
|validator| validator.is_exited_at(finalized_state.current_epoch(spec)),
|
||||
@@ -434,7 +448,7 @@ impl OperationPool {
|
||||
pub fn insert_transfer(
|
||||
&self,
|
||||
transfer: Transfer,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), TransferValidationError> {
|
||||
// The signature of the transfer isn't hashed, but because we check
|
||||
@@ -448,7 +462,7 @@ impl OperationPool {
|
||||
/// Get a list of transfers for inclusion in a block.
|
||||
// TODO: improve the economic optimality of this function by accounting for
|
||||
// dependencies between transfers in the same block e.g. A pays B, B pays C
|
||||
pub fn get_transfers(&self, state: &BeaconState, spec: &ChainSpec) -> Vec<Transfer> {
|
||||
pub fn get_transfers(&self, state: &BeaconState<T>, spec: &ChainSpec) -> Vec<Transfer> {
|
||||
self.transfers
|
||||
.read()
|
||||
.iter()
|
||||
@@ -460,14 +474,14 @@ impl OperationPool {
|
||||
}
|
||||
|
||||
/// Prune the set of transfers by removing all those whose slot has already passed.
|
||||
pub fn prune_transfers(&self, finalized_state: &BeaconState) {
|
||||
pub fn prune_transfers(&self, finalized_state: &BeaconState<T>) {
|
||||
self.transfers
|
||||
.write()
|
||||
.retain(|transfer| transfer.slot > finalized_state.slot)
|
||||
}
|
||||
|
||||
/// Prune all types of transactions given the latest finalized state.
|
||||
pub fn prune_all(&self, finalized_state: &BeaconState, spec: &ChainSpec) {
|
||||
pub fn prune_all(&self, finalized_state: &BeaconState<T>, spec: &ChainSpec) {
|
||||
self.prune_attestations(finalized_state, spec);
|
||||
self.prune_deposits(finalized_state);
|
||||
self.prune_proposer_slashings(finalized_state, spec);
|
||||
@@ -487,7 +501,10 @@ impl OperationPool {
|
||||
///
|
||||
/// - Their `AttestationData` is equal.
|
||||
/// - `attestation` does not contain any signatures that `PendingAttestation` does not have.
|
||||
fn superior_attestation_exists_in_state(state: &BeaconState, attestation: &Attestation) -> bool {
|
||||
fn superior_attestation_exists_in_state<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation: &Attestation,
|
||||
) -> bool {
|
||||
state
|
||||
.current_epoch_attestations
|
||||
.iter()
|
||||
@@ -522,10 +539,10 @@ where
|
||||
/// The keys in the map should be validator indices, which will be looked up
|
||||
/// in the state's validator registry and then passed to `prune_if`.
|
||||
/// Entries for unknown validators will be kept.
|
||||
fn prune_validator_hash_map<T, F>(
|
||||
fn prune_validator_hash_map<T, F, E: EthSpec>(
|
||||
map: &mut HashMap<u64, T>,
|
||||
prune_if: F,
|
||||
finalized_state: &BeaconState,
|
||||
finalized_state: &BeaconState<E>,
|
||||
) where
|
||||
F: Fn(&Validator) -> bool,
|
||||
{
|
||||
@@ -649,7 +666,11 @@ mod tests {
|
||||
}
|
||||
|
||||
// Create a random deposit (with a valid proof of posession)
|
||||
fn make_deposit(rng: &mut XorShiftRng, state: &BeaconState, spec: &ChainSpec) -> Deposit {
|
||||
fn make_deposit<T: EthSpec>(
|
||||
rng: &mut XorShiftRng,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Deposit {
|
||||
let keypair = Keypair::random();
|
||||
let mut deposit = Deposit::random_for_test(rng);
|
||||
let mut deposit_input = DepositInput {
|
||||
@@ -668,9 +689,9 @@ mod tests {
|
||||
}
|
||||
|
||||
// Create `count` dummy deposits with sequential deposit IDs beginning from `start`.
|
||||
fn dummy_deposits(
|
||||
fn dummy_deposits<T: EthSpec>(
|
||||
rng: &mut XorShiftRng,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
start: u64,
|
||||
count: u64,
|
||||
@@ -685,301 +706,315 @@ mod tests {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn test_state(rng: &mut XorShiftRng) -> (ChainSpec, BeaconState) {
|
||||
let spec = ChainSpec::foundation();
|
||||
fn test_state(rng: &mut XorShiftRng) -> (ChainSpec, BeaconState<FoundationEthSpec>) {
|
||||
let spec = FoundationEthSpec::spec();
|
||||
|
||||
let mut state = BeaconState::random_for_test(rng);
|
||||
|
||||
state.fork = Fork::genesis(&spec);
|
||||
|
||||
(spec, state)
|
||||
}
|
||||
|
||||
/// Create a signed attestation for use in tests.
|
||||
/// Signed by all validators in `committee[signing_range]` and `committee[extra_signer]`.
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn signed_attestation<R: std::slice::SliceIndex<[usize], Output = [usize]>>(
|
||||
committee: &CrosslinkCommittee,
|
||||
keypairs: &[Keypair],
|
||||
signing_range: R,
|
||||
slot: Slot,
|
||||
state: &BeaconState,
|
||||
spec: &ChainSpec,
|
||||
extra_signer: Option<usize>,
|
||||
) -> Attestation {
|
||||
let mut builder = TestingAttestationBuilder::new(
|
||||
state,
|
||||
&committee.committee,
|
||||
slot,
|
||||
committee.shard,
|
||||
spec,
|
||||
);
|
||||
let signers = &committee.committee[signing_range];
|
||||
let committee_keys = signers.iter().map(|&i| &keypairs[i].sk).collect::<Vec<_>>();
|
||||
builder.sign(signers, &committee_keys, &state.fork, spec);
|
||||
extra_signer.map(|c_idx| {
|
||||
let validator_index = committee.committee[c_idx];
|
||||
builder.sign(
|
||||
&[validator_index],
|
||||
&[&keypairs[validator_index].sk],
|
||||
&state.fork,
|
||||
mod release_tests {
|
||||
use super::*;
|
||||
|
||||
/// Create a signed attestation for use in tests.
|
||||
/// Signed by all validators in `committee[signing_range]` and `committee[extra_signer]`.
|
||||
fn signed_attestation<R: std::slice::SliceIndex<[usize], Output = [usize]>, E: EthSpec>(
|
||||
committee: &CrosslinkCommittee,
|
||||
keypairs: &[Keypair],
|
||||
signing_range: R,
|
||||
slot: Slot,
|
||||
state: &BeaconState<E>,
|
||||
spec: &ChainSpec,
|
||||
extra_signer: Option<usize>,
|
||||
) -> Attestation {
|
||||
let mut builder = TestingAttestationBuilder::new(
|
||||
state,
|
||||
&committee.committee,
|
||||
slot,
|
||||
committee.shard,
|
||||
spec,
|
||||
)
|
||||
});
|
||||
builder.build()
|
||||
}
|
||||
|
||||
/// Test state for attestation-related tests.
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn attestation_test_state(
|
||||
spec: &ChainSpec,
|
||||
num_committees: usize,
|
||||
) -> (BeaconState, Vec<Keypair>) {
|
||||
let num_validators =
|
||||
num_committees * (spec.slots_per_epoch * spec.target_committee_size) as usize;
|
||||
let mut state_builder =
|
||||
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(num_validators, spec);
|
||||
let slot_offset = 1000 * spec.slots_per_epoch + spec.slots_per_epoch / 2;
|
||||
let slot = spec.genesis_slot + slot_offset;
|
||||
state_builder.teleport_to_slot(slot, spec);
|
||||
state_builder.build_caches(spec).unwrap();
|
||||
state_builder.build()
|
||||
}
|
||||
|
||||
/// Set the latest crosslink in the state to match the attestation.
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn fake_latest_crosslink(att: &Attestation, state: &mut BeaconState, spec: &ChainSpec) {
|
||||
state.latest_crosslinks[att.data.shard as usize] = Crosslink {
|
||||
crosslink_data_root: att.data.crosslink_data_root,
|
||||
epoch: att.data.slot.epoch(spec.slots_per_epoch),
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn test_attestation_score() {
|
||||
let spec = &ChainSpec::foundation();
|
||||
let (ref mut state, ref keypairs) = attestation_test_state(spec, 1);
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
for committee in committees {
|
||||
let att1 = signed_attestation(&committee, keypairs, ..2, slot, state, spec, None);
|
||||
let att2 = signed_attestation(&committee, keypairs, .., slot, state, spec, None);
|
||||
|
||||
assert_eq!(
|
||||
att1.aggregation_bitfield.num_set_bits(),
|
||||
attestation_score(&att1, state, spec)
|
||||
);
|
||||
|
||||
state
|
||||
.current_epoch_attestations
|
||||
.push(PendingAttestation::from_attestation(&att1, state.slot));
|
||||
|
||||
assert_eq!(
|
||||
committee.committee.len() - 2,
|
||||
attestation_score(&att2, state, spec)
|
||||
);
|
||||
let signers = &committee.committee[signing_range];
|
||||
let committee_keys = signers.iter().map(|&i| &keypairs[i].sk).collect::<Vec<_>>();
|
||||
builder.sign(signers, &committee_keys, &state.fork, spec);
|
||||
extra_signer.map(|c_idx| {
|
||||
let validator_index = committee.committee[c_idx];
|
||||
builder.sign(
|
||||
&[validator_index],
|
||||
&[&keypairs[validator_index].sk],
|
||||
&state.fork,
|
||||
spec,
|
||||
)
|
||||
});
|
||||
builder.build()
|
||||
}
|
||||
}
|
||||
|
||||
/// End-to-end test of basic attestation handling.
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn attestation_aggregation_insert_get_prune() {
|
||||
let spec = &ChainSpec::foundation();
|
||||
let (ref mut state, ref keypairs) = attestation_test_state(spec, 1);
|
||||
let op_pool = OperationPool::new();
|
||||
/// Test state for attestation-related tests.
|
||||
fn attestation_test_state<E: EthSpec>(
|
||||
num_committees: usize,
|
||||
) -> (BeaconState<E>, Vec<Keypair>, ChainSpec) {
|
||||
let spec = E::spec();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let num_validators =
|
||||
num_committees * (spec.slots_per_epoch * spec.target_committee_size) as usize;
|
||||
let mut state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(
|
||||
num_validators,
|
||||
&spec,
|
||||
);
|
||||
let slot_offset = 1000 * spec.slots_per_epoch + spec.slots_per_epoch / 2;
|
||||
let slot = spec.genesis_slot + slot_offset;
|
||||
state_builder.teleport_to_slot(slot, &spec);
|
||||
state_builder.build_caches(&spec).unwrap();
|
||||
let (state, keypairs) = state_builder.build();
|
||||
|
||||
assert_eq!(
|
||||
committees.len(),
|
||||
1,
|
||||
"we expect just one committee with this many validators"
|
||||
);
|
||||
(state, keypairs, FoundationEthSpec::spec())
|
||||
}
|
||||
|
||||
/// Set the latest crosslink in the state to match the attestation.
|
||||
fn fake_latest_crosslink<E: EthSpec>(
|
||||
att: &Attestation,
|
||||
state: &mut BeaconState<E>,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
state.latest_crosslinks[att.data.shard as usize] = Crosslink {
|
||||
crosslink_data_root: att.data.crosslink_data_root,
|
||||
epoch: att.data.slot.epoch(spec.slots_per_epoch),
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_attestation_score() {
|
||||
let (ref mut state, ref keypairs, ref spec) =
|
||||
attestation_test_state::<FoundationEthSpec>(1);
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
for committee in committees {
|
||||
let att1 = signed_attestation(&committee, keypairs, ..2, slot, state, spec, None);
|
||||
let att2 = signed_attestation(&committee, keypairs, .., slot, state, spec, None);
|
||||
|
||||
assert_eq!(
|
||||
att1.aggregation_bitfield.num_set_bits(),
|
||||
attestation_score(&att1, state, spec)
|
||||
);
|
||||
|
||||
state
|
||||
.current_epoch_attestations
|
||||
.push(PendingAttestation::from_attestation(&att1, state.slot));
|
||||
|
||||
assert_eq!(
|
||||
committee.committee.len() - 2,
|
||||
attestation_score(&att2, state, spec)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// End-to-end test of basic attestation handling.
|
||||
#[test]
|
||||
fn attestation_aggregation_insert_get_prune() {
|
||||
let (ref mut state, ref keypairs, ref spec) =
|
||||
attestation_test_state::<FoundationEthSpec>(1);
|
||||
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
assert_eq!(
|
||||
committees.len(),
|
||||
1,
|
||||
"we expect just one committee with this many validators"
|
||||
);
|
||||
|
||||
for committee in &committees {
|
||||
let step_size = 2;
|
||||
for i in (0..committee.committee.len()).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
None,
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
|
||||
// Before the min attestation inclusion delay, get_attestations shouldn't return anything.
|
||||
assert_eq!(op_pool.get_attestations(state, spec).len(), 0);
|
||||
|
||||
// Then once the delay has elapsed, we should get a single aggregated attestation.
|
||||
state.slot += spec.min_attestation_inclusion_delay;
|
||||
|
||||
let block_attestations = op_pool.get_attestations(state, spec);
|
||||
assert_eq!(block_attestations.len(), committees.len());
|
||||
|
||||
let agg_att = &block_attestations[0];
|
||||
assert_eq!(
|
||||
agg_att.aggregation_bitfield.num_set_bits(),
|
||||
spec.target_committee_size as usize
|
||||
);
|
||||
|
||||
// Prune attestations shouldn't do anything at this point.
|
||||
op_pool.prune_attestations(state, spec);
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
|
||||
// But once we advance to an epoch after the attestation, it should prune it out of
|
||||
// existence.
|
||||
state.slot = slot + spec.slots_per_epoch;
|
||||
op_pool.prune_attestations(state, spec);
|
||||
assert_eq!(op_pool.num_attestations(), 0);
|
||||
}
|
||||
|
||||
/// Adding an attestation already in the pool should not increase the size of the pool.
|
||||
#[test]
|
||||
fn attestation_duplicate() {
|
||||
let (ref mut state, ref keypairs, ref spec) =
|
||||
attestation_test_state::<FoundationEthSpec>(1);
|
||||
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
for committee in &committees {
|
||||
let att = signed_attestation(committee, keypairs, .., slot, state, spec, None);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool
|
||||
.insert_attestation(att.clone(), state, spec)
|
||||
.unwrap();
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
}
|
||||
|
||||
/// Adding lots of attestations that only intersect pairwise should lead to two aggregate
|
||||
/// attestations.
|
||||
#[test]
|
||||
fn attestation_pairwise_overlapping() {
|
||||
let (ref mut state, ref keypairs, ref spec) =
|
||||
attestation_test_state::<FoundationEthSpec>(1);
|
||||
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
for committee in &committees {
|
||||
let step_size = 2;
|
||||
for i in (0..committee.committee.len()).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
None,
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
for committee in &committees {
|
||||
// Create attestations that overlap on `step_size` validators, like:
|
||||
// {0,1,2,3}, {2,3,4,5}, {4,5,6,7}, ...
|
||||
for i in (0..committee.committee.len() - step_size).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + 2 * step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
None,
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// The attestations should get aggregated into two attestations that comprise all
|
||||
// validators.
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(op_pool.num_attestations(), 2 * committees.len());
|
||||
}
|
||||
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
/// Create a bunch of attestations signed by a small number of validators, and another
|
||||
/// bunch signed by a larger number, such that there are at least `max_attestations`
|
||||
/// signed by the larger number. Then, check that `get_attestations` only returns the
|
||||
/// high-quality attestations. To ensure that no aggregation occurs, ALL attestations
|
||||
/// are also signed by the 0th member of the committee.
|
||||
#[test]
|
||||
fn attestation_get_max() {
|
||||
let small_step_size = 2;
|
||||
let big_step_size = 4;
|
||||
|
||||
// Before the min attestation inclusion delay, get_attestations shouldn't return anything.
|
||||
assert_eq!(op_pool.get_attestations(state, spec).len(), 0);
|
||||
let (ref mut state, ref keypairs, ref spec) =
|
||||
attestation_test_state::<FoundationEthSpec>(big_step_size);
|
||||
|
||||
// Then once the delay has elapsed, we should get a single aggregated attestation.
|
||||
state.slot += spec.min_attestation_inclusion_delay;
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let block_attestations = op_pool.get_attestations(state, spec);
|
||||
assert_eq!(block_attestations.len(), committees.len());
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let agg_att = &block_attestations[0];
|
||||
assert_eq!(
|
||||
agg_att.aggregation_bitfield.num_set_bits(),
|
||||
spec.target_committee_size as usize
|
||||
);
|
||||
let max_attestations = spec.max_attestations as usize;
|
||||
let target_committee_size = spec.target_committee_size as usize;
|
||||
|
||||
// Prune attestations shouldn't do anything at this point.
|
||||
op_pool.prune_attestations(state, spec);
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
let mut insert_attestations = |committee, step_size| {
|
||||
for i in (0..target_committee_size).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
if i == 0 { None } else { Some(0) },
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
// But once we advance to an epoch after the attestation, it should prune it out of
|
||||
// existence.
|
||||
state.slot = slot + spec.slots_per_epoch;
|
||||
op_pool.prune_attestations(state, spec);
|
||||
assert_eq!(op_pool.num_attestations(), 0);
|
||||
}
|
||||
|
||||
/// Adding an attestation already in the pool should not increase the size of the pool.
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn attestation_duplicate() {
|
||||
let spec = &ChainSpec::foundation();
|
||||
let (ref mut state, ref keypairs) = attestation_test_state(spec, 1);
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
for committee in &committees {
|
||||
let att = signed_attestation(committee, keypairs, .., slot, state, spec, None);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool
|
||||
.insert_attestation(att.clone(), state, spec)
|
||||
.unwrap();
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(op_pool.num_attestations(), committees.len());
|
||||
}
|
||||
|
||||
/// Adding lots of attestations that only intersect pairwise should lead to two aggregate
|
||||
/// attestations.
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn attestation_pairwise_overlapping() {
|
||||
let spec = &ChainSpec::foundation();
|
||||
let (ref mut state, ref keypairs) = attestation_test_state(spec, 1);
|
||||
let op_pool = OperationPool::new();
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let step_size = 2;
|
||||
for committee in &committees {
|
||||
// Create attestations that overlap on `step_size` validators, like:
|
||||
// {0,1,2,3}, {2,3,4,5}, {4,5,6,7}, ...
|
||||
for i in (0..committee.committee.len() - step_size).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + 2 * step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
None,
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
for committee in &committees {
|
||||
assert_eq!(committee.committee.len(), target_committee_size);
|
||||
// Attestations signed by only 2-3 validators
|
||||
insert_attestations(committee, small_step_size);
|
||||
// Attestations signed by 4+ validators
|
||||
insert_attestations(committee, big_step_size);
|
||||
}
|
||||
}
|
||||
|
||||
// The attestations should get aggregated into two attestations that comprise all
|
||||
// validators.
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(op_pool.num_attestations(), 2 * committees.len());
|
||||
}
|
||||
let num_small = target_committee_size / small_step_size;
|
||||
let num_big = target_committee_size / big_step_size;
|
||||
|
||||
/// Create a bunch of attestations signed by a small number of validators, and another
|
||||
/// bunch signed by a larger number, such that there are at least `max_attestations`
|
||||
/// signed by the larger number. Then, check that `get_attestations` only returns the
|
||||
/// high-quality attestations. To ensure that no aggregation occurs, ALL attestations
|
||||
/// are also signed by the 0th member of the committee.
|
||||
#[test]
|
||||
#[cfg(not(debug_assertions))]
|
||||
fn attestation_get_max() {
|
||||
let spec = &ChainSpec::foundation();
|
||||
let small_step_size = 2;
|
||||
let big_step_size = 4;
|
||||
let (ref mut state, ref keypairs) = attestation_test_state(spec, big_step_size);
|
||||
let op_pool = OperationPool::new();
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(
|
||||
op_pool.num_attestations(),
|
||||
(num_small + num_big) * committees.len()
|
||||
);
|
||||
assert!(op_pool.num_attestations() > max_attestations);
|
||||
|
||||
let slot = state.slot - 1;
|
||||
let committees = state
|
||||
.get_crosslink_committees_at_slot(slot, spec)
|
||||
.unwrap()
|
||||
.clone();
|
||||
state.slot += spec.min_attestation_inclusion_delay;
|
||||
let best_attestations = op_pool.get_attestations(state, spec);
|
||||
assert_eq!(best_attestations.len(), max_attestations);
|
||||
|
||||
let max_attestations = spec.max_attestations as usize;
|
||||
let target_committee_size = spec.target_committee_size as usize;
|
||||
|
||||
let mut insert_attestations = |committee, step_size| {
|
||||
for i in (0..target_committee_size).step_by(step_size) {
|
||||
let att = signed_attestation(
|
||||
committee,
|
||||
keypairs,
|
||||
i..i + step_size,
|
||||
slot,
|
||||
state,
|
||||
spec,
|
||||
if i == 0 { None } else { Some(0) },
|
||||
);
|
||||
fake_latest_crosslink(&att, state, spec);
|
||||
op_pool.insert_attestation(att, state, spec).unwrap();
|
||||
// All the best attestations should be signed by at least `big_step_size` (4) validators.
|
||||
for att in &best_attestations {
|
||||
assert!(att.aggregation_bitfield.num_set_bits() >= big_step_size);
|
||||
}
|
||||
};
|
||||
|
||||
for committee in &committees {
|
||||
assert_eq!(committee.committee.len(), target_committee_size);
|
||||
// Attestations signed by only 2-3 validators
|
||||
insert_attestations(committee, small_step_size);
|
||||
// Attestations signed by 4+ validators
|
||||
insert_attestations(committee, big_step_size);
|
||||
}
|
||||
|
||||
let num_small = target_committee_size / small_step_size;
|
||||
let num_big = target_committee_size / big_step_size;
|
||||
|
||||
assert_eq!(op_pool.attestations.read().len(), committees.len());
|
||||
assert_eq!(
|
||||
op_pool.num_attestations(),
|
||||
(num_small + num_big) * committees.len()
|
||||
);
|
||||
assert!(op_pool.num_attestations() > max_attestations);
|
||||
|
||||
state.slot += spec.min_attestation_inclusion_delay;
|
||||
let best_attestations = op_pool.get_attestations(state, spec);
|
||||
assert_eq!(best_attestations.len(), max_attestations);
|
||||
|
||||
// All the best attestations should be signed by at least `big_step_size` (4) validators.
|
||||
for att in &best_attestations {
|
||||
assert!(att.aggregation_bitfield.num_set_bits() >= big_step_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ env_logger = "0.6.0"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_yaml = "0.8"
|
||||
yaml-utils = { path = "yaml_utils" }
|
||||
|
||||
[dependencies]
|
||||
bls = { path = "../utils/bls" }
|
||||
@@ -26,5 +25,7 @@ log = "0.4"
|
||||
merkle_proof = { path = "../utils/merkle_proof" }
|
||||
ssz = { path = "../utils/ssz" }
|
||||
ssz_derive = { path = "../utils/ssz_derive" }
|
||||
tree_hash = { path = "../utils/tree_hash" }
|
||||
tree_hash_derive = { path = "../utils/tree_hash_derive" }
|
||||
types = { path = "../types" }
|
||||
rayon = "1.0"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use criterion::Criterion;
|
||||
use criterion::{black_box, Benchmark};
|
||||
use ssz::TreeHash;
|
||||
use state_processing::{
|
||||
per_block_processing,
|
||||
per_block_processing::{
|
||||
@@ -9,6 +8,7 @@ use state_processing::{
|
||||
verify_block_signature,
|
||||
},
|
||||
};
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
|
||||
/// Run the detailed benchmarking suite on the given `BeaconState`.
|
||||
@@ -263,7 +263,7 @@ pub fn bench_block_processing(
|
||||
c.bench(
|
||||
&format!("{}/block_processing", desc),
|
||||
Benchmark::new("tree_hash_block", move |b| {
|
||||
b.iter(|| black_box(block.hash_tree_root()))
|
||||
b.iter(|| black_box(block.tree_hash_root()))
|
||||
})
|
||||
.sample_size(10),
|
||||
);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use criterion::Criterion;
|
||||
use criterion::{black_box, Benchmark};
|
||||
use ssz::TreeHash;
|
||||
use state_processing::{
|
||||
per_epoch_processing,
|
||||
per_epoch_processing::{
|
||||
@@ -9,6 +8,7 @@ use state_processing::{
|
||||
update_active_tree_index_roots, update_latest_slashed_balances,
|
||||
},
|
||||
};
|
||||
use tree_hash::TreeHash;
|
||||
use types::test_utils::TestingBeaconStateBuilder;
|
||||
use types::*;
|
||||
|
||||
@@ -256,7 +256,7 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
|
||||
c.bench(
|
||||
&format!("{}/epoch_processing", desc),
|
||||
Benchmark::new("tree_hash_state", move |b| {
|
||||
b.iter(|| black_box(state_clone.hash_tree_root()))
|
||||
b.iter(|| black_box(state_clone.tree_hash_root()))
|
||||
})
|
||||
.sample_size(SMALL_BENCHING_SAMPLE_SIZE),
|
||||
);
|
||||
|
||||
@@ -2,9 +2,9 @@ use types::{BeaconStateError as Error, *};
|
||||
|
||||
/// Exit the validator of the given `index`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn exit_validator(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn exit_validator<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
|
||||
@@ -3,9 +3,9 @@ use types::{BeaconStateError as Error, *};
|
||||
|
||||
/// Slash the validator with index ``index``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn slash_validator(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn slash_validator<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -36,8 +36,7 @@ pub fn slash_validator(
|
||||
|
||||
state.set_slashed_balance(
|
||||
current_epoch,
|
||||
state.get_slashed_balance(current_epoch, spec)? + effective_balance,
|
||||
spec,
|
||||
state.get_slashed_balance(current_epoch)? + effective_balance,
|
||||
)?;
|
||||
|
||||
let whistleblower_index =
|
||||
@@ -56,7 +55,7 @@ pub fn slash_validator(
|
||||
state.validator_registry[validator_index].slashed = true;
|
||||
|
||||
state.validator_registry[validator_index].withdrawable_epoch =
|
||||
current_epoch + Epoch::from(spec.latest_slashed_exit_length);
|
||||
current_epoch + Epoch::from(T::LatestSlashedExitLength::to_usize());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use types::*;
|
||||
///
|
||||
/// Is title `verify_bitfield` in spec.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_bitfield_length(bitfield: &Bitfield, committee_size: usize) -> bool {
|
||||
if bitfield.num_bytes() != ((committee_size + 7) / 8) {
|
||||
return false;
|
||||
@@ -18,3 +18,62 @@ pub fn verify_bitfield_length(bitfield: &Bitfield, committee_size: usize) -> boo
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bitfield_length() {
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b0000_0001]), 4),
|
||||
true
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b0001_0001]), 4),
|
||||
false
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b0000_0000]), 4),
|
||||
true
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b1000_0000]), 8),
|
||||
true
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b1000_0000, 0b0000_0000]), 16),
|
||||
true
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b1000_0000, 0b0000_0000]), 15),
|
||||
false
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(&Bitfield::from_bytes(&[0b0000_0000, 0b0000_0000]), 8),
|
||||
false
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(
|
||||
&Bitfield::from_bytes(&[0b0000_0000, 0b0000_0000, 0b0000_0000]),
|
||||
8
|
||||
),
|
||||
false
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
verify_bitfield_length(
|
||||
&Bitfield::from_bytes(&[0b0000_0000, 0b0000_0000, 0b0000_0000]),
|
||||
24
|
||||
),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::per_block_processing::{errors::BlockProcessingError, process_deposits};
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
|
||||
pub enum GenesisError {
|
||||
@@ -9,13 +9,13 @@ pub enum GenesisError {
|
||||
|
||||
/// Returns the genesis `BeaconState`
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_genesis_state(
|
||||
/// Spec v0.5.1
|
||||
pub fn get_genesis_state<T: EthSpec>(
|
||||
genesis_validator_deposits: &[Deposit],
|
||||
genesis_time: u64,
|
||||
genesis_eth1_data: Eth1Data,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
) -> Result<BeaconState<T>, BlockProcessingError> {
|
||||
// Get the genesis `BeaconState`
|
||||
let mut state = BeaconState::genesis(genesis_time, genesis_eth1_data, spec);
|
||||
|
||||
@@ -36,13 +36,13 @@ pub fn get_genesis_state(
|
||||
let active_validator_indices = state
|
||||
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
|
||||
.to_vec();
|
||||
let genesis_active_index_root = Hash256::from_slice(&active_validator_indices.hash_tree_root());
|
||||
state.fill_active_index_roots_with(genesis_active_index_root, spec);
|
||||
let genesis_active_index_root = Hash256::from_slice(&active_validator_indices.tree_hash_root());
|
||||
state.fill_active_index_roots_with(genesis_active_index_root);
|
||||
|
||||
// Generate the current shuffling seed.
|
||||
state.current_shuffling_seed = state.generate_seed(spec.genesis_epoch, spec)?;
|
||||
|
||||
Ok(())
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
impl From<BlockProcessingError> for GenesisError {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::common::slash_validator;
|
||||
use errors::{BlockInvalid as Invalid, BlockProcessingError as Error, IntoWithIndex};
|
||||
use rayon::prelude::*;
|
||||
use ssz::{SignedRoot, TreeHash};
|
||||
use tree_hash::{SignedRoot, TreeHash};
|
||||
use types::*;
|
||||
|
||||
pub use self::verify_attester_slashing::{
|
||||
@@ -41,9 +41,9 @@ const VERIFY_DEPOSIT_MERKLE_PROOFS: bool = false;
|
||||
/// Returns `Ok(())` if the block is valid and the state was successfully updated. Otherwise
|
||||
/// returns an error describing why the block was invalid or how the function failed to execute.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn per_block_processing(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn per_block_processing<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -56,9 +56,9 @@ pub fn per_block_processing(
|
||||
/// Returns `Ok(())` if the block is valid and the state was successfully updated. Otherwise
|
||||
/// returns an error describing why the block was invalid or how the function failed to execute.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn per_block_processing_without_verifying_block_signature(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn per_block_processing_without_verifying_block_signature<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -71,9 +71,9 @@ pub fn per_block_processing_without_verifying_block_signature(
|
||||
/// Returns `Ok(())` if the block is valid and the state was successfully updated. Otherwise
|
||||
/// returns an error describing why the block was invalid or how the function failed to execute.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn per_block_processing_signature_optional(
|
||||
mut state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn per_block_processing_signature_optional<T: EthSpec>(
|
||||
mut state: &mut BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
should_verify_block_signature: bool,
|
||||
spec: &ChainSpec,
|
||||
@@ -101,20 +101,22 @@ fn per_block_processing_signature_optional(
|
||||
|
||||
/// Processes the block header.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_block_header(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_block_header<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
verify!(block.slot == state.slot, Invalid::StateSlotMismatch);
|
||||
|
||||
// NOTE: this is not to spec. I think spec is broken. See:
|
||||
//
|
||||
// https://github.com/ethereum/eth2.0-specs/issues/797
|
||||
let expected_previous_block_root =
|
||||
Hash256::from_slice(&state.latest_block_header.signed_root());
|
||||
verify!(
|
||||
block.previous_block_root == *state.get_block_root(state.slot - 1, spec)?,
|
||||
Invalid::ParentBlockRootMismatch
|
||||
block.previous_block_root == expected_previous_block_root,
|
||||
Invalid::ParentBlockRootMismatch {
|
||||
state: expected_previous_block_root,
|
||||
block: block.previous_block_root,
|
||||
}
|
||||
);
|
||||
|
||||
state.latest_block_header = block.temporary_block_header(spec);
|
||||
@@ -124,9 +126,9 @@ pub fn process_block_header(
|
||||
|
||||
/// Verifies the signature of a block.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_block_signature(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_block_signature<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -152,9 +154,9 @@ pub fn verify_block_signature(
|
||||
/// Verifies the `randao_reveal` against the block's proposer pubkey and updates
|
||||
/// `state.latest_randao_mixes`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_randao(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_randao<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
block: &BeaconBlock,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -164,7 +166,7 @@ pub fn process_randao(
|
||||
// Verify the RANDAO is a valid signature of the proposer.
|
||||
verify!(
|
||||
block.body.randao_reveal.verify(
|
||||
&state.current_epoch(spec).hash_tree_root()[..],
|
||||
&state.current_epoch(spec).tree_hash_root()[..],
|
||||
spec.get_domain(
|
||||
block.slot.epoch(spec.slots_per_epoch),
|
||||
Domain::Randao,
|
||||
@@ -183,8 +185,11 @@ pub fn process_randao(
|
||||
|
||||
/// Update the `state.eth1_data_votes` based upon the `eth1_data` provided.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_eth1_data(state: &mut BeaconState, eth1_data: &Eth1Data) -> Result<(), Error> {
|
||||
/// Spec v0.5.1
|
||||
pub fn process_eth1_data<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
eth1_data: &Eth1Data,
|
||||
) -> Result<(), Error> {
|
||||
// Attempt to find a `Eth1DataVote` with matching `Eth1Data`.
|
||||
let matching_eth1_vote_index = state
|
||||
.eth1_data_votes
|
||||
@@ -209,9 +214,9 @@ pub fn process_eth1_data(state: &mut BeaconState, eth1_data: &Eth1Data) -> Resul
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_proposer_slashings(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_proposer_slashings<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
proposer_slashings: &[ProposerSlashing],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -242,9 +247,9 @@ pub fn process_proposer_slashings(
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_attester_slashings(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_attester_slashings<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
attester_slashings: &[AttesterSlashing],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -300,9 +305,9 @@ pub fn process_attester_slashings(
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_attestations(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_attestations<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
attestations: &[Attestation],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -342,9 +347,9 @@ pub fn process_attestations(
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_deposits(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_deposits<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
deposits: &[Deposit],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -412,9 +417,9 @@ pub fn process_deposits(
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_exits(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_exits<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
voluntary_exits: &[VoluntaryExit],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -444,9 +449,9 @@ pub fn process_exits(
|
||||
/// Returns `Ok(())` if the validation and state updates completed successfully, otherwise returns
|
||||
/// an `Err` describing the invalid object or cause of failure.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_transfers(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_transfers<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
transfers: &[Transfer],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
|
||||
@@ -67,7 +67,10 @@ impl_from_beacon_state_error!(BlockProcessingError);
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum BlockInvalid {
|
||||
StateSlotMismatch,
|
||||
ParentBlockRootMismatch,
|
||||
ParentBlockRootMismatch {
|
||||
state: Hash256,
|
||||
block: Hash256,
|
||||
},
|
||||
BadSignature,
|
||||
BadRandaoSignature,
|
||||
MaxAttestationsExceeded,
|
||||
@@ -271,10 +274,10 @@ pub enum ProposerSlashingValidationError {
|
||||
pub enum ProposerSlashingInvalid {
|
||||
/// The proposer index is not a known validator.
|
||||
ProposerUnknown(u64),
|
||||
/// The two proposal have different slots.
|
||||
/// The two proposal have different epochs.
|
||||
///
|
||||
/// (proposal_1_slot, proposal_2_slot)
|
||||
ProposalSlotMismatch(Slot, Slot),
|
||||
ProposalEpochMismatch(Slot, Slot),
|
||||
/// The proposals are identical and therefore not slashable.
|
||||
ProposalsIdentical,
|
||||
/// The specified proposer has already been slashed.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::errors::{AttestationInvalid as Invalid, AttestationValidationError as Error};
|
||||
use crate::common::verify_bitfield_length;
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if an `Attestation` is valid to be included in a block in the current epoch of the
|
||||
@@ -8,9 +8,9 @@ use types::*;
|
||||
///
|
||||
/// Returns `Ok(())` if the `Attestation` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn validate_attestation(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn validate_attestation<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation: &Attestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -18,8 +18,8 @@ pub fn validate_attestation(
|
||||
}
|
||||
|
||||
/// Like `validate_attestation` but doesn't run checks which may become true in future states.
|
||||
pub fn validate_attestation_time_independent_only(
|
||||
state: &BeaconState,
|
||||
pub fn validate_attestation_time_independent_only<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation: &Attestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -31,9 +31,9 @@ pub fn validate_attestation_time_independent_only(
|
||||
///
|
||||
/// Returns `Ok(())` if the `Attestation` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn validate_attestation_without_signature(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn validate_attestation_without_signature<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation: &Attestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -44,9 +44,9 @@ pub fn validate_attestation_without_signature(
|
||||
/// given state, optionally validating the aggregate signature.
|
||||
///
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn validate_attestation_parametric(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn validate_attestation_parametric<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation: &Attestation,
|
||||
spec: &ChainSpec,
|
||||
verify_signature: bool,
|
||||
@@ -167,10 +167,10 @@ fn validate_attestation_parametric(
|
||||
/// Verify that the `source_epoch` and `source_root` of an `Attestation` correctly
|
||||
/// match the current (or previous) justified epoch and root from the state.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn verify_justified_epoch_and_root(
|
||||
/// Spec v0.5.1
|
||||
fn verify_justified_epoch_and_root<T: EthSpec>(
|
||||
attestation: &Attestation,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let state_epoch = state.slot.epoch(spec.slots_per_epoch);
|
||||
@@ -222,9 +222,9 @@ fn verify_justified_epoch_and_root(
|
||||
/// - `custody_bitfield` does not have a bit for each index of `committee`.
|
||||
/// - A `validator_index` in `committee` is not in `state.validator_registry`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn verify_attestation_signature(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn verify_attestation_signature<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
committee: &[usize],
|
||||
a: &Attestation,
|
||||
spec: &ChainSpec,
|
||||
@@ -270,14 +270,14 @@ fn verify_attestation_signature(
|
||||
data: a.data.clone(),
|
||||
custody_bit: false,
|
||||
}
|
||||
.hash_tree_root();
|
||||
.tree_hash_root();
|
||||
|
||||
// Message when custody bitfield is `true`
|
||||
let message_1 = AttestationDataAndCustodyBit {
|
||||
data: a.data.clone(),
|
||||
custody_bit: true,
|
||||
}
|
||||
.hash_tree_root();
|
||||
.tree_hash_root();
|
||||
|
||||
let mut messages = vec![];
|
||||
let mut keys = vec![];
|
||||
|
||||
@@ -7,9 +7,9 @@ use types::*;
|
||||
///
|
||||
/// Returns `Ok(())` if the `AttesterSlashing` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_attester_slashing(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_attester_slashing<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attester_slashing: &AttesterSlashing,
|
||||
should_verify_slashable_attestations: bool,
|
||||
spec: &ChainSpec,
|
||||
@@ -41,9 +41,9 @@ pub fn verify_attester_slashing(
|
||||
///
|
||||
/// Returns Ok(indices) if `indices.len() > 0`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn gather_attester_slashing_indices(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn gather_attester_slashing_indices<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attester_slashing: &AttesterSlashing,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<u64>, Error> {
|
||||
@@ -57,8 +57,8 @@ pub fn gather_attester_slashing_indices(
|
||||
|
||||
/// Same as `gather_attester_slashing_indices` but allows the caller to specify the criteria
|
||||
/// for determining whether a given validator should be considered slashed.
|
||||
pub fn gather_attester_slashing_indices_modular<F>(
|
||||
state: &BeaconState,
|
||||
pub fn gather_attester_slashing_indices_modular<F, T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attester_slashing: &AttesterSlashing,
|
||||
is_slashed: F,
|
||||
spec: &ChainSpec,
|
||||
|
||||
@@ -15,9 +15,9 @@ use types::*;
|
||||
///
|
||||
/// Note: this function is incomplete.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_deposit(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_deposit<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
deposit: &Deposit,
|
||||
verify_merkle_branch: bool,
|
||||
spec: &ChainSpec,
|
||||
@@ -46,8 +46,11 @@ pub fn verify_deposit(
|
||||
|
||||
/// Verify that the `Deposit` index is correct.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_deposit_index(state: &BeaconState, deposit: &Deposit) -> Result<(), Error> {
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_deposit_index<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
deposit: &Deposit,
|
||||
) -> Result<(), Error> {
|
||||
verify!(
|
||||
deposit.index == state.deposit_index,
|
||||
Invalid::BadIndex {
|
||||
@@ -65,8 +68,8 @@ pub fn verify_deposit_index(state: &BeaconState, deposit: &Deposit) -> Result<()
|
||||
/// ## Errors
|
||||
///
|
||||
/// Errors if the state's `pubkey_cache` is not current.
|
||||
pub fn get_existing_validator_index(
|
||||
state: &BeaconState,
|
||||
pub fn get_existing_validator_index<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
deposit: &Deposit,
|
||||
) -> Result<Option<u64>, Error> {
|
||||
let deposit_input = &deposit.deposit_data.deposit_input;
|
||||
@@ -88,12 +91,16 @@ pub fn get_existing_validator_index(
|
||||
|
||||
/// Verify that a deposit is included in the state's eth1 deposit root.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn verify_deposit_merkle_proof(state: &BeaconState, deposit: &Deposit, spec: &ChainSpec) -> bool {
|
||||
/// Spec v0.5.1
|
||||
fn verify_deposit_merkle_proof<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
deposit: &Deposit,
|
||||
spec: &ChainSpec,
|
||||
) -> bool {
|
||||
let leaf = hash(&get_serialized_deposit_data(deposit));
|
||||
verify_merkle_proof(
|
||||
Hash256::from_slice(&leaf),
|
||||
&deposit.proof,
|
||||
&deposit.proof[..],
|
||||
spec.deposit_contract_tree_depth as usize,
|
||||
deposit.index as usize,
|
||||
state.latest_eth1_data.deposit_root,
|
||||
@@ -102,7 +109,7 @@ fn verify_deposit_merkle_proof(state: &BeaconState, deposit: &Deposit, spec: &Ch
|
||||
|
||||
/// Helper struct for easily getting the serialized data generated by the deposit contract.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(Encode)]
|
||||
struct SerializedDepositData {
|
||||
amount: u64,
|
||||
@@ -113,7 +120,7 @@ struct SerializedDepositData {
|
||||
/// Return the serialized data generated by the deposit contract that is used to generate the
|
||||
/// merkle proof.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn get_serialized_deposit_data(deposit: &Deposit) -> Vec<u8> {
|
||||
let serialized_deposit_data = SerializedDepositData {
|
||||
amount: deposit.deposit_data.amount,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::errors::{ExitInvalid as Invalid, ExitValidationError as Error};
|
||||
use ssz::SignedRoot;
|
||||
use tree_hash::SignedRoot;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if an `Exit` is valid to be included in a block in the current epoch of the given
|
||||
@@ -7,9 +7,9 @@ use types::*;
|
||||
///
|
||||
/// Returns `Ok(())` if the `Exit` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_exit(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_exit<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
exit: &VoluntaryExit,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -17,8 +17,8 @@ pub fn verify_exit(
|
||||
}
|
||||
|
||||
/// Like `verify_exit` but doesn't run checks which may become true in future states.
|
||||
pub fn verify_exit_time_independent_only(
|
||||
state: &BeaconState,
|
||||
pub fn verify_exit_time_independent_only<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
exit: &VoluntaryExit,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -26,8 +26,8 @@ pub fn verify_exit_time_independent_only(
|
||||
}
|
||||
|
||||
/// Parametric version of `verify_exit` that skips some checks if `time_independent_only` is true.
|
||||
fn verify_exit_parametric(
|
||||
state: &BeaconState,
|
||||
fn verify_exit_parametric<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
exit: &VoluntaryExit,
|
||||
spec: &ChainSpec,
|
||||
time_independent_only: bool,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::errors::{ProposerSlashingInvalid as Invalid, ProposerSlashingValidationError as Error};
|
||||
use ssz::SignedRoot;
|
||||
use tree_hash::SignedRoot;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if a `ProposerSlashing` is valid to be included in a block in the current epoch of the given
|
||||
@@ -7,10 +7,10 @@ use types::*;
|
||||
///
|
||||
/// Returns `Ok(())` if the `ProposerSlashing` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_proposer_slashing(
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_proposer_slashing<T: EthSpec>(
|
||||
proposer_slashing: &ProposerSlashing,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let proposer = state
|
||||
@@ -21,8 +21,9 @@ pub fn verify_proposer_slashing(
|
||||
})?;
|
||||
|
||||
verify!(
|
||||
proposer_slashing.header_1.slot == proposer_slashing.header_2.slot,
|
||||
Invalid::ProposalSlotMismatch(
|
||||
proposer_slashing.header_1.slot.epoch(spec.slots_per_epoch)
|
||||
== proposer_slashing.header_2.slot.epoch(spec.slots_per_epoch),
|
||||
Invalid::ProposalEpochMismatch(
|
||||
proposer_slashing.header_1.slot,
|
||||
proposer_slashing.header_2.slot
|
||||
)
|
||||
@@ -66,7 +67,7 @@ pub fn verify_proposer_slashing(
|
||||
///
|
||||
/// Returns `true` if the signature is valid.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn verify_header_signature(
|
||||
header: &BeaconBlockHeader,
|
||||
pubkey: &PublicKey,
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::errors::{
|
||||
SlashableAttestationInvalid as Invalid, SlashableAttestationValidationError as Error,
|
||||
};
|
||||
use crate::common::verify_bitfield_length;
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if a `SlashableAttestation` is valid to be included in a block in the current epoch of the given
|
||||
@@ -10,9 +10,9 @@ use types::*;
|
||||
///
|
||||
/// Returns `Ok(())` if the `SlashableAttestation` is valid, otherwise indicates the reason for invalidity.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_slashable_attestation(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_slashable_attestation<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
slashable_attestation: &SlashableAttestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -77,12 +77,12 @@ pub fn verify_slashable_attestation(
|
||||
data: slashable_attestation.data.clone(),
|
||||
custody_bit: false,
|
||||
}
|
||||
.hash_tree_root();
|
||||
.tree_hash_root();
|
||||
let message_1 = AttestationDataAndCustodyBit {
|
||||
data: slashable_attestation.data.clone(),
|
||||
custody_bit: true,
|
||||
}
|
||||
.hash_tree_root();
|
||||
.tree_hash_root();
|
||||
|
||||
let mut messages = vec![];
|
||||
let mut keys = vec![];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::errors::{TransferInvalid as Invalid, TransferValidationError as Error};
|
||||
use bls::get_withdrawal_credentials;
|
||||
use ssz::SignedRoot;
|
||||
use tree_hash::SignedRoot;
|
||||
use types::*;
|
||||
|
||||
/// Indicates if a `Transfer` is valid to be included in a block in the current epoch of the given
|
||||
@@ -10,9 +10,9 @@ use types::*;
|
||||
///
|
||||
/// Note: this function is incomplete.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn verify_transfer(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn verify_transfer<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
transfer: &Transfer,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -20,8 +20,8 @@ pub fn verify_transfer(
|
||||
}
|
||||
|
||||
/// Like `verify_transfer` but doesn't run checks which may become true in future states.
|
||||
pub fn verify_transfer_time_independent_only(
|
||||
state: &BeaconState,
|
||||
pub fn verify_transfer_time_independent_only<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
transfer: &Transfer,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -29,8 +29,8 @@ pub fn verify_transfer_time_independent_only(
|
||||
}
|
||||
|
||||
/// Parametric version of `verify_transfer` that allows some checks to be skipped.
|
||||
fn verify_transfer_parametric(
|
||||
state: &BeaconState,
|
||||
fn verify_transfer_parametric<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
transfer: &Transfer,
|
||||
spec: &ChainSpec,
|
||||
time_independent_only: bool,
|
||||
@@ -122,9 +122,9 @@ fn verify_transfer_parametric(
|
||||
///
|
||||
/// Does not check that the transfer is valid, however checks for overflow in all actions.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn execute_transfer(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn execute_transfer<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
transfer: &Transfer,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
|
||||
@@ -3,8 +3,8 @@ use errors::EpochProcessingError as Error;
|
||||
use process_ejections::process_ejections;
|
||||
use process_exit_queue::process_exit_queue;
|
||||
use process_slashings::process_slashings;
|
||||
use ssz::TreeHash;
|
||||
use std::collections::HashMap;
|
||||
use tree_hash::TreeHash;
|
||||
use types::*;
|
||||
use update_registry_and_shuffling_data::update_registry_and_shuffling_data;
|
||||
use validator_statuses::{TotalBalances, ValidatorStatuses};
|
||||
@@ -32,8 +32,11 @@ pub type WinningRootHashSet = HashMap<u64, WinningRoot>;
|
||||
/// Mutates the given `BeaconState`, returning early if an error is encountered. If an error is
|
||||
/// returned, a state might be "half-processed" and therefore in an invalid state.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
|
||||
/// Spec v0.5.1
|
||||
pub fn per_epoch_processing<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// Ensure the previous and next epoch caches are built.
|
||||
state.build_epoch_cache(RelativeEpoch::Previous, spec)?;
|
||||
state.build_epoch_cache(RelativeEpoch::Current, spec)?;
|
||||
@@ -86,8 +89,8 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
|
||||
|
||||
/// Maybe resets the eth1 period.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn maybe_reset_eth1_period(state: &mut BeaconState, spec: &ChainSpec) {
|
||||
/// Spec v0.5.1
|
||||
pub fn maybe_reset_eth1_period<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainSpec) {
|
||||
let next_epoch = state.next_epoch(spec);
|
||||
let voting_period = spec.epochs_per_eth1_voting_period;
|
||||
|
||||
@@ -108,9 +111,9 @@ pub fn maybe_reset_eth1_period(state: &mut BeaconState, spec: &ChainSpec) {
|
||||
/// - `justified_epoch`
|
||||
/// - `previous_justified_epoch`
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn update_justification_and_finalization(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn update_justification_and_finalization<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
total_balances: &TotalBalances,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -160,13 +163,13 @@ pub fn update_justification_and_finalization(
|
||||
if new_justified_epoch != state.current_justified_epoch {
|
||||
state.current_justified_epoch = new_justified_epoch;
|
||||
state.current_justified_root =
|
||||
*state.get_block_root(new_justified_epoch.start_slot(spec.slots_per_epoch), spec)?;
|
||||
*state.get_block_root(new_justified_epoch.start_slot(spec.slots_per_epoch))?;
|
||||
}
|
||||
|
||||
if new_finalized_epoch != state.finalized_epoch {
|
||||
state.finalized_epoch = new_finalized_epoch;
|
||||
state.finalized_root =
|
||||
*state.get_block_root(new_finalized_epoch.start_slot(spec.slots_per_epoch), spec)?;
|
||||
*state.get_block_root(new_finalized_epoch.start_slot(spec.slots_per_epoch))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -178,9 +181,9 @@ pub fn update_justification_and_finalization(
|
||||
///
|
||||
/// Also returns a `WinningRootHashSet` for later use during epoch processing.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_crosslinks(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_crosslinks<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<WinningRootHashSet, Error> {
|
||||
let mut winning_root_for_shards: WinningRootHashSet = HashMap::new();
|
||||
@@ -221,8 +224,11 @@ pub fn process_crosslinks(
|
||||
|
||||
/// Finish up an epoch update.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn finish_epoch_update(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
|
||||
/// Spec v0.5.1
|
||||
pub fn finish_epoch_update<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
let next_epoch = state.next_epoch(spec);
|
||||
|
||||
@@ -236,16 +242,12 @@ pub fn finish_epoch_update(state: &mut BeaconState, spec: &ChainSpec) -> Result<
|
||||
let active_index_root = Hash256::from_slice(
|
||||
&state
|
||||
.get_active_validator_indices(next_epoch + spec.activation_exit_delay)
|
||||
.hash_tree_root()[..],
|
||||
.tree_hash_root()[..],
|
||||
);
|
||||
state.set_active_index_root(next_epoch, active_index_root, spec)?;
|
||||
|
||||
// Set total slashed balances
|
||||
state.set_slashed_balance(
|
||||
next_epoch,
|
||||
state.get_slashed_balance(current_epoch, spec)?,
|
||||
spec,
|
||||
)?;
|
||||
state.set_slashed_balance(next_epoch, state.get_slashed_balance(current_epoch)?)?;
|
||||
|
||||
// Set randao mix
|
||||
state.set_randao_mix(
|
||||
@@ -257,11 +259,11 @@ pub fn finish_epoch_update(state: &mut BeaconState, spec: &ChainSpec) -> Result<
|
||||
state.slot -= 1;
|
||||
}
|
||||
|
||||
if next_epoch.as_u64() % (spec.slots_per_historical_root as u64 / spec.slots_per_epoch) == 0 {
|
||||
let historical_batch: HistoricalBatch = state.historical_batch();
|
||||
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / spec.slots_per_epoch) == 0 {
|
||||
let historical_batch = state.historical_batch();
|
||||
state
|
||||
.historical_roots
|
||||
.push(Hash256::from_slice(&historical_batch.hash_tree_root()[..]));
|
||||
.push(Hash256::from_slice(&historical_batch.tree_hash_root()[..]));
|
||||
}
|
||||
|
||||
state.previous_epoch_attestations = state.current_epoch_attestations.clone();
|
||||
|
||||
@@ -32,9 +32,9 @@ impl std::ops::AddAssign for Delta {
|
||||
|
||||
/// Apply attester and proposer rewards.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn apply_rewards(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn apply_rewards<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_statuses: &mut ValidatorStatuses,
|
||||
winning_root_for_shards: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
@@ -79,10 +79,10 @@ pub fn apply_rewards(
|
||||
/// Applies the attestation inclusion reward to each proposer for every validator who included an
|
||||
/// attestation in the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_proposer_deltas(
|
||||
/// Spec v0.5.1
|
||||
fn get_proposer_deltas<T: EthSpec>(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &mut BeaconState,
|
||||
state: &mut BeaconState<T>,
|
||||
validator_statuses: &mut ValidatorStatuses,
|
||||
winning_root_for_shards: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
@@ -120,10 +120,10 @@ fn get_proposer_deltas(
|
||||
|
||||
/// Apply rewards for participation in attestations during the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_justification_and_finalization_deltas(
|
||||
/// Spec v0.5.1
|
||||
fn get_justification_and_finalization_deltas<T: EthSpec>(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -163,7 +163,7 @@ fn get_justification_and_finalization_deltas(
|
||||
|
||||
/// Determine the delta for a single validator, if the chain is finalizing normally.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn compute_normal_justification_and_finalization_delta(
|
||||
validator: &ValidatorStatus,
|
||||
total_balances: &TotalBalances,
|
||||
@@ -215,7 +215,7 @@ fn compute_normal_justification_and_finalization_delta(
|
||||
|
||||
/// Determine the delta for a single delta, assuming the chain is _not_ finalizing normally.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn compute_inactivity_leak_delta(
|
||||
validator: &ValidatorStatus,
|
||||
base_reward: u64,
|
||||
@@ -261,10 +261,10 @@ fn compute_inactivity_leak_delta(
|
||||
|
||||
/// Calculate the deltas based upon the winning roots for attestations during the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_crosslink_deltas(
|
||||
/// Spec v0.5.1
|
||||
fn get_crosslink_deltas<T: EthSpec>(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -295,9 +295,9 @@ fn get_crosslink_deltas(
|
||||
|
||||
/// Returns the base reward for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_base_reward(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn get_base_reward<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
index: usize,
|
||||
previous_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
@@ -312,9 +312,9 @@ fn get_base_reward(
|
||||
|
||||
/// Returns the inactivity penalty for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_inactivity_penalty(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn get_inactivity_penalty<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
index: usize,
|
||||
epochs_since_finality: u64,
|
||||
previous_total_balance: u64,
|
||||
@@ -328,7 +328,7 @@ fn get_inactivity_penalty(
|
||||
|
||||
/// Returns the epochs since the last finalized epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn epochs_since_finality(state: &BeaconState, spec: &ChainSpec) -> Epoch {
|
||||
/// Spec v0.5.1
|
||||
fn epochs_since_finality<T: EthSpec>(state: &BeaconState<T>, spec: &ChainSpec) -> Epoch {
|
||||
state.current_epoch(spec) + 1 - state.finalized_epoch
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ use types::*;
|
||||
|
||||
/// Returns validator indices which participated in the attestation.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_attestation_participants(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn get_attestation_participants<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestation_data: &AttestationData,
|
||||
bitfield: &Bitfield,
|
||||
spec: &ChainSpec,
|
||||
|
||||
@@ -5,9 +5,9 @@ use types::*;
|
||||
/// Returns the distance between the first included attestation for some validator and this
|
||||
/// slot.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn inclusion_distance(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn inclusion_distance<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestations: &[&PendingAttestation],
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
@@ -18,9 +18,9 @@ pub fn inclusion_distance(
|
||||
|
||||
/// Returns the slot of the earliest included attestation for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn inclusion_slot(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn inclusion_slot<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestations: &[&PendingAttestation],
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
@@ -31,9 +31,9 @@ pub fn inclusion_slot(
|
||||
|
||||
/// Finds the earliest included attestation for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn earliest_included_attestation(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn earliest_included_attestation<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
attestations: &[&PendingAttestation],
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
|
||||
@@ -4,8 +4,11 @@ use types::{BeaconStateError as Error, *};
|
||||
/// Iterate through the validator registry and eject active validators with balance below
|
||||
/// ``EJECTION_BALANCE``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_ejections(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
|
||||
/// Spec v0.5.1
|
||||
pub fn process_ejections<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// There is an awkward double (triple?) loop here because we can't loop across the borrowed
|
||||
// active validator indices and mutate state in the one loop.
|
||||
let exitable: Vec<usize> = state
|
||||
|
||||
@@ -2,8 +2,8 @@ use types::*;
|
||||
|
||||
/// Process the exit queue.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_exit_queue(state: &mut BeaconState, spec: &ChainSpec) {
|
||||
/// Spec v0.5.1
|
||||
pub fn process_exit_queue<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainSpec) {
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
|
||||
let eligible = |index: usize| {
|
||||
@@ -31,9 +31,9 @@ pub fn process_exit_queue(state: &mut BeaconState, spec: &ChainSpec) {
|
||||
|
||||
/// Initiate an exit for the validator of the given `index`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn prepare_validator_for_withdrawal(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn prepare_validator_for_withdrawal<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
|
||||
@@ -2,21 +2,21 @@ use types::{BeaconStateError as Error, *};
|
||||
|
||||
/// Process slashings.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_slashings(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn process_slashings<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
current_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
|
||||
let total_at_start = state.get_slashed_balance(current_epoch + 1, spec)?;
|
||||
let total_at_end = state.get_slashed_balance(current_epoch, spec)?;
|
||||
let total_at_start = state.get_slashed_balance(current_epoch + 1)?;
|
||||
let total_at_end = state.get_slashed_balance(current_epoch)?;
|
||||
let total_penalities = total_at_end - total_at_start;
|
||||
|
||||
for (index, validator) in state.validator_registry.iter().enumerate() {
|
||||
let should_penalize = current_epoch.as_usize()
|
||||
== validator.withdrawable_epoch.as_usize() - spec.latest_slashed_exit_length / 2;
|
||||
== validator.withdrawable_epoch.as_usize() - T::LatestSlashedExitLength::to_usize() / 2;
|
||||
|
||||
if validator.slashed && should_penalize {
|
||||
let effective_balance = state.get_effective_balance(index, spec)?;
|
||||
|
||||
@@ -8,9 +8,10 @@ use types::*;
|
||||
fn runs_without_error() {
|
||||
Builder::from_env(Env::default().default_filter_or("error")).init();
|
||||
|
||||
let spec = ChainSpec::few_validators();
|
||||
let spec = FewValidatorsEthSpec::spec();
|
||||
|
||||
let mut builder = TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec);
|
||||
let mut builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
|
||||
TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec);
|
||||
|
||||
let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch);
|
||||
builder.teleport_to_slot(target_slot, &spec);
|
||||
|
||||
+12
-12
@@ -4,9 +4,9 @@ use types::*;
|
||||
|
||||
/// Peforms a validator registry update, if required.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn update_registry_and_shuffling_data(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn update_registry_and_shuffling_data<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
current_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -49,9 +49,9 @@ pub fn update_registry_and_shuffling_data(
|
||||
|
||||
/// Returns `true` if the validator registry should be updated during an epoch processing.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn should_update_validator_registry(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn should_update_validator_registry<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
if state.finalized_epoch <= state.validator_registry_update_epoch {
|
||||
@@ -78,9 +78,9 @@ pub fn should_update_validator_registry(
|
||||
///
|
||||
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn update_validator_registry(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn update_validator_registry<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
current_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -133,9 +133,9 @@ pub fn update_validator_registry(
|
||||
|
||||
/// Activate the validator of the given ``index``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn activate_validator(
|
||||
state: &mut BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn activate_validator<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
is_genesis: bool,
|
||||
spec: &ChainSpec,
|
||||
|
||||
@@ -160,8 +160,11 @@ impl ValidatorStatuses {
|
||||
/// - Active validators
|
||||
/// - Total balances for the current and previous epochs.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Result<Self, BeaconStateError> {
|
||||
/// Spec v0.5.1
|
||||
pub fn new<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Self, BeaconStateError> {
|
||||
let mut statuses = Vec::with_capacity(state.validator_registry.len());
|
||||
let mut total_balances = TotalBalances::default();
|
||||
|
||||
@@ -195,10 +198,10 @@ impl ValidatorStatuses {
|
||||
/// Process some attestations from the given `state` updating the `statuses` and
|
||||
/// `total_balances` fields.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_attestations(
|
||||
/// Spec v0.5.1
|
||||
pub fn process_attestations<T: EthSpec>(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
for a in state
|
||||
@@ -243,7 +246,7 @@ impl ValidatorStatuses {
|
||||
status.is_previous_epoch_boundary_attester = true;
|
||||
}
|
||||
|
||||
if has_common_beacon_block_root(a, state, spec)? {
|
||||
if has_common_beacon_block_root(a, state)? {
|
||||
self.total_balances.previous_epoch_head_attesters += attesting_balance;
|
||||
status.is_previous_epoch_head_attester = true;
|
||||
}
|
||||
@@ -261,10 +264,10 @@ impl ValidatorStatuses {
|
||||
/// Update the `statuses` for each validator based upon whether or not they attested to the
|
||||
/// "winning" shard block root for the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn process_winning_roots(
|
||||
/// Spec v0.5.1
|
||||
pub fn process_winning_roots<T: EthSpec>(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
winning_roots: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
@@ -297,14 +300,14 @@ impl ValidatorStatuses {
|
||||
/// Returns the distance between when the attestation was created and when it was included in a
|
||||
/// block.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn inclusion_distance(a: &PendingAttestation) -> Slot {
|
||||
a.inclusion_slot - a.data.slot
|
||||
}
|
||||
|
||||
/// Returns `true` if some `PendingAttestation` is from the supplied `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool {
|
||||
a.data.slot.epoch(spec.slots_per_epoch) == epoch
|
||||
}
|
||||
@@ -312,15 +315,15 @@ fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool
|
||||
/// Returns `true` if a `PendingAttestation` and `BeaconState` share the same beacon block hash for
|
||||
/// the first slot of the given epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn has_common_epoch_boundary_root(
|
||||
/// Spec v0.5.1
|
||||
fn has_common_epoch_boundary_root<T: EthSpec>(
|
||||
a: &PendingAttestation,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
epoch: Epoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
let slot = epoch.start_slot(spec.slots_per_epoch);
|
||||
let state_boundary_root = *state.get_block_root(slot, spec)?;
|
||||
let state_boundary_root = *state.get_block_root(slot)?;
|
||||
|
||||
Ok(a.data.target_root == state_boundary_root)
|
||||
}
|
||||
@@ -328,13 +331,12 @@ fn has_common_epoch_boundary_root(
|
||||
/// Returns `true` if a `PendingAttestation` and `BeaconState` share the same beacon block hash for
|
||||
/// the current slot of the `PendingAttestation`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn has_common_beacon_block_root(
|
||||
/// Spec v0.5.1
|
||||
fn has_common_beacon_block_root<T: EthSpec>(
|
||||
a: &PendingAttestation,
|
||||
state: &BeaconState,
|
||||
spec: &ChainSpec,
|
||||
state: &BeaconState<T>,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
let state_block_root = *state.get_block_root(a.data.slot, spec)?;
|
||||
let state_block_root = *state.get_block_root(a.data.slot)?;
|
||||
|
||||
Ok(a.data.beacon_block_root == state_block_root)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ impl WinningRoot {
|
||||
/// A winning root is "better" than another if it has a higher `total_attesting_balance`. Ties
|
||||
/// are broken by favouring the higher `crosslink_data_root` value.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn is_better_than(&self, other: &Self) -> bool {
|
||||
if self.total_attesting_balance > other.total_attesting_balance {
|
||||
true
|
||||
@@ -34,9 +34,9 @@ impl WinningRoot {
|
||||
/// The `WinningRoot` object also contains additional fields that are useful in later stages of
|
||||
/// per-epoch processing.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn winning_root(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
pub fn winning_root<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
shard: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Option<WinningRoot>, BeaconStateError> {
|
||||
@@ -89,8 +89,12 @@ pub fn winning_root(
|
||||
|
||||
/// Returns `true` if pending attestation `a` is eligible to become a winning root.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn is_eligible_for_winning_root(state: &BeaconState, a: &PendingAttestation, shard: Shard) -> bool {
|
||||
/// Spec v0.5.1
|
||||
fn is_eligible_for_winning_root<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
a: &PendingAttestation,
|
||||
shard: Shard,
|
||||
) -> bool {
|
||||
if shard >= state.latest_crosslinks.len() as u64 {
|
||||
return false;
|
||||
}
|
||||
@@ -100,9 +104,9 @@ fn is_eligible_for_winning_root(state: &BeaconState, a: &PendingAttestation, sha
|
||||
|
||||
/// Returns all indices which voted for a given crosslink. Does not contain duplicates.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_attesting_validator_indices(
|
||||
state: &BeaconState,
|
||||
/// Spec v0.5.1
|
||||
fn get_attesting_validator_indices<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
shard: u64,
|
||||
crosslink_data_root: &Hash256,
|
||||
spec: &ChainSpec,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::*;
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::SignedRoot;
|
||||
use types::*;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -10,13 +10,12 @@ pub enum Error {
|
||||
|
||||
/// Advances a state forward by one slot, performing per-epoch processing if required.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn per_slot_processing(
|
||||
state: &mut BeaconState,
|
||||
latest_block_header: &BeaconBlockHeader,
|
||||
/// Spec v0.5.1
|
||||
pub fn per_slot_processing<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
cache_state(state, latest_block_header, spec)?;
|
||||
cache_state(state, spec)?;
|
||||
|
||||
if (state.slot + 1) % spec.slots_per_epoch == 0 {
|
||||
per_epoch_processing(state, spec)?;
|
||||
@@ -27,12 +26,8 @@ pub fn per_slot_processing(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cache_state(
|
||||
state: &mut BeaconState,
|
||||
latest_block_header: &BeaconBlockHeader,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let previous_slot_state_root = Hash256::from_slice(&state.hash_tree_root()[..]);
|
||||
fn cache_state<T: EthSpec>(state: &mut BeaconState<T>, spec: &ChainSpec) -> Result<(), Error> {
|
||||
let previous_slot_state_root = state.update_tree_hash_cache()?;
|
||||
|
||||
// Note: increment the state slot here to allow use of our `state_root` and `block_root`
|
||||
// getter/setter functions.
|
||||
@@ -46,8 +41,11 @@ fn cache_state(
|
||||
state.latest_block_header.state_root = previous_slot_state_root
|
||||
}
|
||||
|
||||
let latest_block_root = Hash256::from_slice(&latest_block_header.hash_tree_root()[..]);
|
||||
state.set_block_root(previous_slot, latest_block_root, spec)?;
|
||||
// Store the previous slot's post state transition root.
|
||||
state.set_state_root(previous_slot, previous_slot_state_root)?;
|
||||
|
||||
let latest_block_root = Hash256::from_slice(&state.latest_block_header.signed_root()[..]);
|
||||
state.set_block_root(previous_slot, latest_block_root)?;
|
||||
|
||||
// Set the state slot back to what it should be.
|
||||
state.slot -= 1;
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
#![cfg(not(debug_assertions))]
|
||||
|
||||
use serde_derive::Deserialize;
|
||||
use serde_yaml;
|
||||
use state_processing::{
|
||||
per_block_processing, per_block_processing_without_verifying_block_signature,
|
||||
per_slot_processing,
|
||||
};
|
||||
use std::{fs::File, io::prelude::*, path::PathBuf};
|
||||
use types::*;
|
||||
#[allow(unused_imports)]
|
||||
use yaml_utils;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TestCase {
|
||||
pub name: String,
|
||||
pub config: ChainSpec,
|
||||
pub verify_signatures: bool,
|
||||
pub initial_state: BeaconState,
|
||||
pub blocks: Vec<BeaconBlock>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TestDoc {
|
||||
pub title: String,
|
||||
pub summary: String,
|
||||
pub fork: String,
|
||||
pub test_cases: Vec<TestCase>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_yaml() {
|
||||
// Test sanity-check_small-config_32-vals.yaml
|
||||
let mut file = {
|
||||
let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
file_path_buf.push("yaml_utils/specs/sanity-check_small-config_32-vals.yaml");
|
||||
|
||||
File::open(file_path_buf).unwrap()
|
||||
};
|
||||
|
||||
let mut yaml_str = String::new();
|
||||
|
||||
file.read_to_string(&mut yaml_str).unwrap();
|
||||
|
||||
yaml_str = yaml_str.to_lowercase();
|
||||
|
||||
let _doc: TestDoc = serde_yaml::from_str(&yaml_str.as_str()).unwrap();
|
||||
|
||||
// Test sanity-check_default-config_100-vals.yaml
|
||||
file = {
|
||||
let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
file_path_buf.push("yaml_utils/specs/sanity-check_default-config_100-vals.yaml");
|
||||
|
||||
File::open(file_path_buf).unwrap()
|
||||
};
|
||||
|
||||
yaml_str = String::new();
|
||||
|
||||
file.read_to_string(&mut yaml_str).unwrap();
|
||||
|
||||
yaml_str = yaml_str.to_lowercase();
|
||||
|
||||
let _doc: TestDoc = serde_yaml::from_str(&yaml_str.as_str()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_state_transition_tests_small() {
|
||||
// Test sanity-check_small-config_32-vals.yaml
|
||||
let mut file = {
|
||||
let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
file_path_buf.push("yaml_utils/specs/sanity-check_small-config_32-vals.yaml");
|
||||
|
||||
File::open(file_path_buf).unwrap()
|
||||
};
|
||||
let mut yaml_str = String::new();
|
||||
file.read_to_string(&mut yaml_str).unwrap();
|
||||
yaml_str = yaml_str.to_lowercase();
|
||||
|
||||
let doc: TestDoc = serde_yaml::from_str(&yaml_str.as_str()).unwrap();
|
||||
|
||||
// Run Tests
|
||||
for (i, test_case) in doc.test_cases.iter().enumerate() {
|
||||
let mut state = test_case.initial_state.clone();
|
||||
for block in test_case.blocks.iter() {
|
||||
while block.slot > state.slot {
|
||||
let latest_block_header = state.latest_block_header.clone();
|
||||
per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap();
|
||||
}
|
||||
if test_case.verify_signatures {
|
||||
let res = per_block_processing(&mut state, &block, &test_case.config);
|
||||
if res.is_err() {
|
||||
println!("{:?}", i);
|
||||
println!("{:?}", res);
|
||||
};
|
||||
} else {
|
||||
let res = per_block_processing_without_verifying_block_signature(
|
||||
&mut state,
|
||||
&block,
|
||||
&test_case.config,
|
||||
);
|
||||
if res.is_err() {
|
||||
println!("{:?}", i);
|
||||
println!("{:?}", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
[package]
|
||||
name = "yaml-utils"
|
||||
version = "0.1.0"
|
||||
authors = ["Kirk Baird <baird.k@outlook.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[build-dependencies]
|
||||
reqwest = "0.9"
|
||||
tempdir = "0.3"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[lib]
|
||||
name = "yaml_utils"
|
||||
path = "src/lib.rs"
|
||||
@@ -1,28 +0,0 @@
|
||||
extern crate reqwest;
|
||||
extern crate tempdir;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::copy;
|
||||
|
||||
fn main() {
|
||||
// These test files are not to be stored in the lighthouse repo as they are quite large (32MB).
|
||||
// They will be downloaded at build time by yaml-utils crate (in build.rs)
|
||||
let git_path = "https://raw.githubusercontent.com/ethereum/eth2.0-tests/master/state/";
|
||||
let test_names = vec![
|
||||
"sanity-check_default-config_100-vals.yaml",
|
||||
"sanity-check_small-config_32-vals.yaml",
|
||||
];
|
||||
|
||||
for test in test_names {
|
||||
let mut target = String::from(git_path);
|
||||
target.push_str(test);
|
||||
let mut response = reqwest::get(target.as_str()).unwrap();
|
||||
|
||||
let mut dest = {
|
||||
let mut file_name = String::from("specs/");
|
||||
file_name.push_str(test);
|
||||
File::create(file_name).unwrap()
|
||||
};
|
||||
copy(&mut response, &mut dest).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
*.yaml
|
||||
@@ -1 +0,0 @@
|
||||
// This is a place holder such that yaml-utils is now a crate hence build.rs will be run when 'cargo test' is called
|
||||
@@ -7,9 +7,11 @@ edition = "2018"
|
||||
[dependencies]
|
||||
bls = { path = "../utils/bls" }
|
||||
boolean-bitfield = { path = "../utils/boolean-bitfield" }
|
||||
cached_tree_hash = { path = "../utils/cached_tree_hash" }
|
||||
dirs = "1.0"
|
||||
derivative = "1.0"
|
||||
ethereum-types = "0.5"
|
||||
fixed_len_vec = { path = "../utils/fixed_len_vec" }
|
||||
hashing = { path = "../utils/hashing" }
|
||||
hex = "0.3"
|
||||
honey-badger-split = { path = "../utils/honey-badger-split" }
|
||||
@@ -26,6 +28,8 @@ ssz = { path = "../utils/ssz" }
|
||||
ssz_derive = { path = "../utils/ssz_derive" }
|
||||
swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" }
|
||||
test_random_derive = { path = "../utils/test_random_derive" }
|
||||
tree_hash = { path = "../utils/tree_hash" }
|
||||
tree_hash_derive = { path = "../utils/tree_hash_derive" }
|
||||
libp2p = { git = "https://github.com/SigP/rust-libp2p", rev = "b3c32d9a821ae6cc89079499cc6e8a6bab0bffc3" }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
use super::{AggregateSignature, AttestationData, Bitfield};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::TreeHash;
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// Details an attestation that can be slashable.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -18,6 +19,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
SignedRoot,
|
||||
)]
|
||||
@@ -57,4 +59,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Attestation);
|
||||
cached_tree_hash_tests!(Attestation);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::{Crosslink, Epoch, Hash256, Slot};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::TreeHash;
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// The data upon which an attestation is based.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -20,6 +21,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
SignedRoot,
|
||||
)]
|
||||
@@ -46,4 +48,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(AttestationData);
|
||||
cached_tree_hash_tests!(AttestationData);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
use super::AttestationData;
|
||||
use crate::test_utils::TestRandom;
|
||||
|
||||
use rand::RngCore;
|
||||
use serde_derive::Serialize;
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Used for pairing an attestation with a proof-of-custody.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash, CachedTreeHash)]
|
||||
pub struct AttestationDataAndCustodyBit {
|
||||
pub data: AttestationData,
|
||||
pub custody_bit: bool,
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for AttestationDataAndCustodyBit {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
Self {
|
||||
data: <_>::random_for_test(rng),
|
||||
custody_bit: <_>::random_for_test(rng),
|
||||
@@ -27,4 +29,5 @@ mod test {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(AttestationDataAndCustodyBit);
|
||||
cached_tree_hash_tests!(AttestationDataAndCustodyBit);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,25 @@
|
||||
use crate::{test_utils::TestRandom, SlashableAttestation};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Two conflicting attestations.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct AttesterSlashing {
|
||||
pub slashable_attestation_1: SlashableAttestation,
|
||||
pub slashable_attestation_2: SlashableAttestation,
|
||||
@@ -18,4 +30,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(AttesterSlashing);
|
||||
cached_tree_hash_tests!(AttesterSlashing);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::TreeHash;
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// A block of the `BeaconChain`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
@@ -19,6 +20,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
SignedRoot,
|
||||
)]
|
||||
@@ -34,7 +36,7 @@ pub struct BeaconBlock {
|
||||
impl BeaconBlock {
|
||||
/// Returns an empty block to be used during genesis.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn empty(spec: &ChainSpec) -> BeaconBlock {
|
||||
BeaconBlock {
|
||||
slot: spec.genesis_slot,
|
||||
@@ -57,11 +59,11 @@ impl BeaconBlock {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `hash_tree_root` of the block.
|
||||
/// Returns the `tree_hash_root | update` of the block.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn canonical_root(&self) -> Hash256 {
|
||||
Hash256::from_slice(&self.hash_tree_root()[..])
|
||||
Hash256::from_slice(&self.tree_hash_root()[..])
|
||||
}
|
||||
|
||||
/// Returns a full `BeaconBlockHeader` of this block.
|
||||
@@ -71,20 +73,20 @@ impl BeaconBlock {
|
||||
///
|
||||
/// Note: performs a full tree-hash of `self.body`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn block_header(&self) -> BeaconBlockHeader {
|
||||
BeaconBlockHeader {
|
||||
slot: self.slot,
|
||||
previous_block_root: self.previous_block_root,
|
||||
state_root: self.state_root,
|
||||
block_body_root: Hash256::from_slice(&self.body.hash_tree_root()[..]),
|
||||
block_body_root: Hash256::from_slice(&self.body.tree_hash_root()[..]),
|
||||
signature: self.signature.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a "temporary" header, where the `state_root` is `spec.zero_hash`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn temporary_block_header(&self, spec: &ChainSpec) -> BeaconBlockHeader {
|
||||
BeaconBlockHeader {
|
||||
state_root: spec.zero_hash,
|
||||
@@ -99,4 +101,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(BeaconBlock);
|
||||
cached_tree_hash_tests!(BeaconBlock);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// The body of a `BeaconChain` block, containing operations.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct BeaconBlockBody {
|
||||
pub randao_reveal: Signature,
|
||||
pub eth1_data: Eth1Data,
|
||||
@@ -25,4 +37,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(BeaconBlockBody);
|
||||
cached_tree_hash_tests!(BeaconBlockBody);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::TreeHash;
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::{SignedRoot, TreeHash};
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// A header of a `BeaconBlock`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
@@ -19,6 +20,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
SignedRoot,
|
||||
)]
|
||||
@@ -32,16 +34,16 @@ pub struct BeaconBlockHeader {
|
||||
}
|
||||
|
||||
impl BeaconBlockHeader {
|
||||
/// Returns the `hash_tree_root` of the header.
|
||||
/// Returns the `tree_hash_root` of the header.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn canonical_root(&self) -> Hash256 {
|
||||
Hash256::from_slice(&self.hash_tree_root()[..])
|
||||
Hash256::from_slice(&self.signed_root()[..])
|
||||
}
|
||||
|
||||
/// Given a `body`, consumes `self` and returns a complete `BeaconBlock`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn into_block(self, body: BeaconBlockBody) -> BeaconBlock {
|
||||
BeaconBlock {
|
||||
slot: self.slot,
|
||||
@@ -58,4 +60,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(BeaconBlockHeader);
|
||||
cached_tree_hash_tests!(BeaconBlockHeader);
|
||||
}
|
||||
|
||||
+173
-121
@@ -1,14 +1,22 @@
|
||||
use self::epoch_cache::{get_active_validator_indices, EpochCache, Error as EpochCacheError};
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use cached_tree_hash::{Error as TreeHashCacheError, TreeHashCache};
|
||||
use hashing::hash;
|
||||
use int_to_bytes::int_to_bytes32;
|
||||
use pubkey_cache::PubkeyCache;
|
||||
use rand::RngCore;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::{hash, ssz_encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use test_random_derive::TestRandom;
|
||||
|
||||
use fixed_len_vec::{typenum::Unsigned, FixedLenVec};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::ssz_encode;
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
pub use beacon_state_types::*;
|
||||
|
||||
mod beacon_state_types;
|
||||
mod epoch_cache;
|
||||
mod pubkey_cache;
|
||||
mod tests;
|
||||
@@ -40,13 +48,28 @@ pub enum Error {
|
||||
EpochCacheUninitialized(RelativeEpoch),
|
||||
RelativeEpochError(RelativeEpochError),
|
||||
EpochCacheError(EpochCacheError),
|
||||
TreeHashCacheError(TreeHashCacheError),
|
||||
}
|
||||
|
||||
/// The state of the `BeaconChain` at some slot.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, TestRandom, Encode, Decode, TreeHash)]
|
||||
pub struct BeaconState {
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
TestRandom,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
)]
|
||||
pub struct BeaconState<T>
|
||||
where
|
||||
T: EthSpec,
|
||||
{
|
||||
// Misc
|
||||
pub slot: Slot,
|
||||
pub genesis_time: u64,
|
||||
@@ -58,7 +81,7 @@ pub struct BeaconState {
|
||||
pub validator_registry_update_epoch: Epoch,
|
||||
|
||||
// Randomness and committees
|
||||
pub latest_randao_mixes: Vec<Hash256>,
|
||||
pub latest_randao_mixes: FixedLenVec<Hash256, T::LatestRandaoMixesLength>,
|
||||
pub previous_shuffling_start_shard: u64,
|
||||
pub current_shuffling_start_shard: u64,
|
||||
pub previous_shuffling_epoch: Epoch,
|
||||
@@ -78,11 +101,11 @@ pub struct BeaconState {
|
||||
pub finalized_root: Hash256,
|
||||
|
||||
// Recent state
|
||||
pub latest_crosslinks: Vec<Crosslink>,
|
||||
latest_block_roots: Vec<Hash256>,
|
||||
latest_state_roots: Vec<Hash256>,
|
||||
latest_active_index_roots: Vec<Hash256>,
|
||||
latest_slashed_balances: Vec<u64>,
|
||||
pub latest_crosslinks: FixedLenVec<Crosslink, T::ShardCount>,
|
||||
pub latest_block_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
latest_state_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
latest_active_index_roots: FixedLenVec<Hash256, T::LatestActiveIndexRootsLength>,
|
||||
latest_slashed_balances: FixedLenVec<u64, T::LatestSlashedExitLength>,
|
||||
pub latest_block_header: BeaconBlockHeader,
|
||||
pub historical_roots: Vec<Hash256>,
|
||||
|
||||
@@ -110,16 +133,26 @@ pub struct BeaconState {
|
||||
#[tree_hash(skip_hashing)]
|
||||
#[test_random(default)]
|
||||
pub pubkey_cache: PubkeyCache,
|
||||
#[serde(skip_serializing, skip_deserializing)]
|
||||
#[ssz(skip_serializing)]
|
||||
#[ssz(skip_deserializing)]
|
||||
#[tree_hash(skip_hashing)]
|
||||
#[test_random(default)]
|
||||
pub tree_hash_cache: TreeHashCache,
|
||||
}
|
||||
|
||||
impl BeaconState {
|
||||
impl<T: EthSpec> BeaconState<T> {
|
||||
/// Produce the first state of the Beacon Chain.
|
||||
///
|
||||
/// This does not fully build a genesis beacon state, it omits processing of initial validator
|
||||
/// deposits. To obtain a full genesis beacon state, use the `BeaconStateBuilder`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn genesis(genesis_time: u64, latest_eth1_data: Eth1Data, spec: &ChainSpec) -> BeaconState {
|
||||
/// Spec v0.5.1
|
||||
pub fn genesis(
|
||||
genesis_time: u64,
|
||||
latest_eth1_data: Eth1Data,
|
||||
spec: &ChainSpec,
|
||||
) -> BeaconState<T> {
|
||||
let initial_crosslink = Crosslink {
|
||||
epoch: spec.genesis_epoch,
|
||||
crosslink_data_root: spec.zero_hash,
|
||||
@@ -137,7 +170,10 @@ impl BeaconState {
|
||||
validator_registry_update_epoch: spec.genesis_epoch,
|
||||
|
||||
// Randomness and committees
|
||||
latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize],
|
||||
latest_randao_mixes: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::LatestRandaoMixesLength::to_usize()
|
||||
]),
|
||||
previous_shuffling_start_shard: spec.genesis_start_shard,
|
||||
current_shuffling_start_shard: spec.genesis_start_shard,
|
||||
previous_shuffling_epoch: spec.genesis_epoch,
|
||||
@@ -157,11 +193,22 @@ impl BeaconState {
|
||||
finalized_root: spec.zero_hash,
|
||||
|
||||
// Recent state
|
||||
latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize],
|
||||
latest_block_roots: vec![spec.zero_hash; spec.slots_per_historical_root],
|
||||
latest_state_roots: vec![spec.zero_hash; spec.slots_per_historical_root],
|
||||
latest_active_index_roots: vec![spec.zero_hash; spec.latest_active_index_roots_length],
|
||||
latest_slashed_balances: vec![0; spec.latest_slashed_exit_length],
|
||||
latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize].into(),
|
||||
latest_block_roots: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::SlotsPerHistoricalRoot::to_usize()
|
||||
]),
|
||||
latest_state_roots: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::SlotsPerHistoricalRoot::to_usize()
|
||||
]),
|
||||
latest_active_index_roots: FixedLenVec::from(
|
||||
vec![spec.zero_hash; T::LatestActiveIndexRootsLength::to_usize()],
|
||||
),
|
||||
latest_slashed_balances: FixedLenVec::from(vec![
|
||||
0;
|
||||
T::LatestSlashedExitLength::to_usize()
|
||||
]),
|
||||
latest_block_header: BeaconBlock::empty(spec).temporary_block_header(spec),
|
||||
historical_roots: vec![],
|
||||
|
||||
@@ -183,17 +230,18 @@ impl BeaconState {
|
||||
EpochCache::default(),
|
||||
],
|
||||
pubkey_cache: PubkeyCache::default(),
|
||||
tree_hash_cache: TreeHashCache::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `hash_tree_root` of the state.
|
||||
/// Returns the `tree_hash_root` of the state.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn canonical_root(&self) -> Hash256 {
|
||||
Hash256::from_slice(&self.hash_tree_root()[..])
|
||||
Hash256::from_slice(&self.tree_hash_root()[..])
|
||||
}
|
||||
|
||||
pub fn historical_batch(&self) -> HistoricalBatch {
|
||||
pub fn historical_batch(&self) -> HistoricalBatch<T> {
|
||||
HistoricalBatch {
|
||||
block_roots: self.latest_block_roots.clone(),
|
||||
state_roots: self.latest_state_roots.clone(),
|
||||
@@ -217,7 +265,7 @@ impl BeaconState {
|
||||
|
||||
/// The epoch corresponding to `self.slot`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn current_epoch(&self, spec: &ChainSpec) -> Epoch {
|
||||
self.slot.epoch(spec.slots_per_epoch)
|
||||
}
|
||||
@@ -226,14 +274,14 @@ impl BeaconState {
|
||||
///
|
||||
/// If the current epoch is the genesis epoch, the genesis_epoch is returned.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn previous_epoch(&self, spec: &ChainSpec) -> Epoch {
|
||||
self.current_epoch(&spec) - 1
|
||||
}
|
||||
|
||||
/// The epoch following `self.current_epoch()`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn next_epoch(&self, spec: &ChainSpec) -> Epoch {
|
||||
self.current_epoch(spec) + 1
|
||||
}
|
||||
@@ -246,7 +294,7 @@ impl BeaconState {
|
||||
///
|
||||
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_cached_active_validator_indices(
|
||||
&self,
|
||||
relative_epoch: RelativeEpoch,
|
||||
@@ -261,7 +309,7 @@ impl BeaconState {
|
||||
///
|
||||
/// Does not utilize the cache, performs a full iteration over the validator registry.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_active_validator_indices(&self, epoch: Epoch) -> Vec<usize> {
|
||||
get_active_validator_indices(&self.validator_registry, epoch)
|
||||
}
|
||||
@@ -270,7 +318,7 @@ impl BeaconState {
|
||||
///
|
||||
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_crosslink_committees_at_slot(
|
||||
&self,
|
||||
slot: Slot,
|
||||
@@ -295,7 +343,7 @@ impl BeaconState {
|
||||
///
|
||||
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_crosslink_committee_for_shard(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
@@ -321,7 +369,7 @@ impl BeaconState {
|
||||
///
|
||||
/// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_beacon_proposer_index(
|
||||
&self,
|
||||
slot: Slot,
|
||||
@@ -350,15 +398,10 @@ impl BeaconState {
|
||||
|
||||
/// Safely obtains the index for latest block roots, given some `slot`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_latest_block_roots_index(&self, slot: Slot, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + spec.slots_per_historical_root as u64) {
|
||||
let i = slot.as_usize() % spec.slots_per_historical_root;
|
||||
if i >= self.latest_block_roots.len() {
|
||||
Err(Error::InsufficientStateRoots)
|
||||
} else {
|
||||
Ok(i)
|
||||
}
|
||||
/// Spec v0.5.1
|
||||
fn get_latest_block_roots_index(&self, slot: Slot) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + self.latest_block_roots.len() as u64) {
|
||||
Ok(slot.as_usize() % self.latest_block_roots.len())
|
||||
} else {
|
||||
Err(BeaconStateError::SlotOutOfBounds)
|
||||
}
|
||||
@@ -366,45 +409,34 @@ impl BeaconState {
|
||||
|
||||
/// Return the block root at a recent `slot`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_block_root(
|
||||
&self,
|
||||
slot: Slot,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<&Hash256, BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot, spec)?;
|
||||
/// Spec v0.5.1
|
||||
pub fn get_block_root(&self, slot: Slot) -> Result<&Hash256, BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot)?;
|
||||
Ok(&self.latest_block_roots[i])
|
||||
}
|
||||
|
||||
/// Sets the block root for some given slot.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn set_block_root(
|
||||
&mut self,
|
||||
slot: Slot,
|
||||
block_root: Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot, spec)?;
|
||||
let i = self.get_latest_block_roots_index(slot)?;
|
||||
self.latest_block_roots[i] = block_root;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Safely obtains the index for `latest_randao_mixes`
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn get_randao_mix_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let current_epoch = self.current_epoch(spec);
|
||||
let len = T::LatestRandaoMixesLength::to_u64();
|
||||
|
||||
if (current_epoch - (spec.latest_randao_mixes_length as u64) < epoch)
|
||||
& (epoch <= current_epoch)
|
||||
{
|
||||
let i = epoch.as_usize() % spec.latest_randao_mixes_length;
|
||||
if i < self.latest_randao_mixes.len() {
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(Error::InsufficientRandaoMixes)
|
||||
}
|
||||
if (current_epoch - len < epoch) & (epoch <= current_epoch) {
|
||||
Ok(epoch.as_usize() % len as usize)
|
||||
} else {
|
||||
Err(Error::EpochOutOfBounds)
|
||||
}
|
||||
@@ -416,14 +448,14 @@ impl BeaconState {
|
||||
///
|
||||
/// See `Self::get_randao_mix`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn update_randao_mix(
|
||||
&mut self,
|
||||
epoch: Epoch,
|
||||
signature: &Signature,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = epoch.as_usize() % spec.latest_randao_mixes_length;
|
||||
let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize();
|
||||
|
||||
let signature_hash = Hash256::from_slice(&hash(&ssz_encode(signature)));
|
||||
|
||||
@@ -434,7 +466,7 @@ impl BeaconState {
|
||||
|
||||
/// Return the randao mix at a recent ``epoch``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_randao_mix(&self, epoch: Epoch, spec: &ChainSpec) -> Result<&Hash256, Error> {
|
||||
let i = self.get_randao_mix_index(epoch, spec)?;
|
||||
Ok(&self.latest_randao_mixes[i])
|
||||
@@ -442,7 +474,7 @@ impl BeaconState {
|
||||
|
||||
/// Set the randao mix at a recent ``epoch``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn set_randao_mix(
|
||||
&mut self,
|
||||
epoch: Epoch,
|
||||
@@ -456,21 +488,16 @@ impl BeaconState {
|
||||
|
||||
/// Safely obtains the index for `latest_active_index_roots`, given some `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
fn get_active_index_root_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let current_epoch = self.current_epoch(spec);
|
||||
|
||||
if (current_epoch - spec.latest_active_index_roots_length as u64
|
||||
if (current_epoch - self.latest_active_index_roots.len() as u64
|
||||
+ spec.activation_exit_delay
|
||||
< epoch)
|
||||
& (epoch <= current_epoch + spec.activation_exit_delay)
|
||||
{
|
||||
let i = epoch.as_usize() % spec.latest_active_index_roots_length;
|
||||
if i < self.latest_active_index_roots.len() {
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(Error::InsufficientIndexRoots)
|
||||
}
|
||||
Ok(epoch.as_usize() % self.latest_active_index_roots.len())
|
||||
} else {
|
||||
Err(Error::EpochOutOfBounds)
|
||||
}
|
||||
@@ -478,7 +505,7 @@ impl BeaconState {
|
||||
|
||||
/// Return the `active_index_root` at a recent `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_active_index_root(&self, epoch: Epoch, spec: &ChainSpec) -> Result<Hash256, Error> {
|
||||
let i = self.get_active_index_root_index(epoch, spec)?;
|
||||
Ok(self.latest_active_index_roots[i])
|
||||
@@ -486,7 +513,7 @@ impl BeaconState {
|
||||
|
||||
/// Set the `active_index_root` at a recent `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn set_active_index_root(
|
||||
&mut self,
|
||||
epoch: Epoch,
|
||||
@@ -500,23 +527,18 @@ impl BeaconState {
|
||||
|
||||
/// Replace `active_index_roots` with clones of `index_root`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn fill_active_index_roots_with(&mut self, index_root: Hash256, spec: &ChainSpec) {
|
||||
/// Spec v0.5.1
|
||||
pub fn fill_active_index_roots_with(&mut self, index_root: Hash256) {
|
||||
self.latest_active_index_roots =
|
||||
vec![index_root; spec.latest_active_index_roots_length as usize]
|
||||
vec![index_root; self.latest_active_index_roots.len() as usize].into()
|
||||
}
|
||||
|
||||
/// Safely obtains the index for latest state roots, given some `slot`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_latest_state_roots_index(&self, slot: Slot, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + spec.slots_per_historical_root as u64) {
|
||||
let i = slot.as_usize() % spec.slots_per_historical_root;
|
||||
if i >= self.latest_state_roots.len() {
|
||||
Err(Error::InsufficientStateRoots)
|
||||
} else {
|
||||
Ok(i)
|
||||
}
|
||||
/// Spec v0.5.1
|
||||
fn get_latest_state_roots_index(&self, slot: Slot) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + self.latest_state_roots.len() as u64) {
|
||||
Ok(slot.as_usize() % self.latest_state_roots.len())
|
||||
} else {
|
||||
Err(BeaconStateError::SlotOutOfBounds)
|
||||
}
|
||||
@@ -524,31 +546,26 @@ impl BeaconState {
|
||||
|
||||
/// Gets the state root for some slot.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_state_root(&mut self, slot: Slot, spec: &ChainSpec) -> Result<&Hash256, Error> {
|
||||
let i = self.get_latest_state_roots_index(slot, spec)?;
|
||||
/// Spec v0.5.1
|
||||
pub fn get_state_root(&mut self, slot: Slot) -> Result<&Hash256, Error> {
|
||||
let i = self.get_latest_state_roots_index(slot)?;
|
||||
Ok(&self.latest_state_roots[i])
|
||||
}
|
||||
|
||||
/// Sets the latest state root for slot.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn set_state_root(
|
||||
&mut self,
|
||||
slot: Slot,
|
||||
state_root: Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = self.get_latest_state_roots_index(slot, spec)?;
|
||||
/// Spec v0.5.1
|
||||
pub fn set_state_root(&mut self, slot: Slot, state_root: Hash256) -> Result<(), Error> {
|
||||
let i = self.get_latest_state_roots_index(slot)?;
|
||||
self.latest_state_roots[i] = state_root;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Safely obtains the index for `latest_slashed_balances`, given some `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_slashed_balance_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let i = epoch.as_usize() % spec.latest_slashed_exit_length;
|
||||
/// Spec v0.5.1
|
||||
fn get_slashed_balance_index(&self, epoch: Epoch) -> Result<usize, Error> {
|
||||
let i = epoch.as_usize() % self.latest_slashed_balances.len();
|
||||
|
||||
// NOTE: the validity of the epoch is not checked. It is not in the spec but it's probably
|
||||
// useful to have.
|
||||
@@ -561,29 +578,24 @@ impl BeaconState {
|
||||
|
||||
/// Gets the total slashed balances for some epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_slashed_balance(&self, epoch: Epoch, spec: &ChainSpec) -> Result<u64, Error> {
|
||||
let i = self.get_slashed_balance_index(epoch, spec)?;
|
||||
/// Spec v0.5.1
|
||||
pub fn get_slashed_balance(&self, epoch: Epoch) -> Result<u64, Error> {
|
||||
let i = self.get_slashed_balance_index(epoch)?;
|
||||
Ok(self.latest_slashed_balances[i])
|
||||
}
|
||||
|
||||
/// Sets the total slashed balances for some epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn set_slashed_balance(
|
||||
&mut self,
|
||||
epoch: Epoch,
|
||||
balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = self.get_slashed_balance_index(epoch, spec)?;
|
||||
/// Spec v0.5.1
|
||||
pub fn set_slashed_balance(&mut self, epoch: Epoch, balance: u64) -> Result<(), Error> {
|
||||
let i = self.get_slashed_balance_index(epoch)?;
|
||||
self.latest_slashed_balances[i] = balance;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generate a seed for the given `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn generate_seed(&self, epoch: Epoch, spec: &ChainSpec) -> Result<Hash256, Error> {
|
||||
let mut input = self
|
||||
.get_randao_mix(epoch - spec.min_seed_lookahead, spec)?
|
||||
@@ -599,7 +611,7 @@ impl BeaconState {
|
||||
|
||||
/// Return the effective balance (also known as "balance at stake") for a validator with the given ``index``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_effective_balance(
|
||||
&self,
|
||||
validator_index: usize,
|
||||
@@ -614,14 +626,14 @@ impl BeaconState {
|
||||
|
||||
/// Return the epoch at which an activation or exit triggered in ``epoch`` takes effect.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_delayed_activation_exit_epoch(&self, epoch: Epoch, spec: &ChainSpec) -> Epoch {
|
||||
epoch + 1 + spec.activation_exit_delay
|
||||
}
|
||||
|
||||
/// Initiate an exit for the validator of the given `index`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn initiate_validator_exit(&mut self, validator_index: usize) {
|
||||
self.validator_registry[validator_index].initiated_exit = true;
|
||||
}
|
||||
@@ -633,7 +645,7 @@ impl BeaconState {
|
||||
///
|
||||
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_attestation_duties(
|
||||
&self,
|
||||
validator_index: usize,
|
||||
@@ -649,7 +661,7 @@ impl BeaconState {
|
||||
|
||||
/// Return the combined effective balance of an array of validators.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_total_balance(
|
||||
&self,
|
||||
validator_indices: &[usize],
|
||||
@@ -668,6 +680,7 @@ impl BeaconState {
|
||||
self.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec)?;
|
||||
self.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec)?;
|
||||
self.update_pubkey_cache()?;
|
||||
self.update_tree_hash_cache()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -774,6 +787,39 @@ impl BeaconState {
|
||||
pub fn drop_pubkey_cache(&mut self) {
|
||||
self.pubkey_cache = PubkeyCache::default()
|
||||
}
|
||||
|
||||
/// Update the tree hash cache, building it for the first time if it is empty.
|
||||
///
|
||||
/// Returns the `tree_hash_root` resulting from the update. This root can be considered the
|
||||
/// canonical root of `self`.
|
||||
pub fn update_tree_hash_cache(&mut self) -> Result<Hash256, Error> {
|
||||
if self.tree_hash_cache.is_empty() {
|
||||
self.tree_hash_cache = TreeHashCache::new(self)?;
|
||||
} else {
|
||||
// Move the cache outside of `self` to satisfy the borrow checker.
|
||||
let mut cache = std::mem::replace(&mut self.tree_hash_cache, TreeHashCache::default());
|
||||
|
||||
cache.update(self)?;
|
||||
|
||||
// Move the updated cache back into `self`.
|
||||
self.tree_hash_cache = cache
|
||||
}
|
||||
|
||||
self.cached_tree_hash_root()
|
||||
}
|
||||
|
||||
/// Returns the tree hash root determined by the last execution of `self.update_tree_hash_cache(..)`.
|
||||
///
|
||||
/// Note: does _not_ update the cache and may return an outdated root.
|
||||
///
|
||||
/// Returns an error if the cache is not initialized or if an error is encountered during the
|
||||
/// cache update.
|
||||
pub fn cached_tree_hash_root(&self) -> Result<Hash256, Error> {
|
||||
self.tree_hash_cache
|
||||
.tree_hash_root()
|
||||
.and_then(|b| Ok(Hash256::from_slice(b)))
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RelativeEpochError> for Error {
|
||||
@@ -787,3 +833,9 @@ impl From<EpochCacheError> for Error {
|
||||
Error::EpochCacheError(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TreeHashCacheError> for Error {
|
||||
fn from(e: TreeHashCacheError) -> Error {
|
||||
Error::TreeHashCacheError(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
use crate::*;
|
||||
use fixed_len_vec::typenum::{Unsigned, U1024, U8, U8192};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub trait EthSpec:
|
||||
'static + Default + Sync + Send + Clone + Debug + PartialEq + serde::de::DeserializeOwned
|
||||
{
|
||||
type ShardCount: Unsigned + Clone + Sync + Send + Debug + PartialEq;
|
||||
type SlotsPerHistoricalRoot: Unsigned + Clone + Sync + Send + Debug + PartialEq;
|
||||
type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
|
||||
type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
|
||||
type LatestSlashedExitLength: Unsigned + Clone + Sync + Send + Debug + PartialEq;
|
||||
|
||||
fn spec() -> ChainSpec;
|
||||
|
||||
/// Returns the `SHARD_COUNT` constant for this specification.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn shard_count() -> usize {
|
||||
Self::ShardCount::to_usize()
|
||||
}
|
||||
|
||||
/// Returns the `SLOTS_PER_HISTORICAL_ROOT` constant for this specification.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn slots_per_historical_root() -> usize {
|
||||
Self::SlotsPerHistoricalRoot::to_usize()
|
||||
}
|
||||
|
||||
/// Returns the `LATEST_RANDAO_MIXES_LENGTH` constant for this specification.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn latest_randao_mixes_length() -> usize {
|
||||
Self::LatestRandaoMixesLength::to_usize()
|
||||
}
|
||||
|
||||
/// Returns the `LATEST_ACTIVE_INDEX_ROOTS` constant for this specification.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn latest_active_index_roots() -> usize {
|
||||
Self::LatestActiveIndexRootsLength::to_usize()
|
||||
}
|
||||
|
||||
/// Returns the `LATEST_SLASHED_EXIT_LENGTH` constant for this specification.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn latest_slashed_exit_length() -> usize {
|
||||
Self::LatestSlashedExitLength::to_usize()
|
||||
}
|
||||
}
|
||||
|
||||
/// Ethereum Foundation specifications.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct FoundationEthSpec;
|
||||
|
||||
impl EthSpec for FoundationEthSpec {
|
||||
type ShardCount = U1024;
|
||||
type SlotsPerHistoricalRoot = U8192;
|
||||
type LatestRandaoMixesLength = U8192;
|
||||
type LatestActiveIndexRootsLength = U8192;
|
||||
type LatestSlashedExitLength = U8192;
|
||||
|
||||
fn spec() -> ChainSpec {
|
||||
ChainSpec::foundation()
|
||||
}
|
||||
}
|
||||
|
||||
pub type FoundationBeaconState = BeaconState<FoundationEthSpec>;
|
||||
|
||||
/// Ethereum Foundation specifications, modified to be suitable for < 1000 validators.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct FewValidatorsEthSpec;
|
||||
|
||||
impl EthSpec for FewValidatorsEthSpec {
|
||||
type ShardCount = U8;
|
||||
type SlotsPerHistoricalRoot = U8192;
|
||||
type LatestRandaoMixesLength = U8192;
|
||||
type LatestActiveIndexRootsLength = U8192;
|
||||
type LatestSlashedExitLength = U8192;
|
||||
|
||||
fn spec() -> ChainSpec {
|
||||
ChainSpec::few_validators()
|
||||
}
|
||||
}
|
||||
|
||||
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsEthSpec>;
|
||||
|
||||
/// Specifications suitable for a small-scale (< 1000 validators) lighthouse testnet.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
|
||||
pub struct LighthouseTestnetEthSpec;
|
||||
|
||||
impl EthSpec for LighthouseTestnetEthSpec {
|
||||
type ShardCount = U8;
|
||||
type SlotsPerHistoricalRoot = U8192;
|
||||
type LatestRandaoMixesLength = U8192;
|
||||
type LatestActiveIndexRootsLength = U8192;
|
||||
type LatestSlashedExitLength = U8192;
|
||||
|
||||
fn spec() -> ChainSpec {
|
||||
ChainSpec::lighthouse_testnet()
|
||||
}
|
||||
}
|
||||
|
||||
pub type LighthouseTestnetBeaconState = BeaconState<LighthouseTestnetEthSpec>;
|
||||
@@ -28,8 +28,8 @@ pub struct EpochCache {
|
||||
|
||||
impl EpochCache {
|
||||
/// Return a new, fully initialized cache.
|
||||
pub fn initialized(
|
||||
state: &BeaconState,
|
||||
pub fn initialized<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
relative_epoch: RelativeEpoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<EpochCache, Error> {
|
||||
@@ -138,7 +138,7 @@ impl EpochCache {
|
||||
/// Returns a list of all `validator_registry` indices where the validator is active at the given
|
||||
/// `epoch`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> Vec<usize> {
|
||||
let mut active = Vec::with_capacity(validators.len());
|
||||
|
||||
@@ -200,8 +200,8 @@ pub struct EpochCrosslinkCommitteesBuilder {
|
||||
|
||||
impl EpochCrosslinkCommitteesBuilder {
|
||||
/// Instantiates a builder that will build for the `state`'s previous epoch.
|
||||
pub fn for_previous_epoch(
|
||||
state: &BeaconState,
|
||||
pub fn for_previous_epoch<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
active_validator_indices: Vec<usize>,
|
||||
spec: &ChainSpec,
|
||||
) -> Self {
|
||||
@@ -215,8 +215,8 @@ impl EpochCrosslinkCommitteesBuilder {
|
||||
}
|
||||
|
||||
/// Instantiates a builder that will build for the `state`'s next epoch.
|
||||
pub fn for_current_epoch(
|
||||
state: &BeaconState,
|
||||
pub fn for_current_epoch<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
active_validator_indices: Vec<usize>,
|
||||
spec: &ChainSpec,
|
||||
) -> Self {
|
||||
@@ -233,8 +233,8 @@ impl EpochCrosslinkCommitteesBuilder {
|
||||
///
|
||||
/// Note: there are two possible epoch builds for the next epoch, one where there is a registry
|
||||
/// change and one where there is not.
|
||||
pub fn for_next_epoch(
|
||||
state: &BeaconState,
|
||||
pub fn for_next_epoch<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
active_validator_indices: Vec<usize>,
|
||||
registry_change: bool,
|
||||
spec: &ChainSpec,
|
||||
@@ -288,7 +288,7 @@ impl EpochCrosslinkCommitteesBuilder {
|
||||
self.active_validator_indices,
|
||||
spec.shuffle_round_count,
|
||||
&self.shuffling_seed[..],
|
||||
true,
|
||||
false,
|
||||
)
|
||||
.ok_or_else(|| Error::UnableToShuffle)?
|
||||
};
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use crate::beacon_state::FewValidatorsEthSpec;
|
||||
use crate::test_utils::*;
|
||||
use swap_or_not_shuffle::shuffle_list;
|
||||
|
||||
fn do_sane_cache_test(
|
||||
state: BeaconState,
|
||||
fn do_sane_cache_test<T: EthSpec>(
|
||||
state: BeaconState<T>,
|
||||
epoch: Epoch,
|
||||
relative_epoch: RelativeEpoch,
|
||||
validator_count: usize,
|
||||
@@ -27,7 +28,7 @@ fn do_sane_cache_test(
|
||||
active_indices,
|
||||
spec.shuffle_round_count,
|
||||
&expected_seed[..],
|
||||
true,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -64,7 +65,7 @@ fn do_sane_cache_test(
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_sane_cache_test(validator_count: usize, spec: &ChainSpec) -> BeaconState {
|
||||
fn setup_sane_cache_test<T: EthSpec>(validator_count: usize, spec: &ChainSpec) -> BeaconState<T> {
|
||||
let mut builder =
|
||||
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, spec);
|
||||
|
||||
@@ -98,10 +99,13 @@ fn setup_sane_cache_test(validator_count: usize, spec: &ChainSpec) -> BeaconStat
|
||||
|
||||
#[test]
|
||||
fn builds_sane_current_epoch_cache() {
|
||||
let mut spec = ChainSpec::few_validators();
|
||||
let mut spec = FewValidatorsEthSpec::spec();
|
||||
spec.shard_count = 4;
|
||||
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
|
||||
let state = setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
let state: BeaconState<FewValidatorsEthSpec> =
|
||||
setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
do_sane_cache_test(
|
||||
state.clone(),
|
||||
state.current_epoch(&spec),
|
||||
@@ -115,10 +119,13 @@ fn builds_sane_current_epoch_cache() {
|
||||
|
||||
#[test]
|
||||
fn builds_sane_previous_epoch_cache() {
|
||||
let mut spec = ChainSpec::few_validators();
|
||||
let mut spec = FewValidatorsEthSpec::spec();
|
||||
spec.shard_count = 2;
|
||||
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
|
||||
let state = setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
let state: BeaconState<FewValidatorsEthSpec> =
|
||||
setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
do_sane_cache_test(
|
||||
state.clone(),
|
||||
state.previous_epoch(&spec),
|
||||
@@ -132,10 +139,13 @@ fn builds_sane_previous_epoch_cache() {
|
||||
|
||||
#[test]
|
||||
fn builds_sane_next_without_update_epoch_cache() {
|
||||
let mut spec = ChainSpec::few_validators();
|
||||
let mut spec = FewValidatorsEthSpec::spec();
|
||||
spec.shard_count = 2;
|
||||
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
|
||||
let mut state = setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
let mut state: BeaconState<FewValidatorsEthSpec> =
|
||||
setup_sane_cache_test(validator_count as usize, &spec);
|
||||
|
||||
state.validator_registry_update_epoch = state.slot.epoch(spec.slots_per_epoch);
|
||||
do_sane_cache_test(
|
||||
state.clone(),
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
#![cfg(test)]
|
||||
use super::*;
|
||||
use crate::beacon_state::FewValidatorsEthSpec;
|
||||
use crate::test_utils::*;
|
||||
|
||||
ssz_tests!(BeaconState);
|
||||
ssz_tests!(FoundationBeaconState);
|
||||
cached_tree_hash_tests!(FoundationBeaconState);
|
||||
|
||||
/// Test that
|
||||
///
|
||||
/// 1. Using the cache before it's built fails.
|
||||
/// 2. Using the cache after it's build passes.
|
||||
/// 3. Using the cache after it's dropped fails.
|
||||
fn test_cache_initialization<'a>(
|
||||
state: &'a mut BeaconState,
|
||||
fn test_cache_initialization<'a, T: EthSpec>(
|
||||
state: &'a mut BeaconState<T>,
|
||||
relative_epoch: RelativeEpoch,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
@@ -44,9 +46,11 @@ fn test_cache_initialization<'a>(
|
||||
|
||||
#[test]
|
||||
fn cache_initialization() {
|
||||
let spec = ChainSpec::few_validators();
|
||||
let (mut state, _keypairs) =
|
||||
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec).build();
|
||||
let spec = FewValidatorsEthSpec::spec();
|
||||
|
||||
let builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
|
||||
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec);
|
||||
let (mut state, _keypairs) = builder.build();
|
||||
|
||||
state.slot = (spec.genesis_epoch + 1).start_slot(spec.slots_per_epoch);
|
||||
|
||||
@@ -55,3 +59,22 @@ fn cache_initialization() {
|
||||
test_cache_initialization(&mut state, RelativeEpoch::NextWithRegistryChange, &spec);
|
||||
test_cache_initialization(&mut state, RelativeEpoch::NextWithoutRegistryChange, &spec);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tree_hash_cache() {
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
|
||||
let mut state: FoundationBeaconState = BeaconState::random_for_test(&mut rng);
|
||||
|
||||
let root = state.update_tree_hash_cache().unwrap();
|
||||
|
||||
assert_eq!(root.as_bytes(), &state.tree_hash_root()[..]);
|
||||
|
||||
state.slot = state.slot + 1;
|
||||
|
||||
let root = state.update_tree_hash_cache().unwrap();
|
||||
assert_eq!(root.as_bytes(), &state.tree_hash_root()[..]);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ const GWEI: u64 = 1_000_000_000;
|
||||
|
||||
/// Each of the BLS signature domains.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub enum Domain {
|
||||
BeaconBlock,
|
||||
Randao,
|
||||
@@ -20,7 +20,7 @@ pub enum Domain {
|
||||
|
||||
/// Holds all the "constants" for a BeaconChain.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(PartialEq, Debug, Clone, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ChainSpec {
|
||||
@@ -70,17 +70,9 @@ pub struct ChainSpec {
|
||||
pub min_seed_lookahead: Epoch,
|
||||
pub activation_exit_delay: u64,
|
||||
pub epochs_per_eth1_voting_period: u64,
|
||||
pub slots_per_historical_root: usize,
|
||||
pub min_validator_withdrawability_delay: Epoch,
|
||||
pub persistent_committee_period: u64,
|
||||
|
||||
/*
|
||||
* State list lengths
|
||||
*/
|
||||
pub latest_randao_mixes_length: usize,
|
||||
pub latest_active_index_roots_length: usize,
|
||||
pub latest_slashed_exit_length: usize,
|
||||
|
||||
/*
|
||||
* Reward and penalty quotients
|
||||
*/
|
||||
@@ -126,7 +118,7 @@ pub struct ChainSpec {
|
||||
impl ChainSpec {
|
||||
/// Return the number of committees in one epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_epoch_committee_count(&self, active_validator_count: usize) -> u64 {
|
||||
std::cmp::max(
|
||||
1,
|
||||
@@ -139,7 +131,7 @@ impl ChainSpec {
|
||||
|
||||
/// Get the domain number that represents the fork meta and signature domain.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_domain(&self, epoch: Epoch, domain: Domain, fork: &Fork) -> u64 {
|
||||
let domain_constant = match domain {
|
||||
Domain::BeaconBlock => self.domain_beacon_block,
|
||||
@@ -161,8 +153,8 @@ impl ChainSpec {
|
||||
|
||||
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn foundation() -> Self {
|
||||
/// Spec v0.5.1
|
||||
pub(crate) fn foundation() -> Self {
|
||||
let genesis_slot = Slot::new(2_u64.pow(32));
|
||||
let slots_per_epoch = 64;
|
||||
let genesis_epoch = genesis_slot.epoch(slots_per_epoch);
|
||||
@@ -213,17 +205,9 @@ impl ChainSpec {
|
||||
min_seed_lookahead: Epoch::new(1),
|
||||
activation_exit_delay: 4,
|
||||
epochs_per_eth1_voting_period: 16,
|
||||
slots_per_historical_root: 8_192,
|
||||
min_validator_withdrawability_delay: Epoch::new(256),
|
||||
persistent_committee_period: 2_048,
|
||||
|
||||
/*
|
||||
* State list lengths
|
||||
*/
|
||||
latest_randao_mixes_length: 8_192,
|
||||
latest_active_index_roots_length: 8_192,
|
||||
latest_slashed_exit_length: 8_192,
|
||||
|
||||
/*
|
||||
* Reward and penalty quotients
|
||||
*/
|
||||
@@ -264,7 +248,7 @@ impl ChainSpec {
|
||||
/// Returns a `ChainSpec` compatible with the Lighthouse testnet specification.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn lighthouse_testnet() -> Self {
|
||||
pub(crate) fn lighthouse_testnet() -> Self {
|
||||
/*
|
||||
* Lighthouse testnet bootnodes
|
||||
*/
|
||||
@@ -280,7 +264,7 @@ impl ChainSpec {
|
||||
}
|
||||
|
||||
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
|
||||
pub fn few_validators() -> Self {
|
||||
pub(crate) fn few_validators() -> Self {
|
||||
let genesis_slot = Slot::new(2_u64.pow(32));
|
||||
let slots_per_epoch = 8;
|
||||
let genesis_epoch = genesis_slot.epoch(slots_per_epoch);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::{Epoch, Hash256};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Specifies the block hash for a shard at an epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -19,6 +20,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct Crosslink {
|
||||
@@ -31,4 +33,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Crosslink);
|
||||
cached_tree_hash_tests!(Crosslink);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,20 @@
|
||||
use crate::*;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize, Decode, Encode, TreeHash)]
|
||||
#[derive(
|
||||
Default,
|
||||
Clone,
|
||||
Debug,
|
||||
PartialEq,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Decode,
|
||||
Encode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
)]
|
||||
pub struct CrosslinkCommittee {
|
||||
pub slot: Slot,
|
||||
pub shard: Shard,
|
||||
|
||||
@@ -1,16 +1,29 @@
|
||||
use super::{DepositData, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use crate::*;
|
||||
use fixed_len_vec::typenum::U32;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// A deposit to potentially become a beacon chain validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct Deposit {
|
||||
pub proof: Vec<Hash256>,
|
||||
pub proof: FixedLenVec<Hash256, U32>,
|
||||
pub index: u64,
|
||||
pub deposit_data: DepositData,
|
||||
}
|
||||
@@ -20,4 +33,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Deposit);
|
||||
cached_tree_hash_tests!(Deposit);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
use super::DepositInput;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Data generated by the deposit contract.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct DepositData {
|
||||
pub amount: u64,
|
||||
pub timestamp: u64,
|
||||
@@ -20,4 +32,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(DepositData);
|
||||
cached_tree_hash_tests!(DepositData);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use bls::{PublicKey, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::{SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::{SignedRoot, TreeHash};
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// The data supplied by the user to the deposit contract.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
@@ -20,6 +21,7 @@ use test_random_derive::TestRandom;
|
||||
Decode,
|
||||
SignedRoot,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct DepositInput {
|
||||
@@ -32,7 +34,7 @@ pub struct DepositInput {
|
||||
impl DepositInput {
|
||||
/// Generate the 'proof_of_posession' signature for a given DepositInput details.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn create_proof_of_possession(
|
||||
&self,
|
||||
secret_key: &SecretKey,
|
||||
@@ -48,7 +50,7 @@ impl DepositInput {
|
||||
|
||||
/// Verify that proof-of-possession is valid.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn validate_proof_of_possession(
|
||||
&self,
|
||||
epoch: Epoch,
|
||||
@@ -67,6 +69,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(DepositInput);
|
||||
cached_tree_hash_tests!(DepositInput);
|
||||
|
||||
#[test]
|
||||
fn can_create_and_validate() {
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Contains data obtained from the Eth1 chain.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug, PartialEq, Clone, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Default,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct Eth1Data {
|
||||
pub deposit_root: Hash256,
|
||||
@@ -21,4 +32,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Eth1Data);
|
||||
cached_tree_hash_tests!(Eth1Data);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
use super::Eth1Data;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// A summation of votes for some `Eth1Data`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug, PartialEq, Clone, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Default,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct Eth1DataVote {
|
||||
pub eth1_data: Eth1Data,
|
||||
@@ -21,4 +32,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Eth1DataVote);
|
||||
cached_tree_hash_tests!(Eth1DataVote);
|
||||
}
|
||||
|
||||
+18
-6
@@ -3,16 +3,27 @@ use crate::{
|
||||
ChainSpec, Epoch,
|
||||
};
|
||||
use int_to_bytes::int_to_bytes4;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Specifies a fork of the `BeaconChain`, to prevent replay attacks.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug, Clone, PartialEq, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,
|
||||
Debug,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Default,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct Fork {
|
||||
#[serde(deserialize_with = "fork_from_hex_str")]
|
||||
@@ -25,7 +36,7 @@ pub struct Fork {
|
||||
impl Fork {
|
||||
/// Initialize the `Fork` from the genesis parameters in the `spec`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn genesis(spec: &ChainSpec) -> Self {
|
||||
let mut current_version: [u8; 4] = [0; 4];
|
||||
current_version.copy_from_slice(&int_to_bytes4(spec.genesis_fork_version));
|
||||
@@ -39,7 +50,7 @@ impl Fork {
|
||||
|
||||
/// Return the fork version of the given ``epoch``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn get_fork_version(&self, epoch: Epoch) -> [u8; 4] {
|
||||
if epoch < self.epoch {
|
||||
return self.previous_version;
|
||||
@@ -53,6 +64,7 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(Fork);
|
||||
cached_tree_hash_tests!(Fork);
|
||||
|
||||
fn test_genesis(version: u32, epoch: Epoch) {
|
||||
let mut spec = ChainSpec::foundation();
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::Hash256;
|
||||
use rand::RngCore;
|
||||
use crate::*;
|
||||
|
||||
use fixed_len_vec::FixedLenVec;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Historical block and state roots.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
pub struct HistoricalBatch {
|
||||
pub block_roots: Vec<Hash256>,
|
||||
pub state_roots: Vec<Hash256>,
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct HistoricalBatch<T: EthSpec> {
|
||||
pub block_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
pub state_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(HistoricalBatch);
|
||||
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationEthSpec>;
|
||||
|
||||
ssz_tests!(FoundationHistoricalBatch);
|
||||
cached_tree_hash_tests!(FoundationHistoricalBatch);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ pub use crate::attester_slashing::AttesterSlashing;
|
||||
pub use crate::beacon_block::BeaconBlock;
|
||||
pub use crate::beacon_block_body::BeaconBlockBody;
|
||||
pub use crate::beacon_block_header::BeaconBlockHeader;
|
||||
pub use crate::beacon_state::{BeaconState, Error as BeaconStateError};
|
||||
pub use crate::beacon_state::{Error as BeaconStateError, *};
|
||||
pub use crate::chain_spec::{ChainSpec, Domain};
|
||||
pub use crate::crosslink::Crosslink;
|
||||
pub use crate::crosslink_committee::CrosslinkCommittee;
|
||||
@@ -85,6 +85,7 @@ pub type AttesterMap = HashMap<(u64, u64), Vec<usize>>;
|
||||
pub type ProposerMap = HashMap<u64, usize>;
|
||||
|
||||
pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature};
|
||||
pub use fixed_len_vec::{typenum::Unsigned, FixedLenVec};
|
||||
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
|
||||
pub use libp2p::multiaddr;
|
||||
pub use libp2p::Multiaddr;
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::{Attestation, AttestationData, Bitfield, Slot};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// An attestation that has been included in the state but not yet fully processed.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct PendingAttestation {
|
||||
pub aggregation_bitfield: Bitfield,
|
||||
pub data: AttestationData,
|
||||
@@ -33,4 +45,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(PendingAttestation);
|
||||
cached_tree_hash_tests!(PendingAttestation);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
use super::BeaconBlockHeader;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
/// Two conflicting proposals from the same proposer (validator).
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Clone,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct ProposerSlashing {
|
||||
pub proposer_index: u64,
|
||||
pub header_1: BeaconBlockHeader,
|
||||
@@ -20,4 +32,5 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
ssz_tests!(ProposerSlashing);
|
||||
cached_tree_hash_tests!(ProposerSlashing);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub enum Error {
|
||||
/// Defines the epochs relative to some epoch. Most useful when referring to the committees prior
|
||||
/// to and following some epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum RelativeEpoch {
|
||||
/// The prior epoch.
|
||||
@@ -32,7 +32,7 @@ pub enum RelativeEpoch {
|
||||
impl RelativeEpoch {
|
||||
/// Returns the `epoch` that `self` refers to, with respect to the `base` epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn into_epoch(self, base: Epoch) -> Epoch {
|
||||
match self {
|
||||
RelativeEpoch::Previous => base - 1,
|
||||
@@ -51,7 +51,7 @@ impl RelativeEpoch {
|
||||
/// - `AmbiguiousNextEpoch` whenever `other` is one after `base`, because it's unknowable if
|
||||
/// there will be a registry change.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn from_epoch(base: Epoch, other: Epoch) -> Result<Self, Error> {
|
||||
if other == base - 1 {
|
||||
Ok(RelativeEpoch::Previous)
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield, ChainSpec};
|
||||
use rand::RngCore;
|
||||
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::TreeHash;
|
||||
use ssz_derive::{Decode, Encode, SignedRoot, TreeHash};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
use tree_hash::TreeHash;
|
||||
use tree_hash_derive::{CachedTreeHash, SignedRoot, TreeHash};
|
||||
|
||||
/// Details an attestation that can be slashable.
|
||||
///
|
||||
/// To be included in an `AttesterSlashing`.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
@@ -19,6 +20,7 @@ use test_random_derive::TestRandom;
|
||||
Encode,
|
||||
Decode,
|
||||
TreeHash,
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
SignedRoot,
|
||||
)]
|
||||
@@ -34,14 +36,14 @@ pub struct SlashableAttestation {
|
||||
impl SlashableAttestation {
|
||||
/// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn is_double_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
|
||||
self.data.slot.epoch(spec.slots_per_epoch) == other.data.slot.epoch(spec.slots_per_epoch)
|
||||
}
|
||||
|
||||
/// Check if ``attestation_data_1`` surrounds ``attestation_data_2``.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
/// Spec v0.5.1
|
||||
pub fn is_surround_vote(&self, other: &SlashableAttestation, spec: &ChainSpec) -> bool {
|
||||
let source_epoch_1 = self.data.source_epoch;
|
||||
let source_epoch_2 = other.data.source_epoch;
|
||||
@@ -132,6 +134,7 @@ mod tests {
|
||||
}
|
||||
|
||||
ssz_tests!(SlashableAttestation);
|
||||
cached_tree_hash_tests!(SlashableAttestation);
|
||||
|
||||
fn create_slashable_attestation(
|
||||
slot_factor: u64,
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
//! The `Slot` and `Epoch` types are defined as newtypes over u64 to enforce type-safety between
|
||||
//! the two types.
|
||||
//!
|
||||
//! `Slot` and `Epoch` have implementations which permit conversion, comparison and math operations
|
||||
//! between each and `u64`, however specifically not between each other.
|
||||
//!
|
||||
//! All math operations on `Slot` and `Epoch` are saturating, they never wrap.
|
||||
//!
|
||||
//! It would be easy to define `PartialOrd` and other traits generically across all types which
|
||||
//! implement `Into<u64>`, however this would allow operations between `Slots` and `Epochs` which
|
||||
//! may lead to programming errors which are not detected by the compiler.
|
||||
|
||||
use crate::slot_height::SlotHeight;
|
||||
/// The `Slot` and `Epoch` types are defined as newtypes over u64 to enforce type-safety between
|
||||
/// the two types.
|
||||
///
|
||||
/// `Slot` and `Epoch` have implementations which permit conversion, comparison and math operations
|
||||
/// between each and `u64`, however specifically not between each other.
|
||||
///
|
||||
/// All math operations on `Slot` and `Epoch` are saturating, they never wrap.
|
||||
///
|
||||
/// It would be easy to define `PartialOrd` and other traits generically across all types which
|
||||
/// implement `Into<u64>`, however this would allow operations between `Slots` and `Epochs` which
|
||||
/// may lead to programming errors which are not detected by the compiler.
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use slog;
|
||||
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
use ssz::{ssz_encode, Decode, DecodeError, Encode};
|
||||
use std::cmp::{Ord, Ordering};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@@ -144,11 +145,13 @@ mod epoch_tests {
|
||||
#[test]
|
||||
fn max_epoch_ssz() {
|
||||
let max_epoch = Epoch::max_value();
|
||||
let mut ssz = SszStream::new();
|
||||
ssz.append(&max_epoch);
|
||||
let encoded = ssz.drain();
|
||||
assert_eq!(&encoded, &[255, 255, 255, 255, 255, 255, 255, 255]);
|
||||
let (decoded, _i): (Epoch, usize) = <_>::ssz_decode(&encoded, 0).unwrap();
|
||||
assert_eq!(max_epoch, decoded);
|
||||
assert_eq!(
|
||||
&max_epoch.as_ssz_bytes(),
|
||||
&[255, 255, 255, 255, 255, 255, 255, 255]
|
||||
);
|
||||
assert_eq!(
|
||||
max_epoch,
|
||||
Epoch::from_ssz_bytes(&max_epoch.as_ssz_bytes()).unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,30 +192,74 @@ macro_rules! impl_display {
|
||||
|
||||
macro_rules! impl_ssz {
|
||||
($type: ident) => {
|
||||
impl Encodable for $type {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.0);
|
||||
impl Encode for $type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<u64 as Encode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<u64 as Encode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
self.0.ssz_append(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for $type {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (value, i) = <_>::ssz_decode(bytes, i)?;
|
||||
impl Decode for $type {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<u64 as Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
Ok(($type(value), i))
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<u64 as Decode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
Ok($type(u64::from_ssz_bytes(bytes)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for $type {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
let mut result: Vec<u8> = vec![];
|
||||
result.append(&mut self.0.hash_tree_root());
|
||||
hash(&result)
|
||||
impl tree_hash::TreeHash for $type {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
tree_hash::TreeHashType::Basic
|
||||
}
|
||||
|
||||
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
|
||||
fn tree_hash_packing_factor() -> usize {
|
||||
32 / 8
|
||||
}
|
||||
|
||||
fn tree_hash_root(&self) -> Vec<u8> {
|
||||
int_to_bytes::int_to_bytes32(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for $type {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl cached_tree_hash::CachedTreeHash for $type {
|
||||
fn new_tree_hash_cache(
|
||||
&self,
|
||||
depth: usize,
|
||||
) -> Result<cached_tree_hash::TreeHashCache, cached_tree_hash::Error> {
|
||||
self.0.new_tree_hash_cache(depth)
|
||||
}
|
||||
|
||||
fn tree_hash_cache_schema(&self, depth: usize) -> cached_tree_hash::BTreeSchema {
|
||||
self.0.tree_hash_cache_schema(depth)
|
||||
}
|
||||
|
||||
fn update_tree_hash_cache(
|
||||
&self,
|
||||
cache: &mut cached_tree_hash::TreeHashCache,
|
||||
) -> Result<(), cached_tree_hash::Error> {
|
||||
self.0.update_tree_hash_cache(cache)
|
||||
}
|
||||
}
|
||||
|
||||
impl TestRandom for $type {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
$type::from(u64::random_for_test(rng))
|
||||
}
|
||||
}
|
||||
@@ -535,6 +579,7 @@ macro_rules! all_tests {
|
||||
math_between_tests!($type, $type);
|
||||
math_tests!($type);
|
||||
ssz_tests!($type);
|
||||
cached_tree_hash_tests!($type);
|
||||
|
||||
mod u64_tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::slot_epoch::{Epoch, Slot};
|
||||
use crate::test_utils::TestRandom;
|
||||
|
||||
use rand::RngCore;
|
||||
use serde_derive::Serialize;
|
||||
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
use ssz::{ssz_encode, Decode, DecodeError, Encode};
|
||||
use std::cmp::{Ord, Ordering};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
@@ -18,13 +18,17 @@ pub fn generate_deterministic_keypairs(validator_count: usize) -> Vec<Keypair> {
|
||||
let keypairs: Vec<Keypair> = (0..validator_count)
|
||||
.collect::<Vec<usize>>()
|
||||
.par_iter()
|
||||
.map(|&i| {
|
||||
let secret = int_to_bytes48(i as u64 + 1000);
|
||||
let sk = SecretKey::from_bytes(&secret).unwrap();
|
||||
let pk = PublicKey::from_secret_key(&sk);
|
||||
Keypair { sk, pk }
|
||||
})
|
||||
.map(|&i| generate_deterministic_keypair(i))
|
||||
.collect();
|
||||
|
||||
keypairs
|
||||
}
|
||||
|
||||
/// Generates a single deterministic keypair, where the secret key is `validator_index`.
|
||||
///
|
||||
/// This is used for testing only, and not to be used in production!
|
||||
pub fn generate_deterministic_keypair(validator_index: usize) -> Keypair {
|
||||
let secret = int_to_bytes48(validator_index as u64 + 1000);
|
||||
let sk = SecretKey::from_bytes(&secret).unwrap();
|
||||
let pk = PublicKey::from_secret_key(&sk);
|
||||
Keypair { sk, pk }
|
||||
}
|
||||
|
||||
@@ -5,26 +5,26 @@ macro_rules! ssz_tests {
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
use ssz::{ssz_encode, Decodable};
|
||||
use ssz::{ssz_encode, Decode};
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = $type::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _): ($type, usize) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
let decoded = $type::from_ssz_bytes(&bytes).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_hash_tree_root() {
|
||||
pub fn test_tree_hash_root() {
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = $type::random_for_test(&mut rng);
|
||||
|
||||
let result = original.hash_tree_root();
|
||||
let result = original.tree_hash_root();
|
||||
|
||||
assert_eq!(result.len(), 32);
|
||||
// TODO: Add further tests
|
||||
@@ -32,3 +32,51 @@ macro_rules! ssz_tests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[macro_export]
|
||||
macro_rules! cached_tree_hash_tests {
|
||||
($type: ident) => {
|
||||
#[test]
|
||||
pub fn test_cached_tree_hash() {
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
|
||||
// Test the original hash
|
||||
let original = $type::random_for_test(&mut rng);
|
||||
let mut cache = cached_tree_hash::TreeHashCache::new(&original).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
cache.tree_hash_root().unwrap().to_vec(),
|
||||
original.tree_hash_root(),
|
||||
"Original hash failed."
|
||||
);
|
||||
|
||||
// Test the updated hash
|
||||
let modified = $type::random_for_test(&mut rng);
|
||||
cache.update(&modified).unwrap();
|
||||
assert_eq!(
|
||||
cache.tree_hash_root().unwrap().to_vec(),
|
||||
modified.tree_hash_root(),
|
||||
"Modification hash failed"
|
||||
);
|
||||
|
||||
// Produce a new cache for the modified object and compare it to the updated cache.
|
||||
let mut modified_cache = cached_tree_hash::TreeHashCache::new(&modified).unwrap();
|
||||
|
||||
// Reset the caches.
|
||||
cache.reset_modifications();
|
||||
modified_cache.reset_modifications();
|
||||
|
||||
// Ensure the modified cache is the same as a newly created cache. This is a sanity
|
||||
// check to make sure there are no artifacts of the original cache remaining after an
|
||||
// update.
|
||||
assert_eq!(
|
||||
modified_cache, cache,
|
||||
"The modified cache does not match a new cache."
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,9 +15,13 @@ mod testing_proposer_slashing_builder;
|
||||
mod testing_transfer_builder;
|
||||
mod testing_voluntary_exit_builder;
|
||||
|
||||
pub use generate_deterministic_keypairs::generate_deterministic_keypair;
|
||||
pub use generate_deterministic_keypairs::generate_deterministic_keypairs;
|
||||
pub use keypairs_file::KeypairsFile;
|
||||
pub use rand::{prng::XorShiftRng, SeedableRng};
|
||||
pub use rand::{
|
||||
RngCore,
|
||||
{prng::XorShiftRng, SeedableRng},
|
||||
};
|
||||
pub use serde_utils::{fork_from_hex_str, u8_from_hex_str};
|
||||
pub use test_random::TestRandom;
|
||||
pub use testing_attestation_builder::TestingAttestationBuilder;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use crate::*;
|
||||
use fixed_len_vec::typenum::Unsigned;
|
||||
use rand::RngCore;
|
||||
|
||||
mod address;
|
||||
@@ -8,54 +10,68 @@ mod public_key;
|
||||
mod secret_key;
|
||||
mod signature;
|
||||
|
||||
pub trait TestRandom<T>
|
||||
where
|
||||
T: RngCore,
|
||||
{
|
||||
fn random_for_test(rng: &mut T) -> Self;
|
||||
pub trait TestRandom {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self;
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for bool {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for bool {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
(rng.next_u32() % 2) == 1
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for u64 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for u64 {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
rng.next_u64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for u32 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for u32 {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
rng.next_u32()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for usize {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for usize {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
rng.next_u32() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore, U> TestRandom<T> for Vec<U>
|
||||
impl<U> TestRandom for Vec<U>
|
||||
where
|
||||
U: TestRandom<T>,
|
||||
U: TestRandom,
|
||||
{
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
vec![
|
||||
<U>::random_for_test(rng),
|
||||
<U>::random_for_test(rng),
|
||||
<U>::random_for_test(rng),
|
||||
]
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut output = vec![];
|
||||
|
||||
for _ in 0..(usize::random_for_test(rng) % 4) {
|
||||
output.push(<U>::random_for_test(rng));
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, N: Unsigned> TestRandom for FixedLenVec<T, N>
|
||||
where
|
||||
T: TestRandom + Default,
|
||||
{
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut output = vec![];
|
||||
|
||||
for _ in 0..(usize::random_for_test(rng) % std::cmp::min(4, N::to_usize())) {
|
||||
output.push(<T>::random_for_test(rng));
|
||||
}
|
||||
|
||||
output.into()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_test_random_for_u8_array {
|
||||
($len: expr) => {
|
||||
impl<T: RngCore> TestRandom<T> for [u8; $len] {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for [u8; $len] {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut bytes = [0; $len];
|
||||
rng.fill_bytes(&mut bytes);
|
||||
bytes
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use crate::Address;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Address {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for Address {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut key_bytes = vec![0; 20];
|
||||
rng.fill_bytes(&mut key_bytes);
|
||||
Address::from_slice(&key_bytes[..])
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use bls::{AggregateSignature, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for AggregateSignature {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for AggregateSignature {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let signature = Signature::random_for_test(rng);
|
||||
let mut aggregate_signature = AggregateSignature::new();
|
||||
aggregate_signature.add(&signature);
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use crate::Bitfield;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Bitfield {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for Bitfield {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut raw_bytes = vec![0; 32];
|
||||
rng.fill_bytes(&mut raw_bytes);
|
||||
Bitfield::from_bytes(&raw_bytes)
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use crate::Hash256;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Hash256 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for Hash256 {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut key_bytes = vec![0; 32];
|
||||
rng.fill_bytes(&mut key_bytes);
|
||||
Hash256::from_slice(&key_bytes[..])
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use bls::{PublicKey, SecretKey};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for PublicKey {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for PublicKey {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let secret_key = SecretKey::random_for_test(rng);
|
||||
PublicKey::from_secret_key(&secret_key)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use bls::SecretKey;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for SecretKey {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for SecretKey {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let mut key_bytes = vec![0; 48];
|
||||
rng.fill_bytes(&mut key_bytes);
|
||||
/*
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use super::TestRandom;
|
||||
use super::*;
|
||||
use bls::{SecretKey, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Signature {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
impl TestRandom for Signature {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
let secret_key = SecretKey::random_for_test(rng);
|
||||
let mut message = vec![0; 32];
|
||||
rng.fill_bytes(&mut message);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::test_utils::TestingAttestationDataBuilder;
|
||||
use crate::*;
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
/// Builds an attestation to be used for testing purposes.
|
||||
///
|
||||
@@ -12,8 +12,8 @@ pub struct TestingAttestationBuilder {
|
||||
|
||||
impl TestingAttestationBuilder {
|
||||
/// Create a new attestation builder.
|
||||
pub fn new(
|
||||
state: &BeaconState,
|
||||
pub fn new<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
committee: &[usize],
|
||||
slot: Slot,
|
||||
shard: u64,
|
||||
@@ -74,7 +74,7 @@ impl TestingAttestationBuilder {
|
||||
data: self.attestation.data.clone(),
|
||||
custody_bit: false,
|
||||
}
|
||||
.hash_tree_root();
|
||||
.tree_hash_root();
|
||||
|
||||
let domain = spec.get_domain(
|
||||
self.attestation.data.slot.epoch(spec.slots_per_epoch),
|
||||
|
||||
@@ -10,7 +10,12 @@ pub struct TestingAttestationDataBuilder {
|
||||
impl TestingAttestationDataBuilder {
|
||||
/// Configures a new `AttestationData` which attests to all of the same parameters as the
|
||||
/// state.
|
||||
pub fn new(state: &BeaconState, shard: u64, slot: Slot, spec: &ChainSpec) -> Self {
|
||||
pub fn new<T: EthSpec>(
|
||||
state: &BeaconState<T>,
|
||||
shard: u64,
|
||||
slot: Slot,
|
||||
spec: &ChainSpec,
|
||||
) -> Self {
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
let previous_epoch = state.previous_epoch(spec);
|
||||
|
||||
@@ -25,22 +30,22 @@ impl TestingAttestationDataBuilder {
|
||||
|
||||
let target_root = if is_previous_epoch {
|
||||
*state
|
||||
.get_block_root(previous_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(previous_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap()
|
||||
} else {
|
||||
*state
|
||||
.get_block_root(current_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(current_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let source_root = *state
|
||||
.get_block_root(source_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(source_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap();
|
||||
|
||||
let data = AttestationData {
|
||||
// LMD GHOST vote
|
||||
slot,
|
||||
beacon_block_root: *state.get_block_root(slot, spec).unwrap(),
|
||||
beacon_block_root: *state.get_block_root(slot).unwrap(),
|
||||
|
||||
// FFG Vote
|
||||
source_epoch,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::*;
|
||||
use ssz::TreeHash;
|
||||
use tree_hash::TreeHash;
|
||||
|
||||
/// Builds an `AttesterSlashing`.
|
||||
///
|
||||
@@ -66,7 +66,7 @@ impl TestingAttesterSlashingBuilder {
|
||||
data: attestation.data.clone(),
|
||||
custody_bit: false,
|
||||
};
|
||||
let message = attestation_data_and_custody_bit.hash_tree_root();
|
||||
let message = attestation_data_and_custody_bit.tree_hash_root();
|
||||
|
||||
for (i, validator_index) in validator_indices.iter().enumerate() {
|
||||
attestation.custody_bitfield.set(i, false);
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
*,
|
||||
};
|
||||
use rayon::prelude::*;
|
||||
use ssz::{SignedRoot, TreeHash};
|
||||
use tree_hash::{SignedRoot, TreeHash};
|
||||
|
||||
/// Builds a beacon block to be used for testing purposes.
|
||||
///
|
||||
@@ -43,7 +43,7 @@ impl TestingBeaconBlockBuilder {
|
||||
/// Modifying the block's slot after signing may invalidate the signature.
|
||||
pub fn set_randao_reveal(&mut self, sk: &SecretKey, fork: &Fork, spec: &ChainSpec) {
|
||||
let epoch = self.block.slot.epoch(spec.slots_per_epoch);
|
||||
let message = epoch.hash_tree_root();
|
||||
let message = epoch.tree_hash_root();
|
||||
let domain = spec.get_domain(epoch, Domain::Randao, fork);
|
||||
self.block.body.randao_reveal = Signature::new(&message, domain, sk);
|
||||
}
|
||||
@@ -87,9 +87,9 @@ impl TestingBeaconBlockBuilder {
|
||||
///
|
||||
/// Note: the signed messages of the split committees will be identical -- it would be possible
|
||||
/// to aggregate these split attestations.
|
||||
pub fn insert_attestations(
|
||||
pub fn insert_attestations<T: EthSpec>(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
secret_keys: &[&SecretKey],
|
||||
num_attestations: usize,
|
||||
spec: &ChainSpec,
|
||||
@@ -176,11 +176,11 @@ impl TestingBeaconBlockBuilder {
|
||||
}
|
||||
|
||||
/// Insert a `Valid` deposit into the state.
|
||||
pub fn insert_deposit(
|
||||
pub fn insert_deposit<T: EthSpec>(
|
||||
&mut self,
|
||||
amount: u64,
|
||||
index: u64,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
let keypair = Keypair::random();
|
||||
@@ -198,9 +198,9 @@ impl TestingBeaconBlockBuilder {
|
||||
}
|
||||
|
||||
/// Insert a `Valid` exit into the state.
|
||||
pub fn insert_exit(
|
||||
pub fn insert_exit<T: EthSpec>(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
validator_index: u64,
|
||||
secret_key: &SecretKey,
|
||||
spec: &ChainSpec,
|
||||
@@ -219,9 +219,9 @@ impl TestingBeaconBlockBuilder {
|
||||
///
|
||||
/// Note: this will set the validator to be withdrawable by directly modifying the state
|
||||
/// validator registry. This _may_ cause problems historic hashes, etc.
|
||||
pub fn insert_transfer(
|
||||
pub fn insert_transfer<T: EthSpec>(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
state: &BeaconState<T>,
|
||||
from: u64,
|
||||
to: u64,
|
||||
amount: u64,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user