-
Notifications
You must be signed in to change notification settings - Fork 58
feat: Extend p2p functionality, add gossiping raw flashblocks payloads #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sieniven
wants to merge
4
commits into
flashbots:main
Choose a base branch
from
okx:niven/extend-p2p-feat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use crate::{ | ||
| builders::flashblocks::{ | ||
| ctx::OpPayloadSyncerCtx, p2p::Message, payload::FlashblocksExecutionInfo, | ||
| wspub::WebSocketPublisher, | ||
| }, | ||
| primitives::reth::ExecutionInfo, | ||
| traits::ClientBounds, | ||
|
|
@@ -35,7 +36,7 @@ use tracing::warn; | |
| /// In the case of a payload received from a peer, it is executed and if successful, an event is sent to the payload builder. | ||
| pub(crate) struct PayloadHandler<Client, Tasks> { | ||
| // receives new flashblock payloads built by this builder. | ||
| built_fb_payload_rx: mpsc::Receiver<OpBuiltPayload>, | ||
| built_fb_payload_rx: mpsc::Receiver<OpFlashblockPayload>, | ||
| // receives new full block payloads built by this builder. | ||
| built_payload_rx: mpsc::Receiver<OpBuiltPayload>, | ||
| // receives incoming p2p messages from peers. | ||
|
|
@@ -44,13 +45,17 @@ pub(crate) struct PayloadHandler<Client, Tasks> { | |
| p2p_tx: mpsc::Sender<Message>, | ||
| // sends a `Events::BuiltPayload` to the reth payload builder when a new payload is received. | ||
| payload_events_handle: tokio::sync::broadcast::Sender<Events<OpEngineTypes>>, | ||
| // websocket publisher for broadcasting flashblocks to all connected subscribers. | ||
| ws_pub: Arc<WebSocketPublisher>, | ||
|
Comment on lines
+48
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't the payload builder already broadcast flashblocks over websocket? |
||
| // context required for execution of blocks during syncing | ||
| ctx: OpPayloadSyncerCtx, | ||
| // chain client | ||
| client: Client, | ||
| // task executor | ||
| task_executor: Tasks, | ||
| cancel: tokio_util::sync::CancellationToken, | ||
| p2p_send_payload: bool, | ||
| p2p_process_payload: bool, | ||
| } | ||
|
|
||
| impl<Client, Tasks> PayloadHandler<Client, Tasks> | ||
|
|
@@ -60,26 +65,32 @@ where | |
| { | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub(crate) fn new( | ||
| built_fb_payload_rx: mpsc::Receiver<OpBuiltPayload>, | ||
| built_fb_payload_rx: mpsc::Receiver<OpFlashblockPayload>, | ||
| built_payload_rx: mpsc::Receiver<OpBuiltPayload>, | ||
| p2p_rx: mpsc::Receiver<Message>, | ||
| p2p_tx: mpsc::Sender<Message>, | ||
| payload_events_handle: tokio::sync::broadcast::Sender<Events<OpEngineTypes>>, | ||
| ws_pub: Arc<WebSocketPublisher>, | ||
| ctx: OpPayloadSyncerCtx, | ||
| client: Client, | ||
| task_executor: Tasks, | ||
| cancel: tokio_util::sync::CancellationToken, | ||
| p2p_send_payload: bool, | ||
| p2p_process_payload: bool, | ||
| ) -> Self { | ||
| Self { | ||
| built_fb_payload_rx, | ||
| built_payload_rx, | ||
| p2p_rx, | ||
| p2p_tx, | ||
| payload_events_handle, | ||
| ws_pub, | ||
| ctx, | ||
| client, | ||
| task_executor, | ||
| cancel, | ||
| p2p_send_payload, | ||
| p2p_process_payload, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -90,10 +101,13 @@ where | |
| mut p2p_rx, | ||
| p2p_tx, | ||
| payload_events_handle, | ||
| ws_pub, | ||
| ctx, | ||
| client, | ||
| task_executor, | ||
| cancel, | ||
| p2p_send_payload, | ||
| p2p_process_payload, | ||
| } = self; | ||
|
|
||
| tracing::info!(target: "payload_builder", "flashblocks payload handler started"); | ||
|
|
@@ -109,20 +123,42 @@ where | |
| if let Err(e) = payload_events_handle.send(Events::BuiltPayload(payload.clone())) { | ||
| warn!(target: "payload_builder", e = ?e, "failed to send BuiltPayload event"); | ||
| } | ||
| if p2p_send_payload { | ||
| // ignore error here; if p2p was disabled, the channel will be closed. | ||
| let _ = p2p_tx.send(payload.into()).await; | ||
| } | ||
| } | ||
| Some(message) = p2p_rx.recv() => { | ||
| match message { | ||
| Message::OpBuiltPayload(payload) => { | ||
| if !p2p_process_payload { | ||
| continue; | ||
| } | ||
|
|
||
| let payload: OpBuiltPayload = payload.into(); | ||
| let block_hash = payload.block().hash(); | ||
| // Check if this block is already the pending block in canonical state | ||
| if let Ok(Some(pending)) = client.pending_block() | ||
| && pending.hash() == block_hash | ||
| { | ||
| tracing::trace!( | ||
| target: "payload_builder", | ||
| hash = %block_hash, | ||
| block_number = payload.block().header().number, | ||
| "skipping flashblock execution - block already pending in canonical state" | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| let ctx = ctx.clone(); | ||
| let client = client.clone(); | ||
| let payload_events_handle = payload_events_handle.clone(); | ||
| let cancel = cancel.clone(); | ||
|
|
||
| // execute the flashblock on a thread where blocking is acceptable, | ||
| // execute the built full payload on a thread where blocking is acceptable, | ||
| // as it's potentially a heavy operation | ||
| task_executor.spawn_blocking(Box::pin(async move { | ||
| let res = execute_flashblock( | ||
| let res = execute_built_payload( | ||
| payload, | ||
| ctx, | ||
| client, | ||
|
|
@@ -141,6 +177,12 @@ where | |
| } | ||
| })); | ||
| } | ||
| Message::OpFlashblockPayload(fb_payload) => { | ||
| // Skip validation as flashblock builder p2p is trusted | ||
| if let Err(e) = ws_pub.publish(&fb_payload) { | ||
| warn!(target: "payload_builder", e = ?e, "failed to publish flashblock to websocket publisher"); | ||
| } | ||
sieniven marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| else => break, | ||
|
|
@@ -149,7 +191,7 @@ where | |
| } | ||
| } | ||
|
|
||
| fn execute_flashblock<Client>( | ||
| fn execute_built_payload<Client>( | ||
| payload: OpBuiltPayload, | ||
| ctx: OpPayloadSyncerCtx, | ||
| client: Client, | ||
|
|
@@ -173,7 +215,7 @@ where | |
| .wrap_err("failed to get parent header")? | ||
| .ok_or_else(|| eyre::eyre!("parent header not found"))?; | ||
|
|
||
| // For X Layer, validate header and parent relationship before execution | ||
| // Validate header and parent relationship before execution | ||
| let chain_spec = client.chain_spec(); | ||
| validate_pre_execution(&payload, &parent_header, parent_hash, chain_spec.clone()) | ||
| .wrap_err("pre-execution validation failed")?; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.