Skip to content

Commit

Permalink
fix(protocol): remove expect in brotli compression
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Nov 21, 2024
1 parent 05b0227 commit d340778
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 36 deletions.
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_0.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef90afea0877a6077205782ea15a6dc8699fa5ebcec5e0f4389f09cb8eda09488231346f89442100000000000000000000000000000000000008080808305b8d880b90abd3630383036303430353233343830313536313030313035373630303038306664356235303631303533653830363130303230363030303339363030306633666536303830363034303532333438303135363130303130353736303030383066643562353036303034333631303631303066353537363030303335363065303163383036333833383166353861313136313030393735373830363363353938353931383131363130303636353738303633633539383539313831343631303232393537383036336535393162323832313436313032343935373830363365383162326336643134363130323839353738303633663832303631343031343631303239323537363030303830666435623830363338333831663538613134363130316533353738303633386232333966373331343631303166373537383036333965386334393636313436313032303035373830363362383037373765613134363130323039353736303030383066643562383036333534666434643530313136313030643335373830363335346664346435303134363130313333353738303633356366323439363931343631303137633537383036333634636132336566313436313031383535373830363336386435646361363134363130316232353736303030383066643562383036333031356438656239313436313030666135373830363330396264356136303134363130313066353738303633343430613565323031343631303132623537356236303030383066643562363130313064363130313038333636303034363130343463353635623631303239623536356230303562363130313138363030323534383135363562363034303531393038313532363032303031356236303430353138303931303339306633356236313031306436313033646135363562363130313666363034303531383036303430303136303430353238303630303538313532363032303031376633313265333232653330303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030383135323530383135363562363034303531363130313232393139303631303462653536356236313031313836303031353438313536356236303033353436313031393939303637666666666666666666666666666666663136383135363562363034303531363766666666666666666666666666666666393039313136383135323630323030313631303132323536356236303033353436313031636539303638303130303030303030303030303030303030393030343633666666666666666631363831353635623630343035313633666666666666666639303931313638313532363032303031363130313232353635623630303035343631303139393930363766666666666666666666666666666666313638313536356236313031313836303035353438313536356236313031313836303036353438313536356236303030353436313031393939303638303130303030303030303030303030303030393030343637666666666666666666666666666666663136383135363562363030333534363130316365393036633031303030303030303030303030303030303030303030303030393030343633666666666666666631363831353635623631303236343733646561646465616464656164646561646465616464656164646561646465616464656164303030313831353635623630343035313733666666666666666666666666666666666666666666666666666666666666666666666666666666663930393131363831353236303230303136313031323235363562363130313138363030343534383135363562363130313138363030373534383135363562333337336465616464656164646561646465616464656164646561646465616464656164646561643030303131343631303334323537363034303531376630386333373961303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030383135323630323036303034383230313532363033623630323438323031353237663463333134323663366636333662336132303666366536633739323037343638363532303634363537303666373336393734366637323230363136333633366636303434383230313532376637353665373432303633363136653230373336353734323034633331323036323663366636333662323037363631366337353635373330303030303030303030363036343832303135323630383430313630343035313830393130333930666435623630303038303534363766666666666666666666666666666666393838393136363830313030303030303030303030303030303030323766666666666666666666666666666666666666666666666666666666666666666630303030303030303030303030303030303030303030303030303030303030303930393131363939383931363939393039393137393839303938313739303937353536303031393439303934353536303032393239303932353536303033383035343931393039343136376666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666630303030303030303030303030303030393139303931313631373930393235353630303439313930393135353630303535353630303635353536356233333733646561646465616464656164646561646465616464656164646561646465616464656164303030313134363130343033353736333363633530623435363030303532363030343630316366643562363030343335363038303163363030333535363031343335363038303163363030303535363032343335363030313535363034343335363030373535363036343335363030323535363038343335363030343535353635623830333536376666666666666666666666666666666638313136383131343631303434373537363030303830666435623931393035303536356236303030383036303030383036303030383036303030383036313031303038393862303331323135363130343639353736303030383066643562363130343732383936313034326635363562393735303631303438303630323038613031363130343266353635623936353036303430383930313335393535303630363038393031333539343530363130343963363038303861303136313034326635363562393739613936393935303934393739333936393536306130383530313335393535303630633038353031333539343630653030313335393335303931353035303536356236303030363032303830383335323833353138303832383530313532363030303562383138313130313536313034656235373835383130313833303135313835383230313630343030313532383230313631303463663536356238313831313131353631303466643537363030303630343038333837303130313532356235303630316630313766666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666665303136393239303932303136303430303139333932353035303530353666656131363437333666366336333433303030383066303030610a
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_1.hex

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_2.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef86aa018acb38c5ff1c238a7460ebc1b421fa49ec4874bdf1e0a530d234104e5e67dbc940000000000000000000000000000000000000000944210000000000000000000000000000000000000808082c35080983659cfe607dbe8500fc591d1852b76fee44d5a05e13097ff
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_3.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef86aa0ee4f9385eceef498af0be7ec5862229f426dec41c8d42397c7257a5117d9230a940000000000000000000000000000000000000000944210000000000000000000000000000000000001808082c35080983659cfe6b528d11cc114e026f138fe568744c6d45ce6da7a
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_4.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef857a00c1cb38e99dbc9cbfab3bb80863380b0905290b37eb3d6ab18dc01c1f3e75f9394421000000000000000000000000000000000000094b528d11cc114e026f138fe568744c6d45ce6da7a808083013880808422b908b3
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/ecotone_tx_5.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef90115a069b763c48478b9dc2f65ada09b3d92133ec592ea715ec65ad6e7f3dc519dc00c940b799c86a49deeb90402691f1041aa3af2d3c8758080808303d09080b8d536303631383036303039356633393566663333333733666666666666666666666666666666666666666666666666666666666666666666666666666666653134363034643537363032303336313436303234353735663566666435623566333538303135363034393537363230303166666638313036393038313534313436303363353735663566666435623632303031666666303135343566353236303230356666333562356635666664356236323030316666663432303634323831353535663335393036323030316666663031353530300a
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/fjord_tx_0.hex

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/fjord_tx_1.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef86aa01e6bb0c28bfab3dc9b36ffb0f721f00d6937f33577606325692db0965a7d58c694000000000000000000000000000000000000000094b528d11cc114e026f138fe568744c6d45ce6da7a808082c35080983659cfe6a919894851548179a0750865e7974da599c0fac7
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/bytecode/fjord_tx_2.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7ef857a0bac7bb0d5961cad209a345408b0280a0d4686b1b20665e1b0f9cdafd73b19b6b94deaddeaddeaddeaddeaddeaddeaddeaddead000194b528d11cc114e026f138fe568744c6d45ce6da7a808083015f9080848e98b106
39 changes: 28 additions & 11 deletions crates/consensus/src/hardforks/ecotone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
//! [Transaction]: alloy_consensus::Transaction

