Skip to content

Commit

Permalink
feat(protocol): ZLIB Compression (#264)
Browse files Browse the repository at this point in the history
### Description

Adds zlib compression to the `ChannelOut`.

Closes #267
  • Loading branch information
refcell authored Nov 16, 2024
1 parent 8f0d2ad commit bd32aad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ snap = "1.1.1"
bincode = "1.3.3"
ethereum_ssz = "0.8"
brotli = { version = "7.0.0", default-features = false }
miniz_oxide = "0.8.0"

# rpc
jsonrpsee = { version = "0.24", features = [
Expand Down
5 changes: 4 additions & 1 deletion crates/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-consensus.workspace = true

# Misc
# Compression
brotli.workspace = true
miniz_oxide.workspace = true

# Misc
tracing.workspace = true
derive_more.workspace = true
async-trait.workspace = true
Expand Down
17 changes: 10 additions & 7 deletions crates/protocol/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use op_alloy_genesis::RollupConfig;

use crate::{block::BlockInfo, frame::Frame};

/// The best compression.
const BEST_COMPRESSION: u8 = 9;

/// The frame overhead.
const FRAME_V0_OVERHEAD: usize = 23;

Expand Down Expand Up @@ -75,7 +78,13 @@ impl<'a> ChannelOut<'a> {
return Err(ChannelOutError::ChannelClosed);
}

self.compressed = Some(crate::compress_brotli(&buf).into());
if self.config.is_fjord_active(batch.timestamp()) {
self.compressed = Some(crate::compress_brotli(&buf).into());
} else {
self.compressed =
Some(miniz_oxide::deflate::compress_to_vec(&buf, BEST_COMPRESSION).into());
}

Ok(())
}

Expand All @@ -84,12 +93,6 @@ impl<'a> ChannelOut<'a> {
self.compressed.as_ref().map_or(0, |c| c.len())
}

/// Accepts the raw compressed batch data into the [ChannelOut].
#[deprecated]
pub fn add_raw_compressed_batch(&mut self, compressed: Bytes) {
self.compressed = Some(compressed);
}

/// Closes the channel if not already closed.
pub fn close(&mut self) {
self.closed = true;
Expand Down

0 comments on commit bd32aad

Please sign in to comment.