Skip to content

Make state root calculation configurable on Flashblocks builder #164

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
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/op-rbuilder/src/args/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ pub struct FlashblocksArgs {
env = "FLASHBLOCK_LEEWAY_TIME"
)]
pub flashblocks_leeway_time: u64,

/// Should we calculate state root for each flashblock
#[arg(
long = "flashblocks.calculate-state-root",
default_value = "false",
env = "FLASHBLOCKS_CALCULATE_STATE_ROOT"
)]
pub calculate_state_root: bool,
}

impl Default for FlashblocksArgs {
Expand Down
5 changes: 5 additions & 0 deletions crates/op-rbuilder/src/builders/flashblocks/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct FlashblocksConfig {

/// Enables dynamic flashblocks number based on FCU arrival time
pub dynamic_adjustment: bool,

/// Should we calculate state root for each flashblockAdd commentMore actions
pub calculate_state_root: bool,
}

impl Default for FlashblocksConfig {
Expand All @@ -31,6 +34,7 @@ impl Default for FlashblocksConfig {
interval: Duration::from_millis(250),
leeway_time: Duration::from_millis(50),
dynamic_adjustment: false,
calculate_state_root: false,
}
}
}
Expand All @@ -55,6 +59,7 @@ impl TryFrom<OpRbuilderArgs> for FlashblocksConfig {
interval,
leeway_time,
dynamic_adjustment,
calculate_state_root: args.flashblocks.calculate_state_root,
})
}
}
Expand Down
63 changes: 41 additions & 22 deletions crates/op-rbuilder/src/builders/flashblocks/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ where
.sequencer_tx_duration
.record(sequencer_tx_start_time.elapsed());

let (payload, fb_payload, mut bundle_state) = build_block(db, &ctx, &mut info)?;
let (payload, fb_payload, mut bundle_state) = build_block(
db,
&ctx,
&mut info,
self.config.specific.calculate_state_root,
)?;

best_payload.set(payload.clone());
self.ws_pub
Expand Down Expand Up @@ -496,7 +501,12 @@ where
});

let total_block_built_duration = Instant::now();
let build_result = build_block(db, &ctx, &mut info);
let build_result = build_block(
db,
&ctx,
&mut info,
self.config.specific.calculate_state_root,
)?;
ctx.metrics
.total_block_built_duration
.record(total_block_built_duration.elapsed());
Expand Down Expand Up @@ -645,6 +655,7 @@ fn build_block<DB, P>(
mut state: State<DB>,
ctx: &OpPayloadBuilderCtx,
info: &mut ExecutionInfo<ExtraExecutionInfo>,
calculate_state_root: bool,
) -> Result<(OpBuiltPayload, FlashblocksPayloadV1, BundleState), PayloadBuilderError>
where
DB: Database<Error = ProviderError> + AsRef<P>,
Expand Down Expand Up @@ -684,26 +695,34 @@ where
.block_logs_bloom(block_number)
.expect("Number is in range");

// // calculate the state root
let state_root_start_time = Instant::now();
let state_provider = state.database.as_ref();
let hashed_state = state_provider.hashed_post_state(execution_outcome.state());
let (state_root, _trie_output) = {
state
.database
.as_ref()
.state_root_with_updates(hashed_state.clone())
.inspect_err(|err| {
warn!(target: "payload_builder",
parent_header=%ctx.parent().hash(),
%err,
"failed to calculate state root for payload"
);
})?
};
ctx.metrics
.state_root_calculation_duration
.record(state_root_start_time.elapsed());
// calculate the state root
let mut state_root = B256::ZERO;

if calculate_state_root {
let state_root_start_time = Instant::now();

let state_provider = state.database.as_ref();
let hashed_state = state_provider.hashed_post_state(execution_outcome.state());
let (_state_root, _trie_output) = {
state
.database
.as_ref()
.state_root_with_updates(hashed_state.clone())
.inspect_err(|err| {
warn!(
target: "payload_builder",
parent_header=%ctx.parent().hash(),
%err,
"failed to calculate state root for payload"
);
})?
};
state_root = _state_root;

ctx.metrics
.state_root_calculation_duration
.record(state_root_start_time.elapsed());
}

let mut requests_hash = None;
let withdrawals_root = if ctx
Expand Down
2 changes: 2 additions & 0 deletions crates/op-rbuilder/src/tests/framework/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const BUILDER_VARIANTS: &[VariantInfo] = &[
let mut args = #args;
args.flashblocks.enabled = true;
args.flashblocks.flashblocks_port = 0;
args.flashblocks.calculate_state_root = true;
args
}
}
Expand All @@ -39,6 +40,7 @@ const BUILDER_VARIANTS: &[VariantInfo] = &[
let mut args = crate::args::OpRbuilderArgs::default();
args.flashblocks.enabled = true;
args.flashblocks.flashblocks_port = 0;
args.flashblocks.calculate_state_root = true;
args
}
}
Expand Down