Skip to content

Commit 058806e

Browse files
committed
allow debug-bench-machine to read execution payloads from json
1 parent a5e2dc7 commit 058806e

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

crates/rbuilder/src/bin/debug-bench-machine.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! App to benchmark/test the tx block execution.
22
//! This only works when reth node is stopped and the chain moved forward from its synced state
33
//! It downloads block after the last one synced and re-executes all the txs in it.
4+
use alloy_consensus::TxEnvelope;
5+
use alloy_eips::Decodable2718;
46
use alloy_provider::Provider;
57
use clap::Parser;
68
use eyre::Context;
@@ -11,9 +13,11 @@ use rbuilder::{
1113
ThreadBlockBuildingContext,
1214
},
1315
live_builder::{base_config::load_config_toml_and_env, cli::LiveBuilderConfig, config::Config},
16+
mev_boost::submission::SubmitBlockRequest,
1417
provider::StateProviderFactory,
1518
utils::{extract_onchain_block_txs, find_suggested_fee_recipient, http_provider, Signer},
1619
};
20+
use reth_primitives_traits::SignerRecoverable;
1721
use reth_provider::StateProvider;
1822
use std::{path::PathBuf, sync::Arc, time::Instant};
1923
use tracing::{debug, info};
@@ -31,6 +35,11 @@ struct Cli {
3135
rpc_url: String,
3236
#[clap(long, help = "Config file path", env = "RBUILDER_CONFIG")]
3337
config: PathBuf,
38+
#[clap(
39+
long,
40+
help = "Path to submit block request to replay to use instead of the onchain block"
41+
)]
42+
submit_block_request_json: Option<PathBuf>,
3443
}
3544

3645
#[tokio::main]
@@ -54,6 +63,15 @@ async fn main() -> eyre::Result<()> {
5463
.await?
5564
.ok_or_else(|| eyre::eyre!("block not found on rpc"))?;
5665

66+
let onchain_block = if let Some(submit_block_request_json) = cli.submit_block_request_json {
67+
let mut block = read_execution_payload_from_json(submit_block_request_json)?;
68+
// without parent_beacon_block_root we can't build block and its not available in submit_block_request_json
69+
block.header.parent_beacon_block_root = onchain_block.header.parent_beacon_block_root;
70+
block
71+
} else {
72+
onchain_block
73+
};
74+
5775
let txs = extract_onchain_block_txs(&onchain_block)?;
5876
let suggested_fee_recipient = find_suggested_fee_recipient(&onchain_block, &txs);
5977
info!(
@@ -91,21 +109,25 @@ async fn main() -> eyre::Result<()> {
91109
let state_provider = state_provider.clone();
92110
let (build_time, finalize_time) =
93111
tokio::task::spawn_blocking(move || -> eyre::Result<_> {
94-
let partial_block = PartialBlock::new(true);
112+
let mut partial_block = PartialBlock::new(true);
95113
let mut state = BlockState::new_arc(state_provider);
96114
let mut local_ctx = ThreadBlockBuildingContext::default();
97115

98116
let build_time = Instant::now();
99117

118+
partial_block.pre_block_call(&ctx, &mut local_ctx, &mut state)?;
119+
100120
let mut space_state = BlockBuildingSpaceState::ZERO;
101121
for (idx, tx) in txs.into_iter().enumerate() {
102122
let result = {
103123
let mut fork = PartialBlockFork::new(&mut state, &ctx, &mut local_ctx);
124+
104125
fork.commit_tx(&tx, space_state)?.with_context(|| {
105126
format!("Failed to commit tx: {} {:?}", idx, tx.hash())
106127
})?
107128
};
108129
space_state.use_space(result.space_used(), result.blob_gas_used);
130+
partial_block.executed_tx_infos.push(result.tx_info);
109131
}
110132

111133
let build_time = build_time.elapsed();
@@ -132,6 +154,27 @@ async fn main() -> eyre::Result<()> {
132154
Ok(())
133155
}
134156

157+
fn read_execution_payload_from_json(path: PathBuf) -> eyre::Result<alloy_rpc_types::Block> {
158+
let req = std::fs::read_to_string(&path)?;
159+
let req: SubmitBlockRequest = serde_json::from_str(&req)?;
160+
let block_raw = match req {
161+
SubmitBlockRequest::Capella(req) => req.execution_payload.clone().into_block_raw()?,
162+
SubmitBlockRequest::Fulu(req) => req.execution_payload.clone().into_block_raw()?,
163+
SubmitBlockRequest::Deneb(req) => req.execution_payload.clone().into_block_raw()?,
164+
SubmitBlockRequest::Electra(req) => req.execution_payload.clone().into_block_raw()?,
165+
};
166+
let rpc_block = alloy_rpc_types::Block::from_consensus(block_raw, None);
167+
let rpc_block = rpc_block.try_map_transactions(|bytes| -> eyre::Result<_> {
168+
let envelope = TxEnvelope::decode_2718(&mut bytes.as_ref())?;
169+
let recovered = envelope.try_into_recovered()?;
170+
Ok(alloy_rpc_types::Transaction::from_transaction(
171+
recovered,
172+
alloy_rpc_types::TransactionInfo::default(),
173+
))
174+
})?;
175+
Ok(rpc_block)
176+
}
177+
135178
fn report_time_data(action: &str, data: &[u128]) {
136179
let mean = data.iter().sum::<u128>() as f64 / data.len() as f64;
137180
let median = *data.iter().sorted().nth(data.len() / 2).unwrap();

0 commit comments

Comments
 (0)