Skip to content

Commit

Permalink
fix(primitives): re-implement RLP for Log<LogData> (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Mar 13, 2024
1 parent b0bbaa0 commit db41140
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions crates/primitives/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,38 @@ where
}

#[cfg(feature = "rlp")]
impl<T> alloy_rlp::Encodable for Log<T>
where
for<'a> &'a T: Into<LogData>,
{
impl alloy_rlp::Encodable for Log {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
let this = self.reserialize();
let payload_length =
this.address.length() + this.data.data.length() + this.data.topics.length();
self.address.length() + self.data.data.length() + self.data.topics.length();

alloy_rlp::Header { list: true, payload_length }.encode(out);
this.address.encode(out);
this.data.topics.encode(out);
this.data.data.encode(out);
self.address.encode(out);
self.data.topics.encode(out);
self.data.data.encode(out);
}

fn length(&self) -> usize {
let this = self.reserialize();
let payload_length =
this.address.length() + this.data.data.length() + this.data.topics.length();
self.address.length() + self.data.data.length() + self.data.topics.length();
payload_length + alloy_rlp::length_of_length(payload_length)
}
}

#[cfg(feature = "rlp")]
impl<T> alloy_rlp::Encodable for Log<T>
where
for<'a> &'a T: Into<LogData>,
{
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.reserialize().encode(out)
}

fn length(&self) -> usize {
self.reserialize().length()
}
}

#[cfg(feature = "rlp")]
impl alloy_rlp::Decodable for Log {
fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
Expand All @@ -186,3 +195,18 @@ impl alloy_rlp::Decodable for Log {
Ok(Self { address, data: LogData { topics, data } })
}
}

#[cfg(feature = "rlp")]
#[cfg(test)]
mod tests {
use super::*;
use alloy_rlp::{Decodable, Encodable};

#[test]
fn test_roundtrip_rlp_log_data() {
let log = Log::<LogData>::default();
let mut buf = Vec::<u8>::new();
log.encode(&mut buf);
assert_eq!(Log::decode(&mut &buf[..]).unwrap(), log);
}
}

0 comments on commit db41140

Please sign in to comment.