From 3ca83a22895d3a2d8afdd64c22d5bc14ca252fe3 Mon Sep 17 00:00:00 2001 From: nym21 Date: Tue, 16 Dec 2025 20:29:08 +0100 Subject: [PATCH] mempool: fix recommended fees --- crates/brk_mempool/src/entry_pool.rs | 6 ++++++ crates/brk_mempool/src/sync.rs | 6 ++++-- crates/brk_types/src/mempoolinfo.rs | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/brk_mempool/src/entry_pool.rs b/crates/brk_mempool/src/entry_pool.rs index d2f327ce2..dd27e9076 100644 --- a/crates/brk_mempool/src/entry_pool.rs +++ b/crates/brk_mempool/src/entry_pool.rs @@ -34,6 +34,12 @@ impl EntryPool { idx } + /// Get an entry by its txid prefix. + pub fn get(&self, prefix: &TxidPrefix) -> Option<&Entry> { + let idx = self.prefix_to_idx.get(prefix)?; + self.entries.get(idx.as_usize())?.as_ref() + } + /// Remove an entry by its txid prefix. pub fn remove(&mut self, prefix: &TxidPrefix) { if let Some(idx) = self.prefix_to_idx.remove(prefix) { diff --git a/crates/brk_mempool/src/sync.rs b/crates/brk_mempool/src/sync.rs index e816de983..ccaa8dec3 100644 --- a/crates/brk_mempool/src/sync.rs +++ b/crates/brk_mempool/src/sync.rs @@ -173,7 +173,9 @@ impl MempoolInner { let tx = tx_with_hex.tx(); let prefix = TxidPrefix::from(txid); - info.remove(tx); + // Get fee from entries (before removing) - this is the authoritative fee from Bitcoin Core + let fee = entries.get(&prefix).map(|e| e.fee).unwrap_or_default(); + info.remove(tx, fee); addresses.remove_tx(tx, txid); entries.remove(&prefix); }, @@ -188,7 +190,7 @@ impl MempoolInner { continue; }; - info.add(tx); + info.add(tx, entry_info.fee); addresses.add_tx(tx, txid); entries.insert(prefix, Entry::from_info(entry_info)); } diff --git a/crates/brk_types/src/mempoolinfo.rs b/crates/brk_types/src/mempoolinfo.rs index 37aad8f51..19ffe86f1 100644 --- a/crates/brk_types/src/mempoolinfo.rs +++ b/crates/brk_types/src/mempoolinfo.rs @@ -15,19 +15,25 @@ pub struct MempoolInfo { } impl MempoolInfo { - /// Increment stats for a newly added transaction + /// Increment stats for a newly added transaction. + /// + /// Fee must come from `MempoolEntryInfo` (Bitcoin Core) rather than `tx.fee` + /// because `tx.fee` may be 0 for chained mempool transactions where prevouts + /// cannot be looked up via `gettxout`. #[inline] - pub fn add(&mut self, tx: &Transaction) { + pub fn add(&mut self, tx: &Transaction, fee: Sats) { self.count += 1; self.vsize += tx.vsize(); - self.total_fee += tx.fee; + self.total_fee += fee; } - /// Decrement stats for a removed transaction + /// Decrement stats for a removed transaction. + /// + /// Fee must match the fee used when the transaction was added. #[inline] - pub fn remove(&mut self, tx: &Transaction) { + pub fn remove(&mut self, tx: &Transaction, fee: Sats) { self.count -= 1; self.vsize -= tx.vsize(); - self.total_fee -= tx.fee; + self.total_fee -= fee; } }