mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 12:11:59 +00:00
Merge conflicts from master
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::cached_beacon_state::CachedBeaconState;
|
||||
use log::trace;
|
||||
use state_processing::validate_attestation_without_signature;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use types::{
|
||||
beacon_state::BeaconStateError, AggregateSignature, Attestation, AttestationData, BeaconState,
|
||||
Bitfield, ChainSpec, FreeAttestation, Signature,
|
||||
AggregateSignature, Attestation, AttestationData, BeaconState, BeaconStateError, Bitfield,
|
||||
ChainSpec, FreeAttestation, Signature,
|
||||
};
|
||||
|
||||
const PHASE_0_CUSTODY_BIT: bool = false;
|
||||
@@ -42,21 +42,28 @@ pub enum Message {
|
||||
BadSignature,
|
||||
/// The given `slot` does not match the validators committee assignment.
|
||||
BadSlot,
|
||||
/// The given `shard` does not match the validators committee assignment.
|
||||
/// The given `shard` does not match the validators committee assignment, or is not included in
|
||||
/// a committee for the given slot.
|
||||
BadShard,
|
||||
/// Attestation is from the epoch prior to this, ignoring.
|
||||
TooOld,
|
||||
}
|
||||
|
||||
macro_rules! some_or_invalid {
|
||||
($expression: expr, $error: expr) => {
|
||||
match $expression {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
return Ok(Outcome {
|
||||
valid: false,
|
||||
message: $error,
|
||||
});
|
||||
}
|
||||
}
|
||||
macro_rules! valid_outcome {
|
||||
($error: expr) => {
|
||||
return Ok(Outcome {
|
||||
valid: true,
|
||||
message: $error,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! invalid_outcome {
|
||||
($error: expr) => {
|
||||
return Ok(Outcome {
|
||||
valid: false,
|
||||
message: $error,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -77,53 +84,63 @@ impl AttestationAggregator {
|
||||
/// - The signature is verified against that of the validator at `validator_index`.
|
||||
pub fn process_free_attestation(
|
||||
&mut self,
|
||||
cached_state: &CachedBeaconState,
|
||||
cached_state: &BeaconState,
|
||||
free_attestation: &FreeAttestation,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Outcome, BeaconStateError> {
|
||||
let (slot, shard, committee_index) = some_or_invalid!(
|
||||
cached_state.attestation_slot_and_shard_for_validator(
|
||||
free_attestation.validator_index as usize,
|
||||
spec,
|
||||
)?,
|
||||
Message::BadValidatorIndex
|
||||
let attestation_duties = match cached_state.attestation_slot_and_shard_for_validator(
|
||||
free_attestation.validator_index as usize,
|
||||
spec,
|
||||
) {
|
||||
Err(BeaconStateError::EpochCacheUninitialized(e)) => {
|
||||
panic!("Attempted to access unbuilt cache {:?}.", e)
|
||||
}
|
||||
Err(BeaconStateError::EpochOutOfBounds) => invalid_outcome!(Message::TooOld),
|
||||
Err(BeaconStateError::ShardOutOfBounds) => invalid_outcome!(Message::BadShard),
|
||||
Err(e) => return Err(e),
|
||||
Ok(None) => invalid_outcome!(Message::BadValidatorIndex),
|
||||
Ok(Some(attestation_duties)) => attestation_duties,
|
||||
};
|
||||
|
||||
let (slot, shard, committee_index) = attestation_duties;
|
||||
|
||||
trace!(
|
||||
"slot: {}, shard: {}, committee_index: {}, val_index: {}",
|
||||
slot,
|
||||
shard,
|
||||
committee_index,
|
||||
free_attestation.validator_index
|
||||
);
|
||||
|
||||
if free_attestation.data.slot != slot {
|
||||
return Ok(Outcome {
|
||||
valid: false,
|
||||
message: Message::BadSlot,
|
||||
});
|
||||
invalid_outcome!(Message::BadSlot);
|
||||
}
|
||||
if free_attestation.data.shard != shard {
|
||||
return Ok(Outcome {
|
||||
valid: false,
|
||||
message: Message::BadShard,
|
||||
});
|
||||
invalid_outcome!(Message::BadShard);
|
||||
}
|
||||
|
||||
let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT);
|
||||
|
||||
let validator_record = some_or_invalid!(
|
||||
cached_state
|
||||
.state
|
||||
.validator_registry
|
||||
.get(free_attestation.validator_index as usize),
|
||||
Message::BadValidatorIndex
|
||||
);
|
||||
let validator_record = match cached_state
|
||||
.validator_registry
|
||||
.get(free_attestation.validator_index as usize)
|
||||
{
|
||||
None => invalid_outcome!(Message::BadValidatorIndex),
|
||||
Some(validator_record) => validator_record,
|
||||
};
|
||||
|
||||
if !free_attestation.signature.verify(
|
||||
&signable_message,
|
||||
cached_state.state.fork.get_domain(
|
||||
cached_state.state.current_epoch(spec),
|
||||
spec.domain_attestation,
|
||||
),
|
||||
&validator_record.pubkey,
|
||||
) {
|
||||
return Ok(Outcome {
|
||||
valid: false,
|
||||
message: Message::BadSignature,
|
||||
});
|
||||
if !free_attestation
|
||||
.signature
|
||||
.verify(
|
||||
&signable_message,
|
||||
cached_state.fork.get_domain(
|
||||
cached_state.current_epoch(spec),
|
||||
spec.domain_attestation,
|
||||
),
|
||||
&validator_record.pubkey,
|
||||
)
|
||||
{
|
||||
invalid_outcome!(Message::BadSignature);
|
||||
}
|
||||
|
||||
if let Some(existing_attestation) = self.store.get(&signable_message) {
|
||||
@@ -133,15 +150,9 @@ impl AttestationAggregator {
|
||||
committee_index as usize,
|
||||
) {
|
||||
self.store.insert(signable_message, updated_attestation);
|
||||
Ok(Outcome {
|
||||
valid: true,
|
||||
message: Message::Aggregated,
|
||||
})
|
||||
valid_outcome!(Message::Aggregated);
|
||||
} else {
|
||||
Ok(Outcome {
|
||||
valid: true,
|
||||
message: Message::AggregationNotRequired,
|
||||
})
|
||||
valid_outcome!(Message::AggregationNotRequired);
|
||||
}
|
||||
} else {
|
||||
let mut aggregate_signature = AggregateSignature::new();
|
||||
@@ -155,10 +166,7 @@ impl AttestationAggregator {
|
||||
aggregate_signature,
|
||||
};
|
||||
self.store.insert(signable_message, new_attestation);
|
||||
Ok(Outcome {
|
||||
valid: true,
|
||||
message: Message::NewAttestationCreated,
|
||||
})
|
||||
valid_outcome!(Message::NewAttestationCreated);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
|
||||
use crate::cached_beacon_state::CachedBeaconState;
|
||||
use crate::checkpoint::CheckPoint;
|
||||
use db::{
|
||||
stores::{BeaconBlockStore, BeaconStateStore},
|
||||
@@ -15,10 +14,10 @@ use state_processing::{
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use types::{
|
||||
beacon_state::BeaconStateError,
|
||||
readers::{BeaconBlockReader, BeaconStateReader},
|
||||
AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Crosslink, Deposit,
|
||||
Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, Signature, Slot,
|
||||
AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, BeaconStateError, ChainSpec,
|
||||
Crosslink, Deposit, Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, RelativeEpoch,
|
||||
Signature, Slot,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -70,7 +69,6 @@ pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice> {
|
||||
canonical_head: RwLock<CheckPoint>,
|
||||
finalized_head: RwLock<CheckPoint>,
|
||||
pub state: RwLock<BeaconState>,
|
||||
pub cached_state: RwLock<CachedBeaconState>,
|
||||
pub spec: ChainSpec,
|
||||
pub fork_choice: RwLock<F>,
|
||||
}
|
||||
@@ -96,7 +94,7 @@ where
|
||||
return Err(Error::InsufficientValidators);
|
||||
}
|
||||
|
||||
let genesis_state = BeaconState::genesis(
|
||||
let mut genesis_state = BeaconState::genesis(
|
||||
genesis_time,
|
||||
initial_validator_deposits,
|
||||
latest_eth1_data,
|
||||
@@ -109,32 +107,32 @@ where
|
||||
let block_root = genesis_block.canonical_root();
|
||||
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;
|
||||
|
||||
let cached_state = RwLock::new(CachedBeaconState::from_beacon_state(
|
||||
genesis_state.clone(),
|
||||
spec.clone(),
|
||||
)?);
|
||||
|
||||
let finalized_head = RwLock::new(CheckPoint::new(
|
||||
genesis_block.clone(),
|
||||
block_root,
|
||||
// TODO: this is a memory waste; remove full clone.
|
||||
genesis_state.clone(),
|
||||
state_root,
|
||||
));
|
||||
let canonical_head = RwLock::new(CheckPoint::new(
|
||||
genesis_block.clone(),
|
||||
block_root,
|
||||
// TODO: this is a memory waste; remove full clone.
|
||||
genesis_state.clone(),
|
||||
state_root,
|
||||
));
|
||||
let attestation_aggregator = RwLock::new(AttestationAggregator::new());
|
||||
|
||||
genesis_state.build_epoch_cache(RelativeEpoch::Previous, &spec)?;
|
||||
genesis_state.build_epoch_cache(RelativeEpoch::Current, &spec)?;
|
||||
genesis_state.build_epoch_cache(RelativeEpoch::Next, &spec)?;
|
||||
|
||||
Ok(Self {
|
||||
block_store,
|
||||
state_store,
|
||||
slot_clock,
|
||||
attestation_aggregator,
|
||||
state: RwLock::new(genesis_state.clone()),
|
||||
cached_state,
|
||||
state: RwLock::new(genesis_state),
|
||||
finalized_head,
|
||||
canonical_head,
|
||||
spec,
|
||||
@@ -150,6 +148,10 @@ where
|
||||
new_beacon_state: BeaconState,
|
||||
new_beacon_state_root: Hash256,
|
||||
) {
|
||||
debug!(
|
||||
"Updating canonical head with block at slot: {}",
|
||||
new_beacon_block.slot
|
||||
);
|
||||
let mut head = self.canonical_head.write();
|
||||
head.update(
|
||||
new_beacon_block,
|
||||
@@ -288,7 +290,7 @@ where
|
||||
validator_index
|
||||
);
|
||||
if let Some((slot, shard, _committee)) = self
|
||||
.cached_state
|
||||
.state
|
||||
.read()
|
||||
.attestation_slot_and_shard_for_validator(validator_index, &self.spec)?
|
||||
{
|
||||
@@ -346,7 +348,7 @@ where
|
||||
let aggregation_outcome = self
|
||||
.attestation_aggregator
|
||||
.write()
|
||||
.process_free_attestation(&self.cached_state.read(), &free_attestation, &self.spec)?;
|
||||
.process_free_attestation(&self.state.read(), &free_attestation, &self.spec)?;
|
||||
|
||||
// return if the attestation is invalid
|
||||
if !aggregation_outcome.valid {
|
||||
@@ -496,17 +498,9 @@ where
|
||||
// TODO: this is a first-in-best-dressed scenario that is not ideal; fork_choice should be
|
||||
// run instead.
|
||||
if self.head().beacon_block_root == parent_block_root {
|
||||
self.update_canonical_head(
|
||||
block.clone(),
|
||||
block_root.clone(),
|
||||
state.clone(),
|
||||
state_root,
|
||||
);
|
||||
self.update_canonical_head(block.clone(), block_root, state.clone(), state_root);
|
||||
// Update the local state variable.
|
||||
*self.state.write() = state.clone();
|
||||
// Update the cached state variable.
|
||||
*self.cached_state.write() =
|
||||
CachedBeaconState::from_beacon_state(state.clone(), self.spec.clone())?;
|
||||
}
|
||||
|
||||
Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed))
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
use log::{debug, trace};
|
||||
use std::collections::HashMap;
|
||||
use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Epoch, Slot};
|
||||
|
||||
pub const CACHE_PREVIOUS: bool = false;
|
||||
pub const CACHE_CURRENT: bool = true;
|
||||
pub const CACHE_NEXT: bool = false;
|
||||
|
||||
pub type CrosslinkCommittees = Vec<(Vec<usize>, u64)>;
|
||||
pub type Shard = u64;
|
||||
pub type CommitteeIndex = u64;
|
||||
pub type AttestationDuty = (Slot, Shard, CommitteeIndex);
|
||||
pub type AttestationDutyMap = HashMap<u64, AttestationDuty>;
|
||||
|
||||
// TODO: CachedBeaconState is presently duplicating `BeaconState` and `ChainSpec`. This is a
|
||||
// massive memory waste, switch them to references.
|
||||
|
||||
pub struct CachedBeaconState {
|
||||
pub state: BeaconState,
|
||||
committees: Vec<Vec<CrosslinkCommittees>>,
|
||||
attestation_duties: Vec<AttestationDutyMap>,
|
||||
next_epoch: Epoch,
|
||||
current_epoch: Epoch,
|
||||
previous_epoch: Epoch,
|
||||
spec: ChainSpec,
|
||||
}
|
||||
|
||||
impl CachedBeaconState {
|
||||
pub fn from_beacon_state(
|
||||
state: BeaconState,
|
||||
spec: ChainSpec,
|
||||
) -> Result<Self, BeaconStateError> {
|
||||
let current_epoch = state.current_epoch(&spec);
|
||||
let previous_epoch = if current_epoch == spec.genesis_epoch {
|
||||
current_epoch
|
||||
} else {
|
||||
current_epoch.saturating_sub(1_u64)
|
||||
};
|
||||
let next_epoch = state.next_epoch(&spec);
|
||||
|
||||
let mut committees: Vec<Vec<CrosslinkCommittees>> = Vec::with_capacity(3);
|
||||
let mut attestation_duties: Vec<AttestationDutyMap> = Vec::with_capacity(3);
|
||||
|
||||
if CACHE_PREVIOUS {
|
||||
debug!("from_beacon_state: building previous epoch cache.");
|
||||
let cache = build_epoch_cache(&state, previous_epoch, &spec)?;
|
||||
committees.push(cache.committees);
|
||||
attestation_duties.push(cache.attestation_duty_map);
|
||||
} else {
|
||||
committees.push(vec![]);
|
||||
attestation_duties.push(HashMap::new());
|
||||
}
|
||||
if CACHE_CURRENT {
|
||||
debug!("from_beacon_state: building current epoch cache.");
|
||||
let cache = build_epoch_cache(&state, current_epoch, &spec)?;
|
||||
committees.push(cache.committees);
|
||||
attestation_duties.push(cache.attestation_duty_map);
|
||||
} else {
|
||||
committees.push(vec![]);
|
||||
attestation_duties.push(HashMap::new());
|
||||
}
|
||||
if CACHE_NEXT {
|
||||
debug!("from_beacon_state: building next epoch cache.");
|
||||
let cache = build_epoch_cache(&state, next_epoch, &spec)?;
|
||||
committees.push(cache.committees);
|
||||
attestation_duties.push(cache.attestation_duty_map);
|
||||
} else {
|
||||
committees.push(vec![]);
|
||||
attestation_duties.push(HashMap::new());
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
state,
|
||||
committees,
|
||||
attestation_duties,
|
||||
next_epoch,
|
||||
current_epoch,
|
||||
previous_epoch,
|
||||
spec,
|
||||
})
|
||||
}
|
||||
|
||||
fn slot_to_cache_index(&self, slot: Slot) -> Option<usize> {
|
||||
trace!("slot_to_cache_index: cache lookup");
|
||||
match slot.epoch(self.spec.epoch_length) {
|
||||
epoch if (epoch == self.previous_epoch) & CACHE_PREVIOUS => Some(0),
|
||||
epoch if (epoch == self.current_epoch) & CACHE_CURRENT => Some(1),
|
||||
epoch if (epoch == self.next_epoch) & CACHE_NEXT => Some(2),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `slot`, `shard` and `committee_index` for which a validator must produce an
|
||||
/// attestation.
|
||||
///
|
||||
/// Cached method.
|
||||
///
|
||||
/// Spec v0.2.0
|
||||
pub fn attestation_slot_and_shard_for_validator(
|
||||
&self,
|
||||
validator_index: usize,
|
||||
_spec: &ChainSpec,
|
||||
) -> Result<Option<(Slot, u64, u64)>, BeaconStateError> {
|
||||
// Get the result for this epoch.
|
||||
let cache_index = self
|
||||
.slot_to_cache_index(self.state.slot)
|
||||
.expect("Current epoch should always have a cache index.");
|
||||
|
||||
let duties = self.attestation_duties[cache_index]
|
||||
.get(&(validator_index as u64))
|
||||
.and_then(|tuple| Some(*tuple));
|
||||
|
||||
Ok(duties)
|
||||
}
|
||||
}
|
||||
|
||||
struct EpochCacheResult {
|
||||
committees: Vec<CrosslinkCommittees>,
|
||||
attestation_duty_map: AttestationDutyMap,
|
||||
}
|
||||
|
||||
fn build_epoch_cache(
|
||||
state: &BeaconState,
|
||||
epoch: Epoch,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<EpochCacheResult, BeaconStateError> {
|
||||
let mut epoch_committees: Vec<CrosslinkCommittees> =
|
||||
Vec::with_capacity(spec.epoch_length as usize);
|
||||
let mut attestation_duty_map: AttestationDutyMap = HashMap::new();
|
||||
|
||||
for slot in epoch.slot_iter(spec.epoch_length) {
|
||||
let slot_committees = state.get_crosslink_committees_at_slot(slot, false, spec)?;
|
||||
|
||||
for (committee, shard) in slot_committees {
|
||||
for (committee_index, validator_index) in committee.iter().enumerate() {
|
||||
attestation_duty_map.insert(
|
||||
*validator_index as u64,
|
||||
(slot, shard, committee_index as u64),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
epoch_committees.push(state.get_crosslink_committees_at_slot(slot, false, spec)?)
|
||||
}
|
||||
|
||||
Ok(EpochCacheResult {
|
||||
committees: epoch_committees,
|
||||
attestation_duty_map,
|
||||
})
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use types::{BeaconBlock, BeaconState, Hash256};
|
||||
|
||||
/// Represents some block and it's associated state. Generally, this will be used for tracking the
|
||||
/// head, justified head and finalized head.
|
||||
#[derive(PartialEq, Clone, Serialize)]
|
||||
#[derive(Clone, Serialize)]
|
||||
pub struct CheckPoint {
|
||||
pub beacon_block: BeaconBlock,
|
||||
pub beacon_block_root: Hash256,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
mod attestation_aggregator;
|
||||
mod beacon_chain;
|
||||
mod cached_beacon_state;
|
||||
mod checkpoint;
|
||||
|
||||
pub use self::beacon_chain::{BeaconChain, Error};
|
||||
pub use self::beacon_chain::{
|
||||
BeaconChain, BlockProcessingOutcome, Error, InvalidBlock, ValidBlock,
|
||||
};
|
||||
pub use self::checkpoint::CheckPoint;
|
||||
pub use fork_choice::{ForkChoice, ForkChoiceAlgorithm, ForkChoiceError};
|
||||
|
||||
Reference in New Issue
Block a user