Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait methods for constructing alloy_rpc_types_eth::Transaction to alloy_consensus::Transaction #1172

Merged
merged 14 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -278,6 +278,18 @@ impl Transaction for TxEip1559 {
None
}

fn max_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
Some(self.max_priority_fee_per_gas)
}

fn priority_fee_or_price(&self) -> u128 {
self.max_priority_fee_per_gas
}

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -289,6 +301,18 @@ impl Transaction for TxEip1559 {
fn input(&self) -> &[u8] {
&self.input
}

fn ty(&self) -> u8 {
TxType::Eip2930 as u8
}

fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}
}

impl SignableTransaction<Signature> for TxEip1559 {
Expand Down
26 changes: 25 additions & 1 deletion crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -242,6 +242,18 @@ impl Transaction for TxEip2930 {
Some(self.gas_price)
}

fn max_fee_per_gas(&self) -> u128 {
self.gas_price
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
None
}

fn priority_fee_or_price(&self) -> u128 {
self.gas_price
}

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -253,6 +265,18 @@ impl Transaction for TxEip2930 {
fn input(&self) -> &[u8] {
&self.input
}

fn ty(&self) -> u8 {
TxType::Eip2930 as u8
}

fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}
}

impl SignableTransaction<Signature> for TxEip2930 {
Expand Down
87 changes: 87 additions & 0 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ impl Transaction for TxEip4844Variant {
None
}

fn max_fee_per_gas(&self) -> u128 {
match self {
Self::TxEip4844(tx) => tx.max_fee_per_gas(),
Self::TxEip4844WithSidecar(tx) => tx.max_fee_per_gas(),
}
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
match self {
Self::TxEip4844(tx) => tx.max_priority_fee_per_gas(),
Self::TxEip4844WithSidecar(tx) => tx.max_priority_fee_per_gas(),
}
}

fn priority_fee_or_price(&self) -> u128 {
match self {
Self::TxEip4844(tx) => tx.priority_fee_or_price(),
Self::TxEip4844WithSidecar(tx) => tx.priority_fee_or_price(),
}
}

fn to(&self) -> TxKind {
match self {
Self::TxEip4844(tx) => tx.to,
Expand All @@ -237,6 +258,24 @@ impl Transaction for TxEip4844Variant {
Self::TxEip4844WithSidecar(tx) => tx.tx().input.as_ref(),
}
}

fn ty(&self) -> u8 {
TxType::Eip4844 as u8
}

fn access_list(&self) -> Option<&AccessList> {
match self {
Self::TxEip4844(tx) => tx.access_list(),
Self::TxEip4844WithSidecar(tx) => tx.access_list(),
}
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
match self {
Self::TxEip4844(tx) => tx.blob_versioned_hashes(),
Self::TxEip4844WithSidecar(tx) => tx.blob_versioned_hashes(),
}
}
}

impl SignableTransaction<Signature> for TxEip4844Variant {
Expand Down Expand Up @@ -649,6 +688,18 @@ impl Transaction for TxEip4844 {
None
}

fn max_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
Some(self.max_priority_fee_per_gas)
}

fn priority_fee_or_price(&self) -> u128 {
self.max_priority_fee_per_gas
}

fn to(&self) -> TxKind {
self.to.into()
}
Expand All @@ -660,6 +711,18 @@ impl Transaction for TxEip4844 {
fn input(&self) -> &[u8] {
&self.input
}

fn ty(&self) -> u8 {
TxType::Eip4844 as u8
}

fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
Some(&self.blob_versioned_hashes)
}
}

impl Encodable for TxEip4844 {
Expand Down Expand Up @@ -890,6 +953,18 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.gas_price()
}

fn max_fee_per_gas(&self) -> u128 {
self.tx.max_fee_per_gas()
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
self.tx.max_priority_fee_per_gas()
}

fn priority_fee_or_price(&self) -> u128 {
self.tx.priority_fee_or_price()
}

