@@ -25,31 +25,82 @@ pub struct EnvTask {
2525 ru_provider: RuProvider,
2626}
2727
28- /// Contains a signet BlockEnv and its corresponding host Header.
28+ /// An environment for simulating a 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+
59+ /// Contains environments to simulate both host and rollup blocks.
2960#[derive(Debug, Clone)]
3061pub 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,
37- /// A tracing span associated with this block
62+ /// The host environment, for host block simulation.
63+ pub host: Environment ,
64+
65+ /// The rollup environment, for rollup block simulation.
66+ pub rollup: Environment,
67+
68+ /// A tracing span associated with this block simulation.
3869 pub span: Span,
3970}
4071
4172impl SimEnv {
42- /// Returns the block number of the signet block environment.
43- pub const fn block_number(&self) -> u64 {
44- self.prev_header.number.saturating_add(1)
73+ /// Get a reference to previous rollup header.
74+ pub const fn prev_rollup(&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 rollup block environment.
84+ pub const fn rollup_block_number(&self) -> u64 {
85+ self.prev_rollup().number.saturating_add(1)
4586 }
4687
47- /// Returns the host block number for the signet block environment.
88+ /// Get the block number for the host 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,14 +138,15 @@ 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.
94146 async fn task_fut(self, sender: watch::Sender<Option<SimEnv>>) {
95147 let span = info_span!("EnvTask::task_fut::init");
96148
97- let mut headers = match self.ru_provider.subscribe_blocks().await {
149+ let mut rollup_headers = match self.ru_provider.subscribe_blocks().await {
98150 Ok(poller) => poller,
99151 Err(err) => {
100152 span_error!(span, %err, "Failed to subscribe to blocks");
@@ -106,7 +158,7 @@ impl EnvTask {
106158 drop(span);
107159
108160 while let Some(rollup_header) =
109- headers .next().instrument(info_span!("EnvTask::task_fut::stream")).await
161+ rollup_headers .next().instrument(info_span!("EnvTask::task_fut::stream")).await
110162 {
111163 let host_block_number =
112164 self.config.constants.rollup_block_to_host_block_num(rollup_header.number);
@@ -118,7 +170,7 @@ impl EnvTask {
118170 span,
119171 error!("error fetching previous host block - skipping block submission")
120172 );
121- let prev_host = opt_unwrap_or_continue!(
173+ let host_header = opt_unwrap_or_continue!(
122174 host_block_opt,
123175 span,
124176 warn!("previous host block not found - skipping block submission")
@@ -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 rollup_env = self.construct_block_env(rollup_header.into());
183+ let host_env = self.construct_block_env(host_header);
184+
131185 span_debug!(
132186 span,
133- signet_env_number = signet_env .number.to::<u64>(),
134- signet_env_basefee = signet_env .basefee,
135- "constructed signet block env"
187+ rollup_env_number = rollup_env.block_env .number.to::<u64>(),
188+ rollup_env_basefee = rollup_env.block_env .basefee,
189+ "constructed 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: rollup_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;
0 commit comments