From fe04d945cc2841877b326549bb3ef0ded952789d Mon Sep 17 00:00:00 2001 From: realbigsean Date: Thu, 10 Nov 2022 14:22:30 -0500 Subject: [PATCH] make signed block + sidecar consensus spec --- .../src/rpc/codec/ssz_snappy.rs | 4 ++-- .../lighthouse_network/src/rpc/methods.rs | 3 ++- .../src/service/api_types.rs | 3 ++- .../lighthouse_network/src/types/pubsub.rs | 10 ---------- beacon_node/network/src/router/processor.rs | 2 +- beacon_node/network/src/sync/manager.rs | 14 ++++++++++---- consensus/types/src/lib.rs | 2 ++ consensus/types/src/signed_block_and_blobs.rs | 18 ++++++++++++++++++ 8 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 consensus/types/src/signed_block_and_blobs.rs diff --git a/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs b/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs index 3c40fdf8b3..d016d85f74 100644 --- a/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs +++ b/beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs @@ -3,7 +3,7 @@ use crate::rpc::{ protocol::{Encoding, Protocol, ProtocolId, RPCError, Version, ERROR_TYPE_MAX, ERROR_TYPE_MIN}, }; use crate::rpc::{InboundRequest, OutboundRequest, RPCCodedResponse, RPCResponse}; -use crate::{rpc::methods::*, EnrSyncCommitteeBitfield}; +use crate::{rpc::methods::*, EnrSyncCommitteeBitfield, SignedBeaconBlockAndBlobsSidecar}; use libp2p::bytes::BytesMut; use snap::read::FrameDecoder; use snap::write::FrameEncoder; @@ -642,7 +642,7 @@ fn handle_v2_response( }, Protocol::BlobsByRange => match fork_name { ForkName::Eip4844 => Ok(Some(RPCResponse::BlobsByRange(Arc::new( - BlobsSidecar::from_ssz_bytes(decoded_buffer)?, + SignedBeaconBlockAndBlobsSidecar::from_ssz_bytes(decoded_buffer)?, )))), _ => Err(RPCError::ErrorResponse( RPCResponseErrorCode::InvalidRequest, diff --git a/beacon_node/lighthouse_network/src/rpc/methods.rs b/beacon_node/lighthouse_network/src/rpc/methods.rs index 62059610d2..49dc977ff1 100644 --- a/beacon_node/lighthouse_network/src/rpc/methods.rs +++ b/beacon_node/lighthouse_network/src/rpc/methods.rs @@ -14,6 +14,7 @@ use strum::IntoStaticStr; use superstruct::superstruct; use types::blobs_sidecar::BlobsSidecar; use types::{Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot}; +use crate::SignedBeaconBlockAndBlobsSidecar; /// Maximum number of blocks in a single request. pub type MaxRequestBlocks = U1024; @@ -258,7 +259,7 @@ pub enum RPCResponse { BlocksByRoot(Arc>), /// A response to a get BLOBS_BY_RANGE request - BlobsByRange(Arc>), + BlobsByRange(Arc>), /// A PONG response to a PING request. Pong(Ping), diff --git a/beacon_node/lighthouse_network/src/service/api_types.rs b/beacon_node/lighthouse_network/src/service/api_types.rs index 46af7ddb22..190b86f0a6 100644 --- a/beacon_node/lighthouse_network/src/service/api_types.rs +++ b/beacon_node/lighthouse_network/src/service/api_types.rs @@ -11,6 +11,7 @@ use crate::rpc::{ }, OutboundRequest, SubstreamId, }; +use crate::SignedBeaconBlockAndBlobsSidecar; /// Identifier of requests sent by a peer. pub type PeerRequestId = (ConnectionId, SubstreamId); @@ -69,7 +70,7 @@ pub enum Response { /// A response to a get BLOCKS_BY_RANGE request. A None response signals the end of the batch. BlocksByRange(Option>>), /// A response to a get BLOBS_BY_RANGE request. A None response signals the end of the batch. - BlobsByRange(Option>>), + BlobsByRange(Option>>), /// A response to a get BLOCKS_BY_ROOT request. BlocksByRoot(Option>>), } diff --git a/beacon_node/lighthouse_network/src/types/pubsub.rs b/beacon_node/lighthouse_network/src/types/pubsub.rs index 1b14c93c09..172194816d 100644 --- a/beacon_node/lighthouse_network/src/types/pubsub.rs +++ b/beacon_node/lighthouse_network/src/types/pubsub.rs @@ -18,16 +18,6 @@ use types::{ SignedContributionAndProof, SignedVoluntaryExit, SubnetId, SyncCommitteeMessage, SyncSubnetId, }; -/// TODO(pawan): move this to consensus/types? strictly not a consensus type -#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, PartialEq)] -#[serde(bound = "T: EthSpec")] -pub struct SignedBeaconBlockAndBlobsSidecar { - // TODO(pawan): switch to a SignedBeaconBlock and use ssz offsets for decoding to make this - // future proof? - pub beacon_block: SignedBeaconBlockEip4844, - pub blobs_sidecar: BlobsSidecar, -} - #[derive(Debug, Clone, PartialEq)] pub enum PubsubMessage { /// Gossipsub message providing notification of a new block. diff --git a/beacon_node/network/src/router/processor.rs b/beacon_node/network/src/router/processor.rs index dadaf60c1e..2452d1826c 100644 --- a/beacon_node/network/src/router/processor.rs +++ b/beacon_node/network/src/router/processor.rs @@ -220,7 +220,7 @@ impl Processor { &mut self, peer_id: PeerId, request_id: RequestId, - blob_wrapper: Option>>, + blob_wrapper: Option>>, ) { trace!( self.log, diff --git a/beacon_node/network/src/sync/manager.rs b/beacon_node/network/src/sync/manager.rs index 9286695902..ecaf573206 100644 --- a/beacon_node/network/src/sync/manager.rs +++ b/beacon_node/network/src/sync/manager.rs @@ -45,7 +45,7 @@ use beacon_chain::{BeaconChain, BeaconChainTypes, BlockError, EngineState}; use futures::StreamExt; use lighthouse_network::rpc::methods::MAX_REQUEST_BLOCKS; use lighthouse_network::types::{NetworkGlobals, SyncState}; -use lighthouse_network::SyncInfo; +use lighthouse_network::{SignedBeaconBlockAndBlobsSidecar, SyncInfo}; use lighthouse_network::{PeerAction, PeerId}; use slog::{crit, debug, error, info, trace, Logger}; use std::boxed::Box; @@ -97,7 +97,7 @@ pub enum SyncMessage { RpcBlob { peer_id: PeerId, request_id: RequestId, - blob_sidecar: Option>>, + blob_sidecar: Option>>, seen_timestamp: Duration, }, @@ -592,8 +592,14 @@ impl SyncManager { .block_lookups .parent_chain_processed(chain_hash, result, &mut self.network), }, - //FIXME(sean) - SyncMessage::RpcBlob { .. } => todo!(), + SyncMessage::RpcBlob { + peer_id, + request_id, + blob_sidecar, + seen_timestamp, + } => { + self.rpc_block_received(request_id, peer_id, beacon_block, seen_timestamp); + }, } } diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index eecfb27c48..55da9cab0e 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -97,6 +97,7 @@ pub mod sqlite; pub mod blobs_sidecar; pub mod kzg_commitment; pub mod kzg_proof; +pub mod signed_block_and_blobs; use ethereum_types::{H160, H256}; @@ -149,6 +150,7 @@ pub use crate::historical_batch::HistoricalBatch; pub use crate::indexed_attestation::IndexedAttestation; pub use crate::kzg_commitment::KzgCommitment; pub use crate::kzg_proof::KzgProof; +pub use crate::signed_block_and_blobs::SignedBeaconBlockAndBlobsSidecar; pub use crate::participation_flags::ParticipationFlags; pub use crate::participation_list::ParticipationList; pub use crate::payload::{ diff --git a/consensus/types/src/signed_block_and_blobs.rs b/consensus/types/src/signed_block_and_blobs.rs new file mode 100644 index 0000000000..35b7bfdda0 --- /dev/null +++ b/consensus/types/src/signed_block_and_blobs.rs @@ -0,0 +1,18 @@ +use serde_derive::{Deserialize, Serialize}; +use ssz_derive::{Decode, Encode}; +use tree_hash_derive::TreeHash; +use crate::{BlobsSidecar, EthSpec, SignedBeaconBlock}; + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, TreeHash, PartialEq)] +#[serde(bound = "T: EthSpec")] +pub struct SignedBeaconBlockAndBlobsSidecar { + pub beacon_block: SignedBeaconBlock, + pub blobs_sidecar: BlobsSidecar, +} + +impl SignedBeaconBlockAndBlobsSidecar { + /// SSZ decode with fork variant determined by slot. + pub fn from_ssz_bytes(bytes: &[u8], spec: &ChainSpec) -> Result { + SignedBeaconBlock::from_ssz_bytes(bytes, spec) + } +} \ No newline at end of file