Skip to content

Commit 9e90c2d

Browse files
authored
chore(derive): remove batch reader (op-rs#826)
1 parent 9f613a2 commit 9e90c2d

File tree

7 files changed

+7
-227
lines changed

7 files changed

+7
-227
lines changed

Cargo.lock

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ os_pipe = "1.2.1"
106106
reqwest = "0.12.8"
107107
command-fds = "0.3.0"
108108
async-trait = "0.1.83"
109-
alloc-no-stdlib = "2.0.4"
110109
linked_list_allocator = "0.10.5"
111110

112111
# General
@@ -121,10 +120,6 @@ tracing-loki = "0.2.5"
121120
tracing-subscriber = "0.3.18"
122121
tracing = { version = "0.1.40", default-features = false }
123122

124-
# Encoding
125-
miniz_oxide = "0.8.0"
126-
brotli = { version = "7.0.0", default-features = false }
127-
128123
# Testing
129124
pprof = "0.13.0"
130125
proptest = "1.5"

crates/derive/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ op-alloy-rpc-types-engine.workspace = true
2626
op-alloy-consensus = { workspace = true, features = ["k256"] }
2727

2828
# General
29-
brotli.workspace = true
3029
tracing.workspace = true
31-
miniz_oxide.workspace = true
3230
async-trait.workspace = true
33-
alloc-no-stdlib.workspace = true
3431
thiserror.workspace = true
3532

3633
# `serde` feature dependencies

crates/derive/src/stages/batch/batch_queue.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,8 @@ where
460460
#[cfg(test)]
461461
mod tests {
462462
use super::*;
463-
use crate::{
464-
stages::channel::channel_reader::BatchReader,
465-
test_utils::{CollectingLayer, TestL2ChainProvider, TestNextBatchProvider, TraceStorage},
463+
use crate::test_utils::{
464+
CollectingLayer, TestL2ChainProvider, TestNextBatchProvider, TraceStorage,
466465
};
467466
use alloc::vec;
468467
use alloy_consensus::Header;
@@ -471,7 +470,7 @@ mod tests {
471470
use alloy_rlp::{BytesMut, Encodable};
472471
use op_alloy_consensus::{OpBlock, OpTxEnvelope, OpTxType, TxDeposit};
473472
use op_alloy_genesis::{ChainGenesis, MAX_RLP_BYTES_PER_CHANNEL_FJORD};
474-
use op_alloy_protocol::{L1BlockInfoBedrock, L1BlockInfoTx};
473+
use op_alloy_protocol::{BatchReader, L1BlockInfoBedrock, L1BlockInfoTx};
475474
use tracing::Level;
476475
use tracing_subscriber::layer::SubscriberExt;
477476

crates/derive/src/stages/channel/channel_reader.rs

+4-114
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,19 @@
22
33
use crate::{
44
errors::PipelineError,
5-
stages::{decompress_brotli, BatchStreamProvider},
5+
stages::BatchStreamProvider,
66
traits::{OriginAdvancer, OriginProvider, SignalReceiver},
77
types::{PipelineResult, Signal},
88
};
9-
use alloc::{boxed::Box, sync::Arc, vec::Vec};
9+
use alloc::{boxed::Box, sync::Arc};
1010
use alloy_primitives::Bytes;
11-
use alloy_rlp::Decodable;
1211
use async_trait::async_trait;
1312
use core::fmt::Debug;
14-
use miniz_oxide::inflate::decompress_to_vec_zlib;
1513
use op_alloy_genesis::{
1614
RollupConfig, MAX_RLP_BYTES_PER_CHANNEL_BEDROCK, MAX_RLP_BYTES_PER_CHANNEL_FJORD,
1715
};
18-
use op_alloy_protocol::{Batch, BlockInfo};
19-
20-
/// ZLIB Deflate Compression Method.
21-
pub(crate) const ZLIB_DEFLATE_COMPRESSION_METHOD: u8 = 8;
22-
23-
/// ZLIB Reserved Compression Info.
24-
pub(crate) const ZLIB_RESERVED_COMPRESSION_METHOD: u8 = 15;
25-
26-
/// Brotili Compression Channel Version.
27-
pub(crate) const CHANNEL_VERSION_BROTLI: u8 = 1;
16+
use op_alloy_protocol::{Batch, BatchReader, BlockInfo};
17+
use tracing::{debug, warn};
2818

2919
/// The [ChannelReader] provider trait.
3020
#[async_trait]
@@ -171,88 +161,6 @@ where
171161
}
172162
}
173163

174-
/// Batch Reader provides a function that iteratively consumes batches from the reader.
175-
/// The L1Inclusion block is also provided at creation time.
176-
/// Warning: the batch reader can read every batch-type.
177-
/// The caller of the batch-reader should filter the results.
178-
#[derive(Debug)]
179-
pub(crate) struct BatchReader {
180-
/// The raw data to decode.
181-
data: Option<Vec<u8>>,
182-
/// Decompressed data.
183-
decompressed: Vec<u8>,
184-
/// The current cursor in the `decompressed` data.
185-
cursor: usize,
186-
/// The maximum RLP bytes per channel.
187-
max_rlp_bytes_per_channel: usize,
188-
}
189-
190-
impl BatchReader {
191-
/// Creates a new [BatchReader] from the given data and max decompressed RLP bytes per channel.
192-
pub(crate) fn new<T>(data: T, max_rlp_bytes_per_channel: usize) -> Self
193-
where
194-
T: Into<Vec<u8>>,
195-
{
196-
Self {
197-
data: Some(data.into()),
198-
decompressed: Vec::new(),
199-
cursor: 0,
200-
max_rlp_bytes_per_channel,
201-
}
202-
}
203-
204-
/// Pulls out the next batch from the reader.
205-
pub(crate) fn next_batch(&mut self, cfg: &RollupConfig) -> Option<Batch> {
206-
// If the data is not already decompressed, decompress it.
207-
let mut brotli_used = false;
208-
209-
if let Some(data) = self.data.take() {
210-
// Peek at the data to determine the compression type.
211-
if data.is_empty() {
212-
warn!(target: "batch-reader", "Data is too short to determine compression type, skipping batch");
213-
return None;
214-
}
215-
216-
let compression_type = data[0];
217-
if (compression_type & 0x0F) == ZLIB_DEFLATE_COMPRESSION_METHOD ||
218-
(compression_type & 0x0F) == ZLIB_RESERVED_COMPRESSION_METHOD
219-
{
220-
self.decompressed = decompress_to_vec_zlib(&data).ok()?;
221-
222-
// Check the size of the decompressed channel RLP.
223-
if self.decompressed.len() > self.max_rlp_bytes_per_channel {
224-
return None;
225-
}
226-
} else if compression_type == CHANNEL_VERSION_BROTLI {
227-
brotli_used = true;
228-
self.decompressed =
229-
decompress_brotli(&data[1..], self.max_rlp_bytes_per_channel).ok()?;
230-
} else {
231-
error!(target: "batch-reader", "Unsupported compression type: {:x}, skipping batch", compression_type);
232-
return None;
233-
}
234-
}
235-
236-
// Decompress and RLP decode the batch data, before finally decoding the batch itself.
237-
let decompressed_reader = &mut self.decompressed.as_slice()[self.cursor..].as_ref();
238-
let bytes = Bytes::decode(decompressed_reader).ok()?;
239-
let Ok(batch) = Batch::decode(&mut bytes.as_ref(), cfg) else {
240-
error!(target: "batch-reader", "Failed to decode batch, skipping batch");
241-
return None;
242-
};
243-
244-
// Confirm that brotli decompression was performed *after* the Fjord hardfork.
245-
if brotli_used && !cfg.is_fjord_active(batch.timestamp()) {
246-
warn!(target: "batch-reader", "Brotli compression used before Fjord hardfork, skipping batch");
247-
return None;
248-
}
249-
250-
// Advance the cursor on the reader.
251-
self.cursor = self.decompressed.len() - decompressed_reader.len();
252-
Some(batch)
253-
}
254-
}
255-
256164
#[cfg(test)]
257165
mod test {
258166
use super::*;
@@ -334,24 +242,6 @@ mod test {
334242
assert!(reader.next_batch.is_some());
335243
}
336244

337-
#[test]
338-
fn test_batch_reader() {
339-
let raw = new_compressed_batch_data();
340-
let decompressed_len = decompress_to_vec_zlib(&raw).unwrap().len();
341-
let mut reader = BatchReader::new(raw, MAX_RLP_BYTES_PER_CHANNEL_BEDROCK as usize);
342-
reader.next_batch(&RollupConfig::default()).unwrap();
343-
assert_eq!(reader.cursor, decompressed_len);
344-
}
345-
346-
#[test]
347-
fn test_batch_reader_fjord() {
348-
let raw = new_compressed_batch_data();
349-
let decompressed_len = decompress_to_vec_zlib(&raw).unwrap().len();
350-
let mut reader = BatchReader::new(raw, MAX_RLP_BYTES_PER_CHANNEL_FJORD as usize);
351-
reader.next_batch(&RollupConfig { fjord_time: Some(0), ..Default::default() }).unwrap();
352-
assert_eq!(reader.cursor, decompressed_len);
353-
}
354-
355245
#[tokio::test]
356246
async fn test_flush_post_holocene() {
357247
let raw = new_compressed_batch_data();

crates/derive/src/stages/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,3 @@ pub use batch::{
3737

3838
mod attributes_queue;
3939
pub use attributes_queue::AttributesQueue;
40-
41-
mod utils;
42-
pub use utils::decompress_brotli;

crates/derive/src/stages/utils.rs

-95
This file was deleted.

0 commit comments

Comments
 (0)