Skip to main content

Mempool

Struct Mempool 

Source
pub struct Mempool {
    pub entries: Slab<MempoolEntry>,
    pub by_txid: HashMap<Txid, EntryId>,
    pub funding: BTreeSet<(ScriptHash, EntryId)>,
    pub spending: BTreeSet<(OutPoint, EntryId)>,
    pub pareto: ParetoFront,
    pub limits: MempoolLimits,
    /* private fields */
}
Expand description

In-memory transaction pool with txid, funding, spending, and fee-priority indexes.

Fields§

§entries: Slab<MempoolEntry>

Entry arena. Public ids are slab indices represented as u32.

§by_txid: HashMap<Txid, EntryId>

Transaction id to entry id lookup.

§funding: BTreeSet<(ScriptHash, EntryId)>

Funding index keyed by script hash then entry id.

§spending: BTreeSet<(OutPoint, EntryId)>

Spending index keyed by spent outpoint then entry id.

§pareto: ParetoFront

Fee-priority index for mining and eviction consumers.

§limits: MempoolLimits

Active mempool policy limits.

Implementations§

Source§

impl Mempool

Source

pub fn new(limits: MempoolLimits) -> Self

Creates an empty mempool with the supplied limits.

Source

pub fn clear(&mut self)

Removes all entries from the pool, clears every index, and bumps the sequence counter to signal a wholesale invalidation to subscribers.

Source

pub fn sequence_number(&self) -> u64

Returns the current sequence number. Increments on every insert/remove.

Source

pub const fn min_relay_fee_sat_per_kvb(&self) -> u64

Returns the configured min-relay-fee rate in sat/kvB.

Source

pub fn insert_entry( &mut self, entry: MempoolEntry, ) -> Result<EntryId, MempoolError>

Inserts an entry after applying ancestor and descendant policy checks.

Source

pub fn len(&self) -> usize

Returns the number of transactions in the mempool.

Source

pub fn contains_txid(&self, txid: &Txid) -> bool

Returns true when txid is present in the mempool.

Constant-time wrapper over by_txid.contains_key. Cheaper than entry() for callers that only need a presence check.

Source

pub fn entry_by_txid(&self, txid: &Txid) -> Option<&MempoolEntry>

Returns a reference to the MempoolEntry for txid, or None if the transaction is not in the pool.

Composite of self.by_txid.get(txid) and self.entry(*id). Saves the 2-step lookup pattern at electrum/rpc handler callsites.

Source

pub fn transaction_by_txid(&self, txid: &Txid) -> Option<Arc<Transaction>>

Returns a clone of the shared Arc<Transaction> for txid, or None if the transaction is not in the pool.

Cheaper than [entry_by_txid] when only the transaction body is needed — no MempoolEntry indirection, just an Arc::clone.

Source

pub fn is_empty(&self) -> bool

Returns whether the pool currently holds zero entries.

Source

pub fn tx_count(&self) -> usize

Returns the count of in-pool transactions.

Source

pub fn iter_txids(&self) -> Vec<Txid>

Returns the txids of every entry in the pool.

Order is the underlying slab iteration order (i.e., NOT fee-rate sorted; use iter_by_fee_rate_desc for that).

Source

pub fn iter_replaceable_txids(&self) -> Vec<Txid>

Returns txids of mempool entries signalling BIP-125 RBF eligibility.

An entry is replaceable when ANY of its inputs has sequence < 0xFFFFFFFE (the BIP-125 opt-in convention). Used by fee-bumping and replacement-eligibility queries.

Source

pub fn total_vsize(&self) -> u64

Returns the total virtual size of all entries.

Source

pub fn enforce_size_limit(&mut self, max_bytes: u64) -> Vec<EntryId>

Evicts the lowest-fee packages until the pool’s total vsize is at or below max_bytes. Returns the evicted entry ids.

Delegates to the free-function evict_lowest_fee_packages; removals bump the sequence counter through remove_entry_and_descendants.

Source

pub fn aggregate_fees(&self) -> u64

Returns the sum of fees of all entries in the pool, in satoshis.

Used by getmempoolinfo.total_fee (BTC = sats / 1e8). Wraps a single linear pass over self.entries. Saturating add prevents overflow on a pathological pool (would require ~92 quadrillion sat aggregate, well above the 21M BTC monetary cap, so saturation here is purely defensive).

Source

pub fn stats(&self) -> MempoolStats

