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