| 2074 | } |
| 2075 | |
| 2076 | static void remove_htlc_out(struct channel *channel, struct htlc_out *hout) |
| 2077 | { |
| 2078 | struct lightningd *ld = channel->peer->ld; |
| 2079 | |
| 2080 | htlc_out_check(hout, __func__); |
| 2081 | assert(hout->failonion || hout->preimage || hout->failmsg); |
| 2082 | log_debug(channel->log, "Removing out HTLC %"PRIu64" state %s %s", |
| 2083 | hout->key.id, htlc_state_name(hout->hstate), |
| 2084 | hout->preimage ? "FULFILLED" |
| 2085 | : hout->failmsg ? onion_wire_name(fromwire_peektype(hout->failmsg)) |
| 2086 | : "REMOTEFAIL"); |
| 2087 | |
| 2088 | /* If it's failed, now we can forward since it's completely locked-in */ |
| 2089 | if (!hout->preimage) { |
| 2090 | fail_out_htlc(hout, NULL); |
| 2091 | } else { |
| 2092 | const struct channel_coin_mvt *mvt; |
| 2093 | struct amount_msat oldamt = channel->our_msat; |
| 2094 | /* We paid for this HTLC, so deduct balance. */ |
| 2095 | if (!amount_msat_deduct(&channel->our_msat, hout->msat)) { |
| 2096 | channel_internal_error(channel, |
| 2097 | "Underflow our_msat %s - HTLC %s", |
| 2098 | fmt_amount_msat(tmpctx, |
| 2099 | channel->our_msat), |
| 2100 | fmt_amount_msat(tmpctx, |
| 2101 | hout->msat)); |
| 2102 | } |
| 2103 | |
| 2104 | log_debug(channel->log, "Balance %s -> %s", |
| 2105 | fmt_amount_msat(tmpctx, oldamt), |
| 2106 | fmt_amount_msat(tmpctx, channel->our_msat)); |
| 2107 | if (amount_msat_less(channel->our_msat, channel->msat_to_us_min)) |
| 2108 | channel->msat_to_us_min = channel->our_msat; |
| 2109 | |
| 2110 | /* Coins have definitively moved, log a movement */ |
| 2111 | if (hout->am_origin) |
| 2112 | mvt = new_channel_mvt_invoice_hout(hout, hout, channel); |
| 2113 | else |
| 2114 | mvt = new_channel_mvt_routed_hout(hout, hout, channel); |
| 2115 | |
| 2116 | |
| 2117 | if (!mvt) |
| 2118 | log_broken(channel->log, |
| 2119 | "Unable to calculate fees." |
| 2120 | " Not logging an outbound HTLC"); |
| 2121 | else |
| 2122 | wallet_save_channel_mvt(channel->peer->ld, mvt); |
| 2123 | } |
| 2124 | |
| 2125 | tal_free(hout); |
| 2126 | check_graceful_shutdown(ld); |
| 2127 | } |
| 2128 | |
| 2129 | static bool update_in_htlc(struct channel *channel, |
| 2130 | u64 id, enum htlc_state newstate) |
no test coverage detected