Skip to content

Conversation

itsdevbear
Copy link
Contributor

@itsdevbear itsdevbear commented Oct 10, 2025

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

itsdevbear and others added 3 commits October 9, 2025 21:09
Adds symmetric counterpart to BlockImport for announcing blocks to the network.
This enables PoW chains, development scenarios, and L2s to propagate blocks
produced at the execution layer.

Key features:
- 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Moved BlockAnnounce trait, event types, and implementations to a dedicated
announce.rs module for better code organization. Added comprehensive unit
tests to verify:

- ProofOfStakeBlockAnnounce always returns Pending
- Mock BlockAnnounce returns blocks in FIFO order
- BlockAnnounceEvent structure works correctly
- Multiple blocks can be queued and polled sequentially

Tests use a noop waker pattern with proper lifetime management.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added block_announce field to NetworkConfig and NetworkConfigBuilder to enable
configuration of block announcement via the config pattern. This provides an
alternative to the builder pattern for setting up block propagation.

Changes:
- Added block_announce field to NetworkConfig struct
- Added block_announce field to NetworkConfigBuilder
- Added block_announce() setter method to NetworkConfigBuilder
- Updated NetworkManager::new() to extract and use block_announce from config
- Cleaned up imports to use top-level imports instead of inline crate:: paths

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
itsdevbear and others added 6 commits October 9, 2025 21:36
Updated the block announcement handler to follow proper Ethereum block
propagation patterns:
- Announce full block to √n peers (for quick propagation)
- Announce block hash to all peers (for network-wide awareness)

Also updated documentation to be more generic ("custom chains" instead
of specifically "PoW chains") to better reflect the broader use cases.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Implemented flexible block propagation with strategy enum:
- PropagationStrategy::FullBlock - announce full block to √n peers
- PropagationStrategy::HashOnly - announce hash to all peers
- PropagationStrategy::Both (default) - full block to √n + hash to all

Changes:
- Added PropagationStrategy enum with Default trait
- Updated BlockAnnounceRequest to single Announce variant with strategy field
- Removed redundant AnnounceHash variant
- Updated handler to match on strategy and propagate accordingly
- Made block_announce required (Box<dyn>) with ProofOfStakeBlockAnnounce default
- Updated all tests to use new structure with strategy field
- Removed unused imports

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Added detailed example showing:
- How to implement BlockAnnounce with a channel-based approach
- Using PropagationStrategy to control block propagation
- Configuration via NetworkConfig builder
- Sending blocks for announcement

Example demonstrates the most common pattern for integrating
block production with network propagation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@itsdevbear itsdevbear force-pushed the feat/block-announce-support branch from 51e8179 to 805e858 Compare October 10, 2025 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant