Skip to content

Commit daadbb9

Browse files
committed
move bump_fee logic to payment_manager:
- allow base_tx to sign several times
1 parent dca126e commit daadbb9

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

electrum/submarine_swaps.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ async def _claim_swap(self, swap: SwapData) -> None:
346346
self._add_or_reindex_swap(swap) # to update _swaps_by_funding_outpoint
347347
funding_height = self.lnwatcher.adb.get_tx_height(txin.prevout.txid.hex())
348348
spent_height = txin.spent_height
349-
should_bump_fee = False
350349
if spent_height is not None:
351350
swap.spending_txid = txin.spent_txid
352351
if spent_height > 0:
@@ -388,13 +387,6 @@ async def _claim_swap(self, swap: SwapData) -> None:
388387
self.logger.info(f'refund tx confirmed: {txin.spent_txid} {spent_height}')
389388
self._fail_swap(swap, 'refund tx confirmed')
390389
return
391-
else:
392-
claim_tx.add_info_from_wallet(self.wallet)
393-
claim_tx_fee = claim_tx.get_fee()
394-
recommended_fee = self.get_claim_fee()
395-
if claim_tx_fee * 1.1 < recommended_fee:
396-
should_bump_fee = True
397-
self.logger.info(f'claim tx fee too low {claim_tx_fee} < {recommended_fee}. we will bump the fee')
398390

399391
if remaining_time > 0:
400392
# too early for refund
@@ -423,7 +415,7 @@ async def _claim_swap(self, swap: SwapData) -> None:
423415
# for testing: do not create claim tx
424416
return
425417

426-
if spent_height is not None and not should_bump_fee:
418+
if spent_height is not None:
427419
return
428420
try:
429421
txin, locktime = self._create_claim_txin(txin=txin, swap=swap, config=self.wallet.config)

electrum/transaction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1736,13 +1736,15 @@ def finalize(self) -> None:
17361736
def clear_fields_when_finalized():
17371737
# BIP-174: "All other data except the UTXO and unknown fields in the
17381738
# input key-value map should be cleared from the PSBT"
1739+
# FIXME: serialize() should not side-effect the tx inputs
17391740
self.sigs_ecdsa = {}
17401741
self.tap_key_sig = None
17411742
self.tap_merkle_root = None
17421743
self.sighash = None
17431744
self.bip32_paths = {}
17441745
self.redeem_script = None
1745-
self.witness_script = None
1746+
if not hasattr(self, 'make_witness'):
1747+
self.witness_script = None
17461748

17471749
if self.script_sig is not None and self.witness is not None:
17481750
clear_fields_when_finalized()

electrum/wallet.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -1879,8 +1879,10 @@ def make_unsigned_transaction(
18791879
# make sure we don't try to spend change from the tx-to-be-replaced:
18801880
coins = [c for c in coins if c.prevout.txid.hex() != base_tx.txid()]
18811881
is_local = self.adb.get_tx_height(base_tx.txid()).height == TX_HEIGHT_LOCAL
1882-
base_tx = PartialTransaction.from_tx(base_tx)
1883-
base_tx.add_info_from_wallet(self)
1882+
if not isinstance(base_tx, PartialTransaction):
1883+
# don't do this with a PartialTransaction, as this is destructive
1884+
base_tx = PartialTransaction.from_tx(base_tx)
1885+
base_tx.add_info_from_wallet(self)
18841886
base_tx_fee = base_tx.get_fee()
18851887
base_feerate = Decimal(base_tx_fee)/base_tx.estimated_size()
18861888
relayfeerate = Decimal(self.relayfee()) / 1000
@@ -2550,11 +2552,13 @@ def sign_transaction(self, tx: Transaction, password, *, ignore_warnings: bool =
25502552

25512553
for i, txin in enumerate(tx.inputs()):
25522554
if hasattr(txin, 'make_witness'):
2555+
self.logger.info(f'sign_transaction: adding witness using make_witness')
25532556
privkey = txin.privkey
25542557
sig = tx.sign_txin(i, privkey)
25552558
assert sig
25562559
assert txin.witness_script
25572560
txin.witness = txin.make_witness(sig)
2561+
assert txin.is_complete()
25582562

25592563
# check if signing is dangerous
25602564
sh_danger = self.check_sighash(tx)
@@ -3339,6 +3343,18 @@ def to_sweep_after(self, tx):
33393343
tx_prevouts = set(txin.prevout for txin in tx.inputs())
33403344
return dict((k,v) for k,v in self.batch_inputs.items() if k not in tx_prevouts)
33413345

3346+
def should_bump_fee(self, base_tx):
3347+
# fixme: since batch_txs is not persisted, we do not bump after a restart
3348+
# fixme: we use estimated_size because create_transaction returns a PartialTransaction
3349+
if base_tx is None:
3350+
return False
3351+
base_tx_fee = base_tx.get_fee()
3352+
recommended_fee = self.config.estimate_fee(base_tx.estimated_size(), allow_fallback_to_static_rates=True)
3353+
should_bump_fee = base_tx_fee * 1.1 < recommended_fee
3354+
if should_bump_fee:
3355+
self.logger.info(f'base tx fee too low {base_tx_fee} < {recommended_fee}. we will bump the fee')
3356+
return should_bump_fee
3357+
33423358
@log_exceptions
33433359
async def manage_batch_payments(self):
33443360
#
@@ -3386,7 +3402,7 @@ async def manage_batch_payments(self):
33863402
to_pay = self.to_pay_after(base_tx)
33873403
to_sweep = self.to_sweep_after(base_tx)
33883404
to_sweep_now = dict([(k,v) for k,v in to_sweep.items() if self.can_broadcast(v)[0] is True])
3389-
if not to_pay and not to_sweep_now:
3405+
if not to_pay and not to_sweep_now and not self.should_bump_fee(base_tx):
33903406
continue
33913407
try:
33923408
tx = self.create_batch_tx(base_tx, to_sweep_now, to_pay, password)

0 commit comments

Comments
 (0)