Skip to content

Commit

Permalink
More clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Feb 21, 2023
1 parent cc152b0 commit 518b100
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
4 changes: 3 additions & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 41 additions & 27 deletions beacon_node/beacon_chain/src/blob_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,8 +29,6 @@ use types::{
};
use types::{BeaconState, Blob, Epoch, ExecPayload, KzgProof};

pub type SendBlobError<E: EthSpec> = SendError<Arc<SignedBlobSidecar<E>>>;

#[derive(Debug)]
pub enum BlobError<E: EthSpec> {
/// The blob sidecar is from a slot that is later than the current slot (with respect to the
Expand Down Expand Up @@ -110,25 +108,30 @@ pub enum DataAvailabilityError<E: EthSpec> {
/// Receiving an available block from pending-availability blobs cache failed.
RecvBlobError(RecvBlobError),
/// Sending an available block from pending-availability blobs cache failed.
SendBlobError(SendBlobError<E>),
SendBlobError(SendError<Arc<SignedBlobSidecar<E>>>),
// 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)
}
}
};
}

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<SendError<Arc<SignedBlobSidecar<E>>>> for DataAvailabilityError<E> {
fn from(e: BeaconChainError) -> Self {
DataAvailabilityError::SendBlobError(e)
}
}

impl From<BlobReconstructionError> for BlobError {
impl<E: EthSpec> From<BlobReconstructionError> for BlobError<E> {
fn from(e: BlobReconstructionError) -> Self {
match e {
BlobReconstructionError::UnavailableBlobs => BlobError::UnavailableBlobs,
Expand Down Expand Up @@ -273,21 +276,23 @@ pub trait AsBlobSidecar<E: EthSpec> {
}

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<E: EthSpec> AsBlobSidecar<E> for Arc<SignedBlobSidecar<E>> {
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<E> {
self.message().blob()
}
impl_as_blob_sidecar_fn_for_signed_sidecar!(kzg_aggregated_proof, KzgProof);
}

#[derive(Copy, Clone)]
Expand All @@ -296,9 +301,18 @@ pub enum DataAvailabilityCheckRequired {
No,
}

impl<T: BeaconChainTypes> IntoAvailabilityPendingBlock<T> for GossipVerifiedBlock<T> {}
impl<T: BeaconChainTypes> IntoAvailabilityPendingBlock<T> for SignatureVerifiedBlock<T> {}
impl<T: BeaconChainTypes> IntoAvailabilityPendingBlock<T> for Arc<SignedBeaconBlock<T::EthSpec>> {}
impl<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> IntoAvailabilityPendingBlock<T, B>
for GossipVerifiedBlock<T>
{
}
impl<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> IntoAvailabilityPendingBlock<T, B>
for SignatureVerifiedBlock<T>
{
}
impl<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> IntoAvailabilityPendingBlock<T, B>
for Arc<SignedBeaconBlock<T::EthSpec>>
{
}

impl<T: BeaconChainTypes> IntoAvailabilityPendingBlock<T> for AvailabilityPendingBlock {
fn into_availablilty_pending_block(
Expand All @@ -309,11 +323,11 @@ impl<T: BeaconChainTypes> IntoAvailabilityPendingBlock<T> for AvailabilityPendin
self
}
}
pub trait IntoAvailabilityPendingBlock<T: BeaconChainTypes> {
pub trait IntoAvailabilityPendingBlock<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> {
/// 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<T>,
) -> AvailabilityPendingBlock<T::EthSpec> {
Expand Down Expand Up @@ -420,7 +434,7 @@ pub struct AvailabilityPendingBlock<E: EthSpec> {
}

impl<E: EthSpec> Future for AvailabilityPendingBlock<E> {
type Output = Result<AvailableBlock, BlobError>;
type Output = Result<AvailableBlock<E>, BlobError<E>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.data_availability_handle.poll()
}
Expand Down Expand Up @@ -477,14 +491,14 @@ impl<E: EthSpec> AvailableBlock<E> {
}

impl<E: EthSpec> TryInto<AvailableBlock<E>> for AvailableBlock<E> {
type Error = BlobError;
type Error = BlobError<E>;
fn try_into(self, chain: &BeaconChain<E>) -> Result<AvailableBlock<E>, Self::Error> {
Ok(self)
}
}

impl<E: EthSpec> TryInto<AvailableBlock<E>> for &AvailabilityPendingBlock<E> {
type Error = BlobError;
type Error = BlobError<E>;
fn try_into(self) -> Result<AvailableBlock<E>, Self::Error> {
match self.poll() {
Poll::Pending => Err(BlobError::PendingAvailability),
Expand Down
8 changes: 3 additions & 5 deletions beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub struct GossipVerifiedBlock<T: BeaconChainTypes> {

/// A wrapper around a `SignedBeaconBlock` that indicates that all signatures (except the deposit
/// signatures) have been verified.
pub struct SignatureVerifiedBlock<T: BeaconChainTypes, B: TryInto<AvailableBlock<T::EthSpec>>> {
pub struct SignatureVerifiedBlock<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> {
block: B,
block_root: Hash256,
parent: Option<PreProcessingSnapshot<T::EthSpec>>,
Expand All @@ -660,7 +660,7 @@ type PayloadVerificationHandle<E> =
/// 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<T: BeaconChainTypes, B: TryInto<AvailableBlock, T::EthSpec>> {
pub struct ExecutionPendingBlock<T: BeaconChainTypes, B: AsBlock<T::EthSpec>> {
pub block: B,
pub block_root: Hash256,
pub state: BeaconState<T::EthSpec>,
Expand All @@ -674,9 +674,7 @@ pub struct ExecutionPendingBlock<T: BeaconChainTypes, B: TryInto<AvailableBlock,
/// 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<T: BeaconChainTypes, B: TryInto<AvailableBlock, T::EthSpec>>:
Sized
{
pub trait IntoExecutionPendingBlock<T: BeaconChainTypes, B: AsBlock<T::EthSpec>>: Sized {
fn into_execution_pending_block(
self: B,
block_root: Hash256,
Expand Down

0 comments on commit 518b100

Please sign in to comment.