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 to alloy_network_primitives::TransactionResponse #1173

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
32e2904
Add trait methods to alloy_consensus::Transaction needed for reth int…
emhane Aug 21, 2024
924227b
Implement alloy_consensus::Transaction gas trait methods
emhane Aug 21, 2024
7cd7e2b
Implement alloy_consensus::Transaction tx type trait method
emhane Aug 21, 2024
0fce052
Merge branch 'main' into emhane/tx-trait
emhane Aug 21, 2024
b6f2210
fixup! Implement alloy_consensus::Transaction gas trait methods
emhane Aug 21, 2024
ca57840
fixup! Implement alloy_consensus::Transaction tx type trait method
emhane Aug 21, 2024
da14c42
Implement alloy_consensus::Transaction access list trait method
emhane Aug 21, 2024
40d5796
Implement alloy_consensus::Transaction blob versioned hashes trait me…
emhane Aug 21, 2024
76f9d3e
Implement alloy_consensus::Transaction effective tip per gas as defau…
emhane Aug 21, 2024
ca6a44c
Remove alloy_consensus::Transaction::hash in favour of using alloy_co…
emhane Aug 21, 2024
9ed90b0
Implement alloy_consensus::Transaction::fill for alloy_rpc_types_eth:…
emhane Aug 21, 2024
223460c
Add alloy_consensus::Transaction::max_fee_per_blob_gas trait method
emhane Aug 21, 2024
28a8266
Add alloy_consensus::Transaction::authorization_list & conversion bet…
emhane Aug 21, 2024
b8f104b
Impl no-std support
emhane Aug 21, 2024
e689b0a
Fix lint
emhane Aug 21, 2024
e97281d
Fix lint
emhane Aug 21, 2024
f84b9d6
Return slice instead of vec
emhane Aug 22, 2024
66328a3
Fix docs
emhane Aug 22, 2024
9471f6c
Merge branch 'emhane/tx-trait' into emhane/tx-resp-trait
emhane Aug 22, 2024
1192bad
Merge branch 'main' into emhane/tx-resp-trait
emhane Aug 22, 2024
fa5800b
Merge branch 'main' into emhane/tx-resp-trait
emhane Aug 23, 2024
57b8dc2
Merge branch 'main' into emhane/tx-resp-trait
emhane Aug 26, 2024
b6c82d3
Merge branch 'main' into emhane/tx-resp-trait
emhane Aug 26, 2024
6275d28
Implement conversion from alloy_primitives::Signature to alloy_rpc_ty…
emhane Aug 26, 2024
5110b84
Merge branch 'emhane/signature' into emhane/tx-resp-trait
emhane Aug 26, 2024
688bcb8
Remove outdated comment
emhane Aug 26, 2024
74c9bf7
Mark out eip4844 fields
emhane Aug 26, 2024
be6a133
Merge branch 'main' into emhane/tx-resp-trait
emhane Aug 28, 2024
b18979b
Move alloy_rpc_types_eth::TransactionInfo into crate alloy-network-pr…
emhane Aug 28, 2024
ec17b2d
Reduce params of trait method TransactionResponse::fill using alloy_n…
emhane Aug 28, 2024
8c2cc54
Fix lint
emhane Aug 28, 2024
45612d8
Merge branch 'main' into emhane/tx-resp-trait
emhane Sep 5, 2024
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
10 changes: 9 additions & 1 deletion crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;
Expand Down Expand Up @@ -290,6 +290,10 @@ impl Transaction for TxEip1559 {
self.max_priority_fee_per_gas
}

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

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -313,6 +317,10 @@ impl Transaction for TxEip1559 {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

impl SignableTransaction<Signature> for TxEip1559 {
Expand Down
10 changes: 9 additions & 1 deletion crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloy_eips::eip2930::AccessList;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
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 @@ -254,6 +254,10 @@ impl Transaction for TxEip2930 {
self.gas_price
}

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

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -277,6 +281,10 @@ impl Transaction for TxEip2930 {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

impl SignableTransaction<Signature> for TxEip2930 {
Expand Down
29 changes: 28 additions & 1 deletion crates/consensus/src/transaction/eip4844.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, eip4844::DATA_GAS_PER_BLOB};
use alloy_eips::{eip2930::AccessList, eip4844::DATA_GAS_PER_BLOB, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;
Expand Down Expand Up @@ -237,6 +237,13 @@ impl Transaction for TxEip4844Variant {
}
}

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

fn to(&self) -> TxKind {
match self {
Self::TxEip4844(tx) => tx.to,
Expand Down Expand Up @@ -276,6 +283,10 @@ impl Transaction for TxEip4844Variant {
Self::TxEip4844WithSidecar(tx) => tx.blob_versioned_hashes(),
}
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

impl SignableTransaction<Signature> for TxEip4844Variant {
Expand Down Expand Up @@ -700,6 +711,10 @@ impl Transaction for TxEip4844 {
self.max_priority_fee_per_gas
}

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

fn to(&self) -> TxKind {
self.to.into()
}
Expand All @@ -723,6 +738,10 @@ impl Transaction for TxEip4844 {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
Some(&self.blob_versioned_hashes)
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

impl Encodable for TxEip4844 {
Expand Down Expand Up @@ -965,6 +984,10 @@ impl Transaction for TxEip4844WithSidecar {
self.tx.priority_fee_or_price()
}

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

fn to(&self) -> TxKind {
self.tx.to()
}
Expand All @@ -988,6 +1011,10 @@ impl Transaction for TxEip4844WithSidecar {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
self.tx.blob_versioned_hashes()
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

#[cfg(test)]
Expand Down
8 changes: 8 additions & 0 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ impl Transaction for TxEip7702 {
self.max_priority_fee_per_gas
}

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

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -336,6 +340,10 @@ impl Transaction for TxEip7702 {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
Some(&self.authorization_list)
}
}

impl SignableTransaction<Signature> for TxEip7702 {
Expand Down
20 changes: 20 additions & 0 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ impl Transaction for TxEnvelope {
}
}

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

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

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

#[cfg(test)]
Expand Down
10 changes: 9 additions & 1 deletion crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::mem;

use alloy_eips::eip2930::AccessList;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};

Expand Down Expand Up @@ -232,6 +232,10 @@ impl Transaction for TxLegacy {
self.gas_price
}

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

fn to(&self) -> TxKind {
self.to
}
Expand All @@ -255,6 +259,10 @@ impl Transaction for TxLegacy {
fn blob_versioned_hashes(&self) -> Option<&[B256]> {
None
}

fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
None
}
}

impl SignableTransaction<Signature> for TxLegacy {
Expand Down
18 changes: 15 additions & 3 deletions crates/consensus/src/transaction/mod.rs
emhane marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Transaction types.

use crate::Signed;
use alloy_eips::eip2930::AccessList;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, ChainId, TxKind, B256, U256};
use core::any;

Expand Down Expand Up @@ -56,14 +56,21 @@ pub trait Transaction: any::Any + Send + Sync + 'static {
///
/// For legacy transactions this is `gas_price`.
///
/// This is also commonly referred to as the "Gas Fee Cap" (`GasFeeCap`).
/// This is also commonly referred to as the "Gas Fee Cap".
fn max_fee_per_gas(&self) -> u128;

/// Returns the EIP-1559 Priority fee the caller is paying to the block author.
///
/// This will return `None` for non-EIP1559 transactions
fn max_priority_fee_per_gas(&self) -> Option<u128>;

/// Max fee per blob gas for EIP-4844 transaction.
///
/// Returns `None` for non-eip4844 transactions.
///
/// This is also commonly referred to as the "Blob Gas Fee Cap".
fn max_fee_per_blob_gas(&self) -> Option<u128>;

/// Return the max priority fee per gas if the transaction is an EIP-1559 transaction, and
/// otherwise return the gas price.
///
Expand Down Expand Up @@ -107,13 +114,18 @@ pub trait Transaction: any::Any + Send + Sync + 'static {
/// Returns the transaction type
fn ty(&self) -> u8;

/// Returns the EIP2930 `access_list` for the particular transaction type. Returns `None` for
/// Returns the EIP-2930 `access_list` for the particular transaction type. Returns `None` for
/// older transaction types.
fn access_list(&self) -> Option<&AccessList>;

/// Blob versioned hashes for eip4844 transaction. For previous transaction types this is
/// `None`.
fn blob_versioned_hashes(&self) -> Option<&[B256]>;

/// Returns the [`SignedAuthorization`] list of the transaction.
///
/// Returns `None` if this transaction is not EIP-7702.
fn authorization_list(&self) -> Option<&[SignedAuthorization]>;
}

/// A signable transaction.
Expand Down
22 changes: 21 additions & 1 deletion crates/consensus/src/transaction/typed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use alloy_eips::eip2930::AccessList;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{ChainId, TxKind, B256};

use crate::{
Expand Down Expand Up @@ -209,6 +209,16 @@ impl Transaction for TypedTransaction {
}
}

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

fn to(&self) -> TxKind {
match self {
Self::Legacy(tx) => tx.to(),
Expand Down Expand Up @@ -268,6 +278,16 @@ impl Transaction for TypedTransaction {
Self::Eip7702(tx) => tx.blob_versioned_hashes(),
}
}

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

#[cfg(feature = "serde")]
Expand Down
1 change: 1 addition & 0 deletions crates/network-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ workspace = true
[dependencies]
alloy-primitives.workspace = true
alloy-serde.workspace = true
alloy-consensus.workspace = true

serde.workspace = true

Expand Down
38 changes: 37 additions & 1 deletion crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, U256};
use alloy_consensus::Signed;
use alloy_primitives::{Address, BlockHash, Bytes, TxHash, B256, U256};
use alloy_serde::WithOtherFields;

use crate::BlockTransactions;
Expand Down Expand Up @@ -46,6 +47,22 @@ pub trait TransactionResponse {
/// Input data
#[doc(alias = "calldata")]
fn input(&self) -> &Bytes;

/// Returns the gas price formatted for the RPC response.
fn gas_price(tx: &impl alloy_consensus::Transaction, base_fee: Option<u64>) -> u128;

/// Returns the max fee per gas.
fn max_fee_per_gas(tx: impl alloy_consensus::Transaction) -> Option<u128>;
emhane marked this conversation as resolved.
Show resolved Hide resolved

/// Assemble from signed transaction.
fn fill(
signed_tx: Signed<impl alloy_consensus::Transaction>,
signer: Address,
block_hash: Option<B256>,
block_number: Option<u64>,
base_fee: Option<u64>,
transaction_index: Option<usize>,
) -> Self;
emhane marked this conversation as resolved.
Show resolved Hide resolved
}

/// Header JSON-RPC response.
Expand Down Expand Up @@ -107,6 +124,25 @@ impl<T: TransactionResponse> TransactionResponse for WithOtherFields<T> {
fn input(&self) -> &Bytes {
self.inner.input()
}

fn gas_price(tx: &impl alloy_consensus::Transaction, base_fee: Option<u64>) -> u128 {
T::gas_price(tx, base_fee)
}

fn max_fee_per_gas(tx: impl alloy_consensus::Transaction) -> Option<u128> {
T::max_fee_per_gas(tx)
}
emhane marked this conversation as resolved.
Show resolved Hide resolved

fn fill(
signed_tx: Signed<impl alloy_consensus::Transaction>,
signer: Address,
block_hash: Option<B256>,
block_number: Option<u64>,
base_fee: Option<u64>,
transaction_index: Option<usize>,
) -> Self {
emhane marked this conversation as resolved.
Show resolved Hide resolved
Self::new(T::fill(signed_tx, signer, block_hash, block_number, base_fee, transaction_index))
}
}

impl<T: ReceiptResponse> ReceiptResponse for WithOtherFields<T> {
Expand Down
Loading