feat: created BroadcastLatestForkchoice action (#15784)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Soubhik Singha Mahapatra
2025-04-26 13:41:46 +05:30
committed by GitHub
parent 264334640c
commit 0a401d9cbf
2 changed files with 74 additions and 0 deletions

View File

@@ -311,6 +311,74 @@ where
})
}
}
///Action that broadcasts the latest fork choice state to all clients
#[derive(Debug, Default)]
pub struct BroadcastLatestForkchoice {}
impl<Engine> Action<Engine> for BroadcastLatestForkchoice
where
Engine: EngineTypes + PayloadTypes<PayloadAttributes = PayloadAttributes>,
reth_node_ethereum::engine::EthPayloadAttributes:
From<<Engine as EngineTypes>::ExecutionPayloadEnvelopeV3>,
{
fn execute<'a>(&'a mut self, env: &'a mut Environment<Engine>) -> 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::<Engine>::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<Engine> {

View File

@@ -64,6 +64,10 @@ pub struct Environment<I> {
pub latest_payload_built: Option<PayloadAttributes>,
/// Stores the most recent executed payload
pub latest_payload_executed: Option<PayloadAttributes>,
/// 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<I> Default for Environment<I> {
@@ -81,6 +85,8 @@ impl<I> Default for Environment<I> {
latest_fork_choice_state: ForkchoiceState::default(),
latest_payload_built: None,
latest_payload_executed: None,
slots_to_safe: 0,
slots_to_finalized: 0,
}
}
}