chore(txpool): implement ensure_max_init_code_size() (#1062)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Kim, JinSan
2023-01-28 05:53:24 +09:00
committed by GitHub
parent 87306f2892
commit 072a6c56d3
4 changed files with 55 additions and 3 deletions

View File

@@ -25,6 +25,10 @@ pub enum PoolError {
/// respect the size limits of the pool.
#[error("[{0:?}] Transaction's gas limit {1} exceeds block's gas limit {2}.")]
TxExceedsGasLimit(TxHash, u64, u64),
/// Thrown when a new transaction is added to the pool, but then immediately discarded to
/// respect the max_init_code_size.
#[error("[{0:?}] Transaction's size {1} exceeds max_init_code_size {2}.")]
TxExceedsMaxInitCodeSize(TxHash, usize, usize),
}
// === impl PoolError ===
@@ -38,6 +42,7 @@ impl PoolError {
PoolError::SpammerExceededCapacity(_, hash) => hash,
PoolError::DiscardedOnInsert(hash) => hash,
PoolError::TxExceedsGasLimit(hash, _, _) => hash,
PoolError::TxExceedsMaxInitCodeSize(hash, _, _) => hash,
}
}
}

View File

@@ -12,7 +12,7 @@ use rand::{
prelude::Distribution,
};
use reth_primitives::{
Address, FromRecoveredTransaction, IntoRecoveredTransaction, Transaction,
Address, FromRecoveredTransaction, IntoRecoveredTransaction, Transaction, TransactionKind,
TransactionSignedEcRecovered, TxEip1559, TxHash, TxLegacy, H256, U128, U256,
};
use std::{ops::Range, sync::Arc, time::Instant};
@@ -85,6 +85,7 @@ pub enum MockTransaction {
nonce: u64,
gas_price: u128,
gas_limit: u64,
to: TransactionKind,
value: U256,
},
Eip1559 {
@@ -94,6 +95,7 @@ pub enum MockTransaction {
max_fee_per_gas: u128,
max_priority_fee_per_gas: u128,
gas_limit: u64,
to: TransactionKind,
value: U256,
},
}
@@ -117,6 +119,7 @@ impl MockTransaction {
nonce: 0,
gas_price: 0,
gas_limit: 0,
to: TransactionKind::Call(Address::random()),
value: Default::default(),
}
}
@@ -130,6 +133,7 @@ impl MockTransaction {
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE,
max_priority_fee_per_gas: MIN_PROTOCOL_BASE_FEE,
gas_limit: 0,
to: TransactionKind::Call(Address::random()),
value: Default::default(),
}
}
@@ -331,6 +335,13 @@ impl PoolTransaction for MockTransaction {
}
}
fn kind(&self) -> &TransactionKind {
match self {
MockTransaction::Legacy { to, .. } => to,
MockTransaction::Eip1559 { to, .. } => to,
}
}
fn size(&self) -> usize {
0
}
@@ -356,6 +367,7 @@ impl FromRecoveredTransaction for MockTransaction {
nonce,
gas_price,
gas_limit,
to,
value: U256::from(value),
},
Transaction::Eip1559(TxEip1559 {
@@ -375,6 +387,7 @@ impl FromRecoveredTransaction for MockTransaction {
max_fee_per_gas,
max_priority_fee_per_gas,
gas_limit,
to,
value: U256::from(value),
},
Transaction::Eip2930 { .. } => {

View File

@@ -1,7 +1,7 @@
use crate::{error::PoolResult, pool::state::SubPool, validate::ValidPoolTransaction};
use reth_primitives::{
Address, FromRecoveredTransaction, IntoRecoveredTransaction, PeerId, Transaction,
TransactionSignedEcRecovered, TxHash, H256, U256,
TransactionKind, TransactionSignedEcRecovered, TxHash, H256, U256,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt, sync::Arc};
@@ -271,6 +271,10 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + FromRecoveredTransaction {
/// This will return `None` for non-EIP1559 transactions
fn max_priority_fee_per_gas(&self) -> Option<u128>;
/// Returns the transaction's [`TransactionKind`], which is the address of the recipient or
/// [`TransactionKind::Create`] if the transaction is a contract creation.
fn kind(&self) -> &TransactionKind;
/// Returns a measurement of the heap usage of this type and all its internals.
fn size(&self) -> usize;
}
@@ -348,6 +352,12 @@ impl PoolTransaction for PooledTransaction {
}
}
/// Returns the transaction's [`TransactionKind`], which is the address of the recipient or
/// [`TransactionKind::Create`] if the transaction is a contract creation.
fn kind(&self) -> &TransactionKind {
self.transaction.kind()
}
/// Returns a measurement of the heap usage of this type and all its internals.
fn size(&self) -> usize {
self.transaction.transaction.input().len()

View File

@@ -5,7 +5,7 @@ use crate::{
identifier::{SenderId, TransactionId},
traits::{PoolTransaction, TransactionOrigin},
};
use reth_primitives::{Address, TxHash, U256};
use reth_primitives::{Address, TransactionKind, TxHash, U256};
use std::{fmt, time::Instant};
/// A Result type returned after checking a transaction's validity.
@@ -54,6 +54,30 @@ pub trait TransactionValidator: Send + Sync {
origin: TransactionOrigin,
transaction: Self::Transaction,
) -> TransactionValidationOutcome<Self::Transaction>;
/// Ensure that the code size is not greater than `max_init_code_size`.
/// `max_init_code_size` should be configurable so this will take it as an argument.
fn ensure_max_init_code_size(
&self,
transaction: Self::Transaction,
max_init_code_size: usize,
) -> Result<(), PoolError> {
// TODO check whether we are in the Shanghai stage.
// if !self.shanghai {
// return Ok(())
// }
if *transaction.kind() == TransactionKind::Create && transaction.size() > max_init_code_size
{
Err(PoolError::TxExceedsMaxInitCodeSize(
*transaction.hash(),
transaction.size(),
max_init_code_size,
))
} else {
Ok(())
}
}
}
/// A valid transaction in the pool.