Skip to content

Commit 1d01ce0

Browse files
committed
fix: move block env creation for host to sim task
1 parent 19770a3 commit 1d01ce0

File tree

5 files changed

+106
-58
lines changed

5 files changed

+106
-58
lines changed

src/tasks/block/sim.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
config::{BuilderConfig, HostProvider, RuProvider},
66
tasks::env::SimEnv,
77
};
8-
use alloy::{eips::BlockId, network::Ethereum, primitives::BlockNumber};
8+
use alloy::{consensus::Header, eips::BlockId, network::Ethereum};
99
use init4_bin_base::{
1010
deps::metrics::{counter, histogram},
1111
utils::calc::SlotCalculator,
@@ -21,13 +21,9 @@ use tokio::{
2121
task::JoinHandle,
2222
};
2323
use tracing::{Instrument, Span, debug, instrument};
24-
use trevm::{
25-
Block,
26-
revm::{
27-
context::BlockEnv,
28-
database::{AlloyDB, WrapDatabaseAsync},
29-
inspector::NoOpInspector,
30-
},
24+
use trevm::revm::{
25+
database::{AlloyDB, WrapDatabaseAsync},
26+
inspector::NoOpInspector,
3127
};
3228

3329
type HostAlloyDatabaseProvider = WrapDatabaseAsync<AlloyDB<Ethereum, HostProvider>>;
@@ -58,6 +54,16 @@ pub struct SimResult {
5854
}
5955

6056
impl SimResult {
57+
/// Get a reference to the previous host header.
58+
pub const fn prev_host(&self) -> &Header {
59+
self.sim_env.prev_host()
60+
}
61+
62+
/// Get a reference to the previous signet header.
63+
pub const fn prev_header(&self) -> &Header {
64+
self.sim_env.prev_header()
65+
}
66+
6167
/// Returns the block number of the built block.
6268
pub const fn block_number(&self) -> u64 {
6369
self.block.block_number()
@@ -134,7 +140,7 @@ impl Simulator {
134140
sim_env: SimEnv,
135141
) -> eyre::Result<BuiltBlock> {
136142
let concurrency_limit = self.config.concurrency_limit();
137-
let max_host_gas = self.config.max_host_gas(sim_env.prev_host.gas_limit);
143+
let max_host_gas = self.config.max_host_gas(sim_env.prev_host().gas_limit);
138144

139145
let (rollup_env, host_env) = self.create_envs(constants, &sim_env).await;
140146

@@ -170,29 +176,25 @@ impl Simulator {
170176
HostEnv<HostAlloyDatabaseProvider, NoOpInspector>,
171177
) {
172178
// Host DB and Env
173-
let host_block_number = BlockNumber::from(sim_env.prev_host.number);
179+
let host_block_number = sim_env.host_block_number();
174180
let host_db = self.create_host_db(host_block_number).await;
175-
let mut host_block_env = BlockEnv::default();
176-
sim_env.prev_host.fill_block_env(&mut host_block_env);
177181

178182
let host_env = HostEnv::<HostAlloyDatabaseProvider, NoOpInspector>::new(
179183
host_db,
180184
constants.clone(),
181185
&self.config.host_cfg_env(),
182-
&host_block_env,
186+
sim_env.host_env(),
183187
);
184188

185189
// Rollup DB and Env
186-
let rollup_block_number = BlockNumber::from(sim_env.prev_header.number);
190+
let rollup_block_number = sim_env.block_number();
187191
let rollup_db = self.create_rollup_db(rollup_block_number);
188-
let mut rollup_block_env = BlockEnv::default();
189-
sim_env.prev_header.fill_block_env(&mut rollup_block_env);
190192

191193
let rollup_env = RollupEnv::<RollupAlloyDatabaseProvider, NoOpInspector>::new(
192194
rollup_db,
193195
constants,
194196
&self.config.ru_cfg_env(),
195-
&rollup_block_env,
197+
sim_env.rollup_env(),
196198
);
197199

198200
(rollup_env, host_env)

src/tasks/cache/task.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tracing::{debug, info};
1616
#[derive(Debug)]
1717
pub struct CacheTask {
1818
/// The channel to receive the block environment.
19-
env: watch::Receiver<Option<SimEnv>>,
19+
envs: watch::Receiver<Option<SimEnv>>,
2020
/// The channel to receive the transaction bundles.
2121
bundles: mpsc::UnboundedReceiver<TxCacheBundle>,
2222
/// The channel to receive the transactions.
@@ -30,24 +30,27 @@ impl CacheTask {
3030
bundles: mpsc::UnboundedReceiver<TxCacheBundle>,
3131
txns: mpsc::UnboundedReceiver<TxEnvelope>,
3232
) -> Self {
33-
Self { env, bundles, txns }
33+
Self { envs: env, bundles, txns }
3434
}
3535

3636
async fn task_future(mut self, cache: SimCache) {
3737
loop {
3838
let mut basefee = 0;
3939
tokio::select! {
4040
biased;
41-
res = self.env.changed() => {
41+
res = self.envs.changed() => {
4242
if res.is_err() {
4343
debug!("Cache task: env channel closed, exiting");
4444
break;
4545
}
46-
if let Some(env) = self.env.borrow_and_update().as_ref() {
47-
basefee = env.block_env.basefee;
48-
info!(basefee, block_env_number = env.block_env.number.to::<u64>(), block_env_timestamp = env.block_env.timestamp.to::<u64>(), "rollup block env changed, clearing cache");
46+
47+
if let Some(env) = self.envs.borrow_and_update().as_ref() {
48+
let sim_env = env.rollup_env();
49+
50+
basefee = sim_env.basefee;
51+
info!(basefee, block_env_number = sim_env.number.to::<u64>(), block_env_timestamp = sim_env.timestamp.to::<u64>(), "rollup block env changed, clearing cache");
4952
cache.clean(
50-
env.block_env.number.to(), env.block_env.timestamp.to()
53+
sim_env.number.to(), sim_env.timestamp.to()
5154
);
5255
}
5356
}

src/tasks/env.rs

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,82 @@ pub struct EnvTask {
2525
ru_provider: RuProvider,
2626
}
2727

28+
/// An environment for simulating a signet block.
29+
#[derive(Debug, Clone)]
30+
pub struct Environment {
31+
block_env: BlockEnv,
32+
prev_header: Header,
33+
}
34+
35+
impl Environment {
36+
/// Create a new `Environment` with the given block environment and
37+
/// previous header.
38+
pub const fn new(block_env: BlockEnv, prev_header: Header) -> Self {
39+
Self { block_env, prev_header }
40+
}
41+
42+
/// Get a reference to the block environment.
43+
pub const fn block_env(&self) -> &BlockEnv {
44+
&self.block_env
45+
}
46+
47+
/// Get a reference to the previous block header.
48+
pub const fn prev_header(&self) -> &Header {
49+
&self.prev_header
50+
}
51+
52+
/// Create a new empty `Environment` for testing purposes.
53+
#[doc(hidden)]
54+
pub fn for_testing() -> Self {
55+
Self { block_env: Default::default(), prev_header: Header::default() }
56+
}
57+
}
58+
2859
/// Contains a signet BlockEnv and its corresponding host Header.
2960
#[derive(Debug, Clone)]
3061
pub struct SimEnv {
31-
/// The signet block environment, for rollup block simulation.
32-
pub block_env: BlockEnv,
33-
/// The header of the previous rollup block.
34-
pub prev_header: Header,
35-
/// The header of the previous host block.
36-
pub prev_host: Header,
62+
/// The host environment, for host block simulation.
63+
pub host: Environment,
64+
65+
/// The signet environment, for rollup block simulation.
66+
pub rollup: Environment,
67+
3768
/// A tracing span associated with this block
3869
pub span: Span,
3970
}
4071

4172
impl SimEnv {
42-
/// Returns the block number of the signet block environment.
73+
/// Get a reference to previous Signet header.
74+
pub const fn prev_header(&self) -> &Header {
75+
&self.rollup.prev_header
76+
}
77+
78+
/// Get a reference to the previous host header
79+
pub const fn prev_host(&self) -> &Header {
80+
&self.host.prev_header
81+
}
82+
83+
/// Get the block number of the signet block environment.
4384
pub const fn block_number(&self) -> u64 {
44-
self.prev_header.number.saturating_add(1)
85+
self.prev_header().number.saturating_add(1)
4586
}
4687

47-
/// Returns the host block number for the signet block environment.
88+
/// Get the host block number for the signet block environment.
4889
pub const fn host_block_number(&self) -> u64 {
49-
self.prev_host.number.saturating_add(1)
90+
self.prev_host().number.saturating_add(1)
5091
}
5192

52-
/// Returns a reference to the tracing span associated with this block env.
93+
/// Get a reference to the rollup block environment.
94+
pub const fn rollup_env(&self) -> &BlockEnv {
95+
&self.rollup.block_env
96+
}
97+
98+
/// Get a reference to the host block environment.
99+
pub const fn host_env(&self) -> &BlockEnv {
100+
&self.host.block_env
101+
}
102+
103+
/// Get a reference to the tracing span associated with this block env.
53104
pub const fn span(&self) -> &Span {
54105
&self.span
55106
}
@@ -71,8 +122,8 @@ impl EnvTask {
71122
}
72123

73124
/// Construct a [`BlockEnv`] by from the previous block header.
74-
fn construct_block_env(&self, previous: &Header) -> BlockEnv {
75-
BlockEnv {
125+
fn construct_block_env(&self, previous: Header) -> Environment {
126+
let env = BlockEnv {
76127
number: U256::from(previous.number + 1),
77128
beneficiary: self.config.builder_rewards_address,
78129
// NB: EXACTLY the same as the previous block
@@ -87,7 +138,8 @@ impl EnvTask {
87138
excess_blob_gas: 0,
88139
blob_gasprice: 0,
89140
}),
90-
}
141+
};
142+
Environment::new(env, previous)
91143
}
92144

93145
/// Returns a sender that sends [`SimEnv`] for communicating the next block environment.
@@ -127,23 +179,17 @@ impl EnvTask {
127179
.inner;
128180

129181
// Construct the block env using the previous block header
130-
let signet_env = self.construct_block_env(&rollup_header);
182+
let signet_env = self.construct_block_env(rollup_header.into());
183+
let host_env = self.construct_block_env(prev_host);
184+
131185
span_debug!(
132186
span,
133-
signet_env_number = signet_env.number.to::<u64>(),
134-
signet_env_basefee = signet_env.basefee,
187+
signet_env_number = signet_env.block_env.number.to::<u64>(),
188+
signet_env_basefee = signet_env.block_env.basefee,
135189
"constructed signet block env"
136190
);
137191

138-
if sender
139-
.send(Some(SimEnv {
140-
span,
141-
block_env: signet_env,
142-
prev_header: rollup_header.inner,
143-
prev_host,
144-
}))
145-
.is_err()
146-
{
192+
if sender.send(Some(SimEnv { span, rollup: signet_env, host: host_env })).is_err() {
147193
// The receiver has been dropped, so we can stop the task.
148194
tracing::debug!("receiver dropped, stopping task");
149195
break;

src/tasks/submit/flashbots.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl FlashbotsTask {
7979
self.config.clone(),
8080
);
8181

82-
let tx = prep.prep_transaction(&sim_result.sim_env.prev_host).await?;
82+
let tx = prep.prep_transaction(sim_result.prev_host()).await?;
8383

8484
let sendable = self.host_provider().fill(tx.into_request()).await?;
8585

tests/block_builder_test.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for the block building task.
22
33
use alloy::{
4+
eips::BlockId,
45
network::Ethereum,
56
node_bindings::Anvil,
67
primitives::U256,
@@ -10,7 +11,7 @@ use alloy::{
1011
use builder::{
1112
tasks::{
1213
block::sim::Simulator,
13-
env::{EnvTask, SimEnv},
14+
env::{EnvTask, Environment, SimEnv},
1415
},
1516
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
1617
};
@@ -26,8 +27,6 @@ use std::time::{Duration, Instant};
2627
#[ignore = "integration test"]
2728
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2829
async fn test_handle_build() {
29-
use alloy::eips::BlockId;
30-
3130
setup_logging();
3231

3332
// Make a test config
@@ -69,17 +68,15 @@ async fn test_handle_build() {
6968

7069
// Setup the block envs
7170
let finish_by = Instant::now() + Duration::from_secs(2);
72-
let host_header = host_provider.get_block(BlockId::latest()).await.unwrap().unwrap().header;
7371
let ru_header = ru_provider.get_block(BlockId::latest()).await.unwrap().unwrap().header.inner;
7472
let number = ru_header.number + 1;
7573
let timestamp = ru_header.timestamp + config.slot_calculator.slot_duration();
7674
let block_env = test_block_env(config, number, 7, timestamp);
7775

7876
// Spawn the block builder task
7977
let sim_env = SimEnv {
80-
block_env: block_env.clone(),
81-
prev_header: ru_header.clone(),
82-
prev_host: host_header.into(),
78+
host: Environment::for_testing(),
79+
rollup: Environment::new(block_env, ru_header),
8380
span: tracing::Span::none(),
8481
};
8582
let got = block_builder.handle_build(constants, sim_items, finish_by, sim_env).await;

0 commit comments

Comments
 (0)