From c332bc272c4cfc5fe628c671f17a5c8ba2054d5e Mon Sep 17 00:00:00 2001 From: realbigsean Date: Wed, 1 Feb 2023 14:11:42 -0500 Subject: [PATCH] add intoavailableblock trait --- beacon_node/beacon_chain/src/beacon_chain.rs | 14 ++++++------ .../beacon_chain/src/blob_verification.rs | 18 +++++++++++---- .../beacon_chain/src/block_verification.rs | 22 +++++++++++-------- .../beacon_chain/src/early_attester_cache.rs | 4 ++-- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 9632e6e9dd..f1ac907f61 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -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::BeaconProposerCache; 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_verification::{ check_block_is_finalized_checkpoint_or_descendant, check_block_relevancy, get_block_root, @@ -2685,7 +2685,7 @@ impl BeaconChain { /// /// Returns an `Err` if the given block was invalid, or an error was encountered during /// verification. - pub async fn process_block>( + pub async fn process_block>( self: &Arc, block_root: Hash256, unverified_block: B, @@ -2764,9 +2764,9 @@ impl BeaconChain { /// /// An error is returned if the block was unable to be imported. It may be partially imported /// (i.e., this function is not atomic). - async fn import_execution_pending_block( + async fn import_execution_pending_block( self: Arc, - execution_pending_block: ExecutionPendingBlock, + execution_pending_block: ExecutionPendingBlock, count_unrealized: CountUnrealized, ) -> Result> { let ExecutionPendingBlock { @@ -2818,14 +2818,14 @@ impl BeaconChain { ); } - + let available_block = block.into_available_block()?; let chain = self.clone(); let block_hash = self .spawn_blocking_handle( move || { chain.import_block( - block, + available_block, block_root, state, confirmed_state_roots, @@ -2851,7 +2851,7 @@ impl BeaconChain { #[allow(clippy::too_many_arguments)] fn import_block( &self, - signed_block: AvailabilityPendingBlock, + signed_block: AvailableBlock, block_root: Hash256, mut state: BeaconState, confirmed_state_roots: Vec, diff --git a/beacon_node/beacon_chain/src/blob_verification.rs b/beacon_node/beacon_chain/src/blob_verification.rs index 4a1e255870..6a88a3312d 100644 --- a/beacon_node/beacon_chain/src/blob_verification.rs +++ b/beacon_node/beacon_chain/src/blob_verification.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use tokio::task::JoinHandle; use crate::beacon_chain::{BeaconChain, BeaconChainTypes, MAXIMUM_GOSSIP_CLOCK_DISPARITY}; +use crate::block_verification::PayloadVerificationOutcome; use crate::{kzg_utils, BeaconChainError, BlockError}; use state_processing::per_block_processing::eip4844::eip4844::verify_kzg_commitments_against_transactions; use types::signed_beacon_block::BlobReconstructionError; @@ -13,7 +14,6 @@ use types::{ Transactions, }; use types::{Epoch, ExecPayload}; -use crate::block_verification::PayloadVerificationOutcome; #[derive(Debug)] pub enum BlobError { @@ -220,7 +220,9 @@ impl BlockWrapper { } }); 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) => { if matches!(da_check_required, DataAvailabilityCheckRequired::Yes) { let kzg_commitments = block @@ -251,15 +253,23 @@ impl BlockWrapper { } } +pub trait IntoAvailableBlock { + fn into_available_block( + self, + block_root: Hash256, + chain: &BeaconChain, + ) -> Result, BlobError>; +} + /// A wrapper over a [`SignedBeaconBlock`] or a [`SignedBeaconBlockAndBlobsSidecar`]. An /// `AvailableBlock` has passed any required data availability checks and should be used in /// consensus. This newtype wraps `AvailableBlockInner` to ensure data availability checks /// cannot be circumvented on construction. #[derive(Clone, Debug, Derivative)] #[derivative(PartialEq, Hash(bound = "E: EthSpec"))] -pub struct AvailabilityPendingBlock{ +pub struct AvailabilityPendingBlock { block: Arc>, - data_availability_handle: DataAvailabilityHandle + data_availability_handle: DataAvailabilityHandle, } /// Used to await the result of data availability check. diff --git a/beacon_node/beacon_chain/src/block_verification.rs b/beacon_node/beacon_chain/src/block_verification.rs index b68e906640..a6bce27c1b 100644 --- a/beacon_node/beacon_chain/src/block_verification.rs +++ b/beacon_node/beacon_chain/src/block_verification.rs @@ -24,9 +24,6 @@ //! ▼ //! SignedBeaconBlock //! | -//! ▼ -//! AvailableBlock -//! | //! |--------------- //! | | //! | ▼ @@ -52,8 +49,8 @@ #![allow(clippy::result_large_err)] use crate::blob_verification::{ - validate_blob_for_gossip, AsBlock, AvailabilityPendingBlock, BlobError, BlockWrapper, IntoAvailableBlock, - IntoBlockWrapper, + validate_blob_for_gossip, AsBlock, AvailabilityPendingBlock, AvailableBlock, BlobError, + BlockWrapper, IntoAvailableBlock, IntoBlockWrapper, }; use crate::eth1_finalization_cache::Eth1FinalizationData; use crate::execution_payload::{ @@ -668,7 +665,10 @@ type PayloadVerificationHandle = /// 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 /// `BeaconChain` immediately after it is instantiated. -pub struct ExecutionPendingBlock { +pub struct ExecutionPendingBlock< + T: BeaconChainTypes, + B: IntoAvailablBlockk = AvailableBlock, +> { pub block: B, pub block_root: Hash256, pub state: BeaconState, @@ -682,13 +682,17 @@ pub struct ExecutionPendingBlock { /// Implemented on types that can be converted into a `ExecutionPendingBlock`. /// /// Used to allow functions to accept blocks at various stages of verification. -pub trait IntoExecutionPendingBlock: Sized { +pub trait IntoExecutionPendingBlock< + T: BeaconChainTypes, + B: IntoAvailableBlock = AvailableBlock, +>: Sized +{ fn into_execution_pending_block( self, block_root: Hash256, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, BlockError> { + ) -> Result, BlockError> { self.into_execution_pending_block_slashable(block_root, chain, notify_execution_layer) .map(|execution_pending| { // Supply valid block to slasher. @@ -706,7 +710,7 @@ pub trait IntoExecutionPendingBlock: Sized { block_root: Hash256, chain: &Arc>, notify_execution_layer: NotifyExecutionLayer, - ) -> Result, BlockSlashInfo>>; + ) -> Result, BlockSlashInfo>>; fn block(&self) -> &SignedBeaconBlock; } diff --git a/beacon_node/beacon_chain/src/early_attester_cache.rs b/beacon_node/beacon_chain/src/early_attester_cache.rs index 04d741819a..2df1a4f05b 100644 --- a/beacon_node/beacon_chain/src/early_attester_cache.rs +++ b/beacon_node/beacon_chain/src/early_attester_cache.rs @@ -1,4 +1,4 @@ -use crate::blob_verification::AvailabilityPendingBlock; +use crate::blob_verification::{AvailabilityPendingBlock, AvailableBlock}; use crate::{ attester_cache::{CommitteeLengths, Error}, metrics, @@ -51,7 +51,7 @@ impl EarlyAttesterCache { pub fn add_head_block( &self, beacon_block_root: Hash256, - block: AvailabilityPendingBlock, + block: AvailableBlock, proto_block: ProtoBlock, state: &BeaconState, spec: &ChainSpec,