Skip to content

Commit

Permalink
chore(consensus): EIP-2718 Encoding Trait Impls (#300)
Browse files Browse the repository at this point in the history
### Description

Moves custom EIP-2718 encoding and decoding methods on `TxDeposit` into
trait impls for the alloy eip traits.
  • Loading branch information
refcell authored Nov 22, 2024
1 parent be93ab5 commit 863c0b8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion crates/consensus/src/hardforks/ecotone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! [Transaction]: alloy_consensus::Transaction
use alloc::{string::String, vec::Vec};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::{Hardfork, TxDeposit, UpgradeDepositSource};
Expand Down Expand Up @@ -160,7 +161,7 @@ impl Hardfork for Ecotone {
fn txs(&self) -> impl Iterator<Item = Bytes> + '_ {
Self::deposits().map(|tx| {
let mut encoded = Vec::new();
tx.eip2718_encode(&mut encoded);
tx.encode_2718(&mut encoded);
Bytes::from(encoded)
})
}
Expand Down
3 changes: 2 additions & 1 deletion crates/consensus/src/hardforks/fjord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! [Transaction]: alloy_consensus::Transaction
use alloc::{string::String, vec::Vec};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{address, hex, Address, Bytes, TxKind, B256, U256};

use crate::{Hardfork, TxDeposit, UpgradeDepositSource};
Expand Down Expand Up @@ -98,7 +99,7 @@ impl Hardfork for Fjord {
fn txs(&self) -> impl Iterator<Item = Bytes> + '_ {
Self::deposits().map(|tx| {
let mut encoded = Vec::new();
tx.eip2718_encode(&mut encoded);
tx.encode_2718(&mut encoded);
Bytes::from(encoded)
})
}
Expand Down
49 changes: 38 additions & 11 deletions crates/consensus/src/transaction/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use super::OpTxType;
use crate::DepositTransaction;
use alloc::vec::Vec;
use alloy_consensus::{Sealable, Transaction};
use alloy_eips::eip2930::AccessList;
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
};
use alloy_primitives::{
keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256,
};
Expand Down Expand Up @@ -192,13 +195,6 @@ impl TxDeposit {
self.rlp_encoded_length() + 1
}

/// EIP-2718 encode the transaction with the given signature and the default
/// type flag.
pub fn eip2718_encode(&self, out: &mut dyn BufMut) {
out.put_u8(self.tx_type() as u8);
self.rlp_encode(out);
}

fn network_header(&self) -> Header {
Header { list: false, payload_length: self.eip2718_encoded_length() }
}
Expand All @@ -212,13 +208,13 @@ impl TxDeposit {
/// Network encode the transaction with the given signature.
pub fn network_encode(&self, out: &mut dyn BufMut) {
self.network_header().encode(out);
self.eip2718_encode(out);
self.encode_2718(out);
}

/// Calculate the transaction hash.
pub fn tx_hash(&self) -> TxHash {
let mut buf = Vec::with_capacity(self.eip2718_encoded_length());
self.eip2718_encode(&mut buf);
self.encode_2718(&mut buf);
keccak256(&buf)
}

Expand Down Expand Up @@ -299,6 +295,37 @@ impl Transaction for TxDeposit {
}
}

impl Encodable2718 for TxDeposit {
fn type_flag(&self) -> Option<u8> {
Some(OpTxType::Deposit as u8)
}

fn encode_2718_len(&self) -> usize {
self.eip2718_encoded_length()
}

fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) {
out.put_u8(self.tx_type() as u8);
self.rlp_encode(out);
}
}

impl Decodable2718 for TxDeposit {
fn typed_decode(ty: u8, data: &mut &[u8]) -> Eip2718Result<Self> {
let ty: OpTxType = ty.try_into().map_err(|_| Eip2718Error::UnexpectedType(ty))?;
if ty != OpTxType::Deposit as u8 {
return Err(Eip2718Error::UnexpectedType(ty as u8));
}
let tx = Self::decode(data)?;
Ok(tx)
}

fn fallback_decode(data: &mut &[u8]) -> Eip2718Result<Self> {
let tx = Self::decode(data)?;
Ok(tx)
}
}

impl Encodable for TxDeposit {
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.rlp_encoded_fields_length() }.encode(out);
Expand Down Expand Up @@ -495,7 +522,7 @@ mod tests {
tx_deposit.network_encode(&mut buffer_with_header);

let mut buffer_without_header = BytesMut::new();
tx_deposit.eip2718_encode(&mut buffer_without_header);
tx_deposit.encode_2718(&mut buffer_without_header);

assert!(buffer_with_header.len() > buffer_without_header.len());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ impl Encodable2718 for OpTxEnvelope {
tx.eip2718_encode(out);
}
Self::Deposit(tx) => {
tx.eip2718_encode(out);
tx.encode_2718(out);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/protocol/src/deposits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Contains deposit transaction types and helper methods.
use alloc::{string::String, vec::Vec};
use alloy_eips::eip2718::Encodable2718;
use alloy_primitives::{b256, keccak256, Address, Bytes, Log, TxKind, B256, U256, U64};
use core::fmt::Display;
use op_alloy_consensus::TxDeposit;
Expand Down Expand Up @@ -334,7 +335,7 @@ pub fn decode_deposit(block_hash: B256, index: usize, log: &Log) -> Result<Bytes

// Re-encode the deposit transaction
let mut buffer = Vec::with_capacity(deposit_tx.eip2718_encoded_length());
deposit_tx.eip2718_encode(&mut buffer);
deposit_tx.encode_2718(&mut buffer);
Ok(Bytes::from(buffer))
}

Expand Down

0 comments on commit 863c0b8

Please sign in to comment.