fn to(&self) -> TxKind {
self.tx.to()
}
Expand All @@ -901,6 +976,18 @@ impl Transaction for TxEip4844WithSidecar {
fn input(&self) -> &[u8] {
self.tx.input()
}

fn ty(&self) -> u8 {
TxType::Eip4844 as u8
}

fn access_list(&self) -> Option<&AccessList> {
Some(&self.tx.access_list)
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
self.tx.blob_versioned_hashes()
}
}

#[cfg(test)]
Expand Down
26 changes: 25 additions & 1 deletion crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -301,6 +301,18 @@ impl Transaction for TxEip7702 {
None
}

fn max_fee_per_gas(&self) -> u128 {
self.max_fee_per_gas
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
Some(self.max_priority_fee_per_gas)
}

fn priority_fee_or_price(&self) -> u128 {
self.max_priority_fee_per_gas
}

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -312,6 +324,18 @@ impl Transaction for TxEip7702 {
fn input(&self) -> &[u8] {
&self.input
}

fn ty(&self) -> u8 {
TxType::Eip7702 as u8
}

fn access_list(&self) -> Option<&AccessList> {
Some(&self.access_list)
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}
}

impl SignableTransaction<Signature> for TxEip7702 {
Expand Down
67 changes: 66 additions & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::fmt;

use crate::{Signed, Transaction, TxEip1559, TxEip2930, TxEip7702, TxLegacy};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718};
use alloy_eips::{
eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718},
eip2930::AccessList,
};
use alloy_primitives::{TxKind, B256};
use alloy_rlp::{Decodable, Encodable, Header};

Expand Down Expand Up @@ -432,6 +437,36 @@ impl Transaction for TxEnvelope {
}
}

fn max_fee_per_gas(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.tx().max_fee_per_gas(),
Self::Eip2930(tx) => tx.tx().max_fee_per_gas(),
Self::Eip1559(tx) => tx.tx().max_fee_per_gas(),
Self::Eip4844(tx) => tx.tx().max_fee_per_gas(),
Self::Eip7702(tx) => tx.tx().max_fee_per_gas(),
}
}

fn max_priority_fee_per_gas(&self) -> Option<u128> {
match self {
Self::Legacy(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip2930(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip1559(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip4844(tx) => tx.tx().max_priority_fee_per_gas(),
Self::Eip7702(tx) => tx.tx().max_priority_fee_per_gas(),
}
}

fn priority_fee_or_price(&self) -> u128 {
match self {
Self::Legacy(tx) => tx.tx().priority_fee_or_price(),
Self::Eip2930(tx) => tx.tx().priority_fee_or_price(),
Self::Eip1559(tx) => tx.tx().priority_fee_or_price(),
Self::Eip4844(tx) => tx.tx().priority_fee_or_price(),
Self::Eip7702(tx) => tx.tx().priority_fee_or_price(),
}
}

fn input(&self) -> &[u8] {
match self {
Self::Legacy(tx) => tx.tx().input(),
Expand Down Expand Up @@ -471,6 +506,36 @@ impl Transaction for TxEnvelope {
Self::Eip7702(tx) => tx.tx().value(),
}
}

fn ty(&self) -> u8 {
match self {
Self::Legacy(tx) => tx.tx().ty(),
Self::Eip2930(tx) => tx.tx().ty(),
Self::Eip1559(tx) => tx.tx().ty(),
Self::Eip4844(tx) => tx.tx().ty(),
Self::Eip7702(tx) => tx.tx().ty(),
}
}

fn access_list(&self) -> Option<&AccessList> {
match self {
Self::Legacy(tx) => tx.tx().access_list(),
Self::Eip2930(tx) => tx.tx().access_list(),
Self::Eip1559(tx) => tx.tx().access_list(),
Self::Eip4844(tx) => tx.tx().access_list(),
Self::Eip7702(tx) => tx.tx().access_list(),
}
}

fn blob_versioned_hashes(&self) -> Option<&[B256]> {
match self {
Self::Legacy(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip2930(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip1559(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip4844(tx) => tx.tx().blob_versioned_hashes(),
Self::Eip7702(tx) => tx.tx().blob_versioned_hashes(),
}
}
}

#[cfg(test)]
Expand Down
Loading