| 1748 | } |
| 1749 | |
| 1750 | static void remove_htlc_out(struct channel *channel, struct htlc_out *hout) |
| 1751 | { |
| 1752 | htlc_out_check(hout, __func__); |
| 1753 | assert(hout->failonion || hout->preimage || hout->failmsg); |
| 1754 | log_debug(channel->log, "Removing out HTLC %"PRIu64" state %s %s", |
| 1755 | hout->key.id, htlc_state_name(hout->hstate), |
| 1756 | hout->preimage ? "FULFILLED" |
| 1757 | : hout->failmsg ? onion_wire_name(fromwire_peektype(hout->failmsg)) |
| 1758 | : "REMOTEFAIL"); |
| 1759 | |
| 1760 | /* If it's failed, now we can forward since it's completely locked-in */ |
| 1761 | if (!hout->preimage) { |
| 1762 | fail_out_htlc(hout, NULL); |
| 1763 | } else { |
| 1764 | const struct channel_coin_mvt *mvt; |
| 1765 | struct amount_msat oldamt = channel->our_msat; |
| 1766 | /* We paid for this HTLC, so deduct balance. */ |
| 1767 | if (!amount_msat_sub(&channel->our_msat, channel->our_msat, |
| 1768 | hout->msat)) { |
| 1769 | channel_internal_error(channel, |
| 1770 | "Underflow our_msat %s - HTLC %s", |
| 1771 | type_to_string(tmpctx, |
| 1772 | struct amount_msat, |
| 1773 | &channel->our_msat), |
| 1774 | type_to_string(tmpctx, |
| 1775 | struct amount_msat, |
| 1776 | &hout->msat)); |
| 1777 | } |
| 1778 | |
| 1779 | log_debug(channel->log, "Balance %s -> %s", |
| 1780 | type_to_string(tmpctx, struct amount_msat, &oldamt), |
| 1781 | type_to_string(tmpctx, struct amount_msat, |
| 1782 | &channel->our_msat)); |
| 1783 | if (amount_msat_less(channel->our_msat, channel->msat_to_us_min)) |
| 1784 | channel->msat_to_us_min = channel->our_msat; |
| 1785 | |
| 1786 | /* Coins have definitively moved, log a movement */ |
| 1787 | if (hout->am_origin) |
| 1788 | mvt = new_channel_mvt_invoice_hout(hout, hout, channel); |
| 1789 | else |
| 1790 | mvt = new_channel_mvt_routed_hout(hout, hout, channel); |
| 1791 | |
| 1792 | |
| 1793 | if (!mvt) |
| 1794 | log_broken(channel->log, |
| 1795 | "Unable to calculate fees." |
| 1796 | " Not logging an outbound HTLC"); |
| 1797 | else |
| 1798 | notify_channel_mvt(channel->peer->ld, mvt); |
| 1799 | } |
| 1800 | |
| 1801 | tal_free(hout); |
| 1802 | } |
| 1803 | |
| 1804 | static bool update_in_htlc(struct channel *channel, |
| 1805 | u64 id, enum htlc_state newstate) |
no test coverage detected