| 1605 | } |
| 1606 | |
| 1607 | void onchain_failed_our_htlc(const struct channel *channel, |
| 1608 | const struct htlc_stub *htlc, |
| 1609 | const char *why, |
| 1610 | bool should_exist) |
| 1611 | { |
| 1612 | struct lightningd *ld = channel->peer->ld; |
| 1613 | struct htlc_out *hout; |
| 1614 | |
| 1615 | log_debug(channel->log, "onchain_failed_our_htlc"); |
| 1616 | hout = find_htlc_out(&ld->htlcs_out, channel, htlc->id); |
| 1617 | if (!hout) { |
| 1618 | /* For penalty transactions, tell onchaind about all possible |
| 1619 | * HTLCs: they may not all exist any more. */ |
| 1620 | if (should_exist) |
| 1621 | log_broken(channel->log, "HTLC id %"PRIu64" not found!", |
| 1622 | htlc->id); |
| 1623 | /* Immediate corruption sanity check if this happens */ |
| 1624 | htable_check(&ld->htlcs_out.raw, "onchain_failed_our_htlc out"); |
| 1625 | htable_check(&ld->htlcs_in.raw, "onchain_failed_our_htlc in"); |
| 1626 | return; |
| 1627 | } |
| 1628 | |
| 1629 | /* We can have hout->failonion (which gets set when we process the |
| 1630 | * received commitment_signed), but not failed hin yet, because the peer |
| 1631 | * hadn't revoke_and_acked our own commitment without that htlc. */ |
| 1632 | if (hout->failonion && hout->in |
| 1633 | && hout->in->badonion == 0 |
| 1634 | && !hout->in->failonion |
| 1635 | && !hout->in->preimage) { |
| 1636 | log_debug(channel->log, "HTLC out %"PRIu64" can now fail HTLC upstream!", |
| 1637 | htlc->id); |
| 1638 | fail_in_htlc(hout->in, hout->failonion); |
| 1639 | } |
| 1640 | |
| 1641 | /* Don't fail twice (or if already succeeded)! */ |
| 1642 | if (hout->failonion || hout->failmsg || hout->preimage) { |
| 1643 | log_debug(channel->log, "HTLC id %"PRIu64" failonion = %p, failmsg = %p, preimage = %p", |
| 1644 | htlc->id, |
| 1645 | hout->failonion, |
| 1646 | hout->failmsg, |
| 1647 | hout->preimage); |
| 1648 | check_already_failed(channel, hout); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | hout->failmsg = towire_permanent_channel_failure(hout); |
| 1653 | |
| 1654 | /* Force state to something which expects a failure, and save to db */ |
| 1655 | hout->hstate = RCVD_REMOVE_HTLC; |
| 1656 | htlc_out_check(hout, __func__); |
| 1657 | |
| 1658 | bool we_filled = false; |
| 1659 | wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate, |
| 1660 | hout->preimage, |
| 1661 | max_unsigned(channel->next_index[LOCAL], |
| 1662 | channel->next_index[REMOTE]), |
| 1663 | 0, hout->failonion, |
| 1664 | hout->failmsg, &we_filled); |
no test coverage detected