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: ParetoFrontFee-priority index for mining and eviction consumers.
limits: MempoolLimitsActive mempool policy limits.
Implementations§
Source§impl Mempool
impl Mempool
Sourcepub fn new(limits: MempoolLimits) -> Self
pub fn new(limits: MempoolLimits) -> Self
Creates an empty mempool with the supplied limits.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn sequence_number(&self) -> u64
pub fn sequence_number(&self) -> u64
Returns the current sequence number. Increments on every insert/remove.
Sourcepub const fn min_relay_fee_sat_per_kvb(&self) -> u64
pub const fn min_relay_fee_sat_per_kvb(&self) -> u64
Returns the configured min-relay-fee rate in sat/kvB.
Sourcepub fn insert_entry(
&mut self,
entry: MempoolEntry,
) -> Result<EntryId, MempoolError>
pub fn insert_entry( &mut self, entry: MempoolEntry, ) -> Result<EntryId, MempoolError>
Inserts an entry after applying ancestor and descendant policy checks.
Sourcepub fn contains_txid(&self, txid: &Txid) -> bool
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.
Sourcepub fn entry_by_txid(&self, txid: &Txid) -> Option<&MempoolEntry>
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.
Sourcepub fn transaction_by_txid(&self, txid: &Txid) -> Option<Arc<Transaction>>
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.
Sourcepub fn iter_txids(&self) -> Vec<Txid>
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).
Sourcepub fn iter_replaceable_txids(&self) -> Vec<Txid>
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.
Sourcepub fn total_vsize(&self) -> u64
pub fn total_vsize(&self) -> u64
Returns the total virtual size of all entries.
Sourcepub fn enforce_size_limit(&mut self, max_bytes: u64) -> Vec<EntryId> ⓘ
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.
Sourcepub fn aggregate_fees(&self) -> u64
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).
Sourcepub fn stats(&self) -> MempoolStats
pub fn stats(&self) -> MempoolStats
Returns aggregate counters for the current pool.
Sourcepub fn entry(&self, id: EntryId) -> Option<&MempoolEntry>
pub fn entry(&self, id: EntryId) -> Option<&MempoolEntry>
Returns an entry by public id.
Sourcepub fn iter_by_fee_rate_desc(&self) -> Vec<EntryId> ⓘ
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).
Sourcepub fn lowest_fee_rate(&self) -> Option<u64>
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.
Sourcepub fn iter_above_fee_rate(&self, threshold_sat_per_kvb: u64) -> Vec<EntryId> ⓘ
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.
Sourcepub fn find_by_outpoint(&self, outpoint: &OutPoint) -> Option<Txid>
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.
Sourcepub fn is_outpoint_spent(&self, outpoint: &OutPoint) -> bool
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.
Sourcepub fn prioritise(&mut self, txid: Txid, fee_delta: i64) -> bool
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.
Sourcepub fn remove_entry_and_descendants(&mut self, id: EntryId) -> Vec<EntryId> ⓘ
pub fn remove_entry_and_descendants(&mut self, id: EntryId) -> Vec<EntryId> ⓘ
Removes an entry and all descendants that spend its outputs.
Sourcepub fn remove_by_txid(&mut self, txid: &Txid) -> Vec<EntryId> ⓘ
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.
Sourcepub fn evict_below_fee_rate(&mut self, threshold_sat_per_kvb: u64) -> Vec<Txid>
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.
Sourcepub fn ancestor_ids_for_entry(&self, id: EntryId) -> Vec<EntryId> ⓘ
pub fn ancestor_ids_for_entry(&self, id: EntryId) -> Vec<EntryId> ⓘ
Returns all ancestor entry ids for id, excluding id itself.
Sourcepub fn descendant_ids_for_entry(&self, id: EntryId) -> Vec<EntryId> ⓘ
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.
Sourcepub fn descendant_count_inclusive(&self, id: EntryId) -> u32
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.
Sourcepub fn ancestor_count_inclusive(&self, id: EntryId) -> u32
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
impl Mempool
Sourcepub fn check_replacement(
&self,
candidate: &ReplacementCandidate,
) -> Result<ReplacementPlan, RbfError>
pub fn check_replacement( &self, candidate: &ReplacementCandidate, ) -> Result<ReplacementPlan, RbfError>
Checks BIP125 replacement rules without mutating the mempool.
Sourcepub fn replace_transaction(
&mut self,
candidate: ReplacementCandidate,
time: u64,
height: u32,
) -> Result<EntryId, RbfError>
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.