Skip to content

Conversation

noot
Copy link
Contributor

@noot noot commented Sep 24, 2025

📝 Summary

  • implement p2p crate which implements a simple libp2p node
  • p2p node has the ability to support various stream protocols and broadcasting to all peers on a protocol
  • note: this is not gossip; messages will only be sent to directly-connected peers. we currently assume a very small builder network (<10 nodes) so direct connectivity is okay, but for larger networks, this will need to be extended.
  • update flashblocks payload builder to broadcast newly built flashblock payloads over p2p
  • implement payload handler to handle incoming flashblock payloads from peers
  • NOTE: syncing and execution of flashblocks is not yet implemented; it will be added to the payload handler in a follow up PR.

future extensions:

  • connection management/max peer count/peer persistence
  • flashblock sync and execution
  • test fallback building (if one builder fails in the middle of a block after building some flashblocks, can another builder take over flashblock building?) be out of scope for this PR, as the main goal of this PR is to ensure flashblock sync over p2p.

💡 Motivation and Context

see https://hackmd.io/@nZ-twauPRISEa6G9zg3XRw/H1fekrwolx

✅ I have completed the following steps:

  • Run make lint
  • Run make test
  • Added tests (if applicable)

Copy link
Contributor

@akundaz akundaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are you handling the race conditions p2p introduces? Like if two builder build different flashblocks or something like that.

Not really sure what's happening in the p2p crate, please make sure it's documented well so that others can contribute easily

loop {
match reader.next().await {
Some(Ok(str)) => {
let payload: M = serde_json::from_str(&str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't deserialization happen in op-rbuilder code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i originally had it there, i was debating if the serialization format should be internal or external to the p2p layer. i'm going to refactor this to not be json anyways. could change the p2p layer back to just handle Bytes instead of Message if you prefer that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, i'll look again after you refactor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better now that it's over Message, just fix the error messages about flashblocks

@noot
Copy link
Contributor Author

noot commented Sep 30, 2025

How are you handling the race conditions p2p introduces? Like if two builder build different flashblocks or something like that.

out of scope for this PR, i am assuming the high availability setup will be used where op-conductor will manage which party is building at a specific time.

Not really sure what's happening in the p2p crate, please make sure it's documented well so that others can contribute easily

yes will add! PR is still draft, not ready for review yet tbh, will ping you again when it's fully ready.

&self.evm_config
}

pub(super) fn into_op_payload_builder_ctx(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not for conversions between the types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm probably going to remove this function after cleanup

@noot noot marked this pull request as ready for review October 6, 2025 00:15
@noot noot requested review from SozinM and karim-agha as code owners October 6, 2025 00:15
@noot noot changed the title feat: implement p2p layer for flashblocks feat: implement p2p layer and broadcast flashblocks Oct 6, 2025
@noot noot requested review from akundaz and avalonche October 6, 2025 00:22
@Copilot Copilot AI review requested due to automatic review settings October 14, 2025 22:17
@noot noot requested a review from 0x416e746f6e as a code owner October 14, 2025 22:17
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a P2P layer for flashblocks using libp2p to enable broadcasting and receiving flashblock payloads between builder nodes. The implementation adds a new p2p crate with basic networking capabilities and integrates it into the flashblocks payload builder to broadcast newly built payloads.

  • Implements P2P crate with libp2p node supporting stream protocols and broadcasting
  • Updates flashblocks payload builder to broadcast payloads over P2P instead of just WebSocket
  • Adds payload handler to manage incoming P2P flashblock messages and route them to the payload builder service

Reviewed Changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/p2p/src/lib.rs Core P2P node implementation with libp2p networking, message handling, and stream management
crates/p2p/src/outgoing.rs Outgoing stream handler for broadcasting messages to connected peers
crates/p2p/src/behaviour.rs libp2p network behavior configuration with protocols like identify, ping, mDNS
crates/op-rbuilder/src/builders/flashblocks/service.rs Integration of P2P node into flashblocks service with payload handler
crates/op-rbuilder/src/builders/flashblocks/payload_handler.rs Handler for routing built payloads and incoming P2P messages
crates/op-rbuilder/src/builders/flashblocks/payload.rs Updated payload builder to send payloads via channel instead of direct engine calls
crates/op-rbuilder/src/builders/flashblocks/p2p.rs P2P message types and conversions for flashblock payloads
crates/op-rbuilder/src/args/op.rs Added CLI arguments for P2P configuration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 14, 2025 22:24
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 15 out of 16 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 14, 2025 22:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 15 out of 16 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Copilot Copilot AI review requested due to automatic review settings October 14, 2025 22:48
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 15 out of 16 changed files in this pull request and generated 5 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

{
let private_key_hex = std::fs::read_to_string(private_key_file)
.wrap_err_with(|| {
format!("failed to read p2p private key file: {private_key_file}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not include the error here

Comment on lines +7 to +9
pub(super) const AGENT_VERSION: &str = "op-rbuilder/1.0.0";
pub(super) const FLASHBLOCKS_STREAM_PROTOCOL: p2p::StreamProtocol =
p2p::StreamProtocol::new("/flashblocks/1.0.0");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are the versions meant to be updated manually

Some(payload) = built_rx.recv() => {
let _ = payload_events_handle.send(Events::BuiltPayload(payload.clone()));
// TODO: only broadcast if `!no_tx_pool`?
// ignore error here; if p2p was disabled, the channel will be closed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm what is p2p is enabled and an error occurs here?
we'll still want to broadcast on no_tx_pool because it contains state changes around deposit transactions that other builders need

Comment on lines +127 to +136
pub p2p_enabled: bool,

/// Port for the p2p node
pub p2p_port: u16,

/// Optional hex-encoded private key file path for the p2p node
pub p2p_private_key_file: Option<String>,

/// Comma-separated list of multiaddresses of known peers to connect to
pub p2p_known_peers: Option<String>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can wrap these into P2PArgs like GasLimiterArgs

flashblocks_block_time: 200,
flashblocks_leeway_time: 100,
flashblocks_fixed: false,
flashblocks_calculate_state_root: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this is redundant if default is specified

};
use std::{convert::Infallible, time::Duration};

const DEFAULT_MAX_PEER_COUNT: u32 = 200;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this can be an op-rbuilder arg

.map(|addr| {
addr.clone()
.with_p2p(self.peer_id)
.expect("can add peer ID to multiaddr")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are the error cases when peer id can't be added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants