| 1681 | } |
| 1682 | |
| 1683 | void onchain_fulfilled_htlc(struct channel *channel, |
| 1684 | const struct preimage *preimage) |
| 1685 | { |
| 1686 | struct htlc_out_map_iter outi; |
| 1687 | struct htlc_out *hout; |
| 1688 | struct sha256 payment_hash; |
| 1689 | struct lightningd *ld = channel->peer->ld; |
| 1690 | |
| 1691 | sha256(&payment_hash, preimage, sizeof(*preimage)); |
| 1692 | |
| 1693 | /* FIXME: use db to look this up! */ |
| 1694 | for (hout = htlc_out_map_first(ld->htlcs_out, &outi); |
| 1695 | hout; |
| 1696 | hout = htlc_out_map_next(ld->htlcs_out, &outi)) { |
| 1697 | if (hout->key.channel != channel) |
| 1698 | continue; |
| 1699 | |
| 1700 | if (!sha256_eq(&hout->payment_hash, &payment_hash)) |
| 1701 | continue; |
| 1702 | |
| 1703 | /* BOLT #2: |
| 1704 | * - upon receiving an `update_fulfill_htlc` for an outgoing |
| 1705 | * HTLC, OR upon discovering the `payment_preimage` from an |
| 1706 | * on-chain HTLC spend: |
| 1707 | * - MUST fulfill the incoming HTLC that corresponds to |
| 1708 | * that outgoing HTLC. |
| 1709 | */ |
| 1710 | /* A failure which isn't irrevocably committed yet doesn't |
| 1711 | * count: remove_htlc_out() frees hout once we reach |
| 1712 | * RCVD_REMOVE_ACK_REVOCATION, so any hout we still have with |
| 1713 | * failmsg/failonion set is one whose removal isn't final. We |
| 1714 | * can only lose here if we already resolved the incoming HTLC |
| 1715 | * as failed, which is what this check was originally for. */ |
| 1716 | if (hout->in |
| 1717 | && (hout->in->failonion || hout->in->badonion)) { |
| 1718 | /* We can be told this more than once, and |
| 1719 | * fulfill_our_htlc_out() complains itself if we |
| 1720 | * already knew the preimage. */ |
| 1721 | if (!hout->preimage) |
| 1722 | log_broken(channel->log, |
| 1723 | "FUNDS LOSS of %s: peer took funds" |
| 1724 | " onchain with preimage, but we" |
| 1725 | " already failed the incoming HTLC", |
| 1726 | fmt_amount_msat(tmpctx, hout->msat)); |
| 1727 | continue; |
| 1728 | } |
| 1729 | |
| 1730 | /* We may have already fulfilled before going onchain, or |
| 1731 | * we can fulfill onchain multiple times. */ |
| 1732 | if (!hout->preimage) { |
| 1733 | /* The preimage wins over any pending failure, and |
| 1734 | * htlc_out_check() doesn't allow both. */ |
| 1735 | hout->failonion = tal_free(hout->failonion); |
| 1736 | hout->failmsg = tal_free(hout->failmsg); |
| 1737 | /* Force state to something which allows a preimage */ |
| 1738 | hout->hstate = RCVD_REMOVE_HTLC; |
| 1739 | fulfill_our_htlc_out(channel, hout, preimage); |
| 1740 | } |
no test coverage detected