chore(txpool): move basefee to u64 (#3872)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Roman Krasiuk
2023-07-24 13:08:34 +03:00
committed by GitHub
parent 95cbff31f9
commit c878a9f489
15 changed files with 46 additions and 47 deletions

View File

@ -581,7 +581,7 @@ fn build_payload<Pool, Client>(
let base_fee = initialized_block_env.basefee.to::<u64>();
let mut executed_txs = Vec::new();
let mut best_txs = pool.best_transactions_with_base_fee(base_fee as u128);
let mut best_txs = pool.best_transactions_with_base_fee(base_fee);
let mut total_fees = U256::ZERO;

View File

@ -39,7 +39,7 @@ pub const ETHEREUM_BLOCK_GAS_LIMIT: u64 = 30_000_000;
/// The `BASE_FEE_MAX_CHANGE_DENOMINATOR` <https://eips.ethereum.org/EIPS/eip-1559> is `8`, or 12.5%.
/// Once the base fee has dropped to `7` WEI it cannot decrease further because 12.5% of 7 is less
/// than 1.
pub const MIN_PROTOCOL_BASE_FEE: u128 = 7;
pub const MIN_PROTOCOL_BASE_FEE: u64 = 7;
/// Same as [MIN_PROTOCOL_BASE_FEE] but as a U256.
pub const MIN_PROTOCOL_BASE_FEE_U256: U256 = U256::from_limbs([7u64, 0, 0, 0]);
@ -114,6 +114,6 @@ mod tests {
#[test]
fn min_protocol_sanity() {
assert_eq!(MIN_PROTOCOL_BASE_FEE_U256.to::<u128>(), MIN_PROTOCOL_BASE_FEE);
assert_eq!(MIN_PROTOCOL_BASE_FEE_U256.to::<u64>(), MIN_PROTOCOL_BASE_FEE);
}
}

View File

@ -462,7 +462,7 @@ impl Transaction {
///
/// This is different than the `max_priority_fee_per_gas` method, which returns `None` for
/// non-EIP-1559 transactions.
pub(crate) fn priority_fee_or_price(&self) -> u128 {
pub fn priority_fee_or_price(&self) -> u128 {
match self {
Transaction::Legacy(TxLegacy { gas_price, .. }) |
Transaction::Eip2930(TxEip2930 { gas_price, .. }) => *gas_price,

View File

@ -50,7 +50,7 @@ impl PendingBlockEnv {
let block_number = block_env.number.to::<u64>();
let mut executed_txs = Vec::new();
let mut best_txs = pool.best_transactions_with_base_fee(base_fee as u128);
let mut best_txs = pool.best_transactions_with_base_fee(base_fee);
while let Some(pool_tx) = best_txs.next() {
// ensure we still have capacity for this transaction

View File

@ -399,7 +399,7 @@ where
fn best_transactions_with_base_fee(
&self,
base_fee: u128,
base_fee: u64,
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>> {
self.pool.best_transactions_with_base_fee(base_fee)
}

View File

@ -83,7 +83,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
let info = BlockInfo {
last_seen_block_hash: latest.hash,
last_seen_block_number: latest.number,
pending_basefee: latest.next_block_base_fee().unwrap_or_default() as u128,
pending_basefee: latest.next_block_base_fee().unwrap_or_default(),
};
pool.set_block_info(info);
}
@ -205,8 +205,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
}
// base fee for the next block: `new_tip+1`
let pending_block_base_fee =
new_tip.next_block_base_fee().unwrap_or_default() as u128;
let pending_block_base_fee = new_tip.next_block_base_fee().unwrap_or_default();
// we know all changed account in the new chain
let new_changed_accounts: HashSet<_> =
@ -282,7 +281,7 @@ pub async fn maintain_transaction_pool<Client, P, St, Tasks>(
let tip = blocks.tip();
// base fee for the next block: `tip+1`
let pending_block_base_fee = tip.next_block_base_fee().unwrap_or_default() as u128;
let pending_block_base_fee = tip.next_block_base_fee().unwrap_or_default();
let first_block = blocks.first();
trace!(

View File

@ -112,7 +112,7 @@ impl TransactionPool for NoopTransactionPool {
fn best_transactions_with_base_fee(
&self,
_: u128,
_: u64,
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>> {
Box::new(std::iter::empty())
}

View File

@ -18,7 +18,7 @@ use tracing::debug;
/// This iterator guarantees that all transaction it returns satisfy the base fee.
pub(crate) struct BestTransactionsWithBasefee<T: TransactionOrdering> {
pub(crate) best: BestTransactions<T>,
pub(crate) base_fee: u128,
pub(crate) base_fee: u64,
}
impl<T: TransactionOrdering> crate::traits::BestTransactions for BestTransactionsWithBasefee<T> {
@ -34,7 +34,7 @@ impl<T: TransactionOrdering> Iterator for BestTransactionsWithBasefee<T> {
// find the next transaction that satisfies the base fee
loop {
let best = self.best.next()?;
if best.transaction.max_fee_per_gas() < self.base_fee {
if best.transaction.max_fee_per_gas() < self.base_fee as u128 {
// tx violates base fee, mark it as invalid and continue
crate::traits::BestTransactions::mark_invalid(self, &best);
} else {

View File

@ -467,7 +467,7 @@ where
/// the given base fee.
pub(crate) fn best_transactions_with_base_fee(
&self,
base_fee: u128,
base_fee: u64,
) -> Box<dyn crate::traits::BestTransactions<Item = Arc<ValidPoolTransaction<T::Transaction>>>>
{
self.pool.read().best_transactions_with_base_fee(base_fee)

View File

@ -115,7 +115,7 @@ impl<T: PoolTransaction> ParkedPool<BasefeeOrd<T>> {
/// Note: this does _not_ remove the transactions
pub(crate) fn satisfy_base_fee_transactions(
&self,
basefee: u128,
basefee: u64,
) -> Vec<Arc<ValidPoolTransaction<T>>> {
let ids = self.satisfy_base_fee_ids(basefee);
let mut txs = Vec::with_capacity(ids.len());
@ -126,13 +126,13 @@ impl<T: PoolTransaction> ParkedPool<BasefeeOrd<T>> {
}
/// Returns all transactions that satisfy the given basefee.
fn satisfy_base_fee_ids(&self, basefee: u128) -> Vec<TransactionId> {
fn satisfy_base_fee_ids(&self, basefee: u64) -> Vec<TransactionId> {
let mut transactions = Vec::new();
{
let mut iter = self.by_id.iter().peekable();
while let Some((id, tx)) = iter.next() {
if tx.transaction.transaction.max_fee_per_gas() < basefee {
if tx.transaction.transaction.max_fee_per_gas() < basefee as u128 {
// still parked -> skip descendant transactions
'this: while let Some((peek, _)) = iter.peek() {
if peek.sender != id.sender {
@ -152,7 +152,7 @@ impl<T: PoolTransaction> ParkedPool<BasefeeOrd<T>> {
/// satisfy the given basefee.
///
/// Note: the transactions are not returned in a particular order.
pub(crate) fn enforce_basefee(&mut self, basefee: u128) -> Vec<Arc<ValidPoolTransaction<T>>> {
pub(crate) fn enforce_basefee(&mut self, basefee: u64) -> Vec<Arc<ValidPoolTransaction<T>>> {
let to_remove = self.satisfy_base_fee_ids(basefee);
let mut removed = Vec::with_capacity(to_remove.len());
@ -330,10 +330,10 @@ mod tests {
assert!(pool.by_id.contains_key(tx.id()));
assert_eq!(pool.len(), 1);
let removed = pool.enforce_basefee(u128::MAX);
let removed = pool.enforce_basefee(u64::MAX);
assert!(removed.is_empty());
let removed = pool.enforce_basefee(tx.max_fee_per_gas() - 1);
let removed = pool.enforce_basefee((tx.max_fee_per_gas() - 1) as u64);
assert_eq!(removed.len(), 1);
assert!(pool.is_empty());
}
@ -353,14 +353,14 @@ mod tests {
assert!(pool.by_id.contains_key(descendant_tx.id()));
assert_eq!(pool.len(), 2);
let removed = pool.enforce_basefee(u128::MAX);
let removed = pool.enforce_basefee(u64::MAX);
assert!(removed.is_empty());
// two dependent tx in the pool with decreasing fee
{
let mut pool2 = pool.clone();
let removed = pool2.enforce_basefee(descendant_tx.max_fee_per_gas());
let removed = pool2.enforce_basefee(descendant_tx.max_fee_per_gas() as u64);
assert_eq!(removed.len(), 1);
assert_eq!(pool2.len(), 1);
// descendant got popped
@ -369,7 +369,7 @@ mod tests {
}
// remove root transaction via root tx fee
let removed = pool.enforce_basefee(root_tx.max_fee_per_gas());
let removed = pool.enforce_basefee(root_tx.max_fee_per_gas() as u64);
assert_eq!(removed.len(), 2);
assert!(pool.is_empty());
}

View File

@ -86,7 +86,7 @@ impl<T: TransactionOrdering> PendingPool<T> {
}
/// Same as `best` but only returns transactions that satisfy the given basefee.
pub(crate) fn best_with_basefee(&self, base_fee: u128) -> BestTransactionsWithBasefee<T> {
pub(crate) fn best_with_basefee(&self, base_fee: u64) -> BestTransactionsWithBasefee<T> {
BestTransactionsWithBasefee { best: self.best(), base_fee }
}
@ -135,14 +135,14 @@ impl<T: TransactionOrdering> PendingPool<T> {
/// Note: the transactions are not returned in a particular order.
pub(crate) fn enforce_basefee(
&mut self,
basefee: u128,
basefee: u64,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
let mut to_remove = Vec::new();
{
let mut iter = self.by_id.iter().peekable();
while let Some((id, tx)) = iter.next() {
if tx.transaction.transaction.max_fee_per_gas() < basefee {
if tx.transaction.transaction.max_fee_per_gas() < basefee as u128 {
// this transaction no longer satisfies the basefee: remove it and all its
// descendants
to_remove.push(*id);
@ -346,7 +346,7 @@ mod tests {
let removed = pool.enforce_basefee(0);
assert!(removed.is_empty());
let removed = pool.enforce_basefee(tx.max_fee_per_gas() + 1);
let removed = pool.enforce_basefee((tx.max_fee_per_gas() + 1) as u64);
assert_eq!(removed.len(), 1);
assert!(pool.is_empty());
}
@ -375,7 +375,7 @@ mod tests {
{
let mut pool2 = pool.clone();
let removed = pool2.enforce_basefee(descendant_tx.max_fee_per_gas() + 1);
let removed = pool2.enforce_basefee((descendant_tx.max_fee_per_gas() + 1) as u64);
assert_eq!(removed.len(), 1);
assert_eq!(pool2.len(), 1);
// descendant got popped
@ -384,7 +384,7 @@ mod tests {
}
// remove root transaction via fee
let removed = pool.enforce_basefee(root_tx.max_fee_per_gas() + 1);
let removed = pool.enforce_basefee((root_tx.max_fee_per_gas() + 1) as u64);
assert_eq!(removed.len(), 2);
assert!(pool.is_empty());
}

View File

@ -142,7 +142,7 @@ impl<T: TransactionOrdering> TxPool<T> {
///
/// Depending on the change in direction of the basefee, this will promote or demote
/// transactions from the basefee pool.
fn update_basefee(&mut self, pending_basefee: u128) {
fn update_basefee(&mut self, pending_basefee: u64) {
match pending_basefee.cmp(&self.all_transactions.pending_basefee) {
Ordering::Equal => {
// fee unchanged, nothing to update
@ -195,7 +195,7 @@ impl<T: TransactionOrdering> TxPool<T> {
/// the given base fee.
pub(crate) fn best_transactions_with_base_fee(
&self,
basefee: u128,
basefee: u64,
) -> Box<dyn crate::traits::BestTransactions<Item = Arc<ValidPoolTransaction<T::Transaction>>>>
{
match basefee.cmp(&self.all_transactions.pending_basefee) {
@ -649,7 +649,7 @@ pub(crate) struct AllTransactions<T: PoolTransaction> {
/// Minimum base fee required by the protocol.
///
/// Transactions with a lower base fee will never be included by the chain
minimal_protocol_basefee: u128,
minimal_protocol_basefee: u64,
/// The max gas limit of the block
block_gas_limit: u64,
/// Max number of executable transaction slots guaranteed per account
@ -665,7 +665,7 @@ pub(crate) struct AllTransactions<T: PoolTransaction> {
/// The current block hash the pool keeps track of.
last_seen_block_hash: H256,
/// Expected base fee for the pending block.
pending_basefee: u128,
pending_basefee: u64,
}
impl<T: PoolTransaction> AllTransactions<T> {
@ -812,7 +812,7 @@ impl<T: PoolTransaction> AllTransactions<T> {
tx.state.insert(TxState::NO_PARKED_ANCESTORS);
// Update the first transaction of this sender.
Self::update_tx_base_fee(&self.pending_basefee, tx);
Self::update_tx_base_fee(self.pending_basefee, tx);
// Track if the transaction's sub-pool changed.
Self::record_subpool_update(&mut updates, tx);
@ -858,7 +858,7 @@ impl<T: PoolTransaction> AllTransactions<T> {
has_parked_ancestor = !tx.state.is_pending();
// Update and record sub-pool changes.
Self::update_tx_base_fee(&self.pending_basefee, tx);
Self::update_tx_base_fee(self.pending_basefee, tx);
Self::record_subpool_update(&mut updates, tx);
// Advance iterator
@ -887,9 +887,9 @@ impl<T: PoolTransaction> AllTransactions<T> {
}
/// Rechecks the transaction's dynamic fee condition.
fn update_tx_base_fee(pending_block_base_fee: &u128, tx: &mut PoolInternalTransaction<T>) {
fn update_tx_base_fee(pending_block_base_fee: u64, tx: &mut PoolInternalTransaction<T>) {
// Recheck dynamic fee condition.
match tx.transaction.max_fee_per_gas().cmp(pending_block_base_fee) {
match tx.transaction.max_fee_per_gas().cmp(&(pending_block_base_fee as u128)) {
Ordering::Greater | Ordering::Equal => {
tx.state.insert(TxState::ENOUGH_FEE_CAP_BLOCK);
}
@ -1070,10 +1070,10 @@ impl<T: PoolTransaction> AllTransactions<T> {
// Check dynamic fee
let fee_cap = transaction.max_fee_per_gas();
if fee_cap < self.minimal_protocol_basefee {
if fee_cap < self.minimal_protocol_basefee as u128 {
return Err(InsertErr::FeeCapBelowMinimumProtocolFeeCap { transaction, fee_cap })
}
if fee_cap >= self.pending_basefee {
if fee_cap >= self.pending_basefee as u128 {
state.insert(TxState::ENOUGH_FEE_CAP_BLOCK);
}
@ -1570,7 +1570,7 @@ mod tests {
let first_in_pool = pool.get(first.id()).unwrap();
assert!(tx.get_gas_price() < pool.pending_basefee);
assert!(tx.get_gas_price() < pool.pending_basefee as u128);
// has nonce gap
assert!(!first_in_pool.state.contains(TxState::NO_NONCE_GAPS));
@ -1671,7 +1671,7 @@ mod tests {
assert_eq!(pool.pending_pool.len(), 1);
pool.update_basefee(tx.max_fee_per_gas() + 1);
pool.update_basefee((tx.max_fee_per_gas() + 1) as u64);
assert!(pool.pending_pool.is_empty());
assert_eq!(pool.basefee_pool.len(), 1);

View File

@ -131,8 +131,8 @@ impl MockTransaction {
hash: H256::random(),
sender: Address::random(),
nonce: 0,
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE,
max_priority_fee_per_gas: MIN_PROTOCOL_BASE_FEE,
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
max_priority_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128,
gas_limit: 0,
to: TransactionKind::Call(Address::random()),
value: Default::default(),

View File

@ -181,7 +181,7 @@ pub trait TransactionPool: Send + Sync + Clone {
/// Consumer: Block production
fn best_transactions_with_base_fee(
&self,
base_fee: u128,
base_fee: u64,
) -> Box<dyn BestTransactions<Item = Arc<ValidPoolTransaction<Self::Transaction>>>>;
/// Returns all transactions that can be included in the next block.
@ -397,7 +397,7 @@ pub struct CanonicalStateUpdate {
/// EIP-1559 Base fee of the _next_ (pending) block
///
/// The base fee of a block depends on the utilization of the last block and its base fee.
pub pending_block_base_fee: u128,
pub pending_block_base_fee: u64,
/// A set of changed accounts across a range of blocks.
pub changed_accounts: Vec<ChangedAccount>,
/// All mined transactions in the block range.
@ -677,7 +677,7 @@ pub struct BlockInfo {
///
/// Note: this is the derived base fee of the _next_ block that builds on the clock the pool is
/// currently tracking.
pub pending_basefee: u128,
pub pending_basefee: u64,
}
/// A Stream that yields full transactions the subpool

View File

@ -164,7 +164,7 @@ impl<T: PoolTransaction> ValidPoolTransaction<T> {
self.transaction.cost()
}
/// Returns the gas cost for this transaction.
/// Returns the effective tip for this transaction.
///
/// For EIP-1559 transactions: `max_fee_per_gas * gas_limit`.
/// For legacy transactions: `gas_price * gas_limit`.