Skip to content

Commit f451fe8

Browse files
authored
feat: eth wire announce block (#298)
* eth wire announce block * refactor EthWireProivder trait * fix noop network api
1 parent 094dbb1 commit f451fe8

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use reth_primitives_traits::Block;
2+
use alloy_primitives::B256;
33
use reth_tokio_util::EventStream;
44
use tokio::sync::oneshot;
55

@@ -13,14 +13,14 @@ pub struct NewBlockWithPeer<B> {
1313
}
1414

1515
/// Provides a listener for new blocks on the eth wire protocol.
16-
pub trait EthWireBlockListenerProvider {
17-
/// The network primitives.
18-
type Block: Block;
19-
16+
pub trait EthWireProvider<N: NetworkPrimitives> {
2017
/// Create a new eth wire block listener.
2118
fn eth_wire_block_listener(
2219
&self,
2320
) -> impl Future<
24-
Output = Result<EventStream<NewBlockWithPeer<Self::Block>>, oneshot::error::RecvError>,
21+
Output = Result<EventStream<NewBlockWithPeer<N::Block>>, oneshot::error::RecvError>,
2522
> + Send;
23+
24+
/// Announce a new block to the network over the eth wire protocol.
25+
fn eth_wire_announce_block(&self, block: N::NewBlockPayload, hash: B256);
2626
}

crates/net/network-api/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod noop;
2424
pub mod block;
2525

2626
pub mod test_utils;
27-
use block::EthWireBlockListenerProvider;
27+
use block::EthWireProvider;
2828
use test_utils::PeersHandleProvider;
2929

3030
pub use alloy_rpc_types_admin::EthProtocolInfo;
@@ -58,7 +58,7 @@ pub trait FullNetwork:
5858
+ NetworkEventListenerProvider
5959
+ Peers
6060
+ PeersHandleProvider
61-
+ EthWireBlockListenerProvider<Block = <Self::Primitives as NetworkPrimitives>::Block>
61+
+ EthWireProvider<Self::Primitives>
6262
+ Clone
6363
+ Unpin
6464
+ 'static
@@ -73,7 +73,7 @@ impl<T> FullNetwork for T where
7373
+ NetworkEventListenerProvider
7474
+ Peers
7575
+ PeersHandleProvider
76-
+ EthWireBlockListenerProvider<Block = <Self::Primitives as NetworkPrimitives>::Block>
76+
+ EthWireProvider<Self::Primitives>
7777
+ Clone
7878
+ Unpin
7979
+ 'static

crates/net/network-api/src/noop.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{fmt, marker::PhantomData};
77
use std::net::{IpAddr, SocketAddr};
88

99
use crate::{
10-
block::{EthWireBlockListenerProvider, NewBlockWithPeer},
10+
block::{EthWireProvider, NewBlockWithPeer},
1111
events::{NetworkPeersEvents, PeerEventStream},
1212
test_utils::{PeersHandle, PeersHandleProvider},
1313
BlockDownloaderProvider, DiscoveryEvent, NetworkError, NetworkEvent,
@@ -201,12 +201,18 @@ where
201201
}
202202
}
203203

204-
impl<N: NetworkPrimitives> EthWireBlockListenerProvider for NoopNetwork<N> {
205-
type Block = N::Block;
206-
204+
impl<N: NetworkPrimitives> EthWireProvider<N> for NoopNetwork<N> {
207205
async fn eth_wire_block_listener(
208206
&self,
209-
) -> Result<EventStream<NewBlockWithPeer<Self::Block>>, RecvError> {
207+
) -> Result<EventStream<NewBlockWithPeer<N::Block>>, RecvError> {
208+
unreachable!()
209+
}
210+
211+
fn eth_wire_announce_block(
212+
&self,
213+
_block: <N as NetworkPrimitives>::NewBlockPayload,
214+
_hash: alloy_primitives::B256,
215+
) {
210216
unreachable!()
211217
}
212218
}

crates/net/network/src/network.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use reth_eth_wire::{
1414
};
1515
use reth_ethereum_forks::Head;
1616
use reth_network_api::{
17-
block::{EthWireBlockListenerProvider, NewBlockWithPeer},
17+
block::{EthWireProvider, NewBlockWithPeer},
1818
events::{NetworkPeersEvents, PeerEvent, PeerEventStream},
1919
test_utils::{PeersHandle, PeersHandleProvider},
2020
BlockDownloaderProvider, DiscoveryEvent, NetworkError, NetworkEvent,
@@ -225,16 +225,18 @@ impl<N: NetworkPrimitives> NetworkEventListenerProvider for NetworkHandle<N> {
225225
}
226226
}
227227

228-
impl<N: NetworkPrimitives> EthWireBlockListenerProvider for NetworkHandle<N> {
229-
type Block = <N as NetworkPrimitives>::Block;
230-
228+
impl<N: NetworkPrimitives> EthWireProvider<N> for NetworkHandle<N> {
231229
async fn eth_wire_block_listener(
232230
&self,
233-
) -> Result<EventStream<NewBlockWithPeer<Self::Block>>, oneshot::error::RecvError> {
231+
) -> Result<EventStream<NewBlockWithPeer<N::Block>>, oneshot::error::RecvError> {
234232
let (tx, rx) = oneshot::channel();
235233
self.send_message(NetworkHandleMessage::EthWireBlockListener(tx));
236234
rx.await
237235
}
236+
237+
fn eth_wire_announce_block(&self, block: N::NewBlockPayload, hash: B256) {
238+
self.announce_block(block, hash)
239+
}
238240
}
239241

240242
impl<N: NetworkPrimitives> NetworkProtocols for NetworkHandle<N> {

0 commit comments

Comments
 (0)