Skip to content

Commit

Permalink
fix API changes from tx builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Mar 11, 2024
1 parent fab095c commit 6b99d2a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 44 deletions.
24 changes: 16 additions & 8 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_network::{Signed, Transaction};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use std::mem;

Expand Down Expand Up @@ -190,9 +189,7 @@ impl TxEip1559 {
/// header.
///
/// This __does__ expect the bytes to start with a list header and include a signature.
pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -303,6 +300,18 @@ impl SignableTransaction<Signature> for TxEip1559 {
}
}

impl Encodable for TxEip1559 {
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.fields_len() }.encode(out);
self.encode_fields(out);
}

fn length(&self) -> usize {
let payload_length = self.fields_len();
Header { list: true, payload_length }.length() + payload_length
}
}

impl Decodable for TxEip1559 {
fn decode(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
let header = Header::decode(data)?;
Expand All @@ -312,7 +321,7 @@ impl Decodable for TxEip1559 {
return Err(alloy_rlp::Error::InputTooShort);
}

Self::decode_inner(data)
Self::decode_fields(data)
}
}

Expand All @@ -321,8 +330,7 @@ mod tests {
use super::TxEip1559;
use crate::SignableTransaction;
use alloy_eips::eip2930::AccessList;
use alloy_network::Transaction;
use alloy_primitives::{address, b256, hex, Address, Signature, B256, U256};
use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256};

#[test]
fn recover_signer_eip1559() {
Expand Down
12 changes: 4 additions & 8 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ impl TxEip2930 {
/// header.
///
/// This __does__ expect the bytes to start with a list header and include a signature.
pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -270,7 +268,6 @@ impl SignableTransaction<Signature> for TxEip2930 {
}
}


impl Encodable for TxEip2930 {
fn encode(&self, out: &mut dyn BufMut) {
Header { list: true, payload_length: self.fields_len() }.encode(out);
Expand All @@ -292,16 +289,15 @@ impl Decodable for TxEip2930 {
return Err(alloy_rlp::Error::InputTooShort);
}

Self::decode_inner(data)
Self::decode_fields(data)
}
}

#[cfg(test)]
mod tests {
use super::TxEip2930;
use crate::{TxEnvelope, TxKind};
use alloy_network::Transaction;
use alloy_primitives::{Address, Bytes, Signature, U256};
use crate::{SignableTransaction, TxEnvelope};
use alloy_primitives::{Address, Bytes, Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};

#[test]
Expand Down
25 changes: 7 additions & 18 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ impl TxEip4844Variant {
}
}

pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let mut current_buf = *buf;
let _header = Header::decode(&mut current_buf)?;

Expand Down Expand Up @@ -576,9 +574,7 @@ impl TxEip4844 {
/// header.
///
/// This __does__ expect the bytes to start with a list header and include a signature.
pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -653,10 +649,6 @@ impl SignableTransaction<Signature> for TxEip4844 {
}

impl Transaction for TxEip4844 {
fn chain_id(&self) -> Option<ChainId> {
Some(self.chain_id)
}

fn input(&self) -> &[u8] {
&self.input
}
Expand Down Expand Up @@ -707,7 +699,7 @@ impl Decodable for TxEip4844 {
return Err(alloy_rlp::Error::InputTooShort);
}

Self::decode_inner(data)
Self::decode_fields(data)
}
}

Expand Down Expand Up @@ -812,9 +804,7 @@ impl TxEip4844WithSidecar {
/// This __does__ expect the bytes to start with a list header and include a signature.
///
/// This is the inverse of [TxEip4844WithSidecar::encode_with_signature_fields].
pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -862,7 +852,7 @@ impl SignableTransaction<Signature> for TxEip4844WithSidecar {
self.tx.encode_for_signing(out);
}

fn into_signed(self, signature: Signature) -> Signed<Self, Self::Signature> {
fn into_signed(self, signature: Signature) -> Signed<Self, Signature> {
let mut buf = Vec::with_capacity(self.tx.encoded_len_with_signature(&signature, false));
// The sidecar is NOT included in the signed payload, only the transaction fields and the
// type byte. Include the type byte.
Expand Down Expand Up @@ -1041,11 +1031,10 @@ pub(crate) fn kzg_to_versioned_hash(commitment: KzgCommitment) -> B256 {
#[cfg(test)]
mod tests {
use super::{BlobTransactionSidecar, TxEip4844, TxEip4844WithSidecar};
use crate::{TxEnvelope, TxKind};
use crate::{SignableTransaction, TxEnvelope};
#[cfg(not(feature = "kzg"))]
use alloy_eips::eip4844::{Blob, Bytes48};
use alloy_network::Transaction;
use alloy_primitives::{Signature, U256};
use alloy_primitives::{Signature, TxKind, U256};
use alloy_rlp::{Decodable, Encodable};
#[cfg(feature = "kzg")]
use c_kzg::{Blob, Bytes48};
Expand Down
7 changes: 4 additions & 3 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy};
use crate::{
Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar, TxLegacy,
};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy_network::Signed;
use alloy_rlp::{Decodable, Encodable, Header};

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], and
Expand Down Expand Up @@ -229,7 +230,7 @@ impl Encodable2718 for TxEnvelope {
fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) {
match self {
// Legacy transactions have no difference between network and 2718
TxEnvelope::Legacy(_) => self.network_encode(out),
TxEnvelope::Legacy(tx) => tx.tx().encode_with_signature_fields(tx.signature(), out),
TxEnvelope::Eip2930(tx) => {
tx.tx().encode_with_signature(tx.signature(), out, false);
}
Expand Down
9 changes: 3 additions & 6 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ impl TxLegacy {
/// header.
///
/// This __does__ expect the bytes to start with a list header and include a signature.
pub(crate) fn decode_signed_fields(
buf: &mut &[u8],
) -> alloy_rlp::Result<alloy_network::Signed<Self>> {
pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -279,9 +277,8 @@ impl Decodable for TxLegacy {

#[cfg(all(test, feature = "k256"))]
mod tests {
use crate::{TxKind, TxLegacy};
use alloy_network::Transaction;
use alloy_primitives::{address, b256, hex, Address, Signature, B256, U256};
use crate::{SignableTransaction, TxLegacy};
use alloy_primitives::{address, b256, hex, Address, Signature, TxKind, B256, U256};

#[test]
fn recover_signer_legacy() {
Expand Down
1 change: 0 additions & 1 deletion crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::Signed;
use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256};
use alloy_rlp::BufMut;

mod eip1559;
pub use eip1559::TxEip1559;
Expand Down

0 comments on commit 6b99d2a

Please sign in to comment.