From c4dcd85afc1884e987565c48390c103b805e3a8f Mon Sep 17 00:00:00 2001 From: f321x Date: Thu, 31 Jul 2025 11:47:21 +0200 Subject: [PATCH] txbatcher: don't spend anchors if ctx fee is sufficient If the tx fee of the ctx is already higher than the required target it is not useful to spend the anchor with a lower fee (the current target), so instead it is skipped. --- electrum/txbatcher.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/electrum/txbatcher.py b/electrum/txbatcher.py index ee1727e04..48279b40e 100644 --- a/electrum/txbatcher.py +++ b/electrum/txbatcher.py @@ -329,7 +329,7 @@ class TxBatch(Logger): for prevout, sweep_info in list(self.batch_inputs.items()): assert prevout == sweep_info.txin.prevout prev_txid, index = prevout.to_str().split(':') - if not self.wallet.adb.db.get_transaction(prev_txid): + if not (prev_tx := self.wallet.adb.db.get_transaction(prev_txid)): continue if sweep_info.is_anchor(): prev_tx_mined_status = self.wallet.adb.get_tx_height(prev_txid) @@ -337,6 +337,21 @@ class TxBatch(Logger): self.logger.info(f"anchor not needed {prevout}") self.batch_inputs.pop(prevout) # note: if the input is already in a batch tx, this will trigger assert error continue + prev_tx_current_fee = self.wallet.adb.get_tx_fee(prev_txid) + try: + prev_tx_target_fee = self.fee_policy.estimate_fee( + prev_tx.estimated_size(), + network=self.wallet.network, + ) + except NoDynamicFeeEstimates: + prev_tx_target_fee = None + fees_available = prev_tx_current_fee and prev_tx_target_fee + if fees_available and prev_tx_current_fee > prev_tx_target_fee: + self.logger.info( + f"not using anchor now, fee sufficient: " + f"{prev_tx_current_fee=} > {prev_tx_target_fee=}", only_once=True, + ) + continue if spender_txid := self.wallet.adb.db.get_spent_outpoint(prev_txid, int(index)): tx_mined_status = self.wallet.adb.get_tx_height(spender_txid) if tx_mined_status.height() not in [TX_HEIGHT_LOCAL, TX_HEIGHT_FUTURE]: