Skip to content

Commit 5bff1b5

Browse files
committed
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.
1 parent 71255c1 commit 5bff1b5

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

electrum/txbatcher.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,26 @@ def _to_sweep_after(self, tx: Optional[PartialTransaction]) -> Dict[TxOutpoint,
321321
for prevout, sweep_info in list(self.batch_inputs.items()):
322322
assert prevout == sweep_info.txin.prevout
323323
prev_txid, index = prevout.to_str().split(':')
324-
if not self.wallet.adb.db.get_transaction(prev_txid):
324+
if not (prev_tx := self.wallet.adb.db.get_transaction(prev_txid)):
325325
continue
326326
if sweep_info.is_anchor():
327327
prev_tx_mined_status = self.wallet.adb.get_tx_height(prev_txid)
328328
if prev_tx_mined_status.conf > 0:
329329
self.logger.info(f"anchor not needed {prevout}")
330330
self.batch_inputs.pop(prevout) # note: if the input is already in a batch tx, this will trigger assert error
331331
continue
332+
prev_tx_current_fee = self.wallet.adb.get_tx_fee(prev_txid)
333+
try:
334+
prev_tx_target_fee = self.fee_policy.estimate_fee(
335+
prev_tx.estimated_size(),
336+
network=self.wallet.network,
337+
)
338+
except NoDynamicFeeEstimates:
339+
prev_tx_target_fee = None
340+
if prev_tx_current_fee and prev_tx_target_fee and \
341+
prev_tx_current_fee > prev_tx_target_fee:
342+
self.logger.info(f"not using anchor now: {prev_tx_current_fee=} > {prev_tx_target_fee=}", only_once=True)
343+
continue
332344
if spender_txid := self.wallet.adb.db.get_spent_outpoint(prev_txid, int(index)):
333345
tx_mined_status = self.wallet.adb.get_tx_height(spender_txid)
334346
if tx_mined_status.height not in [TX_HEIGHT_LOCAL, TX_HEIGHT_FUTURE]:

0 commit comments

Comments
 (0)