From 0a401d9cbf67819e09f2df4800db46ec13894d08 Mon Sep 17 00:00:00 2001 From: Soubhik Singha Mahapatra <160333583+Soubhik-10@users.noreply.github.com> Date: Sat, 26 Apr 2025 13:41:46 +0530 Subject: [PATCH] feat: created BroadcastLatestForkchoice action (#15784) Co-authored-by: Matthias Seitz --- .../e2e-test-utils/src/testsuite/actions.rs | 68 +++++++++++++++++++ crates/e2e-test-utils/src/testsuite/mod.rs | 6 ++ 2 files changed, 74 insertions(+) diff --git a/crates/e2e-test-utils/src/testsuite/actions.rs b/crates/e2e-test-utils/src/testsuite/actions.rs index 54ad0187a1..e7ed9b801c 100644 --- a/crates/e2e-test-utils/src/testsuite/actions.rs +++ b/crates/e2e-test-utils/src/testsuite/actions.rs @@ -311,6 +311,74 @@ where }) } } + +///Action that broadcasts the latest fork choice state to all clients +#[derive(Debug, Default)] +pub struct BroadcastLatestForkchoice {} + +impl Action for BroadcastLatestForkchoice +where + Engine: EngineTypes + PayloadTypes, + reth_node_ethereum::engine::EthPayloadAttributes: + From<::ExecutionPayloadEnvelopeV3>, +{ + fn execute<'a>(&'a mut self, env: &'a mut Environment) -> BoxFuture<'a, Result<()>> { + Box::pin(async move { + let payload = env.latest_payload_executed.clone(); + + if env.node_clients.is_empty() { + return Err(eyre::eyre!("No node clients available")); + } + let latest_block = env + .latest_block_info + .as_ref() + .ok_or_else(|| eyre::eyre!("No latest block information available"))?; + + let parent_hash = latest_block.hash; + debug!("Latest block hash: {parent_hash}"); + + let fork_choice_state = ForkchoiceState { + head_block_hash: parent_hash, + safe_block_hash: parent_hash, + finalized_block_hash: parent_hash, + }; + debug!( + "Broadcasting forkchoice update to {} clients. Head: {:?}", + env.node_clients.len(), + fork_choice_state.head_block_hash + ); + + for (idx, client) in env.node_clients.iter().enumerate() { + let engine_client = &client.engine; + + match EngineApiClient::::fork_choice_updated_v3( + engine_client, + fork_choice_state, + payload.clone(), + ) + .await + { + Ok(resp) => { + debug!( + "Client {}: Forkchoice update status: {:?}", + idx, resp.payload_status.status + ); + } + Err(err) => { + return Err(eyre::eyre!( + "Client {}: Failed to broadcast forkchoice: {:?}", + idx, + err + )); + } + } + } + debug!("Forkchoice update broadcasted successfully"); + Ok(()) + }) + } +} + /// Action that produces a sequence of blocks using the available clients #[derive(Debug)] pub struct ProduceBlocks { diff --git a/crates/e2e-test-utils/src/testsuite/mod.rs b/crates/e2e-test-utils/src/testsuite/mod.rs index 20d85de073..db3d98883c 100644 --- a/crates/e2e-test-utils/src/testsuite/mod.rs +++ b/crates/e2e-test-utils/src/testsuite/mod.rs @@ -64,6 +64,10 @@ pub struct Environment { pub latest_payload_built: Option, /// Stores the most recent executed payload pub latest_payload_executed: Option, + /// Number of slots until a block is considered safe + pub slots_to_safe: u64, + /// Number of slots until a block is considered finalized + pub slots_to_finalized: u64, } impl Default for Environment { @@ -81,6 +85,8 @@ impl Default for Environment { latest_fork_choice_state: ForkchoiceState::default(), latest_payload_built: None, latest_payload_executed: None, + slots_to_safe: 0, + slots_to_finalized: 0, } } }