From 22f5fd316ec947f3871f20859b7bd8c6d60fb18a Mon Sep 17 00:00:00 2001 From: Sumit <106421807+startup-dreamer@users.noreply.github.com> Date: Thu, 20 Mar 2025 03:53:52 +0530 Subject: [PATCH] feat: add new `NewBlockEvent` enum for different types of block announcement events (#15133) --- crates/net/network/src/import.rs | 26 ++++++++++++++------ crates/net/network/src/manager.rs | 8 +++--- examples/bsc-p2p/src/block_import/mod.rs | 15 ++++++----- examples/bsc-p2p/src/block_import/service.rs | 2 +- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/crates/net/network/src/import.rs b/crates/net/network/src/import.rs index 498072e021..491fabba9a 100644 --- a/crates/net/network/src/import.rs +++ b/crates/net/network/src/import.rs @@ -1,6 +1,7 @@ //! This module provides an abstraction over block import in the form of the `BlockImport` trait. use crate::message::NewBlockMessage; +use reth_eth_wire_types::broadcast::NewBlockHashes; use reth_network_peers::PeerId; use std::{ error::Error, @@ -9,19 +10,30 @@ use std::{ /// Abstraction over block import. pub trait BlockImport: std::fmt::Debug + Send + Sync { - /// Invoked for a received `NewBlock` broadcast message from the peer. + /// Invoked for a received block announcement from the peer. /// + /// For a `NewBlock` message: /// > When a `NewBlock` announcement message is received from a peer, the client first verifies /// > the basic header validity of the block, checking whether the proof-of-work value is valid. /// - /// This is supposed to start verification. The results are then expected to be returned via - /// [`BlockImport::poll`]. - fn on_new_block(&mut self, peer_id: PeerId, incoming_block: NewBlockMessage); + /// For a `NewBlockHashes` message, hash announcement should be processed accordingly. + /// + /// The results are expected to be returned via [`BlockImport::poll`]. + fn on_new_block(&mut self, peer_id: PeerId, incoming_block: NewBlockEvent); /// Returns the results of a [`BlockImport::on_new_block`] fn poll(&mut self, cx: &mut Context<'_>) -> Poll>; } +/// Represents different types of block announcement events from the network. +#[derive(Debug, Clone)] +pub enum NewBlockEvent { + /// A new full block announcement + Block(NewBlockMessage), + /// Only the hashes of new blocks + Hashes(NewBlockHashes), +} + /// Represents different types of block import events #[derive(Debug)] pub enum BlockImportEvent { @@ -34,13 +46,13 @@ pub enum BlockImportEvent { /// Outcome of the [`BlockImport`]'s block handling. #[derive(Debug)] pub struct BlockImportOutcome { - /// Sender of the `NewBlock` message. + /// Sender of the block announcement message. pub peer: PeerId, /// The result after validating the block pub result: Result, BlockImportError>, } -/// Represents the successful validation of a received `NewBlock` message. +/// Represents the successful validation of a received block announcement. #[derive(Debug)] pub enum BlockValidation { /// Basic Header validity check, after which the block should be relayed to peers via a @@ -76,7 +88,7 @@ pub enum BlockImportError { pub struct ProofOfStakeBlockImport; impl BlockImport for ProofOfStakeBlockImport { - fn on_new_block(&mut self, _peer_id: PeerId, _incoming_block: NewBlockMessage) {} + fn on_new_block(&mut self, _peer_id: PeerId, _incoming_block: NewBlockEvent) {} fn poll(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Pending diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index bf1047caf4..d9daed4f53 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -21,7 +21,7 @@ use crate::{ discovery::Discovery, error::{NetworkError, ServiceKind}, eth_requests::IncomingEthRequest, - import::{BlockImport, BlockImportEvent, BlockImportOutcome, BlockValidation}, + import::{BlockImport, BlockImportEvent, BlockImportOutcome, BlockValidation, NewBlockEvent}, listener::ConnectionListener, message::{NewBlockMessage, PeerMessage}, metrics::{DisconnectMetrics, NetworkMetrics, NETWORK_POOL_TRANSACTIONS_SCOPE}, @@ -583,14 +583,16 @@ impl NetworkManager { PeerMessage::NewBlockHashes(hashes) => { self.within_pow_or_disconnect(peer_id, |this| { // update peer's state, to track what blocks this peer has seen - this.swarm.state_mut().on_new_block_hashes(peer_id, hashes.0) + this.swarm.state_mut().on_new_block_hashes(peer_id, hashes.0.clone()); + // start block import process for the hashes + this.block_import.on_new_block(peer_id, NewBlockEvent::Hashes(hashes)); }) } PeerMessage::NewBlock(block) => { self.within_pow_or_disconnect(peer_id, move |this| { this.swarm.state_mut().on_new_block(peer_id, block.hash); // start block import process - this.block_import.on_new_block(peer_id, block); + this.block_import.on_new_block(peer_id, NewBlockEvent::Block(block)); }); } PeerMessage::PooledTransactions(msg) => { diff --git a/examples/bsc-p2p/src/block_import/mod.rs b/examples/bsc-p2p/src/block_import/mod.rs index d03d2fdcd2..ba7820bd32 100644 --- a/examples/bsc-p2p/src/block_import/mod.rs +++ b/examples/bsc-p2p/src/block_import/mod.rs @@ -1,11 +1,11 @@ #![allow(unused)] use handle::ImportHandle; use reth_engine_primitives::EngineTypes; -use reth_network::import::BlockImport; +use reth_network::import::{BlockImport, BlockImportOutcome, NewBlockEvent}; use reth_network_peers::PeerId; use reth_payload_primitives::{BuiltPayload, PayloadTypes}; use reth_primitives::NodePrimitives; -use service::{BlockMsg, ImportEvent, Outcome}; +use service::{BlockMsg, BscBlock, ImportEvent, Outcome}; use std::{ fmt, task::{ready, Context, Poll}, @@ -25,12 +25,11 @@ impl BscBlockImport { } } -impl - BlockImport<<::Primitives as NodePrimitives>::Block> - for BscBlockImport -{ - fn on_new_block(&mut self, peer_id: PeerId, incoming_block: BlockMsg) { - let _ = self.handle.send_block(incoming_block, peer_id); +impl BlockImport> for BscBlockImport { + fn on_new_block(&mut self, peer_id: PeerId, incoming_block: NewBlockEvent>) { + if let NewBlockEvent::Block(block) = incoming_block { + let _ = self.handle.send_block(block, peer_id); + } } fn poll(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/examples/bsc-p2p/src/block_import/service.rs b/examples/bsc-p2p/src/block_import/service.rs index 5b47eb8d54..e5f61b2e6b 100644 --- a/examples/bsc-p2p/src/block_import/service.rs +++ b/examples/bsc-p2p/src/block_import/service.rs @@ -21,7 +21,7 @@ use std::{ use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; /// The block type for a given engine -type BscBlock = +pub type BscBlock = <<::BuiltPayload as BuiltPayload>::Primitives as NodePrimitives>::Block; /// Network message containing a new block