| 353 | } |
| 354 | |
| 355 | void drop_to_chain(struct lightningd *ld, struct channel *channel, |
| 356 | bool cooperative, |
| 357 | const struct bitcoin_tx *unilateral_tx) |
| 358 | { |
| 359 | struct channel_inflight *inflight; |
| 360 | const char *cmd_id; |
| 361 | |
| 362 | /* If we withheld the funding tx, we simply close */ |
| 363 | if (channel->withheld) { |
| 364 | struct htlc_out_map_iter outi; |
| 365 | struct htlc_out *hout; |
| 366 | |
| 367 | log_info(channel->log, |
| 368 | "Withheld channel: not sending a close transaction"); |
| 369 | |
| 370 | for (hout = htlc_out_map_first(ld->htlcs_out, &outi); |
| 371 | hout; |
| 372 | hout = htlc_out_map_next(ld->htlcs_out, &outi)) { |
| 373 | if (hout->key.channel != channel) |
| 374 | continue; |
| 375 | /* Has already been settled or failed */ |
| 376 | if (!hout->in |
| 377 | || hout->in->badonion != 0 |
| 378 | || hout->in->failonion |
| 379 | || hout->in->preimage) |
| 380 | continue; |
| 381 | local_fail_in_htlc(hout->in, |
| 382 | take(towire_permanent_channel_failure(NULL))); |
| 383 | } |
| 384 | |
| 385 | /* Unreserve any UTXOs from the withheld funding PSBT */ |
| 386 | if (channel->funding_psbt) { |
| 387 | for (size_t i = 0; i < channel->funding_psbt->num_inputs; i++) { |
| 388 | struct bitcoin_outpoint outpoint; |
| 389 | struct utxo *utxo; |
| 390 | |
| 391 | wally_psbt_input_get_outpoint( |
| 392 | &channel->funding_psbt->inputs[i], &outpoint); |
| 393 | utxo = wallet_utxo_get(tmpctx, ld->wallet, &outpoint); |
| 394 | if (!utxo || utxo->status != OUTPUT_STATE_RESERVED) |
| 395 | continue; |
| 396 | |
| 397 | wallet_unreserve_utxo(ld->wallet, utxo, |
| 398 | get_block_height(ld->topology), |
| 399 | utxo->reserved_til); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | resolve_close_command(ld, channel, cooperative, |
| 404 | tal_arr(tmpctx, const struct bitcoin_tx *, 0)); |
| 405 | free_htlcs(ld, channel); |
| 406 | delete_channel(channel, false); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* If we're not already (e.g. close before channel fully open), |
| 411 | * make sure we're watching for the funding spend */ |
| 412 | if (!channel->funding_spend_watch) { |
no test coverage detected