Merge remote-tracking branch 'origin/unstable' into tree-states

This commit is contained in:
Michael Sproul
2022-05-24 10:01:05 +10:00
237 changed files with 8506 additions and 3598 deletions

View File

@@ -4,10 +4,12 @@ use crate::{
VerifyBlockRoot,
};
use std::marker::PhantomData;
use types::{BeaconState, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
use types::{BeaconState, BlindedPayload, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
type PreBlockHook<'a, E, Error> =
Box<dyn FnMut(&mut BeaconState<E>, &SignedBeaconBlock<E>) -> Result<(), Error> + 'a>;
type PreBlockHook<'a, E, Error> = Box<
dyn FnMut(&mut BeaconState<E>, &SignedBeaconBlock<E, BlindedPayload<E>>) -> Result<(), Error>
+ 'a,
>;
type PostBlockHook<'a, E, Error> = PreBlockHook<'a, E, Error>;
type PreSlotHook<'a, E, Error> = Box<dyn FnMut(&mut BeaconState<E>) -> Result<(), Error> + 'a>;
type PostSlotHook<'a, E, Error> = Box<
@@ -155,7 +157,7 @@ where
fn get_state_root(
&mut self,
slot: Slot,
blocks: &[SignedBeaconBlock<E>],
blocks: &[SignedBeaconBlock<E, BlindedPayload<E>>],
i: usize,
) -> Result<Option<Hash256>, Error> {
// If a state root iterator is configured, use it to find the root.
@@ -189,7 +191,7 @@ where
/// after the blocks have been applied.
pub fn apply_blocks(
mut self,
blocks: Vec<SignedBeaconBlock<E>>,
blocks: Vec<SignedBeaconBlock<E, BlindedPayload<E>>>,
target_slot: Option<Slot>,
) -> Result<Self, Error> {
for (i, block) in blocks.iter().enumerate() {

View File

@@ -1,6 +1,9 @@
use std::marker::PhantomData;
use tree_hash::TreeHash;
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Hash256, SignedBeaconBlock, Slot};
use types::{
BeaconState, BeaconStateError, ChainSpec, EthSpec, ExecPayload, Hash256, SignedBeaconBlock,
Slot,
};
#[derive(Debug)]
pub struct ConsensusContext<T: EthSpec> {
@@ -61,9 +64,9 @@ impl<T: EthSpec> ConsensusContext<T> {
self
}
pub fn get_current_block_root(
pub fn get_current_block_root<Payload: ExecPayload<T>>(
&mut self,
block: &SignedBeaconBlock<T>,
block: &SignedBeaconBlock<T, Payload>,
) -> Result<Hash256, ContextError> {
self.check_slot(block.slot())?;

View File

@@ -88,9 +88,9 @@ pub enum VerifyBlockRoot {
/// re-calculating the root when it is already known. Note `block_root` should be equal to the
/// tree hash root of the block, NOT the signing root of the block. This function takes
/// care of mixing in the domain.
pub fn per_block_processing<T: EthSpec>(
pub fn per_block_processing<T: EthSpec, Payload: ExecPayload<T>>(
state: &mut BeaconState<T>,
signed_block: &SignedBeaconBlock<T>,
signed_block: &SignedBeaconBlock<T, Payload>,
block_signature_strategy: BlockSignatureStrategy,
verify_block_root: VerifyBlockRoot,
ctxt: &mut ConsensusContext<T>,
@@ -131,7 +131,13 @@ pub fn per_block_processing<T: EthSpec>(
BlockSignatureStrategy::VerifyRandao => VerifySignatures::False,
};
let proposer_index = process_block_header(state, block, verify_block_root, ctxt, spec)?;
let proposer_index = process_block_header(
state,
block.temporary_block_header(),
verify_block_root,
ctxt,
spec,
)?;
if verify_signatures.is_true() {
verify_block_signature(state, signed_block, ctxt, spec)?;
@@ -174,28 +180,28 @@ pub fn per_block_processing<T: EthSpec>(
/// Processes the block header, returning the proposer index.
pub fn process_block_header<T: EthSpec>(
state: &mut BeaconState<T>,
block: BeaconBlockRef<'_, T>,
block_header: BeaconBlockHeader,
verify_block_root: VerifyBlockRoot,
ctxt: &mut ConsensusContext<T>,
spec: &ChainSpec,
) -> Result<u64, BlockOperationError<HeaderInvalid>> {
// Verify that the slots match
verify!(
block.slot() == state.slot(),
block_header.slot == state.slot(),
HeaderInvalid::StateSlotMismatch
);
// Verify that the block is newer than the latest block header
verify!(
block.slot() > state.latest_block_header().slot,
block_header.slot > state.latest_block_header().slot,
HeaderInvalid::OlderThanLatestBlockHeader {
block_slot: block.slot(),
block_slot: block_header.slot,
latest_block_header_slot: state.latest_block_header().slot,
}
);
// Verify that proposer index is the correct index
let proposer_index = block.proposer_index();
let proposer_index = block_header.proposer_index;
let state_proposer_index = ctxt.get_proposer_index(state, spec)?;
verify!(
proposer_index == state_proposer_index,
@@ -208,15 +214,15 @@ pub fn process_block_header<T: EthSpec>(
if verify_block_root == VerifyBlockRoot::True {
let expected_previous_block_root = state.latest_block_header().tree_hash_root();
verify!(
block.parent_root() == expected_previous_block_root,
block_header.parent_root == expected_previous_block_root,
HeaderInvalid::ParentBlockRootMismatch {
state: expected_previous_block_root,
block: block.parent_root(),
block: block_header.parent_root,
}
);
}
*state.latest_block_header_mut() = block.temporary_block_header();
*state.latest_block_header_mut() = block_header;
// Verify proposer is not slashed
verify!(
@@ -224,15 +230,15 @@ pub fn process_block_header<T: EthSpec>(
HeaderInvalid::ProposerSlashed(proposer_index)
);
Ok(proposer_index)
Ok(proposer_index as u64)
}
/// Verifies the signature of a block.
///
/// Spec v0.12.1
pub fn verify_block_signature<T: EthSpec>(
pub fn verify_block_signature<T: EthSpec, Payload: ExecPayload<T>>(
state: &BeaconState<T>,
block: &SignedBeaconBlock<T>,
block: &SignedBeaconBlock<T, Payload>,
ctxt: &mut ConsensusContext<T>,
spec: &ChainSpec,
) -> Result<(), BlockOperationError<HeaderInvalid>> {
@@ -254,9 +260,9 @@ pub fn verify_block_signature<T: EthSpec>(
/// Verifies the `randao_reveal` against the block's proposer pubkey and updates
/// `state.latest_randao_mixes`.
pub fn process_randao<T: EthSpec>(
pub fn process_randao<T: EthSpec, Payload: ExecPayload<T>>(
state: &mut BeaconState<T>,
block: BeaconBlockRef<'_, T>,
block: BeaconBlockRef<'_, T, Payload>,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
@@ -318,34 +324,34 @@ pub fn get_new_eth1_data<T: EthSpec>(
/// Contains a partial set of checks from the `process_execution_payload` function:
///
/// https://github.com/ethereum/consensus-specs/blob/v1.1.5/specs/merge/beacon-chain.md#process_execution_payload
pub fn partially_verify_execution_payload<T: EthSpec>(
pub fn partially_verify_execution_payload<T: EthSpec, Payload: ExecPayload<T>>(
state: &BeaconState<T>,
payload: &ExecutionPayload<T>,
payload: &Payload,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
if is_merge_transition_complete(state) {
block_verify!(
payload.parent_hash == state.latest_execution_payload_header()?.block_hash,
payload.parent_hash() == state.latest_execution_payload_header()?.block_hash,
BlockProcessingError::ExecutionHashChainIncontiguous {
expected: state.latest_execution_payload_header()?.block_hash,
found: payload.parent_hash,
found: payload.parent_hash(),
}
);
}
block_verify!(
payload.prev_randao == *state.get_randao_mix(state.current_epoch())?,
payload.prev_randao() == *state.get_randao_mix(state.current_epoch())?,
BlockProcessingError::ExecutionRandaoMismatch {
expected: *state.get_randao_mix(state.current_epoch())?,
found: payload.prev_randao,
found: payload.prev_randao(),
}
);
let timestamp = compute_timestamp_at_slot(state, spec)?;
block_verify!(
payload.timestamp == timestamp,
payload.timestamp() == timestamp,
BlockProcessingError::ExecutionInvalidTimestamp {
expected: timestamp,
found: payload.timestamp,
found: payload.timestamp(),
}
);
@@ -359,29 +365,14 @@ pub fn partially_verify_execution_payload<T: EthSpec>(
/// Partially equivalent to the `process_execution_payload` function:
///
/// https://github.com/ethereum/consensus-specs/blob/v1.1.5/specs/merge/beacon-chain.md#process_execution_payload
pub fn process_execution_payload<T: EthSpec>(
pub fn process_execution_payload<T: EthSpec, Payload: ExecPayload<T>>(
state: &mut BeaconState<T>,
payload: &ExecutionPayload<T>,
payload: &Payload,
spec: &ChainSpec,
) -> Result<(), BlockProcessingError> {
partially_verify_execution_payload(state, payload, spec)?;
*state.latest_execution_payload_header_mut()? = ExecutionPayloadHeader {
parent_hash: payload.parent_hash,
fee_recipient: payload.fee_recipient,
state_root: payload.state_root,
receipts_root: payload.receipts_root,
logs_bloom: payload.logs_bloom.clone(),
prev_randao: payload.prev_randao,
block_number: payload.block_number,
gas_limit: payload.gas_limit,
gas_used: payload.gas_used,
timestamp: payload.timestamp,
extra_data: payload.extra_data.clone(),
base_fee_per_gas: payload.base_fee_per_gas,
block_hash: payload.block_hash,
transactions_root: payload.transactions.tree_hash_root(),
};
*state.latest_execution_payload_header_mut()? = payload.to_execution_payload_header();
Ok(())
}
@@ -398,24 +389,21 @@ pub fn is_merge_transition_complete<T: EthSpec>(state: &BeaconState<T>) -> bool
.unwrap_or(false)
}
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/beacon-chain.md#is_merge_transition_block
pub fn is_merge_transition_block<T: EthSpec>(
pub fn is_merge_transition_block<T: EthSpec, Payload: ExecPayload<T>>(
state: &BeaconState<T>,
body: BeaconBlockBodyRef<T>,
body: BeaconBlockBodyRef<T, Payload>,
) -> bool {
body.execution_payload()
.map(|payload| {
!is_merge_transition_complete(state) && *payload != <ExecutionPayload<T>>::default()
})
.map(|payload| !is_merge_transition_complete(state) && *payload != Payload::default())
.unwrap_or(false)
}
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/beacon-chain.md#is_execution_enabled
pub fn is_execution_enabled<T: EthSpec>(
pub fn is_execution_enabled<T: EthSpec, Payload: ExecPayload<T>>(
state: &BeaconState<T>,
body: BeaconBlockBodyRef<T>,
body: BeaconBlockBodyRef<T, Payload>,
) -> bool {
is_merge_transition_block(state, body) || is_merge_transition_complete(state)
}
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/merge/beacon-chain.md#compute_timestamp_at_slot
pub fn compute_timestamp_at_slot<T: EthSpec>(
state: &BeaconState<T>,

View File

@@ -7,7 +7,7 @@ use bls::{verify_signature_sets, PublicKey, PublicKeyBytes, SignatureSet};
use rayon::prelude::*;
use std::borrow::Cow;
use types::{
BeaconState, BeaconStateError, ChainSpec, EthSpec, Hash256, IndexedAttestation,
BeaconState, BeaconStateError, ChainSpec, EthSpec, ExecPayload, Hash256, IndexedAttestation,
SignedBeaconBlock,
};
@@ -117,11 +117,11 @@ where
/// contains invalid signatures on deposits._
///
/// See `Self::verify` for more detail.
pub fn verify_entire_block(
pub fn verify_entire_block<Payload: ExecPayload<T>>(
state: &'a BeaconState<T>,
get_pubkey: F,
decompressor: D,
block: &'a SignedBeaconBlock<T>,
block: &'a SignedBeaconBlock<T, Payload>,
block_root: Option<Hash256>,
spec: &'a ChainSpec,
) -> Result<()> {
@@ -131,9 +131,9 @@ where
}
/// Includes all signatures on the block (except the deposit signatures) for verification.
pub fn include_all_signatures(
pub fn include_all_signatures<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T>,
block: &'a SignedBeaconBlock<T, Payload>,
block_root: Option<Hash256>,
) -> Result<()> {
self.include_block_proposal(block, block_root)?;
@@ -144,9 +144,9 @@ where
/// Includes all signatures on the block (except the deposit signatures and the proposal
/// signature) for verification.
pub fn include_all_signatures_except_proposal(
pub fn include_all_signatures_except_proposal<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T>,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
self.include_randao_reveal(block)?;
self.include_proposer_slashings(block)?;
@@ -160,9 +160,9 @@ where
}
/// Includes the block signature for `self.block` for verification.
pub fn include_block_proposal(
pub fn include_block_proposal<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T>,
block: &'a SignedBeaconBlock<T, Payload>,
block_root: Option<Hash256>,
) -> Result<()> {
let set = block_proposal_signature_set(
@@ -177,7 +177,10 @@ where
}
/// Includes the randao signature for `self.block` for verification.
pub fn include_randao_reveal(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
pub fn include_randao_reveal<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
let set = randao_signature_set(
self.state,
self.get_pubkey.clone(),
@@ -189,7 +192,10 @@ where
}
/// Includes all signatures in `self.block.body.proposer_slashings` for verification.
pub fn include_proposer_slashings(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
pub fn include_proposer_slashings<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
self.sets
.sets
.reserve(block.message().body().proposer_slashings().len() * 2);
@@ -215,7 +221,10 @@ where
}
/// Includes all signatures in `self.block.body.attester_slashings` for verification.
pub fn include_attester_slashings(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
pub fn include_attester_slashings<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
self.sets
.sets
.reserve(block.message().body().attester_slashings().len() * 2);
@@ -241,9 +250,9 @@ where
}
/// Includes all signatures in `self.block.body.attestations` for verification.
pub fn include_attestations(
pub fn include_attestations<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T>,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<Vec<IndexedAttestation<T>>> {
self.sets
.sets
@@ -280,7 +289,10 @@ where
}
/// Includes all signatures in `self.block.body.voluntary_exits` for verification.
pub fn include_exits(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
pub fn include_exits<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
self.sets
.sets
.reserve(block.message().body().voluntary_exits().len());
@@ -301,7 +313,10 @@ where
}
/// Include the signature of the block's sync aggregate (if it exists) for verification.
pub fn include_sync_aggregate(&mut self, block: &'a SignedBeaconBlock<T>) -> Result<()> {
pub fn include_sync_aggregate<Payload: ExecPayload<T>>(
&mut self,
block: &'a SignedBeaconBlock<T, Payload>,
) -> Result<()> {
if let Ok(sync_aggregate) = block.message().body().sync_aggregate() {
if let Some(signature_set) = sync_aggregate_signature_set(
&self.decompressor,

View File

@@ -9,9 +9,9 @@ use safe_arith::SafeArith;
use std::sync::Arc;
use types::consts::altair::{PARTICIPATION_FLAG_WEIGHTS, PROPOSER_WEIGHT, WEIGHT_DENOMINATOR};
pub fn process_operations<'a, T: EthSpec>(
pub fn process_operations<'a, T: EthSpec, Payload: ExecPayload<T>>(
state: &mut BeaconState<T>,
block_body: BeaconBlockBodyRef<'a, T>,
block_body: BeaconBlockBodyRef<'a, T, Payload>,
proposer_index: u64,
verify_signatures: VerifySignatures,
spec: &ChainSpec,
@@ -220,9 +220,9 @@ pub fn process_attester_slashings<T: EthSpec>(
}
/// Wrapper function to handle calling the correct version of `process_attestations` based on
/// the fork.
pub fn process_attestations<'a, T: EthSpec>(
pub fn process_attestations<'a, T: EthSpec, Payload: ExecPayload<T>>(
state: &mut BeaconState<T>,
block_body: BeaconBlockBodyRef<'a, T>,
block_body: BeaconBlockBodyRef<'a, T, Payload>,
proposer_index: u64,
verify_signatures: VerifySignatures,
spec: &ChainSpec,

View File

@@ -8,10 +8,11 @@ use std::borrow::Cow;
use tree_hash::TreeHash;
use types::{
AggregateSignature, AttesterSlashing, BeaconBlockRef, BeaconState, BeaconStateError, ChainSpec,
DepositData, Domain, Epoch, EthSpec, Fork, Hash256, InconsistentFork, IndexedAttestation,
ProposerSlashing, PublicKey, PublicKeyBytes, Signature, SignedAggregateAndProof,
SignedBeaconBlock, SignedBeaconBlockHeader, SignedContributionAndProof, SignedRoot,
SignedVoluntaryExit, SigningData, Slot, SyncAggregate, SyncAggregatorSelectionData, Unsigned,
DepositData, Domain, Epoch, EthSpec, ExecPayload, Fork, Hash256, InconsistentFork,
IndexedAttestation, ProposerSlashing, PublicKey, PublicKeyBytes, Signature,
SignedAggregateAndProof, SignedBeaconBlock, SignedBeaconBlockHeader,
SignedContributionAndProof, SignedRoot, SignedVoluntaryExit, SigningData, Slot, SyncAggregate,
SyncAggregatorSelectionData, Unsigned,
};
pub type Result<T> = std::result::Result<T, Error>;
@@ -70,10 +71,10 @@ where
}
/// A signature set that is valid if a block was signed by the expected block producer.
pub fn block_proposal_signature_set<'a, T, F>(
pub fn block_proposal_signature_set<'a, T, F, Payload: ExecPayload<T>>(
state: &'a BeaconState<T>,
get_pubkey: F,
signed_block: &'a SignedBeaconBlock<T>,
signed_block: &'a SignedBeaconBlock<T, Payload>,
block_root: Option<Hash256>,
spec: &'a ChainSpec,
) -> Result<SignatureSet<'a>>
@@ -107,8 +108,8 @@ where
/// Unlike `block_proposal_signature_set` this does **not** check that the proposer index is
/// correct according to the shuffling. It should only be used if no suitable `BeaconState` is
/// available.
pub fn block_proposal_signature_set_from_parts<'a, T, F>(
signed_block: &'a SignedBeaconBlock<T>,
pub fn block_proposal_signature_set_from_parts<'a, T, F, Payload: ExecPayload<T>>(
signed_block: &'a SignedBeaconBlock<T, Payload>,
block_root: Option<Hash256>,
proposer_index: u64,
fork: &Fork,
@@ -151,10 +152,10 @@ where
}
/// A signature set that is valid if the block proposers randao reveal signature is correct.
pub fn randao_signature_set<'a, T, F>(
pub fn randao_signature_set<'a, T, F, Payload: ExecPayload<T>>(
state: &'a BeaconState<T>,
get_pubkey: F,
block: BeaconBlockRef<'a, T>,
block: BeaconBlockRef<'a, T, Payload>,
spec: &'a ChainSpec,
) -> Result<SignatureSet<'a>>
where

View File

@@ -63,7 +63,7 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
///
/// Spec v1.1.0
pub fn get_flag_index_deltas<T: EthSpec>(
deltas: &mut Vec<Delta>,
deltas: &mut [Delta],
state: &BeaconState<T>,
flag_index: usize,
total_active_balance: u64,
@@ -112,7 +112,7 @@ pub fn get_flag_weight(flag_index: usize) -> Result<u64, Error> {
}
pub fn get_inactivity_penalty_deltas<T: EthSpec>(
deltas: &mut Vec<Delta>,
deltas: &mut [Delta],
state: &BeaconState<T>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,