From 88109cc973553b18ef2f99efaf07afb5ee3a9f28 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 21 Feb 2023 20:40:07 +0100 Subject: [PATCH] More clean up --- beacon_node/beacon_chain/src/beacon_chain.rs | 6 +- .../beacon_chain/src/blob_verification.rs | 70 +++++++++++-------- .../beacon_chain/src/block_verification.rs | 12 ++-- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index da885139166..6c3309850e7 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -7,7 +7,9 @@ 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, AvailableBlock, BlobError, BlockWrapper, ExecutedBlock}; +use crate::blob_verification::{ + AsBlock, AvailableBlock, BlobError, BlockWrapper, ExecutedBlock, IntoAvailabilityPendingBlock, +}; use crate::block_times_cache::BlockTimesCache; use crate::block_verification::{ check_block_is_finalized_descendant, check_block_relevancy, get_block_root, @@ -2725,7 +2727,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, B: IntoAvailabilityPendingBlock>( self: &Arc, block_root: Hash256, unverified_block: B, diff --git a/beacon_node/beacon_chain/src/blob_verification.rs b/beacon_node/beacon_chain/src/blob_verification.rs index 228eea4c75b..2ec7a2c321d 100644 --- a/beacon_node/beacon_chain/src/blob_verification.rs +++ b/beacon_node/beacon_chain/src/blob_verification.rs @@ -6,7 +6,7 @@ use derivative::Derivative; use fork_choice::{CountUnrealized, PayloadVerificationStatus}; use futures::channel::{ mpsc, - mpsc::{error::RecvError as RecvBlobError, error::SendError}, + mpsc::{RecvError as RecvBlobError, SendError}, }; use kzg::Kzg; use slot_clock::SlotClock; @@ -29,8 +29,6 @@ use types::{ }; use types::{BeaconState, Blob, Epoch, ExecPayload, KzgProof}; -pub type SendBlobError = SendError>>; - #[derive(Debug)] pub enum BlobError { /// The blob sidecar is from a slot that is later than the current slot (with respect to the @@ -110,13 +108,13 @@ pub enum DataAvailabilityError { /// Receiving an available block from pending-availability blobs cache failed. RecvBlobError(RecvBlobError), /// Sending an available block from pending-availability blobs cache failed. - SendBlobError(SendBlobError), + SendBlobError(SendError>>), // todo(emhane): move kzg error here to take care of blob or block } macro_rules! impl_from_error { - ($error: ident, $parent_error: ident) => { - impl From<$error> for $parent_error { + ($error: ident, $parent_error: ident, $generic: ident) => { + impl From<$error> for $parent_error<$generic> { fn from(e: $error) -> Self { Self::$error(e) } @@ -124,11 +122,16 @@ macro_rules! impl_from_error { }; } -impl_from_error!(TimedOut, DataAvailabilityError); -impl_from_error!(RecvBlobError, DataAvailabilityError); -impl_from_error!(SendBlobError, DataAvailabilityError); +impl_from_error!(TimedOut, DataAvailabilityError, EthSpec); +impl_from_error!(RecvBlobError, DataAvailabilityError, EthSpec); + +impl From>>> for DataAvailabilityError { + fn from(e: BeaconChainError) -> Self { + DataAvailabilityError::SendBlobError(e) + } +} -impl From for BlobError { +impl From for BlobError { fn from(e: BlobReconstructionError) -> Self { match e { BlobReconstructionError::UnavailableBlobs => BlobError::UnavailableBlobs, @@ -273,21 +276,23 @@ pub trait AsBlobSidecar { } macro_rules! impl_as_blob_sidecar_fn_for_signed_sidecar { - ($fn_name: ident, $return_type: ident, $self: ident) => { + ($fn_name: ident, $return_type: ident) => { fn $fn_name() -> $return_type { - $self.message().$fn_name() + Self::message().$fn_name() } }; } impl AsBlobSidecar for Arc> { - impl_as_blob_sidecar_fn_for_signed_sidecar!(beacon_block_root, Hash256, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(beacon_block_slot, Slot, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(proposer_index, u64, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(block_parent_root, Hash256, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(blob_index, u64, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(blob, Blob, Self); - impl_as_blob_sidecar_fn_for_signed_sidecar!(kzg_aggregated_proof, KzgProof, Self); + impl_as_blob_sidecar_fn_for_signed_sidecar!(beacon_block_root, Hash256); + impl_as_blob_sidecar_fn_for_signed_sidecar!(beacon_block_slot, Slot); + impl_as_blob_sidecar_fn_for_signed_sidecar!(proposer_index, u64); + impl_as_blob_sidecar_fn_for_signed_sidecar!(block_parent_root, Hash256); + impl_as_blob_sidecar_fn_for_signed_sidecar!(blob_index, u64); + fn blob(&self) -> Blob { + self.message().blob() + } + impl_as_blob_sidecar_fn_for_signed_sidecar!(kzg_aggregated_proof, KzgProof); } #[derive(Copy, Clone)] @@ -296,11 +301,20 @@ pub enum DataAvailabilityCheckRequired { No, } -impl IntoAvailabilityPendingBlock for GossipVerifiedBlock {} -impl IntoAvailabilityPendingBlock for SignatureVerifiedBlock {} -impl IntoAvailabilityPendingBlock for Arc> {} +impl> IntoAvailabilityPendingBlock + for GossipVerifiedBlock +{ +} +impl> IntoAvailabilityPendingBlock + for SignatureVerifiedBlock +{ +} +impl> IntoAvailabilityPendingBlock + for Arc> +{ +} -impl IntoAvailabilityPendingBlock for AvailabilityPendingBlock { +impl> IntoAvailabilityPendingBlock for AvailabilityPendingBlock { fn into_availablilty_pending_block( self, block_root: Hash256, @@ -309,11 +323,11 @@ impl IntoAvailabilityPendingBlock for AvailabilityPendin self } } -pub trait IntoAvailabilityPendingBlock { +pub trait IntoAvailabilityPendingBlock> { /// Takes a receiver as param, on which the availability-pending block receives kzg-verified /// blobs. fn into_availablilty_pending_block( - self, + self: B, block_root: Hash256, chain: &BeaconChain, ) -> AvailabilityPendingBlock { @@ -420,7 +434,7 @@ pub struct AvailabilityPendingBlock { } impl Future for AvailabilityPendingBlock { - type Output = Result; + type Output = Result, BlobError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { self.data_availability_handle.poll() } @@ -477,14 +491,14 @@ impl AvailableBlock { } impl TryInto> for AvailableBlock { - type Error = BlobError; + type Error = BlobError; fn try_into(self, chain: &BeaconChain) -> Result, Self::Error> { Ok(self) } } impl TryInto> for &AvailabilityPendingBlock { - type Error = BlobError; + type Error = BlobError; fn try_into(self) -> Result, Self::Error> { match self.poll() { Poll::Pending => Err(BlobError::PendingAvailability), diff --git a/beacon_node/beacon_chain/src/block_verification.rs b/beacon_node/beacon_chain/src/block_verification.rs index 5758846b854..df197d07a41 100644 --- a/beacon_node/beacon_chain/src/block_verification.rs +++ b/beacon_node/beacon_chain/src/block_verification.rs @@ -628,8 +628,8 @@ pub fn signature_verify_chain_segment( /// the p2p network. #[derive(Derivative)] #[derivative(Debug(bound = "T: BeaconChainTypes"))] -pub struct GossipVerifiedBlock { - pub block: AvailabilityPendingBlock, +pub struct GossipVerifiedBlock> { + pub block: B, pub block_root: Hash256, parent: Option>, consensus_context: ConsensusContext, @@ -637,7 +637,7 @@ pub struct GossipVerifiedBlock { /// A wrapper around a `SignedBeaconBlock` that indicates that all signatures (except the deposit /// signatures) have been verified. -pub struct SignatureVerifiedBlock>> { +pub struct SignatureVerifiedBlock> { block: B, block_root: Hash256, parent: Option>, @@ -660,7 +660,7 @@ 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> { pub block: B, pub block_root: Hash256, pub state: BeaconState, @@ -674,9 +674,7 @@ pub struct ExecutionPendingBlock>: - Sized -{ +pub trait IntoExecutionPendingBlock>: Sized { fn into_execution_pending_block( self: B, block_root: Hash256,