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

feat: add info tx types #1793

Merged
merged 1 commit into from
Dec 13, 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
51 changes: 51 additions & 0 deletions crates/consensus/src/transaction/meta.rs
Original file line number Diff line number Diff line change
@@ -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<u64>,
/// The excess blob gas of the block.
pub excess_blob_gas: Option<u64>,
/// 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<TxHash>,
/// Index of the transaction in the block
pub index: Option<u64>,
/// Hash of the block.
pub block_hash: Option<BlockHash>,
/// Number of the block.
pub block_number: Option<u64>,
/// Base fee of the block.
pub base_fee: Option<u128>,
}

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
}
}
3 changes: 3 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
44 changes: 0 additions & 44 deletions crates/rpc-types-eth/src/transaction/common.rs

This file was deleted.

32 changes: 30 additions & 2 deletions crates/rpc-types-eth/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +79,26 @@ where
}
}

impl<T> Transaction<T>
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it take base_fee as an argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we set this via the additional builder fn: into().with_base_fee()

}
}
}

impl<T> Transaction<T>
where
T: Into<TransactionRequest>,
Expand All @@ -93,6 +112,15 @@ where
}
}

impl<T> From<&Transaction<T>> for TransactionInfo
where
T: TransactionTrait + Encodable2718,
{
fn from(tx: &Transaction<T>) -> Self {
tx.info()
}
}

impl TryFrom<Transaction> for Signed<TxLegacy> {
type Error = ConversionError;

Expand Down
Loading