use alloc::{string::String, vec::Vec};
use alloy_consensus::Sealable;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::{OpTxEnvelope, TxDeposit, UpgradeDepositSource};
use crate::{Hardfork, TxDeposit, UpgradeDepositSource};

/// The Ecotone network upgrade transactions.
#[derive(Debug, Default, Clone, Copy)]
Expand Down Expand Up @@ -155,20 +153,39 @@ impl Ecotone {
])
.into_iter()
}

/// Returns the list of [OpTxEnvelope]s for the Ecotone network upgrade.
pub fn enveloped_txs() -> impl Iterator<Item = OpTxEnvelope> {
Self::deposits().map(|deposit| OpTxEnvelope::from(deposit.seal_slow()))
}
}

impl super::Hardfork for Ecotone {
impl Hardfork for Ecotone {
/// Constructs the Ecotone network upgrade transactions.
fn txs(&self) -> impl Iterator<Item = Bytes> + '_ {
Self::enveloped_txs().map(|tx| {
Self::deposits().map(|tx| {
let mut encoded = Vec::new();
tx.encode_2718(&mut encoded);
tx.eip2718_encode(&mut encoded);
Bytes::from(encoded)
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

#[test]
fn test_ecotone_txs_encoded() {
let ecotone_upgrade_tx = Ecotone.txs().collect::<Vec<_>>();
assert_eq!(ecotone_upgrade_tx.len(), 6);

let expected_txs: Vec<Bytes> = vec![
hex::decode(include_bytes!("./bytecode/ecotone_tx_0.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/ecotone_tx_1.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/ecotone_tx_2.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/ecotone_tx_3.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/ecotone_tx_4.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/ecotone_tx_5.hex")).unwrap().into(),
];
for (i, expected) in expected_txs.iter().enumerate() {
assert_eq!(ecotone_upgrade_tx[i], *expected);
}
}
}
37 changes: 26 additions & 11 deletions crates/consensus/src/hardforks/fjord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
//!
//! [Transaction]: alloy_consensus::Transaction

use crate::{OpTxEnvelope, TxDeposit, UpgradeDepositSource};
use alloc::{string::String, vec::Vec};
use alloy_consensus::Sealable;
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::{Hardfork, TxDeposit, UpgradeDepositSource};

/// The Fjord network upgrade transactions.
#[derive(Debug, Default, Clone, Copy)]
pub struct Fjord;
Expand Down Expand Up @@ -92,20 +91,36 @@ impl Fjord {
])
.into_iter()
}

/// Returns the list of [OpTxEnvelope]s for the Fjord network upgrade.
pub fn enveloped_txs() -> impl Iterator<Item = OpTxEnvelope> {
Self::deposits().map(|deposit| OpTxEnvelope::Deposit(deposit.seal_slow()))
}
}

impl super::Hardfork for Fjord {
impl Hardfork for Fjord {
/// Constructs the Fjord network upgrade transactions.
fn txs(&self) -> impl Iterator<Item = Bytes> + '_ {
Self::enveloped_txs().map(|tx| {
Self::deposits().map(|tx| {
let mut encoded = Vec::new();
tx.encode_2718(&mut encoded);
tx.eip2718_encode(&mut encoded);
Bytes::from(encoded)
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;

#[test]
fn test_fjord_txs_encoded() {
let fjord_upgrade_tx = Fjord.txs().collect::<Vec<_>>();
assert_eq!(fjord_upgrade_tx.len(), 3);

let expected_txs: Vec<Bytes> = vec![
hex::decode(include_bytes!("./bytecode/fjord_tx_0.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/fjord_tx_1.hex")).unwrap().into(),
hex::decode(include_bytes!("./bytecode/fjord_tx_2.hex")).unwrap().into(),
];
for (i, expected) in expected_txs.iter().enumerate() {
assert_eq!(fjord_upgrade_tx[i], *expected);
}
}
}
1 change: 1 addition & 0 deletions crates/consensus/src/hardforks/forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use alloc::vec::Vec;
/// assert_eq!(fjord_upgrade_txs.collect::<Vec<_>>().len(), 3);
/// ```
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub struct Hardforks;

impl Hardforks {
Expand Down
61 changes: 61 additions & 0 deletions crates/protocol/src/brotli.rs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion crates/protocol/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ impl<'a> ChannelOut<'a> {
}

if self.config.is_fjord_active(batch.timestamp()) {
self.compressed = Some(crate::compress_brotli(&buf).into());
let level = crate::BrotliLevel::Brotli10;
let compressed = crate::compress_brotli(&buf, level)
.map_err(|_| ChannelOutError::BrotliCompression)?;
self.compressed = Some(compressed.into());
} else {
self.compressed =
Some(miniz_oxide::deflate::compress_to_vec(&buf, BEST_COMPRESSION).into());
Expand Down
6 changes: 3 additions & 3 deletions crates/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ pub use frame::{
};

mod brotli;
pub use brotli::{decompress_brotli, BatchDecompressionError};
#[cfg(feature = "std")]
pub use brotli::compress_brotli;
pub use brotli::{decompress_brotli, BatchDecompressionError, BrotliLevel};

mod iter;
pub use iter::FrameIter;

mod utils;
#[cfg(feature = "std")]
pub use utils::compress_brotli;
pub use utils::{read_tx_data, starts_with_2718_deposit, to_system_config};

mod channel;
Expand Down
10 changes: 0 additions & 10 deletions crates/protocol/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ use crate::{
SpanDecodingError,
};

/// Compresses the given bytes data using the Brotli compressor implemented
/// in the [`brotli`](https://crates.io/crates/brotli) crate.
#[cfg(feature = "std")]
pub fn compress_brotli(mut input: &[u8]) -> Vec<u8> {
use brotli::enc::{BrotliCompress, BrotliEncoderParams};
let mut output = alloc::vec![];
BrotliCompress(&mut input, &mut output, &BrotliEncoderParams::default()).expect("succeeds");
output
}

/// Returns if the given `value` is a deposit transaction.
pub fn starts_with_2718_deposit<B>(value: &B) -> bool
where
Expand Down

0 comments on commit d340778

Please sign in to comment.