Update BeaconChain
for spec v0.2.0
This commit is contained in:
parent
36f441c968
commit
5fefc79521
@ -20,4 +20,5 @@ serde_derive = "1.0"
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||||
ssz = { path = "../../eth2/utils/ssz" }
|
ssz = { path = "../../eth2/utils/ssz" }
|
||||||
|
state_processing = { path = "../../eth2/state_processing" }
|
||||||
types = { path = "../../eth2/types" }
|
types = { path = "../../eth2/types" }
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use state_processing::validate_attestation_without_signature;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use types::{
|
use types::{
|
||||||
beacon_state::CommitteesError, AggregateSignature, Attestation, AttestationData, BeaconState,
|
beacon_state::CommitteesError, AggregateSignature, Attestation, AttestationData, BeaconState,
|
||||||
@ -172,9 +173,7 @@ impl AttestationAggregator {
|
|||||||
self.store
|
self.store
|
||||||
.values()
|
.values()
|
||||||
.filter_map(|attestation| {
|
.filter_map(|attestation| {
|
||||||
if state
|
if validate_attestation_without_signature(&state, attestation, spec).is_ok()
|
||||||
.validate_attestation_without_signature(attestation, spec)
|
|
||||||
.is_ok()
|
|
||||||
&& !known_attestation_data.contains(&attestation.data)
|
&& !known_attestation_data.contains(&attestation.data)
|
||||||
{
|
{
|
||||||
Some(attestation.clone())
|
Some(attestation.clone())
|
||||||
|
@ -1,24 +1,25 @@
|
|||||||
use db::{
|
|
||||||
stores::{BeaconBlockStore, BeaconStateStore},
|
|
||||||
ClientDB, DBError,
|
|
||||||
};
|
|
||||||
use genesis::{genesis_beacon_block, genesis_beacon_state};
|
|
||||||
use log::{debug, trace};
|
|
||||||
use parking_lot::{RwLock, RwLockReadGuard};
|
|
||||||
use slot_clock::SlotClock;
|
|
||||||
use ssz::ssz_encode;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use types::{
|
|
||||||
beacon_state::{BlockProcessingError, CommitteesError, SlotProcessingError},
|
|
||||||
readers::{BeaconBlockReader, BeaconStateReader},
|
|
||||||
AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Eth1Data,
|
|
||||||
FreeAttestation, Hash256, PublicKey, Signature, Slot,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
|
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
|
||||||
use crate::attestation_targets::AttestationTargets;
|
use crate::attestation_targets::AttestationTargets;
|
||||||
use crate::block_graph::BlockGraph;
|
use crate::block_graph::BlockGraph;
|
||||||
use crate::checkpoint::CheckPoint;
|
use crate::checkpoint::CheckPoint;
|
||||||
|
use db::{
|
||||||
|
stores::{BeaconBlockStore, BeaconStateStore},
|
||||||
|
ClientDB, DBError,
|
||||||
|
};
|
||||||
|
use log::{debug, trace};
|
||||||
|
use parking_lot::{RwLock, RwLockReadGuard};
|
||||||
|
use slot_clock::SlotClock;
|
||||||
|
use ssz::ssz_encode;
|
||||||
|
use state_processing::{
|
||||||
|
BlockProcessable, BlockProcessingError, SlotProcessable, SlotProcessingError,
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use types::{
|
||||||
|
beacon_state::CommitteesError,
|
||||||
|
readers::{BeaconBlockReader, BeaconStateReader},
|
||||||
|
AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Crosslink, Deposit,
|
||||||
|
Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, Signature, Slot,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@ -82,17 +83,25 @@ where
|
|||||||
state_store: Arc<BeaconStateStore<T>>,
|
state_store: Arc<BeaconStateStore<T>>,
|
||||||
block_store: Arc<BeaconBlockStore<T>>,
|
block_store: Arc<BeaconBlockStore<T>>,
|
||||||
slot_clock: U,
|
slot_clock: U,
|
||||||
|
genesis_time: u64,
|
||||||
|
latest_eth1_data: Eth1Data,
|
||||||
|
initial_validator_deposits: Vec<Deposit>,
|
||||||
spec: ChainSpec,
|
spec: ChainSpec,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
if spec.initial_validators.is_empty() {
|
if initial_validator_deposits.is_empty() {
|
||||||
return Err(Error::InsufficientValidators);
|
return Err(Error::InsufficientValidators);
|
||||||
}
|
}
|
||||||
|
|
||||||
let genesis_state = genesis_beacon_state(&spec);
|
let genesis_state = BeaconState::genesis(
|
||||||
|
genesis_time,
|
||||||
|
initial_validator_deposits,
|
||||||
|
latest_eth1_data,
|
||||||
|
&spec,
|
||||||
|
);
|
||||||
let state_root = genesis_state.canonical_root();
|
let state_root = genesis_state.canonical_root();
|
||||||
state_store.put(&state_root, &ssz_encode(&genesis_state)[..])?;
|
state_store.put(&state_root, &ssz_encode(&genesis_state)[..])?;
|
||||||
|
|
||||||
let genesis_block = genesis_beacon_block(state_root, &spec);
|
let genesis_block = BeaconBlock::genesis(state_root, &spec);
|
||||||
let block_root = genesis_block.canonical_root();
|
let block_root = genesis_block.canonical_root();
|
||||||
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;
|
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;
|
||||||
|
|
||||||
@ -225,19 +234,6 @@ where
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of slots the validator has been required to propose.
|
|
||||||
///
|
|
||||||
/// Returns `None` if the `validator_index` is invalid.
|
|
||||||
///
|
|
||||||
/// Information is retrieved from the present `beacon_state.validator_registry`.
|
|
||||||
pub fn proposer_slots(&self, validator_index: usize) -> Option<u64> {
|
|
||||||
if let Some(validator) = self.state.read().validator_registry.get(validator_index) {
|
|
||||||
Some(validator.proposer_slots)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reads the slot clock, returns `None` if the slot is unavailable.
|
/// Reads the slot clock, returns `None` if the slot is unavailable.
|
||||||
///
|
///
|
||||||
/// The slot might be unavailable due to an error with the system clock, or if the present time
|
/// The slot might be unavailable due to an error with the system clock, or if the present time
|
||||||
@ -277,8 +273,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the justified slot for the present state.
|
/// Returns the justified slot for the present state.
|
||||||
pub fn justified_slot(&self) -> Slot {
|
pub fn justified_epoch(&self) -> Epoch {
|
||||||
self.state.read().justified_slot
|
self.state.read().justified_epoch
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the attestation slot and shard for a given validator index.
|
/// Returns the attestation slot and shard for a given validator index.
|
||||||
@ -302,11 +298,14 @@ where
|
|||||||
|
|
||||||
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
|
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
|
||||||
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
|
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
|
||||||
let justified_slot = self.justified_slot();
|
let justified_epoch = self.justified_epoch();
|
||||||
let justified_block_root = self
|
let justified_block_root = self
|
||||||
.state
|
.state
|
||||||
.read()
|
.read()
|
||||||
.get_block_root(justified_slot, &self.spec)
|
.get_block_root(
|
||||||
|
justified_epoch.start_slot(self.spec.epoch_length),
|
||||||
|
&self.spec,
|
||||||
|
)
|
||||||
.ok_or_else(|| Error::BadRecentBlockRoots)?
|
.ok_or_else(|| Error::BadRecentBlockRoots)?
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
@ -326,8 +325,11 @@ where
|
|||||||
beacon_block_root: self.head().beacon_block_root.clone(),
|
beacon_block_root: self.head().beacon_block_root.clone(),
|
||||||
epoch_boundary_root,
|
epoch_boundary_root,
|
||||||
shard_block_root: Hash256::zero(),
|
shard_block_root: Hash256::zero(),
|
||||||
latest_crosslink_root: Hash256::zero(),
|
latest_crosslink: Crosslink {
|
||||||
justified_slot,
|
epoch: self.state.read().slot.epoch(self.spec.epoch_length),
|
||||||
|
shard_block_root: Hash256::zero(),
|
||||||
|
},
|
||||||
|
justified_epoch,
|
||||||
justified_block_root,
|
justified_block_root,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -550,11 +552,8 @@ where
|
|||||||
signature: self.spec.empty_signature.clone(), // To be completed by a validator.
|
signature: self.spec.empty_signature.clone(), // To be completed by a validator.
|
||||||
body: BeaconBlockBody {
|
body: BeaconBlockBody {
|
||||||
proposer_slashings: vec![],
|
proposer_slashings: vec![],
|
||||||
casper_slashings: vec![],
|
attester_slashings: vec![],
|
||||||
attestations: attestations,
|
attestations: attestations,
|
||||||
custody_reseeds: vec![],
|
|
||||||
custody_challenges: vec![],
|
|
||||||
custody_responses: vec![],
|
|
||||||
deposits: vec![],
|
deposits: vec![],
|
||||||
exits: vec![],
|
exits: vec![],
|
||||||
},
|
},
|
||||||
|
@ -68,8 +68,10 @@ where
|
|||||||
.into_beacon_state()
|
.into_beacon_state()
|
||||||
.ok_or(Error::InvalidBeaconState(start_state_root))?;
|
.ok_or(Error::InvalidBeaconState(start_state_root))?;
|
||||||
|
|
||||||
let active_validator_indices =
|
let active_validator_indices = get_active_validator_indices(
|
||||||
get_active_validator_indices(&state.validator_registry, start.slot());
|
&state.validator_registry,
|
||||||
|
start.slot().epoch(self.spec.epoch_length),
|
||||||
|
);
|
||||||
|
|
||||||
let mut attestation_targets = Vec::with_capacity(active_validator_indices.len());
|
let mut attestation_targets = Vec::with_capacity(active_validator_indices.len());
|
||||||
for i in active_validator_indices {
|
for i in active_validator_indices {
|
||||||
|
@ -2,6 +2,9 @@ mod block_processable;
|
|||||||
mod epoch_processable;
|
mod epoch_processable;
|
||||||
mod slot_processable;
|
mod slot_processable;
|
||||||
|
|
||||||
pub use block_processable::{BlockProcessable, Error as BlockProcessingError};
|
pub use block_processable::{
|
||||||
|
validate_attestation, validate_attestation_without_signature, BlockProcessable,
|
||||||
|
Error as BlockProcessingError,
|
||||||
|
};
|
||||||
pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError};
|
pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError};
|
||||||
pub use slot_processable::{Error as SlotProcessingError, SlotProcessable};
|
pub use slot_processable::{Error as SlotProcessingError, SlotProcessable};
|
||||||
|
Loading…
Reference in New Issue
Block a user