Skip to content

Commit

Permalink
impl Encodable and Decodable for Log
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Oct 18, 2022
1 parent 3589812 commit 6c54310
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion crates/primitives/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header};

use crate::{Address, Bytes, H256};

/// Ethereum Log
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct Log {
/// Contract that emitted this log.
pub address: Address,
Expand All @@ -10,3 +12,34 @@ pub struct Log {
/// Arbitrary length data.
pub data: Bytes,
}

impl Encodable for Log {
fn length(&self) -> usize {
let mut len = 0;
len += self.address.length();
len += self.topics.length();
len += self.data.0.length();
len + length_of_length(len)
}

fn encode(&self, out: &mut dyn bytes::BufMut) {
let header = Header { list: true, payload_length: self.length() };
header.encode(out);
self.address.encode(out);
self.topics.encode(out);
self.data.0.encode(out);
}
}

impl Decodable for Log {
fn decode(buf: &mut &[u8]) -> Result<Self, DecodeError> {
let header = Header::decode(buf)?;
if !header.list {
return Err(DecodeError::UnexpectedString)
}
let address = Decodable::decode(buf)?;
let topics = Decodable::decode(buf)?;
let data = Bytes(Decodable::decode(buf)?);
Ok(Log { address, topics, data })
}
}
2 changes: 1 addition & 1 deletion crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use tx_type::TxType;
/// Transaction type is introduced in EIP-2718: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Transaction {
/// Legacy transaciton.
/// Legacy transaction.
Legacy {
/// Added as EIP-155: Simple replay attack protection
chain_id: Option<u64>,
Expand Down

0 comments on commit 6c54310

Please sign in to comment.