From 82784183e74deb78556eb01da5b015904eb8d194 Mon Sep 17 00:00:00 2001 From: tedison <76473430+edisontim@users.noreply.github.com> Date: Tue, 29 Oct 2024 12:47:12 -0400 Subject: [PATCH] feat: transaction trait (#11877) --- crates/primitives-traits/Cargo.toml | 1 - crates/primitives-traits/src/lib.rs | 3 +- .../primitives-traits/src/transaction/mod.rs | 67 ++++++++++++++----- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/crates/primitives-traits/Cargo.toml b/crates/primitives-traits/Cargo.toml index 4319232f8..6cafe8b8b 100644 --- a/crates/primitives-traits/Cargo.toml +++ b/crates/primitives-traits/Cargo.toml @@ -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 diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index 57d1119b0..0489a250b 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -14,6 +14,7 @@ extern crate alloc; /// Common constants. pub mod constants; + pub use constants::gas_units::{format_gas, format_gas_throughput}; /// Minimal account @@ -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}; diff --git a/crates/primitives-traits/src/transaction/mod.rs b/crates/primitives-traits/src/transaction/mod.rs index a306c5f76..a1ad81ab3 100644 --- a/crates/primitives-traits/src/transaction/mod.rs +++ b/crates/primitives-traits/src/transaction/mod.rs @@ -1,28 +1,61 @@ //! Transaction abstraction -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}; +pub mod signed; + +#[allow(dead_code)] +/// Abstraction of a transaction. +pub trait Transaction: + Debug + + Default + + Clone + + Eq + + PartialEq + + Hash + + Serialize + + alloy_rlp::Encodable + + alloy_rlp::Decodable + + for<'de> Deserialize<'de> + + alloy_consensus::Transaction + + MaybeArbitrary +{ + /// 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) -> 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; +} + +#[cfg(not(feature = "arbitrary"))] +/// Helper trait that requires arbitrary implementation if the feature is enabled. +pub trait MaybeArbitrary {} + +#[cfg(feature = "arbitrary")] +/// Helper trait that requires arbitrary implementation if the feature is enabled. +pub trait MaybeArbitrary: for<'a> arbitrary::Arbitrary<'a> {} + /// Helper trait that unifies all behaviour required by transaction to support full node operations. pub trait FullTransaction: Transaction + Compact {} impl FullTransaction for T where T: Transaction + Compact {} - -/// Abstraction of a transaction. -pub trait Transaction: - alloy_consensus::Transaction - + Clone - + fmt::Debug - + PartialEq - + Eq - + Default - + alloy_rlp::Encodable - + alloy_rlp::Decodable - + Serialize - + for<'de> Deserialize<'de> -{ -}