| 2004 | } |
| 2005 | |
| 2006 | static void remove_htlc_out(struct channel *channel, struct htlc_out *hout) |
| 2007 | { |
| 2008 | struct lightningd *ld = channel->peer->ld; |
| 2009 | |
| 2010 | htlc_out_check(hout, __func__); |
| 2011 | assert(hout->failonion || hout->preimage || hout->failmsg); |
| 2012 | log_debug(channel->log, "Removing out HTLC %"PRIu64" state %s %s", |
| 2013 | hout->key.id, htlc_state_name(hout->hstate), |
| 2014 | hout->preimage ? "FULFILLED" |
| 2015 | : hout->failmsg ? onion_wire_name(fromwire_peektype(hout->failmsg)) |
| 2016 | : "REMOTEFAIL"); |
| 2017 | |
| 2018 | /* If it's failed, now we can forward since it's completely locked-in */ |
| 2019 | if (!hout->preimage) { |
| 2020 | fail_out_htlc(hout, NULL); |
| 2021 | } else { |
| 2022 | const struct channel_coin_mvt *mvt; |
| 2023 | struct amount_msat oldamt = channel->our_msat; |
| 2024 | /* We paid for this HTLC, so deduct balance. */ |
| 2025 | if (!amount_msat_deduct(&channel->our_msat, hout->msat)) { |
| 2026 | channel_internal_error(channel, |
| 2027 | "Underflow our_msat %s - HTLC %s", |
| 2028 | fmt_amount_msat(tmpctx, |
| 2029 | channel->our_msat), |
| 2030 | fmt_amount_msat(tmpctx, |
| 2031 | hout->msat)); |
| 2032 | } |
| 2033 | |
| 2034 | log_debug(channel->log, "Balance %s -> %s", |
| 2035 | fmt_amount_msat(tmpctx, oldamt), |
| 2036 | fmt_amount_msat(tmpctx, channel->our_msat)); |
| 2037 | if (amount_msat_less(channel->our_msat, channel->msat_to_us_min)) |
| 2038 | channel->msat_to_us_min = channel->our_msat; |
| 2039 | |
| 2040 | /* Coins have definitively moved, log a movement */ |
| 2041 | if (hout->am_origin) |
| 2042 | mvt = new_channel_mvt_invoice_hout(hout, hout, channel); |
| 2043 | else |
| 2044 | mvt = new_channel_mvt_routed_hout(hout, hout, channel); |
| 2045 | |
| 2046 | |
| 2047 | if (!mvt) |
| 2048 | log_broken(channel->log, |
| 2049 | "Unable to calculate fees." |
| 2050 | " Not logging an outbound HTLC"); |
| 2051 | else |
| 2052 | wallet_save_channel_mvt(channel->peer->ld, mvt); |
| 2053 | } |
| 2054 | |
| 2055 | tal_free(hout); |
| 2056 | check_graceful_shutdown(ld); |
| 2057 | } |
| 2058 | |
| 2059 | static bool update_in_htlc(struct channel *channel, |
| 2060 | u64 id, enum htlc_state newstate) |
no test coverage detected