Skip to content

Commit

Permalink
chore(protocol): Cleanup Examples (#278)
Browse files Browse the repository at this point in the history
### Description

Cleans up `op-alloy-protocol` examples using the exported
`decompress_brotli` method.
  • Loading branch information
refcell authored Nov 18, 2024
1 parent 17a84e1 commit ed40d3b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 46 deletions.
10 changes: 2 additions & 8 deletions book/src/examples/batch-to-frames.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ need to then be compressed prior to adding them to the
> the [`Batch`][batch] itself, handling encoding and compression, but
> this method is not available yet.
Once compressed using the helper `compress_brotli` method, the compressed
bytes can be added to a newly constructed [`ChannelOut`][channel-out].
Once compressed using the [`compress_brotli`][compress-brotli] method, the
compressed bytes can be added to a newly constructed [`ChannelOut`][channel-out].
As long as the [`ChannelOut`][channel-out] has [`ready_bytes()`][ready-bytes],
[`Frame`][frame]s can be constructed using the
[`ChannelOut::output_frame()`][output-frame] method, specifying the maximum
Expand All @@ -46,12 +46,6 @@ they can be [`Frame::encode`][encode-frame] into raw, serialized data
ready to be batch-submitted to the data-availability layer.


> [!Note]
>
> In the example below, the additional `example_transactions()` and `compress_brotli()`
> methods are helper functions that can be ignored for the sake of the example.

## Running this example:

- Clone the examples repository: `git clone git@github.com:alloy-rs/op-alloy.git`
Expand Down
7 changes: 0 additions & 7 deletions book/src/examples/frames-to-batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ brotli bytes can then be passed right into [`Batch::decode`][decode-batch]
to wind up with the example's desired [`Batch`][batch].


> [!Note]
>
> In the example below, the additional `example_transactions()` and `decompress_brotli()`
> methods are helper functions that can be ignored for the sake of the example.


## Running this example:

- Clone the examples repository: `git clone git@github.com:alloy-rs/op-alloy.git`
Expand Down
1 change: 1 addition & 0 deletions book/src/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<!-- op-alloy-protocol -->

[compress-brotli]: https://docs.rs/op-alloy-protocol/latest/op_alloy_protocol/fn.compress_brotli.html
[encode-batch]: https://docs.rs/op-alloy-protocol/latest/op_alloy_protocol/struct.SingleBatch.html#method.encode
[encode-frame]: https://docs.rs/op-alloy-protocol/latest/op_alloy_protocol/struct.Frame.html#method.encode
[add-frame]: https://docs.rs/op-alloy-protocol/latest/op_alloy_protocol/struct.Channel.html#method.add_frame
Expand Down
35 changes: 4 additions & 31 deletions crates/protocol/examples/frames_to_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{hex, Address, BlockHash, Bytes, PrimitiveSignature, U256};
use op_alloy_consensus::OpTxEnvelope;
use op_alloy_genesis::RollupConfig;
use op_alloy_protocol::{Batch, BlockInfo, Channel, Frame, SingleBatch};
use std::io::Read;
use op_alloy_protocol::{decompress_brotli, Batch, BlockInfo, Channel, Frame, SingleBatch};

fn main() {
// Raw frame data taken from the `encode_channel` example.
Expand All @@ -32,11 +31,12 @@ fn main() {
println!("Frame data: {}", hex::encode(&frame_data));

// Decompress the frame data with brotli.
let decompressed = decompress_brotli(&frame_data);
let config = RollupConfig::default();
let max = config.max_rlp_bytes_per_channel(open_block.timestamp) as usize;
let decompressed = decompress_brotli(&frame_data, max).expect("decompresses brotli");
println!("Decompressed frame data: {}", hex::encode(&decompressed));

// Decode the single batch from the decompressed data.
let config = RollupConfig::default();
let batch = Batch::decode(&mut decompressed.as_slice(), &config).expect("batch decodes");
assert_eq!(
batch,
Expand All @@ -52,33 +52,6 @@ fn main() {
println!("Successfully decoded frames into a Batch");
}

/// Decompresses the given bytes data using the Brotli decompressor implemented
/// in the [`brotli`](https://crates.io/crates/brotli) crate.
pub fn decompress_brotli(data: &[u8]) -> Vec<u8> {
let mut reader = brotli::Decompressor::new(data, 4096);
let mut buf = [0u8; 4096];
let mut written = 0;
loop {
match reader.read(&mut buf[..]) {
Err(e) => {
panic!("{}", e);
}
Ok(size) => {
if size == 0 {
break;
}
written += size;
// Re-size the buffer if needed
}
}
}

// Truncate the output buffer to the written bytes
let mut output: Vec<u8> = buf.into();
output.truncate(written);
output
}

fn example_transactions() -> Vec<Bytes> {
let mut transactions = Vec::new();

Expand Down

0 comments on commit ed40d3b

Please sign in to comment.