Skip to content

Commit

Permalink
move byte wrapping into RLP encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Dec 18, 2023
1 parent ed6d81f commit 34b7292
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
6 changes: 2 additions & 4 deletions lib/src/optimism/batcher_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,8 @@ impl Channel {
let mut batches = Vec::new();

while !channel_data.is_empty() {
let batch_data = Header::decode_bytes(&mut channel_data, false)
.context("failed to decode batch data")?;

let mut batch = Batch::decode(&mut &batch_data[..])?;
let mut batch =
Batch::decode(&mut channel_data).context("failed to decode batch data")?;
batch.inclusion_block_number = l1_block_number;

batches.push(batch);
Expand Down
57 changes: 47 additions & 10 deletions primitives/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::cmp::Ordering;
use alloy_primitives::{BlockNumber, Bytes, B256};
use alloy_rlp::{Decodable, Encodable};
use alloy_rlp_derive::{RlpDecodable, RlpEncodable};
use bytes::Buf;

pub type RawTransaction = Bytes;

Expand Down Expand Up @@ -72,28 +71,66 @@ impl Batch {
impl Encodable for Batch {
#[inline]
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
// wrap the RLP-essence inside a bytes payload
alloy_rlp::Header {
list: false,
payload_length: self.essence.length() + 1,
}
.encode(out);
out.put_u8(0);
self.essence.encode(out);
}

#[inline]
fn length(&self) -> usize {
self.essence.length() + 1
let bytes_length = self.essence.length() + 1;
alloy_rlp::length_of_length(bytes_length) + bytes_length
}
}

impl Decodable for Batch {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
match buf.first() {
Some(0) => {
buf.advance(1);
Ok(Self {
inclusion_block_number: 0,
essence: BatchEssence::decode(buf)?,
})
}
let bytes = alloy_rlp::Header::decode_bytes(buf, false)?;
match bytes.split_first() {
Some((0, mut payload)) => Ok(Self {
inclusion_block_number: 0,
essence: BatchEssence::decode(&mut payload)?,
}),
Some(_) => Err(alloy_rlp::Error::Custom("invalid version")),
None => Err(alloy_rlp::Error::InputTooShort),
}
}
}

#[cfg(test)]
mod tests {
use alloy_primitives::{b256, hex::FromHex};

use super::*;

#[test]
fn rlp_roundtrip() {
let batch = Batch {
inclusion_block_number: 0,
essence: BatchEssence {
parent_hash: b256!(
"55b11b918355b1ef9c5db810302ebad0bf2544255b530cdce90674d5887bb286"
),
epoch_num: 1,
epoch_hash: b256!(
"1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
),
timestamp: 1647026951,
transactions: vec![
Bytes::from_hex("0x000000").unwrap(),
Bytes::from_hex("0x76fd7c").unwrap(),
],
},
};

let encoded = alloy_rlp::encode(&batch);
assert_eq!(encoded.len(), batch.length());
let decoded = Batch::decode(&mut &encoded[..]).unwrap();
assert_eq!(batch, decoded);
}
}

0 comments on commit 34b7292

Please sign in to comment.