Skip to content

Commit 0ebd938

Browse files
committed
lightningd: use wallet_utxo_boost for zero-fee htlc_tx.
The previous logic looked wrong anyway! Signed-off-by: Rusty Russell <[email protected]>
1 parent bf8893a commit 0ebd938

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

lightningd/onchain_control.c

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -883,57 +883,46 @@ static bool consider_onchain_htlc_tx_rebroadcast(struct channel *channel,
883883
/* Make a copy to play with */
884884
newtx = clone_bitcoin_tx(tmpctx, info->raw_htlc_tx);
885885
weight = bitcoin_tx_weight(newtx);
886-
utxos = tal_arr(tmpctx, struct utxo *, 0);
887886

888887
/* We'll need this to regenerate PSBT */
889888
if (wally_psbt_get_locktime(newtx->psbt, &locktime) != WALLY_OK) {
890889
log_broken(channel->log, "Cannot get psbt locktime?!");
891890
return true;
892891
}
893892

894-
/* Keep attaching input inputs until we get sufficient fees */
895-
while (tx_feerate(newtx) < feerate) {
896-
struct utxo *utxo;
897-
898-
/* Get fresh utxo */
899-
utxo = wallet_find_utxo(tmpctx, ld->wallet,
900-
get_block_height(ld->topology),
901-
NULL,
902-
0, /* FIXME: unused! */
903-
0, false,
904-
cast_const2(const struct utxo **, utxos));
905-
if (!utxo) {
906-
/* Did we get nothing at all? */
907-
if (tal_count(utxos) == 0) {
908-
log_unusual(channel->log,
909-
"We want to bump HTLC fee, but no funds!");
910-
return true;
911-
}
912-
/* At least we got something, right? */
913-
break;
914-
}
915-
916-
/* Add to any UTXOs we have already */
917-
tal_arr_expand(&utxos, utxo);
918-
weight += bitcoin_tx_simple_input_weight(utxo->is_p2sh);
919-
}
920-
921-
/* We were happy with feerate already (can't happen with zero-fee
922-
* anchors!)? */
923-
if (tal_count(utxos) == 0)
924-
return true;
893+
utxos = wallet_utxo_boost(tmpctx,
894+
ld->wallet,
895+
get_block_height(ld->topology),
896+
bitcoin_tx_compute_fee(newtx),
897+
feerate,
898+
&weight);
925899

926-
/* PSBT knows how to spend utxos; append to existing. */
900+
/* Add those to create a new PSBT */
927901
psbt = psbt_using_utxos(tmpctx, ld->wallet, utxos, locktime,
928902
BITCOIN_TX_RBF_SEQUENCE, newtx->psbt);
929903

930904
/* Subtract how much we pay in fees for this tx, to calc excess. */
931905
if (!amount_sat_sub(&excess,
932906
psbt_compute_fee(psbt),
933-
amount_sat((u64)weight * feerate / 1000))) {
907+
amount_tx_fee(feerate, weight))) {
908+
/* We didn't make the feerate. Did we get nothing at all? */
909+
if (tal_count(utxos) == 0) {
910+
log_unusual(channel->log,
911+
"We want to bump HTLC fee, but no funds!");
912+
return true;
913+
}
914+
/* At least we got something! */
915+
log_unusual(channel->log,
916+
"We want to bump HTLC fee more, but ran out of funds!");
934917
excess = AMOUNT_SAT(0);
935918
}
936919

920+
/* We were happy with feerate already (can't happen with zero-fee
921+
* anchors!)? */
922+
if (tal_count(utxos) == 0)
923+
return true;
924+
925+
/* Maybe add change */
937926
change = change_amount(excess, feerate, weight);
938927
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
939928
/* Append change output. */

0 commit comments

Comments
 (0)