Returns aggregate counters for the current pool.

Source

pub fn entry(&self, id: EntryId) -> Option<&MempoolEntry>

Returns an entry by public id.

Source

pub fn iter_by_fee_rate_desc(&self) -> Vec<EntryId>

Returns mempool entry ids in order of descending fee_rate (sat/kvB).

Walks entries and sorts; cost O(N log N) per call. Used by mining template builders and fee estimators that want fee-ordered traversal without going through ParetoFront (which uses ancestor-aware scoring).

Source

pub fn lowest_fee_rate(&self) -> Option<u64>

Returns the minimum fee_rate (sat/kvB) among all entries, or None for an empty pool.

Linear pass over self.entries. Used for mempoolminfee tightening and fee-histogram tail bounds. The min is over MempoolEntry.fee_rate which is already pre-computed at insert time.

Source

pub fn iter_above_fee_rate(&self, threshold_sat_per_kvb: u64) -> Vec<EntryId>

Returns mempool entry ids whose fee_rate >= threshold_sat_per_kvb.

Linear scan over entries. Used by mining template builders and eviction strategies that want a fee-rate cohort without sorting.

Source

pub fn find_by_outpoint(&self, outpoint: &OutPoint) -> Option<Txid>

Returns the txid of the in-mempool transaction that spends outpoint, or None if no entry spends it. Linear scan over entries; acceptable for early IBD where the pool is small, future strands may add a per- outpoint index.

Source

pub fn is_outpoint_spent(&self, outpoint: &OutPoint) -> bool

Returns whether any in-pool transaction spends outpoint.

Composes find_by_outpoint(...).is_some(). Cheaper than find_by_outpoint when only the presence answer matters — the wider accessor returns the spending txid which callers may not need.

Source

pub fn prioritise(&mut self, txid: Txid, fee_delta: i64) -> bool

Adjusts the effective fee of txid in the pool by fee_delta satoshis.

The delta can be negative (saturating at 0). Bumps the entry’s fee, recomputes fee_rate against the existing vsize, and propagates the realized delta into ancestor and descendant aggregate fees. Returns true when the txid was present and the adjustment was applied; false when the txid was not in the mempool.

Source

pub fn remove_entry_and_descendants(&mut self, id: EntryId) -> Vec<EntryId>

Removes an entry and all descendants that spend its outputs.

Source

pub fn remove_by_txid(&mut self, txid: &Txid) -> Vec<EntryId>

Removes the entry for txid along with all descendants that spend its outputs. Returns the set of removed entry ids in stable order.

Returns an empty vector when the txid is not present in the pool.

Source

pub fn evict_below_fee_rate(&mut self, threshold_sat_per_kvb: u64) -> Vec<Txid>

Removes every entry whose fee_rate (sat/kvB) is strictly below threshold_sat_per_kvb. Returns the evicted transaction ids.

Bumps the sequence counter for each successful removal. Use this for min-relay-fee tightening or size-bound eviction policies.

Source

pub fn ancestor_ids_for_entry(&self, id: EntryId) -> Vec<EntryId>

Returns all ancestor entry ids for id, excluding id itself.

Source

pub fn descendant_ids_for_entry(&self, id: EntryId) -> Vec<EntryId>

Returns all descendant entry ids for id, EXCLUDING id itself.

Walks the spend graph forward via output references. Empty Vec when the entry has no descendants or is unknown.

Source

pub fn descendant_count_inclusive(&self, id: EntryId) -> u32

Returns the descendant-package count for id (inclusive of id itself).

Saturates at u32::MAX for pathological packages.

Source

pub fn ancestor_count_inclusive(&self, id: EntryId) -> u32

Returns the ancestor-package count for id (inclusive of id itself).

Saturates at u32::MAX for pathological packages. Composes ancestor_ids_for_entry + plus-one (caller is itself an ancestor of the inclusive count).

Source§

impl Mempool

Source

pub fn check_replacement( &self, candidate: &ReplacementCandidate, ) -> Result<ReplacementPlan, RbfError>

Checks BIP125 replacement rules without mutating the mempool.

Source

pub fn replace_transaction( &mut self, candidate: ReplacementCandidate, time: u64, height: u32, ) -> Result<EntryId, RbfError>

Applies a BIP125 replacement after validation and returns the new entry id.

Trait Implementations§

Source§

impl Debug for Mempool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V