Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from anton-rs/refcell/tx-pool-updates
Browse files Browse the repository at this point in the history
feat(transaction-pool): Tx Pool Cost Updates
  • Loading branch information
refcell authored Aug 17, 2023
2 parents e458a09 + 2961b0b commit ff1f580
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use reth_primitives::{
use reth_provider::{AccountReader, HeaderProvider, WithdrawalsProvider};
use std::collections::{hash_map::Entry, HashMap};

#[cfg(feature = "optimism")]
use reth_primitives::TxDeposit;

/// Validate header standalone
pub fn validate_header_standalone(
header: &SealedHeader,
Expand Down Expand Up @@ -125,7 +122,7 @@ pub fn validate_transaction_regarding_header(
Some(*chain_id)
}
#[cfg(feature = "optimism")]
Transaction::Deposit(TxDeposit { .. }) => None,
Transaction::Deposit(_) => None,
};
if let Some(chain_id) = chain_id {
if chain_id != chain_spec.chain().id() {
Expand Down
7 changes: 6 additions & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ reth-rlp.workspace = true
once_cell = "1.17.0"

[features]
optimism = ["reth-primitives/optimism", "reth-revm-primitives/optimism"]
optimism = [
"reth-primitives/optimism",
"reth-revm-primitives/optimism",
"reth-consensus-common/optimism",
"reth-interfaces/optimism",
]

7 changes: 4 additions & 3 deletions crates/revm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,10 @@ where
#[cfg(feature = "optimism")]
{
let db = self.db();
let l1_cost = l1_block_info
.as_ref()
.map(|l1_block_info| l1_block_info.calculate_tx_l1_cost(transaction));
let l1_cost = l1_block_info.as_ref().map(|l1_block_info| {
l1_block_info
.calculate_tx_l1_cost(transaction.input(), transaction.is_deposit())
});

let sender_account =
db.load_account(sender).map_err(|_| BlockExecutionError::ProviderError)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/revm/src/optimism.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use reth_interfaces::executor;
use reth_primitives::{Address, Block, TransactionKind, TransactionSigned, U256};
use reth_primitives::{Address, Block, Bytes, TransactionKind, U256};

const L1_FEE_RECIPIENT: &str = "0x420000000000000000000000000000000000001A";
const BASE_FEE_RECIPIENT: &str = "0x4200000000000000000000000000000000000019";
Expand Down Expand Up @@ -84,12 +84,12 @@ impl TryFrom<&Block> for L1BlockInfo {

impl L1BlockInfo {
/// Calculate the gas cost of a transaction based on L1 block data posted on L2
pub fn calculate_tx_l1_cost(&self, tx: &TransactionSigned) -> U256 {
let rollup_data_gas_cost = U256::from(tx.input().iter().fold(0, |acc, byte| {
pub fn calculate_tx_l1_cost(&self, input: &Bytes, is_deposit: bool) -> U256 {
let rollup_data_gas_cost = U256::from(input.iter().fold(0, |acc, byte| {
acc + if *byte == 0x00 { ZERO_BYTE_COST } else { NON_ZERO_BYTE_COST }
}));

if tx.is_deposit() || rollup_data_gas_cost == U256::ZERO {
if is_deposit || rollup_data_gas_cost == U256::ZERO {
return U256::ZERO
}

Expand Down
8 changes: 7 additions & 1 deletion crates/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ reth-interfaces.workspace = true
reth-rlp.workspace = true
reth-metrics.workspace = true
reth-tasks.workspace = true
reth-revm.workspace = true

# async/futures
async-trait.workspace = true
Expand Down Expand Up @@ -59,7 +60,12 @@ default = ["serde"]
serde = ["dep:serde"]
test-utils = ["rand", "paste", "serde"]
arbitrary = ["proptest", "reth-primitives/arbitrary"]
optimism = ["reth-primitives/optimism"]
optimism = [
"reth-primitives/optimism",
"reth-revm/optimism",
"reth-provider/test-utils",
"reth-provider/optimism",
]

[[bench]]
name = "reorder"
Expand Down
10 changes: 5 additions & 5 deletions crates/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@
//!
//! ```
//! use reth_primitives::MAINNET;
//! use reth_provider::{ChainSpecProvider, StateProviderFactory};
//! use reth_provider::{BlockReaderIdExt, ChainSpecProvider, StateProviderFactory};
//! use reth_tasks::TokioTaskExecutor;
//! use reth_transaction_pool::{EthTransactionValidator, Pool, TransactionPool};
//! async fn t<C>(client: C) where C: StateProviderFactory + ChainSpecProvider + Clone + 'static{
//! async fn t<C>(client: C) where C: StateProviderFactory + BlockReaderIdExt + ChainSpecProvider + Clone + 'static{
//! let pool = Pool::eth_pool(
//! EthTransactionValidator::new(client, MAINNET.clone(), TokioTaskExecutor::default()),
//! Default::default(),
Expand Down Expand Up @@ -263,19 +263,19 @@ where
impl<Client>
Pool<EthTransactionValidator<Client, PooledTransaction>, CoinbaseTipOrdering<PooledTransaction>>
where
Client: StateProviderFactory + Clone + 'static,
Client: StateProviderFactory + reth_provider::BlockReaderIdExt + Clone + 'static,
{
/// Returns a new [Pool] that uses the default [EthTransactionValidator] when validating
/// [PooledTransaction]s and ords via [CoinbaseTipOrdering]
///
/// # Example
///
/// ```
/// use reth_provider::StateProviderFactory;
/// use reth_provider::{BlockReaderIdExt, StateProviderFactory};
/// use reth_primitives::MAINNET;
/// use reth_tasks::TokioTaskExecutor;
/// use reth_transaction_pool::{EthTransactionValidator, Pool};
/// # fn t<C>(client: C) where C: StateProviderFactory + Clone + 'static{
/// # fn t<C>(client: C) where C: StateProviderFactory + BlockReaderIdExt + Clone + 'static {
/// let pool = Pool::eth_pool(
/// EthTransactionValidator::new(client, MAINNET.clone(), TokioTaskExecutor::default()),
/// Default::default(),
Expand Down
10 changes: 9 additions & 1 deletion crates/transaction-pool/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
TransactionOrigin, TransactionPool, TransactionValidationOutcome, TransactionValidator,
ValidPoolTransaction,
};
use reth_primitives::{Address, TxHash};
use reth_primitives::{Address, InvalidTransactionError, TxHash};
use std::{collections::HashSet, marker::PhantomData, sync::Arc};
use tokio::sync::{mpsc, mpsc::Receiver};

Expand Down Expand Up @@ -181,6 +181,14 @@ impl<T: PoolTransaction> TransactionValidator for MockTransactionValidator<T> {
origin: TransactionOrigin,
transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction> {
#[cfg(feature = "optimism")]
if transaction.is_deposit() {
return TransactionValidationOutcome::Invalid(
transaction,
InvalidTransactionError::TxTypeNotSupported.into(),
)
}

TransactionValidationOutcome::Valid {
balance: Default::default(),
state_nonce: 0,
Expand Down
54 changes: 49 additions & 5 deletions crates/transaction-pool/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rand::{
prelude::Distribution,
};
use reth_primitives::{
constants::MIN_PROTOCOL_BASE_FEE, hex, Address, FromRecoveredTransaction,
constants::MIN_PROTOCOL_BASE_FEE, hex, Address, Bytes, FromRecoveredTransaction,
IntoRecoveredTransaction, Signature, Transaction, TransactionKind, TransactionSigned,
TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxHash, TxLegacy, TxType, H256,
U128, U256,
Expand All @@ -29,9 +29,6 @@ use reth_primitives::DEPOSIT_TX_TYPE_ID;
#[cfg(feature = "optimism")]
use reth_primitives::TxDeposit;

#[cfg(feature = "optimism")]
use reth_primitives::Bytes;

/// Create an empty `TxPool`
pub(crate) fn mock_tx_pool() -> MockTxPool {
MockTxPool::new(Default::default(), Default::default())
Expand All @@ -51,17 +48,25 @@ macro_rules! set_value {
MockTransaction::Eip4844 { ref mut $field, .. } => {
*$field = new_value;
}
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => {
panic!("not implemented")
}
}
};
}

/// Sets the value fo the field
/// Gets the value for the field
macro_rules! get_value {
($this:ident => $field:ident) => {
match $this {
MockTransaction::Legacy { $field, .. } => $field,
MockTransaction::Eip1559 { $field, .. } => $field,
MockTransaction::Eip4844 { $field, .. } => $field,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => {
panic!("not implemented")
}
}
};
}
Expand Down Expand Up @@ -123,6 +128,8 @@ pub enum MockTransaction {
to: TransactionKind,
value: U256,
},
#[cfg(feature = "optimism")]
Deposit(TxDeposit),
}

// === impl MockTransaction ===
Expand Down Expand Up @@ -232,6 +239,10 @@ impl MockTransaction {
*max_fee_per_gas = val;
*max_priority_fee_per_gas = val;
}
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => {
panic!("not implemented")
}
}
self
}
Expand All @@ -257,6 +268,10 @@ impl MockTransaction {
*max_fee_per_gas = val;
*max_priority_fee_per_gas = val;
}
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => {
panic!("not implemented")
}
}
self
}
Expand All @@ -266,6 +281,8 @@ impl MockTransaction {
MockTransaction::Legacy { gas_price, .. } => *gas_price,
MockTransaction::Eip1559 { max_fee_per_gas, .. } => *max_fee_per_gas,
MockTransaction::Eip4844 { max_fee_per_gas, .. } => *max_fee_per_gas,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => panic!("not implemented"),
}
}

Expand Down Expand Up @@ -351,6 +368,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { hash, .. } => hash,
MockTransaction::Eip1559 { hash, .. } => hash,
MockTransaction::Eip4844 { hash, .. } => hash,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(TxDeposit { source_hash, .. }) => source_hash,
}
}

Expand All @@ -359,6 +378,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { sender, .. } => *sender,
MockTransaction::Eip1559 { sender, .. } => *sender,
MockTransaction::Eip4844 { sender, .. } => *sender,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(TxDeposit { from, .. }) => *from,
}
}

Expand All @@ -367,9 +388,15 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { nonce, .. } => *nonce,
MockTransaction::Eip1559 { nonce, .. } => *nonce,
MockTransaction::Eip4844 { nonce, .. } => *nonce,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => 0u64,
}
}

fn input(&self) -> &Bytes {
panic!("not implemented")
}

fn cost(&self) -> U256 {
match self {
MockTransaction::Legacy { gas_price, value, gas_limit, .. } => {
Expand All @@ -381,6 +408,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Eip4844 { max_fee_per_gas, value, gas_limit, .. } => {
U256::from(*gas_limit) * U256::from(*max_fee_per_gas) + *value
}
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => U256::ZERO,
}
}

Expand All @@ -393,6 +422,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { gas_price, .. } => *gas_price,
MockTransaction::Eip1559 { max_fee_per_gas, .. } => *max_fee_per_gas,
MockTransaction::Eip4844 { max_fee_per_gas, .. } => *max_fee_per_gas,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => 0u128,
}
}

Expand All @@ -405,6 +436,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => {
Some(*max_priority_fee_per_gas)
}
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => None,
}
}

