Skip to content

Commit f6321f9

Browse files
committed
host env and rollup env fixes
- properly builds a cfg env and block env from prev host header - makes max host gas a configurable property - cleans up the build prep in the sim loop
1 parent 224c415 commit f6321f9

File tree

4 files changed

+73
-30
lines changed

4 files changed

+73
-30
lines changed

src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ pub struct BuilderConfig {
185185
)]
186186
pub concurrency_limit: Option<usize>,
187187

188+
/// Optional maximum host gas coefficient to use when building blocks.
189+
/// Defaults to 80% (80) if not set.
190+
#[from_env(
191+
var = "MAX_HOST_GAS_COEFFICIENT",
192+
desc = "Optional maximum host gas coefficient, as a percentage, to use when building blocks",
193+
default = 80
194+
)]
195+
pub max_host_gas_coefficient: Option<u8>,
196+
188197
/// The slot calculator for the builder.
189198
pub slot_calculator: SlotCalculator,
190199

@@ -328,6 +337,15 @@ impl BuilderConfig {
328337
})
329338
}
330339

340+
/// Returns the maximum host gas to use for block building based on the configured max host gas coefficient.
341+
pub fn max_host_gas(&self, gas_limit: u64) -> u64 {
342+
// Set max host gas to a percentage of the host block gas limit
343+
let max_host_gas = ((gas_limit as u128
344+
* (self.max_host_gas_coefficient.unwrap_or(80) as u128))
345+
/ 100u128) as u64;
346+
max_host_gas
347+
}
348+
331349
/// Spawn a submit task, either Flashbots or BuilderHelper depending on
332350
/// configuration.
333351
pub async fn spawn_submit_task(

src/tasks/block/sim.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,30 +135,9 @@ impl Simulator {
135135
) -> eyre::Result<BuiltBlock> {
136136
let concurrency_limit = self.config.concurrency_limit();
137137

138-
let host_block_number = BlockNumber::from(sim_env.prev_host.number);
139-
let host_db = self.create_host_db(host_block_number).await;
140-
let mut host_block_env = BlockEnv::default();
141-
sim_env.prev_host.fill_block_env(&mut host_block_env);
142-
let host_env = HostEnv::<_, NoOpInspector>::new(
143-
host_db,
144-
constants.clone(),
145-
&self.config.host_cfg_env(),
146-
&host_block_env,
147-
);
138+
let (rollup_env, host_env) = self.create_envs(constants, &sim_env).await;
148139

149-
let rollup_block_number = BlockNumber::from(sim_env.prev_header.number);
150-
let rollup_db = self.create_rollup_db(rollup_block_number);
151-
let mut rollup_block_env = BlockEnv::default();
152-
sim_env.prev_header.fill_block_env(&mut rollup_block_env);
153-
let rollup_env = RollupEnv::<_, NoOpInspector>::new(
154-
rollup_db,
155-
constants,
156-
&self.config.ru_cfg_env(),
157-
&rollup_block_env,
158-
);
159-
160-
// TODO
161-
let max_host_gas: u64 = 0;
140+
let max_host_gas = self.config.max_host_gas(sim_env.prev_host.gas_limit);
162141

163142
let block_build = BlockBuild::new(
164143
rollup_env,
@@ -182,6 +161,44 @@ impl Simulator {
182161
Ok(built_block)
183162
}
184163

164+
// Helper to create rollup + host envs from the sim env.
165+
async fn create_envs(
166+
&self,
167+
constants: SignetSystemConstants,
168+
sim_env: &SimEnv,
169+
) -> (
170+
RollupEnv<RollupAlloyDatabaseProvider, NoOpInspector>,
171+
HostEnv<HostAlloyDatabaseProvider, NoOpInspector>,
172+
) {
173+
// Host DB and Env
174+
let host_block_number = BlockNumber::from(sim_env.prev_host.number);
175+
let host_db = self.create_host_db(host_block_number).await;
176+
let mut host_block_env = BlockEnv::default();
177+
sim_env.prev_host.fill_block_env(&mut host_block_env);
178+
179+
let host_env = HostEnv::<HostAlloyDatabaseProvider, NoOpInspector>::new(
180+
host_db,
181+
constants.clone(),
182+
&self.config.host_cfg_env(),
183+
&host_block_env,
184+
);
185+
186+
// Rollup DB and Env
187+
let rollup_block_number = BlockNumber::from(sim_env.prev_header.number);
188+
let rollup_db = self.create_rollup_db(rollup_block_number);
189+
let mut rollup_block_env = BlockEnv::default();
190+
sim_env.prev_header.fill_block_env(&mut rollup_block_env);
191+
192+
let rollup_env = RollupEnv::<RollupAlloyDatabaseProvider, NoOpInspector>::new(
193+
rollup_db,
194+
constants,
195+
&self.config.ru_cfg_env(),
196+
&rollup_block_env,
197+
);
198+
199+
(rollup_env, host_env)
200+
}
201+
185202
/// Spawns the simulator task, which ticks along the simulation loop
186203
/// as it receives block environments.
187204
///

src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
5858
1740681556, // pecorino start timestamp as sane default
5959
0, 1,
6060
),
61+
max_host_gas_coefficient: Some(80),
6162
constants: SignetSystemConstants::pecorino(),
6263
};
6364
Ok(config)

tests/block_builder_test.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloy::{
88
signers::local::PrivateKeySigner,
99
};
1010
use builder::{
11-
tasks::{block::sim::Simulator, env::EnvTask},
11+
tasks::{block::sim::Simulator, env::{EnvTask, SimEnv}},
1212
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
1313
};
1414
use signet_sim::SimCache;
@@ -51,7 +51,7 @@ async fn test_handle_build() {
5151
.spawn()
5252
.0;
5353

54-
let block_builder = Simulator::new(&config, host_provider, ru_provider.clone(), block_env);
54+
let block_builder = Simulator::new(&config, host_provider.clone(), ru_provider.clone(), block_env);
5555

5656
// Setup a sim cache
5757
let sim_items = SimCache::new();
@@ -63,15 +63,22 @@ async fn test_handle_build() {
6363
let tx_2 = new_signed_tx(&test_key_1, 0, U256::from(2_f64), 10_000).unwrap();
6464
sim_items.add_tx(tx_2, 0);
6565

66-
// Setup the block env
66+
// Setup the block envs
6767
let finish_by = Instant::now() + Duration::from_secs(2);
68-
let header = ru_provider.get_block(BlockId::latest()).await.unwrap().unwrap().header.inner;
69-
let number = header.number + 1;
70-
let timestamp = header.timestamp + config.slot_calculator.slot_duration();
68+
let host_header = host_provider.get_block(BlockId::latest()).await.unwrap().unwrap().header;
69+
let ru_header = ru_provider.get_block(BlockId::latest()).await.unwrap().unwrap().header.inner;
70+
let number = ru_header.number + 1;
71+
let timestamp = ru_header.timestamp + config.slot_calculator.slot_duration();
7172
let block_env = test_block_env(config, number, 7, timestamp);
7273

7374
// Spawn the block builder task
74-
let got = block_builder.handle_build(constants, sim_items, finish_by, block_env).await;
75+
let sim_env = SimEnv {
76+
block_env: block_env.clone(),
77+
prev_header: ru_header.clone(),
78+
prev_host: host_header.into(),
79+
span: tracing::Span::none(),
80+
};
81+
let got = block_builder.handle_build(constants, sim_items, finish_by, sim_env).await;
7582

7683
// Assert on the built block
7784
assert!(got.is_ok());

0 commit comments

Comments
 (0)