19
19
#include <lightningd/lightningd.h>
20
20
#include <lightningd/log.h>
21
21
#include <lightningd/peer_control.h>
22
+ #include <wally_psbt.h>
22
23
23
24
/* This is attached to each anchor tx retransmission */
24
25
struct one_anchor {
@@ -209,15 +210,51 @@ struct anchor_details *create_anchor_details(const tal_t *ctx,
209
210
return adet ;
210
211
}
211
212
212
- static u32 commit_feerate (const struct local_anchor_info * info )
213
+ /* total_weight includes the commitment tx we're trying to push! */
214
+ static struct wally_psbt * anchor_psbt (const tal_t * ctx ,
215
+ struct channel * channel ,
216
+ struct one_anchor * anch ,
217
+ struct utxo * * utxos ,
218
+ u32 feerate_target ,
219
+ size_t total_weight )
213
220
{
214
- u32 feerate ;
221
+ struct lightningd * ld = channel -> peer -> ld ;
222
+ struct wally_psbt * psbt ;
223
+ struct amount_sat change , fee ;
224
+ struct pubkey final_key ;
215
225
216
- /* Fee should not overflow! */
217
- if (!amount_feerate (& feerate , info -> commitment_fee , info -> commitment_weight ))
218
- abort ();
226
+ /* PSBT knows how to spend utxos. */
227
+ psbt = psbt_using_utxos (ctx , ld -> wallet , utxos ,
228
+ default_locktime (ld -> topology ),
229
+ BITCOIN_TX_RBF_SEQUENCE , NULL );
219
230
220
- return feerate ;
231
+ /* BOLT #3:
232
+ * #### `to_local_anchor` and `to_remote_anchor` Output (option_anchors)
233
+ *...
234
+ * The amount of the output is fixed at 330 sats, the default
235
+ * dust limit for P2WSH.
236
+ */
237
+ psbt_append_input (psbt , & anch -> info .anchor_point , BITCOIN_TX_RBF_SEQUENCE ,
238
+ NULL , anch -> adet -> anchor_wscript , NULL );
239
+ psbt_input_set_wit_utxo (psbt , psbt -> num_inputs - 1 ,
240
+ scriptpubkey_p2wsh (tmpctx , anch -> adet -> anchor_wscript ),
241
+ AMOUNT_SAT (330 ));
242
+ psbt_input_add_pubkey (psbt , psbt -> num_inputs - 1 , & channel -> local_funding_pubkey , false);
243
+
244
+ /* A zero-output tx is invalid: we must have change, even if not really economic */
245
+ change = psbt_compute_fee (psbt );
246
+ /* Assume we add a change output, what would the total fee be? */
247
+ fee = amount_tx_fee (feerate_target , total_weight + change_weight ());
248
+ if (!amount_sat_sub (& change , change , fee )
249
+ || amount_sat_less (change , chainparams -> dust_limit )) {
250
+ change = chainparams -> dust_limit ;
251
+ }
252
+
253
+ bip32_pubkey (ld , & final_key , channel -> final_key_idx );
254
+ psbt_append_output (psbt ,
255
+ scriptpubkey_p2wpkh (tmpctx , & final_key ),
256
+ change );
257
+ return psbt ;
221
258
}
222
259
223
260
/* If it's possible and worth it, return signed tx. Otherwise NULL. */
@@ -227,59 +264,93 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
227
264
{
228
265
struct lightningd * ld = channel -> peer -> ld ;
229
266
struct utxo * * utxos ;
230
- size_t weight ;
231
- struct amount_sat fee , diff , change ;
267
+ size_t base_weight , weight ;
268
+ struct amount_sat fee , diff ;
232
269
struct bitcoin_tx * tx ;
233
- bool worthwhile ;
234
270
struct wally_psbt * psbt ;
235
271
struct amount_msat total_value ;
236
- struct pubkey final_key ;
237
272
const u8 * msg ;
238
273
239
- /* Estimate weight of spend tx plus commitment_tx */
240
- weight = bitcoin_tx_core_weight (2 , 1 )
241
- + bitcoin_tx_input_weight (false, bitcoin_tx_simple_input_witness_weight ())
274
+ /* Estimate weight of spend tx plus commitment_tx (not including any UTXO we add) */
275
+ base_weight = bitcoin_tx_core_weight (2 , 1 )
242
276
+ bitcoin_tx_input_weight (false,
243
277
bitcoin_tx_input_sig_weight ()
244
278
+ 1 + tal_bytelen (anch -> adet -> anchor_wscript ))
245
279
+ bitcoin_tx_output_weight (BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN )
246
280
+ anch -> info .commitment_weight ;
247
281
248
- worthwhile = false;
249
282
total_value = AMOUNT_MSAT (0 );
250
- for (int i = tal_count (anch -> adet -> vals ) - 1 ; i >= 0 && !worthwhile ; -- i ) {
283
+ psbt = NULL ;
284
+ for (int i = tal_count (anch -> adet -> vals ) - 1 ; i >= 0 ; -- i ) {
251
285
const struct deadline_value * val = & anch -> adet -> vals [i ];
252
- u32 feerate ;
286
+ u32 feerate , feerate_target ;
287
+ struct wally_psbt * candidate_psbt ;
253
288
254
289
/* Calculate the total value for the current deadline
255
290
* and all the following */
256
291
if (!amount_msat_add (& total_value , total_value , val -> msat ))
257
292
return NULL ;
258
293
259
- feerate = feerate_for_target (ld -> topology , val -> block );
260
- /* Would the commit tx make that feerate by itself? */
261
- if (commit_feerate (& anch -> info ) >= feerate )
294
+ feerate_target = feerate_for_target (ld -> topology , val -> block );
295
+
296
+ /* Ask for some UTXOs which could meet this feerate */
297
+ weight = base_weight ;
298
+ utxos = wallet_utxo_boost (tmpctx ,
299
+ ld -> wallet ,
300
+ get_block_height (ld -> topology ),
301
+ anch -> info .commitment_fee ,
302
+ feerate_target ,
303
+ & weight );
304
+
305
+ /* Create a new candidate PSBT */
306
+ candidate_psbt = anchor_psbt (tmpctx , channel , anch , utxos , feerate_target , weight );
307
+ if (!candidate_psbt )
262
308
continue ;
263
309
264
- /* Get the fee required to meet the current block */
265
- fee = amount_tx_fee (feerate , weight );
310
+ fee = psbt_compute_fee (candidate_psbt );
311
+
312
+ /* Is it even worth spending this fee to meet the deadline? */
313
+ if (!amount_msat_greater_sat (total_value , fee )) {
314
+ log_debug (channel -> log ,
315
+ "Not worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
316
+ fmt_amount_sat (tmpctx , fee ),
317
+ anch -> commit_side == LOCAL ? "local" : "remote" ,
318
+ fmt_amount_msat (tmpctx , val -> msat ),
319
+ val -> block , feerate );
320
+ break ;
321
+ }
266
322
267
- /* We already have part of the fee in commitment_tx. */
268
- if (amount_sat_sub (& fee , fee , anch -> info .commitment_fee )
269
- && amount_msat_greater_sat (total_value , fee )) {
270
- worthwhile = true;
323
+ if (!amount_feerate (& feerate , fee , weight ))
324
+ abort ();
325
+
326
+ if (feerate < feerate_target ) {
327
+ /* We might have had lower feerates which worked: only complain if
328
+ * we have *nothing* */
329
+ if (tal_count (utxos ) == 0 && !psbt ) {
330
+ log_unusual (channel -> log ,
331
+ "No utxos to bump commit_tx to feerate %uperkw!" ,
332
+ feerate_target );
333
+ break ;
334
+ }
335
+
336
+ log_unusual (channel -> log ,
337
+ "We want to bump commit_tx to feerate %uperkw, but can only bump to %uperkw!" ,
338
+ feerate_target , feerate );
339
+ psbt = candidate_psbt ;
340
+ /* We don't expect to do any better at higher feerates */
341
+ break ;
271
342
}
272
343
273
- log_debug (channel -> log , "%s fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
274
- worthwhile ? "Worth" : "Not worth" ,
344
+ log_debug (channel -> log , "Worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
275
345
fmt_amount_sat (tmpctx , fee ),
276
346
anch -> commit_side == LOCAL ? "local" : "remote" ,
277
347
fmt_amount_msat (tmpctx , val -> msat ),
278
348
val -> block , feerate );
349
+ psbt = candidate_psbt ;
279
350
}
280
351
281
- /* Not worth it? */
282
- if (!worthwhile )
352
+ /* No psbt was worth it? */
353
+ if (!psbt )
283
354
return NULL ;
284
355
285
356
/* Higher enough than previous to be valid RBF?
@@ -299,63 +370,6 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
299
370
(fee .satoshis + anch -> info .commitment_fee .satoshis ) /* Raw: debug log */
300
371
* 1000 / weight );
301
372
302
- /* FIXME: Use more than one utxo! */
303
- utxos = tal_arr (tmpctx , struct utxo * , 1 );
304
- utxos [0 ] = wallet_find_utxo (utxos , ld -> wallet ,
305
- get_block_height (ld -> topology ),
306
- NULL ,
307
- 0 , /* FIXME: unused! */
308
- 0 , false, NULL );
309
- if (!utxos [0 ]) {
310
- log_unusual (channel -> log ,
311
- "We want to bump commit_tx fee, but no funds!" );
312
- return NULL ;
313
- }
314
-
315
- /* FIXME: we get a random UTXO. We should really allow
316
- * multiple UTXOs here */
317
- if (amount_sat_less (utxos [0 ]-> amount , fee )) {
318
- log_unusual (channel -> log ,
319
- "We want to bump commit_tx with fee %s, but utxo %s is only %s!" ,
320
- fmt_amount_sat (tmpctx , fee ),
321
- type_to_string (tmpctx , struct bitcoin_outpoint ,
322
- & utxos [0 ]-> outpoint ),
323
- fmt_amount_sat (tmpctx , utxos [0 ]-> amount ));
324
- return NULL ;
325
- }
326
-
327
- /* PSBT knows how to spend utxos. */
328
- psbt = psbt_using_utxos (tmpctx , ld -> wallet , utxos ,
329
- default_locktime (ld -> topology ),
330
- BITCOIN_TX_RBF_SEQUENCE , NULL );
331
-
332
- /* BOLT #3:
333
- * #### `to_local_anchor` and `to_remote_anchor` Output (option_anchors)
334
- *...
335
- * The amount of the output is fixed at 330 sats, the default
336
- * dust limit for P2WSH.
337
- */
338
- psbt_append_input (psbt , & anch -> info .anchor_point , BITCOIN_TX_RBF_SEQUENCE ,
339
- NULL , anch -> adet -> anchor_wscript , NULL );
340
- psbt_input_set_wit_utxo (psbt , 1 ,
341
- scriptpubkey_p2wsh (tmpctx , anch -> adet -> anchor_wscript ),
342
- AMOUNT_SAT (330 ));
343
- psbt_input_add_pubkey (psbt , 1 , & channel -> local_funding_pubkey , false);
344
-
345
- if (!amount_sat_add (& change , utxos [0 ]-> amount , AMOUNT_SAT (330 ))
346
- || !amount_sat_sub (& change , change , fee )) {
347
- log_broken (channel -> log ,
348
- "Error calculating anchorspend change: utxo %s fee %s" ,
349
- fmt_amount_sat (tmpctx , utxos [0 ]-> amount ),
350
- fmt_amount_sat (tmpctx , fee ));
351
- return NULL ;
352
- }
353
-
354
- bip32_pubkey (ld , & final_key , channel -> final_key_idx );
355
- psbt_append_output (psbt ,
356
- scriptpubkey_p2wpkh (tmpctx , & final_key ),
357
- change );
358
-
359
373
/* OK, HSM, sign it! */
360
374
msg = towire_hsmd_sign_anchorspend (NULL ,
361
375
& channel -> peer -> id ,
0 commit comments