| 1867 | } |
| 1868 | |
| 1869 | void onchain_failed_our_htlc(const struct channel *channel, |
| 1870 | const struct htlc_stub *htlc, |
| 1871 | const char *why, |
| 1872 | bool should_exist) |
| 1873 | { |
| 1874 | struct lightningd *ld = channel->peer->ld; |
| 1875 | struct htlc_out *hout; |
| 1876 | |
| 1877 | log_debug(channel->log, "onchain_failed_our_htlc"); |
| 1878 | hout = find_htlc_out(ld->htlcs_out, channel, htlc->id); |
| 1879 | if (!hout) { |
| 1880 | /* For penalty transactions, tell onchaind about all possible |
| 1881 | * HTLCs: they may not all exist any more. */ |
| 1882 | if (should_exist) |
| 1883 | log_broken(channel->log, "HTLC id %"PRIu64" not found!", |
| 1884 | htlc->id); |
| 1885 | /* Immediate corruption sanity check if this happens */ |
| 1886 | htable_check(&ld->htlcs_out->raw, "onchain_failed_our_htlc out"); |
| 1887 | htable_check(&ld->htlcs_in->raw, "onchain_failed_our_htlc in"); |
| 1888 | return; |
| 1889 | } |
| 1890 | |
| 1891 | /* We can have hout->failonion (which gets set when we process the |
| 1892 | * received commitment_signed), but not failed hin yet, because the peer |
| 1893 | * hadn't revoke_and_acked our own commitment without that htlc. */ |
| 1894 | if (hout->failonion && hout->in |
| 1895 | && hout->in->badonion == 0 |
| 1896 | && !hout->in->failonion |
| 1897 | && !hout->in->preimage) { |
| 1898 | log_debug(channel->log, "HTLC out %"PRIu64" can now fail HTLC upstream!", |
| 1899 | htlc->id); |
| 1900 | fail_in_htlc(hout->in, hout->failonion); |
| 1901 | } |
| 1902 | |
| 1903 | /* Don't fail twice (or if already succeeded)! */ |
| 1904 | if (hout->failonion || hout->failmsg || hout->preimage) { |
| 1905 | log_debug(channel->log, "HTLC id %"PRIu64" failonion = %p, failmsg = %p, preimage = %p", |
| 1906 | htlc->id, |
| 1907 | hout->failonion, |
| 1908 | hout->failmsg, |
| 1909 | hout->preimage); |
| 1910 | check_already_failed(channel, hout); |
| 1911 | return; |
| 1912 | } |
| 1913 | |
| 1914 | hout->failmsg = towire_permanent_channel_failure(hout); |
| 1915 | |
| 1916 | /* Force state to something which expects a failure, and save to db */ |
| 1917 | hout->hstate = RCVD_REMOVE_HTLC; |
| 1918 | htlc_out_check(hout, __func__); |
| 1919 | |
| 1920 | bool we_filled = false; |
| 1921 | wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate, |
| 1922 | hout->preimage, |
| 1923 | max_unsigned(channel->next_index[LOCAL], |
| 1924 | channel->next_index[REMOTE]), |
| 1925 | 0, hout->failonion, |
| 1926 | hout->failmsg, &we_filled, |
no test coverage detected