add intoavailableblock trait

This commit is contained in:
realbigsean
2023-02-01 14:11:42 -05:00
committed by Pawan Dhananjay
parent 110eaf92c4
commit c332bc272c
4 changed files with 36 additions and 22 deletions

View File

@@ -7,7 +7,7 @@ use crate::attester_cache::{AttesterCache, AttesterCacheKey};
use crate::beacon_proposer_cache::compute_proposer_duties_from_head; use crate::beacon_proposer_cache::compute_proposer_duties_from_head;
use crate::beacon_proposer_cache::BeaconProposerCache; use crate::beacon_proposer_cache::BeaconProposerCache;
use crate::blob_cache::BlobCache; use crate::blob_cache::BlobCache;
use crate::blob_verification::{AsBlock, AvailabilityPendingBlock, BlockWrapper}; use crate::blob_verification::{AsBlock, AvailabilityPendingBlock, AvailableBlock, BlockWrapper, IntoAvailableBlock};
use crate::block_times_cache::BlockTimesCache; use crate::block_times_cache::BlockTimesCache;
use crate::block_verification::{ use crate::block_verification::{
check_block_is_finalized_checkpoint_or_descendant, check_block_relevancy, get_block_root, check_block_is_finalized_checkpoint_or_descendant, check_block_relevancy, get_block_root,
@@ -2685,7 +2685,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// ///
/// Returns an `Err` if the given block was invalid, or an error was encountered during /// Returns an `Err` if the given block was invalid, or an error was encountered during
/// verification. /// verification.
pub async fn process_block<B: IntoExecutionPendingBlock<T>>( pub async fn process_block<A: IntoAvailableBlock, B: IntoExecutionPendingBlock<T, A>>(
self: &Arc<Self>, self: &Arc<Self>,
block_root: Hash256, block_root: Hash256,
unverified_block: B, unverified_block: B,
@@ -2764,9 +2764,9 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
/// ///
/// An error is returned if the block was unable to be imported. It may be partially imported /// An error is returned if the block was unable to be imported. It may be partially imported
/// (i.e., this function is not atomic). /// (i.e., this function is not atomic).
async fn import_execution_pending_block( async fn import_execution_pending_block<B: IntoAvailableBlock>(
self: Arc<Self>, self: Arc<Self>,
execution_pending_block: ExecutionPendingBlock<T>, execution_pending_block: ExecutionPendingBlock<T, B>,
count_unrealized: CountUnrealized, count_unrealized: CountUnrealized,
) -> Result<Hash256, BlockError<T::EthSpec>> { ) -> Result<Hash256, BlockError<T::EthSpec>> {
let ExecutionPendingBlock { let ExecutionPendingBlock {
@@ -2818,14 +2818,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
); );
} }
let available_block = block.into_available_block()?;
let chain = self.clone(); let chain = self.clone();
let block_hash = self let block_hash = self
.spawn_blocking_handle( .spawn_blocking_handle(
move || { move || {
chain.import_block( chain.import_block(
block, available_block,
block_root, block_root,
state, state,
confirmed_state_roots, confirmed_state_roots,
@@ -2851,7 +2851,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn import_block( fn import_block(
&self, &self,
signed_block: AvailabilityPendingBlock<T::EthSpec>, signed_block: AvailableBlock<T::EthSpec>,
block_root: Hash256, block_root: Hash256,
mut state: BeaconState<T::EthSpec>, mut state: BeaconState<T::EthSpec>,
confirmed_state_roots: Vec<Hash256>, confirmed_state_roots: Vec<Hash256>,

View File

@@ -4,6 +4,7 @@ use std::sync::Arc;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use crate::beacon_chain::{BeaconChain, BeaconChainTypes, MAXIMUM_GOSSIP_CLOCK_DISPARITY}; use crate::beacon_chain::{BeaconChain, BeaconChainTypes, MAXIMUM_GOSSIP_CLOCK_DISPARITY};
use crate::block_verification::PayloadVerificationOutcome;
use crate::{kzg_utils, BeaconChainError, BlockError}; use crate::{kzg_utils, BeaconChainError, BlockError};
use state_processing::per_block_processing::eip4844::eip4844::verify_kzg_commitments_against_transactions; use state_processing::per_block_processing::eip4844::eip4844::verify_kzg_commitments_against_transactions;
use types::signed_beacon_block::BlobReconstructionError; use types::signed_beacon_block::BlobReconstructionError;
@@ -13,7 +14,6 @@ use types::{
Transactions, Transactions,
}; };
use types::{Epoch, ExecPayload}; use types::{Epoch, ExecPayload};
use crate::block_verification::PayloadVerificationOutcome;
#[derive(Debug)] #[derive(Debug)]
pub enum BlobError { pub enum BlobError {
@@ -220,7 +220,9 @@ impl<T: BeaconChainTypes> BlockWrapper<T::EthSpec> {
} }
}); });
match self { match self {
BlockWrapper::Block(block) => AvailabilityPendingBlock::new(block, block_root, da_check_required), BlockWrapper::Block(block) => {
AvailabilityPendingBlock::new(block, block_root, da_check_required)
}
BlockWrapper::BlockAndBlobs(block, blobs_sidecar) => { BlockWrapper::BlockAndBlobs(block, blobs_sidecar) => {
if matches!(da_check_required, DataAvailabilityCheckRequired::Yes) { if matches!(da_check_required, DataAvailabilityCheckRequired::Yes) {
let kzg_commitments = block let kzg_commitments = block
@@ -251,15 +253,23 @@ impl<T: BeaconChainTypes> BlockWrapper<T::EthSpec> {
} }
} }
pub trait IntoAvailableBlock<T: BeaconChainTypes> {
fn into_available_block(
self,
block_root: Hash256,
chain: &BeaconChain<T>,
) -> Result<AvailableBlock<T::EthSpec>, BlobError>;
}
/// A wrapper over a [`SignedBeaconBlock`] or a [`SignedBeaconBlockAndBlobsSidecar`]. An /// A wrapper over a [`SignedBeaconBlock`] or a [`SignedBeaconBlockAndBlobsSidecar`]. An
/// `AvailableBlock` has passed any required data availability checks and should be used in /// `AvailableBlock` has passed any required data availability checks and should be used in
/// consensus. This newtype wraps `AvailableBlockInner` to ensure data availability checks /// consensus. This newtype wraps `AvailableBlockInner` to ensure data availability checks
/// cannot be circumvented on construction. /// cannot be circumvented on construction.
#[derive(Clone, Debug, Derivative)] #[derive(Clone, Debug, Derivative)]
#[derivative(PartialEq, Hash(bound = "E: EthSpec"))] #[derivative(PartialEq, Hash(bound = "E: EthSpec"))]
pub struct AvailabilityPendingBlock<E: EthSpec>{ pub struct AvailabilityPendingBlock<E: EthSpec> {
block: Arc<SignedBeaconBlock<E>>, block: Arc<SignedBeaconBlock<E>>,
data_availability_handle: DataAvailabilityHandle<E> data_availability_handle: DataAvailabilityHandle<E>,
} }
/// Used to await the result of data availability check. /// Used to await the result of data availability check.

View File

@@ -24,9 +24,6 @@
//! ▼ //! ▼
//! SignedBeaconBlock //! SignedBeaconBlock
//! | //! |
//! ▼
//! AvailableBlock
//! |
//! |--------------- //! |---------------
//! | | //! | |
//! | ▼ //! | ▼
@@ -52,8 +49,8 @@
#![allow(clippy::result_large_err)] #![allow(clippy::result_large_err)]
use crate::blob_verification::{ use crate::blob_verification::{
validate_blob_for_gossip, AsBlock, AvailabilityPendingBlock, BlobError, BlockWrapper, IntoAvailableBlock, validate_blob_for_gossip, AsBlock, AvailabilityPendingBlock, AvailableBlock, BlobError,
IntoBlockWrapper, BlockWrapper, IntoAvailableBlock, IntoBlockWrapper,
}; };
use crate::eth1_finalization_cache::Eth1FinalizationData; use crate::eth1_finalization_cache::Eth1FinalizationData;
use crate::execution_payload::{ use crate::execution_payload::{
@@ -668,7 +665,10 @@ type PayloadVerificationHandle<E> =
/// Note: a `ExecutionPendingBlock` is not _forever_ valid to be imported, it may later become invalid /// Note: a `ExecutionPendingBlock` is not _forever_ valid to be imported, it may later become invalid
/// due to finality or some other event. A `ExecutionPendingBlock` should be imported into the /// due to finality or some other event. A `ExecutionPendingBlock` should be imported into the
/// `BeaconChain` immediately after it is instantiated. /// `BeaconChain` immediately after it is instantiated.
pub struct ExecutionPendingBlock<T: BeaconChainTypes, B: IntoAvailablBlock> { pub struct ExecutionPendingBlock<
T: BeaconChainTypes,
B: IntoAvailablBlockk = AvailableBlock<T::EthSpec>,
> {
pub block: B, pub block: B,
pub block_root: Hash256, pub block_root: Hash256,
pub state: BeaconState<T::EthSpec>, pub state: BeaconState<T::EthSpec>,
@@ -682,13 +682,17 @@ pub struct ExecutionPendingBlock<T: BeaconChainTypes, B: IntoAvailablBlock> {
/// Implemented on types that can be converted into a `ExecutionPendingBlock`. /// Implemented on types that can be converted into a `ExecutionPendingBlock`.
/// ///
/// Used to allow functions to accept blocks at various stages of verification. /// Used to allow functions to accept blocks at various stages of verification.
pub trait IntoExecutionPendingBlock<T: BeaconChainTypes>: Sized { pub trait IntoExecutionPendingBlock<
T: BeaconChainTypes,
B: IntoAvailableBlock = AvailableBlock<T::EthSpec>,
>: Sized
{
fn into_execution_pending_block( fn into_execution_pending_block(
self, self,
block_root: Hash256, block_root: Hash256,
chain: &Arc<BeaconChain<T>>, chain: &Arc<BeaconChain<T>>,
notify_execution_layer: NotifyExecutionLayer, notify_execution_layer: NotifyExecutionLayer,
) -> Result<ExecutionPendingBlock<T>, BlockError<T::EthSpec>> { ) -> Result<ExecutionPendingBlock<T, B>, BlockError<T::EthSpec>> {
self.into_execution_pending_block_slashable(block_root, chain, notify_execution_layer) self.into_execution_pending_block_slashable(block_root, chain, notify_execution_layer)
.map(|execution_pending| { .map(|execution_pending| {
// Supply valid block to slasher. // Supply valid block to slasher.
@@ -706,7 +710,7 @@ pub trait IntoExecutionPendingBlock<T: BeaconChainTypes>: Sized {
block_root: Hash256, block_root: Hash256,
chain: &Arc<BeaconChain<T>>, chain: &Arc<BeaconChain<T>>,
notify_execution_layer: NotifyExecutionLayer, notify_execution_layer: NotifyExecutionLayer,
) -> Result<ExecutionPendingBlock<T>, BlockSlashInfo<BlockError<T::EthSpec>>>; ) -> Result<ExecutionPendingBlock<T, B>, BlockSlashInfo<BlockError<T::EthSpec>>>;
fn block(&self) -> &SignedBeaconBlock<T::EthSpec>; fn block(&self) -> &SignedBeaconBlock<T::EthSpec>;
} }

View File

@@ -1,4 +1,4 @@
use crate::blob_verification::AvailabilityPendingBlock; use crate::blob_verification::{AvailabilityPendingBlock, AvailableBlock};
use crate::{ use crate::{
attester_cache::{CommitteeLengths, Error}, attester_cache::{CommitteeLengths, Error},
metrics, metrics,
@@ -51,7 +51,7 @@ impl<E: EthSpec> EarlyAttesterCache<E> {
pub fn add_head_block( pub fn add_head_block(
&self, &self,
beacon_block_root: Hash256, beacon_block_root: Hash256,
block: AvailabilityPendingBlock<E>, block: AvailableBlock<E>,
proto_block: ProtoBlock, proto_block: ProtoBlock,
state: &BeaconState<E>, state: &BeaconState<E>,
spec: &ChainSpec, spec: &ChainSpec,