-
Notifications
You must be signed in to change notification settings - Fork 46
/
core.rs
327 lines (297 loc) · 12.3 KB
/
core.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! Contains the core derivation pipeline.
use crate::{
errors::{PipelineError, PipelineErrorKind},
traits::{
L2ChainProvider, NextAttributes, OriginAdvancer, OriginProvider, Pipeline, SignalReceiver,
},
types::{ActivationSignal, PipelineResult, ResetSignal, Signal, StepResult},
};
use alloc::{boxed::Box, collections::VecDeque, sync::Arc};
use async_trait::async_trait;
use core::fmt::Debug;
use op_alloy_genesis::{RollupConfig, SystemConfig};
use op_alloy_protocol::{BlockInfo, L2BlockInfo};
use op_alloy_rpc_types_engine::OpAttributesWithParent;
/// The derivation pipeline is responsible for deriving L2 inputs from L1 data.
#[derive(Debug)]
pub struct DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send,
P: L2ChainProvider + Send + Sync + Debug,
{
/// A handle to the next attributes.
pub attributes: S,
/// Reset provider for the pipeline.
/// A list of prepared [OpAttributesWithParent] to be used by the derivation pipeline
/// consumer.
pub prepared: VecDeque<OpAttributesWithParent>,
/// The rollup config.
pub rollup_config: Arc<RollupConfig>,
/// The L2 Chain Provider used to fetch the system config on reset.
pub l2_chain_provider: P,
}
impl<S, P> DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send,
P: L2ChainProvider + Send + Sync + Debug,
{
/// Creates a new instance of the [DerivationPipeline].
pub const fn new(
attributes: S,
rollup_config: Arc<RollupConfig>,
l2_chain_provider: P,
) -> Self {
Self { attributes, prepared: VecDeque::new(), rollup_config, l2_chain_provider }
}
}
impl<S, P> OriginProvider for DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send,
P: L2ChainProvider + Send + Sync + Debug,
{
fn origin(&self) -> Option<BlockInfo> {
self.attributes.origin()
}
}
impl<S, P> Iterator for DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send + Sync,
P: L2ChainProvider + Send + Sync + Debug,
{
type Item = OpAttributesWithParent;
fn next(&mut self) -> Option<Self::Item> {
self.prepared.pop_front()
}
}
#[async_trait]
impl<S, P> SignalReceiver for DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send + Sync,
P: L2ChainProvider + Send + Sync + Debug,
{
/// Signals the pipeline by calling the [`SignalReceiver::signal`] method.
///
/// During a [`Signal::Reset`], each stage is recursively called from the top-level
/// [crate::stages::AttributesQueue] to the bottom [crate::stages::L1Traversal]
/// with a head-recursion pattern. This effectively clears the internal state
/// of each stage in the pipeline from bottom on up.
///
/// [`Signal::Activation`] does a similar thing to the reset, with different
/// holocene-specific reset rules.
///
/// ### Parameters
///
/// The `signal` is contains the signal variant with any necessary parameters.
async fn signal(&mut self, signal: Signal) -> PipelineResult<()> {
match signal {
mut s @ Signal::Reset(ResetSignal { l2_safe_head, .. }) |
mut s @ Signal::Activation(ActivationSignal { l2_safe_head, .. }) => {
let system_config = self
.l2_chain_provider
.system_config_by_number(
l2_safe_head.block_info.number,
Arc::clone(&self.rollup_config),
)
.await
.map_err(Into::into)?;
s = s.with_system_config(system_config);
match self.attributes.signal(s).await {
Ok(()) => trace!(target: "pipeline", "Stages reset"),
Err(err) => {
if let PipelineErrorKind::Temporary(PipelineError::Eof) = err {
trace!(target: "pipeline", "Stages reset with EOF");
} else {
error!(target: "pipeline", "Stage reset errored: {:?}", err);
return Err(err);
}
}
}
}
Signal::FlushChannel => {
self.attributes.signal(signal).await?;
}
}
Ok(())
}
}
#[async_trait]
impl<S, P> Pipeline for DerivationPipeline<S, P>
where
S: NextAttributes + SignalReceiver + OriginProvider + OriginAdvancer + Debug + Send + Sync,
P: L2ChainProvider + Send + Sync + Debug,
{
/// Peeks at the next prepared [OpAttributesWithParent] from the pipeline.
fn peek(&self) -> Option<&OpAttributesWithParent> {
self.prepared.front()
}
/// Returns the rollup config.
fn rollup_config(&self) -> &RollupConfig {
&self.rollup_config
}
/// Returns the [SystemConfig] by L2 number.
async fn system_config_by_number(
&mut self,
number: u64,
) -> Result<SystemConfig, PipelineErrorKind> {
self.l2_chain_provider
.system_config_by_number(number, self.rollup_config.clone())
.await
.map_err(Into::into)
}
/// Attempts to progress the pipeline.
///
/// ## Returns
///
/// A [PipelineError::Eof] is returned if the pipeline is blocked by waiting for new L1 data.
/// Any other error is critical and the derivation pipeline should be reset.
/// An error is expected when the underlying source closes.
///
/// When [DerivationPipeline::step] returns [Ok(())], it should be called again, to continue the
/// derivation process.
///
/// [PipelineError]: crate::errors::PipelineError
async fn step(&mut self, cursor: L2BlockInfo) -> StepResult {
match self.attributes.next_attributes(cursor).await {
Ok(a) => {
trace!(target: "pipeline", "Prepared L2 attributes: {:?}", a);
self.prepared.push_back(a);
StepResult::PreparedAttributes
}
Err(err) => match err {
PipelineErrorKind::Temporary(PipelineError::Eof) => {
trace!(target: "pipeline", "Pipeline advancing origin");
if let Err(e) = self.attributes.advance_origin().await {
return StepResult::OriginAdvanceErr(e);
}
StepResult::AdvancedOrigin
}
_ => {
warn!(target: "pipeline", "Attributes queue step failed: {:?}", err);
StepResult::StepFailed(err)
}
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{pipeline::DerivationPipeline, test_utils::*};
use alloc::{string::ToString, sync::Arc};
use alloy_rpc_types_engine::PayloadAttributes;
use op_alloy_genesis::{RollupConfig, SystemConfig};
use op_alloy_protocol::L2BlockInfo;
use op_alloy_rpc_types_engine::{OpAttributesWithParent, OpPayloadAttributes};
fn default_test_payload_attributes() -> OpAttributesWithParent {
OpAttributesWithParent {
attributes: OpPayloadAttributes {
payload_attributes: PayloadAttributes {
timestamp: 0,
prev_randao: Default::default(),
suggested_fee_recipient: Default::default(),
withdrawals: None,
parent_beacon_block_root: None,
},
transactions: None,
no_tx_pool: None,
gas_limit: None,
eip_1559_params: None,
},
parent: Default::default(),
is_last_in_span: false,
}
}
#[test]
fn test_pipeline_next_attributes_empty() {
let mut pipeline = new_test_pipeline();
let result = pipeline.next();
assert_eq!(result, None);
}
#[test]
fn test_pipeline_next_attributes_with_peek() {
let mut pipeline = new_test_pipeline();
let expected = default_test_payload_attributes();
pipeline.prepared.push_back(expected.clone());
let result = pipeline.peek();
assert_eq!(result, Some(&expected));
let result = pipeline.next();
assert_eq!(result, Some(expected));
}
#[tokio::test]
async fn test_derivation_pipeline_missing_block() {
let mut pipeline = new_test_pipeline();
let cursor = L2BlockInfo::default();
let result = pipeline.step(cursor).await;
assert_eq!(
result,
StepResult::OriginAdvanceErr(
PipelineError::Provider("Block not found".to_string()).temp()
)
);
}
#[tokio::test]
async fn test_derivation_pipeline_prepared_attributes() {
let rollup_config = Arc::new(RollupConfig::default());
let l2_chain_provider = TestL2ChainProvider::default();
let expected = default_test_payload_attributes();
let attributes = TestNextAttributes { next_attributes: Some(expected) };
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Step on the pipeline and expect the result.
let cursor = L2BlockInfo::default();
let result = pipeline.step(cursor).await;
assert_eq!(result, StepResult::PreparedAttributes);
}
#[tokio::test]
async fn test_derivation_pipeline_advance_origin() {
let rollup_config = Arc::new(RollupConfig::default());
let l2_chain_provider = TestL2ChainProvider::default();
let attributes = TestNextAttributes::default();
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Step on the pipeline and expect the result.
let cursor = L2BlockInfo::default();
let result = pipeline.step(cursor).await;
assert_eq!(result, StepResult::AdvancedOrigin);
}
#[tokio::test]
async fn test_derivation_pipeline_signal_activation() {
let rollup_config = Arc::new(RollupConfig::default());
let mut l2_chain_provider = TestL2ChainProvider::default();
l2_chain_provider.system_configs.insert(0, SystemConfig::default());
let attributes = TestNextAttributes::default();
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Signal the pipeline to reset.
let result = pipeline.signal(ActivationSignal::default().signal()).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_derivation_pipeline_flush_channel() {
let rollup_config = Arc::new(RollupConfig::default());
let l2_chain_provider = TestL2ChainProvider::default();
let attributes = TestNextAttributes::default();
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Signal the pipeline to reset.
let result = pipeline.signal(Signal::FlushChannel).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_derivation_pipeline_signal_reset_missing_sys_config() {
let rollup_config = Arc::new(RollupConfig::default());
let l2_chain_provider = TestL2ChainProvider::default();
let attributes = TestNextAttributes::default();
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Signal the pipeline to reset.
let result = pipeline.signal(ResetSignal::default().signal()).await.unwrap_err();
assert_eq!(result, PipelineError::Provider("System config not found".to_string()).temp());
}
#[tokio::test]
async fn test_derivation_pipeline_signal_reset_ok() {
let rollup_config = Arc::new(RollupConfig::default());
let mut l2_chain_provider = TestL2ChainProvider::default();
l2_chain_provider.system_configs.insert(0, SystemConfig::default());
let attributes = TestNextAttributes::default();
let mut pipeline = DerivationPipeline::new(attributes, rollup_config, l2_chain_provider);
// Signal the pipeline to reset.
let result = pipeline.signal(ResetSignal::default().signal()).await;
assert!(result.is_ok());
}
}