From 93b89f4c171f7b39bde7356ce4256ffff83ba638 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 13 Dec 2024 15:24:57 +0100 Subject: [PATCH] feat: add info tx types --- crates/consensus/src/transaction/meta.rs | 51 +++++++++++++++++++ crates/consensus/src/transaction/mod.rs | 3 ++ .../rpc-types-eth/src/transaction/common.rs | 44 ---------------- crates/rpc-types-eth/src/transaction/mod.rs | 32 +++++++++++- 4 files changed, 84 insertions(+), 46 deletions(-) create mode 100644 crates/consensus/src/transaction/meta.rs delete mode 100644 crates/rpc-types-eth/src/transaction/common.rs diff --git a/crates/consensus/src/transaction/meta.rs b/crates/consensus/src/transaction/meta.rs new file mode 100644 index 00000000000..6aeb54ad7f3 --- /dev/null +++ b/crates/consensus/src/transaction/meta.rs @@ -0,0 +1,51 @@ +//! Commonly used types that contain metadata about a transaction. + +use alloy_primitives::{BlockHash, TxHash, B256}; + +/// Additional fields in the context of a block that contains this _mined_ transaction. +/// +/// This contains mandatory block fields (block hash, number, timestamp, index). +#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)] +pub struct TransactionMeta { + /// Hash of the transaction. + pub tx_hash: B256, + /// Index of the transaction in the block + pub index: u64, + /// Hash of the block. + pub block_hash: B256, + /// Number of the block. + pub block_number: u64, + /// Base fee of the block. + pub base_fee: Option, + /// The excess blob gas of the block. + pub excess_blob_gas: Option, + /// The block's timestamp. + pub timestamp: u64, +} + +/// Additional fields in the context of a (maybe) pending block that contains this transaction. +/// +/// This is commonly used when dealing with transactions for rpc where the block context is not +/// known. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[doc(alias = "TxInfo")] +pub struct TransactionInfo { + /// Hash of the transaction. + pub hash: Option, + /// Index of the transaction in the block + pub index: Option, + /// Hash of the block. + pub block_hash: Option, + /// Number of the block. + pub block_number: Option, + /// Base fee of the block. + pub base_fee: Option, +} + +impl TransactionInfo { + /// Returns a new [`TransactionInfo`] with the provided base fee. + pub const fn with_base_fee(mut self, base_fee: u128) -> Self { + self.base_fee = Some(base_fee); + self + } +} diff --git a/crates/consensus/src/transaction/mod.rs b/crates/consensus/src/transaction/mod.rs index 5290beea5e1..9e107740d7e 100644 --- a/crates/consensus/src/transaction/mod.rs +++ b/crates/consensus/src/transaction/mod.rs @@ -42,6 +42,9 @@ pub use rlp::RlpEcdsaTx; mod typed; pub use typed::TypedTransaction; +mod meta; +pub use meta::{TransactionInfo, TransactionMeta}; + mod recovered; pub use recovered::{Recovered, SignerRecoverable}; diff --git a/crates/rpc-types-eth/src/transaction/common.rs b/crates/rpc-types-eth/src/transaction/common.rs deleted file mode 100644 index b28bc0fd956..00000000000 --- a/crates/rpc-types-eth/src/transaction/common.rs +++ /dev/null @@ -1,44 +0,0 @@ -//! Commonly used additional types that are not part of the JSON RPC spec but are often required -//! when working with RPC types, such as [Transaction] - -use crate::Transaction; -use alloy_network_primitives::TransactionResponse; -use alloy_primitives::{BlockHash, TxHash}; - -/// Additional fields in the context of a block that contains this transaction. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -#[doc(alias = "TxInfo")] -pub struct TransactionInfo { - /// Hash of the transaction. - pub hash: Option, - /// Index of the transaction in the block - pub index: Option, - /// Hash of the block. - pub block_hash: Option, - /// Number of the block. - pub block_number: Option, - /// Base fee of the block. - pub base_fee: Option, -} - -impl TransactionInfo { - /// Returns a new [`TransactionInfo`] with the provided base fee. - pub const fn with_base_fee(mut self, base_fee: u128) -> Self { - self.base_fee = Some(base_fee); - self - } -} - -impl From<&Transaction> for TransactionInfo { - fn from(tx: &Transaction) -> Self { - Self { - hash: Some(tx.tx_hash()), - index: tx.transaction_index, - block_hash: tx.block_hash, - block_number: tx.block_number, - // We don't know the base fee of the block when we're constructing this from - // `Transaction` - base_fee: None, - } - } -} diff --git a/crates/rpc-types-eth/src/transaction/mod.rs b/crates/rpc-types-eth/src/transaction/mod.rs index ad60f3e0e7e..feb59e56793 100644 --- a/crates/rpc-types-eth/src/transaction/mod.rs +++ b/crates/rpc-types-eth/src/transaction/mod.rs @@ -14,8 +14,7 @@ pub use alloy_eips::{ eip7702::Authorization, }; -mod common; -pub use common::TransactionInfo; +pub use alloy_consensus::transaction::TransactionInfo; mod error; pub use error::ConversionError; @@ -80,6 +79,26 @@ where } } +impl Transaction +where + T: TransactionTrait + Encodable2718, +{ + /// Returns the [`TransactionInfo`] for this transaction. + /// + /// This contains various metadata about the transaction and block context if available. + pub fn info(&self) -> TransactionInfo { + TransactionInfo { + hash: Some(self.tx_hash()), + index: self.transaction_index, + block_hash: self.block_hash, + block_number: self.block_number, + // We don't know the base fee of the block when we're constructing this from + // `Transaction` + base_fee: None, + } + } +} + impl Transaction where T: Into, @@ -93,6 +112,15 @@ where } } +impl From<&Transaction> for TransactionInfo +where + T: TransactionTrait + Encodable2718, +{ + fn from(tx: &Transaction) -> Self { + tx.info() + } +} + impl TryFrom for Signed { type Error = ConversionError;