Expand All @@ -428,6 +461,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { gas_price, .. } => *gas_price,
MockTransaction::Eip1559 { max_priority_fee_per_gas, .. } => *max_priority_fee_per_gas,
MockTransaction::Eip4844 { max_priority_fee_per_gas, .. } => *max_priority_fee_per_gas,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => 0u128,
}
}

Expand All @@ -436,6 +471,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { to, .. } => to,
MockTransaction::Eip1559 { to, .. } => to,
MockTransaction::Eip4844 { to, .. } => to,
#[cfg(feature = "optimism")]
MockTransaction::Deposit(TxDeposit { to, .. }) => to,
}
}

Expand All @@ -448,6 +485,8 @@ impl PoolTransaction for MockTransaction {
MockTransaction::Legacy { .. } => TxType::Legacy.into(),
MockTransaction::Eip1559 { .. } => TxType::EIP1559.into(),
MockTransaction::Eip4844 { .. } => TxType::EIP4844.into(),
#[cfg(feature = "optimism")]
MockTransaction::Deposit(_) => DEPOSIT_TX_TYPE_ID,
}
}

Expand All @@ -458,6 +497,11 @@ impl PoolTransaction for MockTransaction {
fn chain_id(&self) -> Option<u64> {
Some(1)
}

#[cfg(feature = "optimism")]
fn is_deposit(&self) -> bool {
matches!(self, MockTransaction::Deposit(_))
}
}

impl FromRecoveredTransaction for MockTransaction {
Expand Down
Loading

0 comments on commit ff1f580

Please sign in to comment.