Skip to content

Commit

Permalink
lightningd: use wallet_utxo_boost for zero-fee htlc_tx.
Browse files Browse the repository at this point in the history
The previous logic looked wrong anyway!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Oct 16, 2023
1 parent bf8893a commit 0ebd938
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,57 +883,46 @@ static bool consider_onchain_htlc_tx_rebroadcast(struct channel *channel,
/* Make a copy to play with */
newtx = clone_bitcoin_tx(tmpctx, info->raw_htlc_tx);
weight = bitcoin_tx_weight(newtx);
utxos = tal_arr(tmpctx, struct utxo *, 0);

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

/* Keep attaching input inputs until we get sufficient fees */
while (tx_feerate(newtx) < feerate) {
struct utxo *utxo;

/* Get fresh utxo */
utxo = wallet_find_utxo(tmpctx, ld->wallet,
get_block_height(ld->topology),
NULL,
0, /* FIXME: unused! */
0, false,
cast_const2(const struct utxo **, utxos));
if (!utxo) {
/* Did we get nothing at all? */
if (tal_count(utxos) == 0) {
log_unusual(channel->log,
"We want to bump HTLC fee, but no funds!");
return true;
}
/* At least we got something, right? */
break;
}

/* Add to any UTXOs we have already */
tal_arr_expand(&utxos, utxo);
weight += bitcoin_tx_simple_input_weight(utxo->is_p2sh);
}

/* We were happy with feerate already (can't happen with zero-fee
* anchors!)? */
if (tal_count(utxos) == 0)
return true;
utxos = wallet_utxo_boost(tmpctx,
ld->wallet,
get_block_height(ld->topology),
bitcoin_tx_compute_fee(newtx),
feerate,
&weight);

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

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

/* We were happy with feerate already (can't happen with zero-fee
* anchors!)? */
if (tal_count(utxos) == 0)
return true;

/* Maybe add change */
change = change_amount(excess, feerate, weight);
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
/* Append change output. */
Expand Down

0 comments on commit 0ebd938

Please sign in to comment.