pub struct Mempool { /* private fields */ }Expand description
Priority-based mempool with deduplication, eviction, and replace-by-fee (RBF).
Transactions are ordered by priority (highest first). When the pool is full, a new transaction with higher priority than the lowest-priority entry will evict it. This prevents spam DoS and enables fee-based ordering for DeFi applications.
RBF: submitting the same tx bytes with a higher priority replaces the existing pending entry. This allows wallets to bump fees on stuck txs.
Implementations§
Source§impl Mempool
impl Mempool
pub fn new(max_size: usize, max_tx_bytes: usize) -> Self
Sourcepub async fn add_tx(&self, tx: Vec<u8>, priority: u64) -> bool
pub async fn add_tx(&self, tx: Vec<u8>, priority: u64) -> bool
Add a transaction with a given priority.
Returns true if accepted. When the pool is full, the new tx is
accepted only if its priority exceeds the lowest-priority entry,
which is then evicted.
Replace-by-fee (RBF): if the same tx bytes are already pending with a lower priority, the existing entry is replaced with the new higher-priority one. This lets wallets bump stuck transactions.
Sourcepub async fn add_tx_with_gas(
&self,
tx: Vec<u8>,
priority: u64,
gas_wanted: u64,
) -> bool
pub async fn add_tx_with_gas( &self, tx: Vec<u8>, priority: u64, gas_wanted: u64, ) -> bool
Add a transaction with priority and gas_wanted.
Sourcepub async fn collect_payload(&self, max_bytes: usize) -> Vec<u8> ⓘ
pub async fn collect_payload(&self, max_bytes: usize) -> Vec<u8> ⓘ
Collect transactions for a block proposal (up to max_bytes and max_gas total).
Collected transactions are removed from the pool and the seen set.
Transactions are collected in priority order (highest first).
The payload is length-prefixed: [u32_le len][bytes]...
max_gas of 0 disables gas accounting (byte limit only).
Sourcepub async fn collect_payload_with_gas(
&self,
max_bytes: usize,
max_gas: u64,
) -> Vec<u8> ⓘ
pub async fn collect_payload_with_gas( &self, max_bytes: usize, max_gas: u64, ) -> Vec<u8> ⓘ
Collect with both byte and gas limits.
Skips transactions that exceed the remaining gas budget (instead of stopping) to avoid head-of-line starvation by a single high-gas tx.
Sourcepub fn decode_payload(payload: &[u8]) -> Vec<Vec<u8>>
pub fn decode_payload(payload: &[u8]) -> Vec<Vec<u8>>
Reap collected payload back into individual transactions
pub async fn size(&self) -> usize
Sourcepub async fn recheck(&self, validator: impl Fn(&[u8]) -> Option<(u64, u64)>)
pub async fn recheck(&self, validator: impl Fn(&[u8]) -> Option<(u64, u64)>)
Re-validate all pending transactions after a block commit.
Calls validator on each pending tx. If it returns None, the tx
is evicted (no longer valid against updated state). If it returns
Some((priority, gas_wanted)), the tx is kept with possibly updated
priority.