reth
reth copied to clipboard
feat: add BlockAnnounce trait for outbound block propagation
trafficstars
This PR adds a symmetric counterpart to BlockImport for announcing blocks to the network. This is useful for custom chains that want to leverage a singular p2p layer instead of running a separate consensus client.
Scope:
- BlockAnnounce trait with poll-based interface
- Integration with NetworkManager's event loop
- Builder API for easy configuration
- PoS safety check to prevent invalid propagation
- Optional by default, backward compatible
Example Usage:
use reth_network::announce::{BlockAnnounce, BlockAnnounceRequest, PropagationStrategy};
use tokio::sync::mpsc;
use std::task::{Context, Poll};
#[derive(Debug)]
struct MyCustomBlockAnnouncer {
rx: mpsc::UnboundedReceiver<(Block, B256)>,
}
impl BlockAnnounce<Block> for MyCustomBlockAnnouncer {
fn on_announced_block(&mut self, _block: Block) {
// do some stuff
}
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<BlockAnnounceRequest<Block>> {
match self.rx.poll_recv(cx) {
Poll::Ready(Some((block, hash))) => {
Poll::Ready(BlockAnnounceRequest::Announce {
block,
hash,
strategy: PropagationStrategy::Both, // Full block + hash
})
}
_ => Poll::Pending,
}
}
}
// Usage:
let (to_announcer, from_sender) = mpsc::unbounded_channel();
let announcer = Box::new(MyCustomBlockAnnouncer { from_sender });
let config = NetworkConfig::builder(secret_key)
.block_announce(announcer)
.build(client);
// When you produce a block:
to_announcer.send((new_block, block_hash)).unwrap();
// NetworkManager will poll and announce it automatically, and then call the `on_announced_block` callback