From 2a771386b9bf9a2e8a5e9727488a5eaee53fa178 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 31 Aug 2023 15:54:34 -0700 Subject: [PATCH] feat: support pending blob fee (#4443) --- crates/transaction-pool/src/maintain.rs | 12 +++++-- crates/transaction-pool/src/noop.rs | 1 + crates/transaction-pool/src/pool/txpool.rs | 38 +++++++++++++++++++--- crates/transaction-pool/src/traits.rs | 16 +++++++-- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/crates/transaction-pool/src/maintain.rs b/crates/transaction-pool/src/maintain.rs index fd3c83a485..257c389e69 100644 --- a/crates/transaction-pool/src/maintain.rs +++ b/crates/transaction-pool/src/maintain.rs @@ -92,6 +92,7 @@ pub async fn maintain_transaction_pool( pending_basefee: latest .next_block_base_fee(chain_spec.base_fee_params) .unwrap_or_default(), + pending_blob_fee: latest.next_block_blob_fee().map(|fee| fee.saturating_to()), }; pool.set_block_info(info); } @@ -234,9 +235,11 @@ pub async fn maintain_transaction_pool( let chain_spec = client.chain_spec(); - // base fee for the next block: `new_tip+1` + // fees for the next block: `new_tip+1` let pending_block_base_fee = new_tip.next_block_base_fee(chain_spec.base_fee_params).unwrap_or_default(); + let pending_block_blob_fee = + new_tip.next_block_blob_fee().map(|fee| fee.saturating_to()); // we know all changed account in the new chain let new_changed_accounts: HashSet<_> = @@ -292,6 +295,7 @@ pub async fn maintain_transaction_pool( let update = CanonicalStateUpdate { new_tip: &new_tip.block, pending_block_base_fee, + pending_block_blob_fee, changed_accounts, // all transactions mined in the new chain need to be removed from the pool mined_transactions: new_mined_transactions.into_iter().collect(), @@ -314,9 +318,11 @@ pub async fn maintain_transaction_pool( let tip = blocks.tip(); let chain_spec = client.chain_spec(); - // base fee for the next block: `tip+1` + // fees for the next block: `tip+1` let pending_block_base_fee = tip.next_block_base_fee(chain_spec.base_fee_params).unwrap_or_default(); + let pending_block_blob_fee = + tip.next_block_blob_fee().map(|fee| fee.saturating_to()); let first_block = blocks.first(); trace!( @@ -337,6 +343,7 @@ pub async fn maintain_transaction_pool( last_seen_block_hash: tip.hash, last_seen_block_number: tip.number, pending_basefee: pending_block_base_fee, + pending_blob_fee: pending_block_blob_fee, }; pool.set_block_info(info); @@ -367,6 +374,7 @@ pub async fn maintain_transaction_pool( let update = CanonicalStateUpdate { new_tip: &tip.block, pending_block_base_fee, + pending_block_blob_fee, changed_accounts, mined_transactions, }; diff --git a/crates/transaction-pool/src/noop.rs b/crates/transaction-pool/src/noop.rs index abbbddf857..d2c8bcd713 100644 --- a/crates/transaction-pool/src/noop.rs +++ b/crates/transaction-pool/src/noop.rs @@ -38,6 +38,7 @@ impl TransactionPool for NoopTransactionPool { last_seen_block_hash: Default::default(), last_seen_block_number: 0, pending_basefee: 0, + pending_blob_fee: None, } } diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index bd6665ea63..98e07d39ca 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -18,7 +18,9 @@ use crate::{ }; use fnv::FnvHashMap; use reth_primitives::{ - constants::{ETHEREUM_BLOCK_GAS_LIMIT, MIN_PROTOCOL_BASE_FEE}, + constants::{ + eip4844::BLOB_TX_MIN_BLOB_GASPRICE, ETHEREUM_BLOCK_GAS_LIMIT, MIN_PROTOCOL_BASE_FEE, + }, Address, TxHash, H256, }; use std::{ @@ -135,9 +137,15 @@ impl TxPool { last_seen_block_hash: self.all_transactions.last_seen_block_hash, last_seen_block_number: self.all_transactions.last_seen_block_number, pending_basefee: self.all_transactions.pending_basefee, + pending_blob_fee: Some(self.all_transactions.pending_blob_fee), } } + /// Updates the tracked blob fee + fn update_blob_fee(&mut self, _pending_blob_fee: u64) { + // TODO(mattsse): update blob txs + } + /// Updates the tracked basefee /// /// Depending on the change in direction of the basefee, this will promote or demote @@ -182,11 +190,21 @@ impl TxPool { /// /// This will also apply updates to the pool based on the new base fee pub(crate) fn set_block_info(&mut self, info: BlockInfo) { - let BlockInfo { last_seen_block_hash, last_seen_block_number, pending_basefee } = info; + let BlockInfo { + last_seen_block_hash, + last_seen_block_number, + pending_basefee, + pending_blob_fee, + } = info; self.all_transactions.last_seen_block_hash = last_seen_block_hash; self.all_transactions.last_seen_block_number = last_seen_block_number; self.all_transactions.pending_basefee = pending_basefee; - self.update_basefee(pending_basefee) + self.update_basefee(pending_basefee); + + if let Some(blob_fee) = pending_blob_fee { + self.all_transactions.pending_blob_fee = blob_fee; + self.update_blob_fee(pending_basefee) + } } /// Returns an iterator that yields transactions that are ready to be included in the block. @@ -683,6 +701,8 @@ pub(crate) struct AllTransactions { last_seen_block_hash: H256, /// Expected base fee for the pending block. pending_basefee: u64, + /// Expected blob fee for the pending block. + pending_blob_fee: u64, /// Configured price bump settings for replacements price_bumps: PriceBumpConfig, } @@ -741,11 +761,18 @@ impl AllTransactions { /// Updates the block specific info fn set_block_info(&mut self, block_info: BlockInfo) { - let BlockInfo { last_seen_block_hash, last_seen_block_number, pending_basefee } = - block_info; + let BlockInfo { + last_seen_block_hash, + last_seen_block_number, + pending_basefee, + pending_blob_fee, + } = block_info; self.last_seen_block_number = last_seen_block_number; self.last_seen_block_hash = last_seen_block_hash; self.pending_basefee = pending_basefee; + if let Some(pending_blob_fee) = pending_blob_fee { + self.pending_blob_fee = pending_blob_fee; + } } /// Rechecks all transactions in the pool against the changes. @@ -1296,6 +1323,7 @@ impl Default for AllTransactions { last_seen_block_number: 0, last_seen_block_hash: Default::default(), pending_basefee: Default::default(), + pending_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE, price_bumps: Default::default(), } } diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 93f17aa45b..6ef34cb6f7 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -484,6 +484,10 @@ pub struct CanonicalStateUpdate<'a> { /// /// The base fee of a block depends on the utilization of the last block and its base fee. pub pending_block_base_fee: u64, + /// EIP-4844 blob fee of the _next_ (pending) block + /// + /// Only after Cancun + pub pending_block_blob_fee: Option, /// A set of changed accounts across a range of blocks. pub changed_accounts: Vec, /// All mined transactions in the block range. @@ -512,14 +516,15 @@ impl<'a> CanonicalStateUpdate<'a> { last_seen_block_hash: self.hash(), last_seen_block_number: self.number(), pending_basefee: self.pending_block_base_fee, + pending_blob_fee: self.pending_block_blob_fee, } } } impl<'a> fmt::Display for CanonicalStateUpdate<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{{ hash: {}, number: {}, pending_block_base_fee: {}, changed_accounts: {}, mined_transactions: {} }}", - self.hash(), self.number(), self.pending_block_base_fee, self.changed_accounts.len(), self.mined_transactions.len()) + write!(f, "{{ hash: {}, number: {}, pending_block_base_fee: {}, pending_block_blob_fee: {:?}, changed_accounts: {}, mined_transactions: {} }}", + self.hash(), self.number(), self.pending_block_base_fee, self.pending_block_blob_fee, self.changed_accounts.len(), self.mined_transactions.len()) } } @@ -921,9 +926,14 @@ pub struct BlockInfo { pub last_seen_block_number: u64, /// Currently enforced base fee: the threshold for the basefee sub-pool. /// - /// Note: this is the derived base fee of the _next_ block that builds on the clock the pool is + /// Note: this is the derived base fee of the _next_ block that builds on the block the pool is /// currently tracking. pub pending_basefee: u64, + /// Currently enforced blob fee: the threshold for eip-4844 blob transactions. + /// + /// Note: this is the derived blob fee of the _next_ block that builds on the block the pool is + /// currently tracking + pub pending_blob_fee: Option, } /// The limit to enforce for [TransactionPool::get_pooled_transaction_elements].