feat: transaction trait (#11877)

This commit is contained in:
tedison
2024-10-29 12:47:12 -04:00
committed by GitHub
parent 2e750f0ca0
commit 82784183e7
3 changed files with 52 additions and 19 deletions

View File

@ -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

View File

@ -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};

View File

@ -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<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;
}
#[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<T> 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>
{
}