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: transaction trait #11877

Merged
merged 13 commits into from
Oct 29, 2024
Merged
1 change: 0 additions & 1 deletion crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ reth-testing-utils.workspace = true
alloy-primitives = { workspace = true, features = ["arbitrary"] }
alloy-consensus = { workspace = true, features = ["arbitrary"] }

arbitrary = { workspace = true, features = ["derive"] }
bincode.workspace = true
proptest-arbitrary-interop.workspace = true
proptest.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern crate alloc;

/// Common constants.
pub mod constants;

pub use constants::gas_units::{format_gas, format_gas_throughput};

/// Minimal account
Expand All @@ -24,7 +25,7 @@ pub mod receipt;
pub use receipt::Receipt;

pub mod transaction;
pub use transaction::{signed::SignedTransaction, Transaction};
pub use transaction::{signed::SignedTransaction, FullTransaction, Transaction};

mod integer_list;
pub use integer_list::{IntegerList, IntegerListError};
Expand Down
55 changes: 44 additions & 11 deletions crates/primitives-traits/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
//! Transaction abstraction
emhane marked this conversation as resolved.
Show resolved Hide resolved

pub mod signed;
use core::{fmt::Debug, hash::Hash};

use alloc::fmt;
use alloy_primitives::{TxKind, B256};

use reth_codecs::Compact;
use serde::{Deserialize, Serialize};

/// Helper trait that unifies all behaviour required by transaction to support full node operations.
pub trait FullTransaction: Transaction + Compact {}

impl<T> FullTransaction for T where T: Transaction + Compact {}
Comment on lines -10 to -13
Copy link
Member

Choose a reason for hiding this comment

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

revert this, we don't want to require Compact be a super trait for Transaction

pub mod signed;

#[allow(dead_code)]
/// Abstraction of a transaction.
pub trait Transaction:
alloy_consensus::Transaction
Debug
+ Default
+ Clone
+ fmt::Debug
+ PartialEq
+ Eq
+ Default
+ PartialEq
+ Hash
+ Serialize
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ Serialize
+ for<'de> Deserialize<'de>
+ alloy_consensus::Transaction
{
/// Heavy operation that return signature hash over rlp encoded transaction.
/// It is only for signature signing or signer recovery.
fn signature_hash(&self) -> B256;

/// Gets the transaction's [`TxKind`], which is the address of the recipient or
/// [`TxKind::Create`] if the transaction is a contract creation.
fn kind(&self) -> TxKind;

/// Returns true if the tx supports dynamic fees
fn is_dynamic_fee(&self) -> bool;

/// Returns the effective gas price for the given base fee.
fn effective_gas_price(&self, base_fee: Option<u64>) -> u128;

/// This encodes the transaction _without_ the signature, and is only suitable for creating a
/// hash intended for signing.
fn encode_without_signature(&self, out: &mut dyn bytes::BufMut);

/// Calculates a heuristic for the in-memory size of the [Transaction].
fn size(&self) -> usize;
}

/// Helper trait that unifies all behaviour required by transaction to support full node operations.
#[cfg(feature = "arbitrary")]
pub trait FullTransaction: Transaction + Compact + for<'b> arbitrary::Arbitrary<'b> {}

#[cfg(feature = "arbitrary")]
impl<T> FullTransaction for T where T: Transaction + Compact + for<'b> arbitrary::Arbitrary<'b> {}

/// Helper trait that unifies all behaviour required by transaction to support full node operations.
#[cfg(not(feature = "arbitrary"))]
pub trait FullTransaction: Transaction + Compact {}

#[cfg(not(feature = "arbitrary"))]
impl<T> FullTransaction for T where T: Transaction + Compact {}
Loading