Skip to main content

Mempool

Struct Mempool 

Source
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

Source

pub fn new(max_size: usize, max_tx_bytes: usize) -> Self

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn decode_payload(payload: &[u8]) -> Vec<Vec<u8>>

Reap collected payload back into individual transactions

Source

pub async fn size(&self) -> usize

Source

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.

Trait Implementations§

Source§

impl Default for Mempool

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more