2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2021-06-22 20:25:59 +02:00
|
|
|
#include <common/blockheight_states.h>
|
2020-01-03 14:08:29 +01:00
|
|
|
#include <common/closing_fee.h>
|
2019-12-12 18:18:25 +01:00
|
|
|
#include <common/fee_states.h>
|
2019-08-24 23:06:08 +02:00
|
|
|
#include <common/json_command.h>
|
2018-03-16 15:19:11 +01:00
|
|
|
#include <common/wire_error.h>
|
2020-08-25 04:16:22 +02:00
|
|
|
#include <connectd/connectd_wiregen.h>
|
2018-07-23 04:23:03 +02:00
|
|
|
#include <errno.h>
|
2020-08-25 03:55:38 +02:00
|
|
|
#include <hsmd/hsmd_wiregen.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <lightningd/channel.h>
|
2024-01-31 04:16:17 +01:00
|
|
|
#include <lightningd/channel_gossip.h>
|
2020-10-22 01:51:08 +02:00
|
|
|
#include <lightningd/channel_state_names_gen.h>
|
2018-08-09 02:25:29 +02:00
|
|
|
#include <lightningd/connect_control.h>
|
2024-01-31 04:16:17 +01:00
|
|
|
#include <lightningd/gossip_control.h>
|
2023-04-10 02:12:56 +02:00
|
|
|
#include <lightningd/hsm_control.h>
|
2020-09-08 16:11:05 +02:00
|
|
|
#include <lightningd/notification.h>
|
2020-09-17 22:28:46 +02:00
|
|
|
#include <lightningd/opening_common.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/subd.h>
|
2024-01-31 04:16:17 +01:00
|
|
|
#include <sodium/randombytes.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <wallet/txfilter.h>
|
2022-08-08 21:58:09 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
|
2019-07-25 04:47:34 +02:00
|
|
|
void channel_set_owner(struct channel *channel, struct subd *owner)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
|
|
|
struct subd *old_owner = channel->owner;
|
|
|
|
channel->owner = owner;
|
|
|
|
|
2022-07-18 14:12:28 +02:00
|
|
|
if (old_owner)
|
2018-02-12 11:13:04 +01:00
|
|
|
subd_release_channel(old_owner, channel);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:23:45 +01:00
|
|
|
struct htlc_out *channel_has_htlc_out(struct channel *channel)
|
2018-02-12 11:10:46 +01:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
struct htlc_out *hout;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
|
2023-01-03 05:46:52 +01:00
|
|
|
for (hout = htlc_out_map_first(ld->htlcs_out, &outi);
|
2018-02-12 11:13:04 +01:00
|
|
|
hout;
|
2023-01-03 05:46:52 +01:00
|
|
|
hout = htlc_out_map_next(ld->htlcs_out, &outi)) {
|
2018-02-28 23:23:45 +01:00
|
|
|
if (hout->key.channel == channel)
|
|
|
|
return hout;
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:23:45 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct htlc_in *channel_has_htlc_in(struct channel *channel)
|
|
|
|
{
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
|
2023-01-03 05:46:52 +01:00
|
|
|
for (hin = htlc_in_map_first(ld->htlcs_in, &ini);
|
2018-02-12 11:13:04 +01:00
|
|
|
hin;
|
2023-01-03 05:46:52 +01:00
|
|
|
hin = htlc_in_map_next(ld->htlcs_in, &ini)) {
|
2018-02-28 23:23:45 +01:00
|
|
|
if (hin->key.channel == channel)
|
|
|
|
return hin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_channel(struct channel *channel)
|
|
|
|
{
|
|
|
|
/* Must not have any HTLCs! */
|
|
|
|
struct htlc_out *hout = channel_has_htlc_out(channel);
|
|
|
|
struct htlc_in *hin = channel_has_htlc_in(channel);
|
|
|
|
|
|
|
|
if (hout)
|
|
|
|
fatal("Freeing channel %s has hout %s",
|
|
|
|
channel_state_name(channel),
|
|
|
|
htlc_state_name(hout->hstate));
|
|
|
|
|
|
|
|
if (hin)
|
2018-02-12 11:13:04 +01:00
|
|
|
fatal("Freeing channel %s has hin %s",
|
|
|
|
channel_state_name(channel),
|
|
|
|
htlc_state_name(hin->hstate));
|
|
|
|
|
2019-08-24 23:06:08 +02:00
|
|
|
for (size_t i = 0; i < tal_count(channel->forgets); i++)
|
|
|
|
was_pending(command_fail(channel->forgets[i], LIGHTNINGD,
|
|
|
|
"Channel structure was freed!"));
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
/* Free any old owner still hanging around. */
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
list_del_from(&channel->peer->channels, &channel->list);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 08:52:36 +02:00
|
|
|
void delete_channel(struct channel *channel STEALS)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
2023-12-04 04:39:45 +01:00
|
|
|
const u8 *msg;
|
|
|
|
|
2018-02-14 02:53:04 +01:00
|
|
|
struct peer *peer = channel->peer;
|
2021-01-20 01:21:45 +01:00
|
|
|
if (channel->dbid != 0)
|
|
|
|
wallet_channel_close(channel->peer->ld->wallet, channel->dbid);
|
2023-12-04 04:39:45 +01:00
|
|
|
|
|
|
|
/* Tell the hsm to forget the channel, needs to be after it's
|
|
|
|
* been forgotten here */
|
|
|
|
if (hsm_capable(channel->peer->ld, WIRE_HSMD_FORGET_CHANNEL)) {
|
|
|
|
msg = towire_hsmd_forget_channel(NULL, &channel->peer->id, channel->dbid);
|
|
|
|
msg = hsm_sync_req(tmpctx, channel->peer->ld, take(msg));
|
|
|
|
if (!fromwire_hsmd_forget_channel_reply(msg))
|
|
|
|
fatal("HSM gave bad hsm_forget_channel_reply %s", tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_free(channel);
|
2018-02-14 02:53:04 +01:00
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
maybe_delete_peer(peer);
|
2018-02-12 11:10:46 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 21:16:23 +02:00
|
|
|
bool maybe_cleanup_last_inflight(struct channel *channel)
|
|
|
|
{
|
|
|
|
struct channel_inflight *inflight;
|
|
|
|
inflight = channel_current_inflight(channel);
|
|
|
|
if (!inflight)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (inflight->last_tx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Remove from database */
|
|
|
|
wallet_channel_inflight_cleanup_incomplete(
|
|
|
|
channel->peer->ld->wallet, channel->dbid);
|
|
|
|
tal_free(inflight);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
void get_channel_basepoints(struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *peer_id,
|
2018-07-23 04:23:03 +02:00
|
|
|
const u64 dbid,
|
|
|
|
struct basepoints *local_basepoints,
|
|
|
|
struct pubkey *local_funding_pubkey)
|
2018-02-12 11:10:46 +01:00
|
|
|
{
|
2023-04-10 02:12:56 +02:00
|
|
|
const u8 *msg;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
|
|
|
assert(dbid != 0);
|
2020-08-25 03:55:38 +02:00
|
|
|
msg = towire_hsmd_get_channel_basepoints(NULL, peer_id, dbid);
|
2023-04-10 02:12:56 +02:00
|
|
|
msg = hsm_sync_req(tmpctx, ld, take(msg));
|
2020-08-25 03:55:38 +02:00
|
|
|
if (!fromwire_hsmd_get_channel_basepoints_reply(msg, local_basepoints,
|
2018-07-23 04:23:03 +02:00
|
|
|
local_funding_pubkey))
|
|
|
|
fatal("HSM gave bad hsm_get_channel_basepoints_reply %s",
|
|
|
|
tal_hex(msg, msg));
|
2018-02-12 11:10:46 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 22:14:44 +01:00
|
|
|
static void destroy_inflight(struct channel_inflight *inflight)
|
|
|
|
{
|
|
|
|
list_del_from(&inflight->channel->inflights, &inflight->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct channel_inflight *
|
|
|
|
new_inflight(struct channel *channel,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *funding_outpoint,
|
2021-02-04 22:14:44 +01:00
|
|
|
u32 funding_feerate,
|
|
|
|
struct amount_sat total_funds,
|
|
|
|
struct amount_sat our_funds,
|
|
|
|
struct wally_psbt *psbt STEALS,
|
2021-06-17 03:28:18 +02:00
|
|
|
const u32 lease_expiry,
|
|
|
|
const secp256k1_ecdsa_signature *lease_commit_sig,
|
2021-06-22 20:25:59 +02:00
|
|
|
const u32 lease_chan_max_msat, const u16 lease_chan_max_ppt,
|
2021-12-08 18:42:07 +01:00
|
|
|
const u32 lease_blockheight_start,
|
2022-10-24 20:12:36 +02:00
|
|
|
const struct amount_msat lease_fee,
|
2023-07-27 23:37:52 +02:00
|
|
|
const struct amount_sat lease_amt,
|
|
|
|
s64 splice_amnt,
|
2023-11-03 01:08:33 +01:00
|
|
|
bool i_am_initiator,
|
|
|
|
bool force_sign_first)
|
2021-02-04 22:14:44 +01:00
|
|
|
{
|
|
|
|
struct channel_inflight *inflight
|
|
|
|
= tal(channel, struct channel_inflight);
|
|
|
|
struct funding_info *funding
|
|
|
|
= tal(inflight, struct funding_info);
|
|
|
|
|
2021-10-13 05:45:36 +02:00
|
|
|
funding->outpoint = *funding_outpoint;
|
2021-02-04 22:14:44 +01:00
|
|
|
funding->total_funds = total_funds;
|
|
|
|
funding->feerate = funding_feerate;
|
|
|
|
funding->our_funds = our_funds;
|
2023-07-27 23:37:52 +02:00
|
|
|
funding->splice_amnt = splice_amnt;
|
2021-02-04 22:14:44 +01:00
|
|
|
|
|
|
|
inflight->funding = funding;
|
2021-03-16 01:43:33 +01:00
|
|
|
inflight->channel = channel;
|
|
|
|
inflight->remote_tx_sigs = false;
|
2021-02-04 22:14:44 +01:00
|
|
|
inflight->funding_psbt = tal_steal(inflight, psbt);
|
2023-07-27 22:17:33 +02:00
|
|
|
inflight->last_tx = NULL;
|
2021-03-18 01:33:35 +01:00
|
|
|
inflight->tx_broadcast = false;
|
2021-02-04 22:14:44 +01:00
|
|
|
|
2021-06-22 20:25:59 +02:00
|
|
|
/* Channel lease infos */
|
|
|
|
inflight->lease_blockheight_start = lease_blockheight_start;
|
2021-06-17 03:28:18 +02:00
|
|
|
inflight->lease_expiry = lease_expiry;
|
2021-12-28 00:21:09 +01:00
|
|
|
inflight->lease_commit_sig
|
|
|
|
= tal_dup_or_null(inflight, secp256k1_ecdsa_signature,
|
2021-06-22 20:25:59 +02:00
|
|
|
lease_commit_sig);
|
|
|
|
|
2021-06-17 03:28:18 +02:00
|
|
|
inflight->lease_chan_max_msat = lease_chan_max_msat;
|
|
|
|
inflight->lease_chan_max_ppt = lease_chan_max_ppt;
|
2021-12-08 18:42:07 +01:00
|
|
|
inflight->lease_fee = lease_fee;
|
2022-10-24 20:12:36 +02:00
|
|
|
inflight->lease_amt = lease_amt;
|
2021-06-17 03:28:18 +02:00
|
|
|
|
2023-07-27 23:37:52 +02:00
|
|
|
inflight->i_am_initiator = i_am_initiator;
|
2023-11-03 01:08:33 +01:00
|
|
|
inflight->force_sign_first = force_sign_first;
|
2023-08-16 04:58:53 +02:00
|
|
|
inflight->splice_locked_memonly = false;
|
2023-07-27 23:37:52 +02:00
|
|
|
|
2021-02-04 22:14:44 +01:00
|
|
|
list_add_tail(&channel->inflights, &inflight->list);
|
|
|
|
tal_add_destructor(inflight, destroy_inflight);
|
|
|
|
|
|
|
|
return inflight;
|
|
|
|
}
|
|
|
|
|
2023-10-26 18:41:34 +02:00
|
|
|
void inflight_set_last_tx(struct channel_inflight *inflight,
|
|
|
|
struct bitcoin_tx *last_tx,
|
|
|
|
const struct bitcoin_signature last_sig)
|
|
|
|
{
|
|
|
|
struct wally_psbt *last_tx_psbt_clone;
|
|
|
|
assert(inflight->last_tx == NULL);
|
|
|
|
assert(last_tx);
|
|
|
|
|
|
|
|
last_tx_psbt_clone = clone_psbt(inflight, last_tx->psbt);
|
|
|
|
inflight->last_tx = bitcoin_tx_with_psbt(inflight, last_tx_psbt_clone);
|
|
|
|
inflight->last_sig = last_sig;
|
|
|
|
}
|
|
|
|
|
2021-01-20 02:07:04 +01:00
|
|
|
struct open_attempt *new_channel_open_attempt(struct channel *channel)
|
|
|
|
{
|
|
|
|
struct open_attempt *oa = tal(channel, struct open_attempt);
|
|
|
|
oa->channel = channel;
|
|
|
|
/* Copy over the config; we'll clobber the reserve */
|
|
|
|
oa->our_config = channel->our_config;
|
|
|
|
oa->role = channel->opener == LOCAL ? TX_INITIATOR : TX_ACCEPTER;
|
|
|
|
oa->our_upfront_shutdown_script = NULL;
|
|
|
|
oa->cmd = NULL;
|
2021-03-09 22:14:08 +01:00
|
|
|
oa->aborted = false;
|
2022-03-22 21:27:29 +01:00
|
|
|
oa->open_msg = NULL;
|
2021-01-20 02:07:04 +01:00
|
|
|
|
|
|
|
return oa;
|
|
|
|
}
|
|
|
|
|
2021-01-20 02:09:38 +01:00
|
|
|
struct channel *new_unsaved_channel(struct peer *peer,
|
|
|
|
u32 feerate_base,
|
|
|
|
u32 feerate_ppm)
|
|
|
|
{
|
|
|
|
struct lightningd *ld = peer->ld;
|
|
|
|
struct channel *channel = tal(ld, struct channel);
|
2023-04-10 02:12:56 +02:00
|
|
|
const u8 *msg;
|
2021-01-20 02:09:38 +01:00
|
|
|
|
|
|
|
channel->peer = peer;
|
|
|
|
/* Not saved to the database yet! */
|
|
|
|
channel->unsaved_dbid = wallet_get_channel_dbid(ld->wallet);
|
|
|
|
/* A zero value database id means it's not saved in the database yet */
|
|
|
|
channel->dbid = 0;
|
|
|
|
channel->error = NULL;
|
|
|
|
channel->openchannel_signed_cmd = NULL;
|
|
|
|
channel->state = DUALOPEND_OPEN_INIT;
|
|
|
|
channel->owner = NULL;
|
2022-06-24 02:48:18 +02:00
|
|
|
channel->scb = NULL;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->reestablished = false;
|
2021-01-20 02:09:38 +01:00
|
|
|
memset(&channel->billboard, 0, sizeof(channel->billboard));
|
|
|
|
channel->billboard.transient = tal_fmt(channel, "%s",
|
|
|
|
"Empty channel init'd");
|
2023-07-16 07:56:52 +02:00
|
|
|
channel->log = new_logger(channel, ld->log_book,
|
|
|
|
&peer->id,
|
|
|
|
"chan#%"PRIu64,
|
|
|
|
channel->unsaved_dbid);
|
2021-01-20 02:09:38 +01:00
|
|
|
|
|
|
|
channel->our_config.id = 0;
|
|
|
|
channel->open_attempt = NULL;
|
|
|
|
|
|
|
|
channel->last_htlc_sigs = NULL;
|
2022-09-10 04:10:31 +02:00
|
|
|
channel->remote_channel_ready = false;
|
2021-01-20 02:09:38 +01:00
|
|
|
channel->scid = NULL;
|
|
|
|
channel->next_index[LOCAL] = 1;
|
|
|
|
channel->next_index[REMOTE] = 1;
|
|
|
|
channel->next_htlc_id = 0;
|
2023-10-02 00:59:52 +02:00
|
|
|
channel->funding_spend_watch = NULL;
|
2021-01-20 02:09:38 +01:00
|
|
|
/* FIXME: remove push when v1 deprecated */
|
|
|
|
channel->push = AMOUNT_MSAT(0);
|
|
|
|
channel->closing_fee_negotiation_step = 50;
|
|
|
|
channel->closing_fee_negotiation_step_unit
|
|
|
|
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
|
2021-03-15 21:25:41 +01:00
|
|
|
channel->shutdown_wrong_funding = NULL;
|
2021-09-08 06:41:46 +02:00
|
|
|
channel->closing_feerate_range = NULL;
|
2024-01-31 04:16:17 +01:00
|
|
|
channel->alias[REMOTE] = NULL;
|
|
|
|
/* We don't even bother checking for clashes. */
|
|
|
|
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
|
|
|
|
randombytes_buf(channel->alias[LOCAL], sizeof(struct short_channel_id));
|
2021-01-20 02:09:38 +01:00
|
|
|
|
|
|
|
channel->shutdown_scriptpubkey[REMOTE] = NULL;
|
|
|
|
channel->last_was_revoke = false;
|
|
|
|
channel->last_sent_commit = NULL;
|
|
|
|
|
|
|
|
channel->feerate_base = feerate_base;
|
|
|
|
channel->feerate_ppm = feerate_ppm;
|
2021-09-23 04:42:47 +02:00
|
|
|
channel->old_feerate_timeout.ts.tv_sec = 0;
|
|
|
|
channel->old_feerate_timeout.ts.tv_nsec = 0;
|
2021-01-20 02:09:38 +01:00
|
|
|
/* closer not yet known */
|
|
|
|
channel->closer = NUM_SIDES;
|
2023-04-06 01:33:24 +02:00
|
|
|
channel->close_blockheight = NULL;
|
2023-06-26 00:56:21 +02:00
|
|
|
/* In case someone looks at channels before open negotiation,
|
|
|
|
* initialize this with default */
|
|
|
|
channel->type = default_channel_type(channel,
|
|
|
|
ld->our_features,
|
|
|
|
peer->their_features);
|
2021-01-20 02:09:38 +01:00
|
|
|
|
|
|
|
/* BOLT-7b04b1461739c5036add61782d58ac490842d98b #9
|
|
|
|
* | 222/223 | `option_dual_fund`
|
|
|
|
* | Use v2 of channel open, enables dual funding
|
2022-10-19 20:35:45 +02:00
|
|
|
* | IN9 */
|
2021-06-04 07:13:47 +02:00
|
|
|
channel->static_remotekey_start[LOCAL]
|
|
|
|
= channel->static_remotekey_start[REMOTE] = 0;
|
2022-10-19 20:35:45 +02:00
|
|
|
|
2021-01-20 02:09:38 +01:00
|
|
|
channel->future_per_commitment_point = NULL;
|
|
|
|
|
2021-06-17 03:28:18 +02:00
|
|
|
channel->lease_commit_sig = NULL;
|
2023-07-21 09:15:22 +02:00
|
|
|
channel->ignore_fee_limits = ld->config.ignore_fee_limits;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->last_stable_connection = 0;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->stable_conn_timer = NULL;
|
2021-06-17 03:28:18 +02:00
|
|
|
|
2021-01-20 02:09:38 +01:00
|
|
|
/* No shachain yet */
|
|
|
|
channel->their_shachain.id = 0;
|
|
|
|
shachain_init(&channel->their_shachain.chain);
|
|
|
|
|
2021-11-04 18:02:12 +01:00
|
|
|
msg = towire_hsmd_new_channel(NULL, &peer->id, channel->unsaved_dbid);
|
2023-04-10 02:12:56 +02:00
|
|
|
msg = hsm_sync_req(tmpctx, ld, take(msg));
|
2021-11-04 18:02:12 +01:00
|
|
|
if (!fromwire_hsmd_new_channel_reply(msg))
|
|
|
|
fatal("HSM gave bad hsm_new_channel_reply %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
|
2021-01-20 02:09:38 +01:00
|
|
|
get_channel_basepoints(ld, &peer->id, channel->unsaved_dbid,
|
|
|
|
&channel->local_basepoints,
|
|
|
|
&channel->local_funding_pubkey);
|
|
|
|
|
2024-01-31 04:16:17 +01:00
|
|
|
/* channel->channel_gossip gets populated once we know if it's public. */
|
|
|
|
channel->channel_gossip = NULL;
|
2021-01-20 02:09:38 +01:00
|
|
|
channel->forgets = tal_arr(channel, struct command *, 0);
|
|
|
|
list_add_tail(&peer->channels, &channel->list);
|
|
|
|
channel->rr_number = peer->ld->rr_counter++;
|
|
|
|
tal_add_destructor(channel, destroy_channel);
|
|
|
|
|
|
|
|
list_head_init(&channel->inflights);
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2022-03-21 01:58:23 +01:00
|
|
|
/*
|
|
|
|
* The maximum msat that this node could possibly accept for an htlc.
|
|
|
|
* It's the default htlc_maximum_msat in channel_updates, if none is
|
|
|
|
* explicitly set (and the cap on what can be set!).
|
|
|
|
*
|
|
|
|
* We advertize the maximum value possible, defined as the smaller
|
|
|
|
* of the remote's maximum in-flight HTLC or the total channel
|
|
|
|
* capacity the reserve we have to keep.
|
|
|
|
* FIXME: does this need fuzz?
|
|
|
|
*/
|
2022-03-21 01:58:27 +01:00
|
|
|
struct amount_msat htlc_max_possible_send(const struct channel *channel)
|
2022-03-21 01:58:23 +01:00
|
|
|
{
|
|
|
|
struct amount_sat lower_bound;
|
|
|
|
struct amount_msat lower_bound_msat;
|
|
|
|
|
|
|
|
/* These shouldn't fail */
|
|
|
|
if (!amount_sat_sub(&lower_bound, channel->funding_sats,
|
|
|
|
channel->channel_info.their_config.channel_reserve)) {
|
|
|
|
log_broken(channel->log, "%s: their reserve %s > funding %s!",
|
|
|
|
__func__,
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_amount_sat(tmpctx, channel->funding_sats),
|
|
|
|
fmt_amount_sat(tmpctx,
|
|
|
|
channel->channel_info.their_config.channel_reserve));
|
2022-03-21 01:58:23 +01:00
|
|
|
return AMOUNT_MSAT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!amount_sat_to_msat(&lower_bound_msat, lower_bound)) {
|
|
|
|
log_broken(channel->log, "%s: impossible size channel %s!",
|
|
|
|
__func__,
|
2024-03-20 01:47:52 +01:00
|
|
|
fmt_amount_sat(tmpctx, lower_bound));
|
2022-03-21 01:58:23 +01:00
|
|
|
return AMOUNT_MSAT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amount_msat_less(channel->channel_info.their_config.max_htlc_value_in_flight,
|
|
|
|
lower_bound_msat))
|
|
|
|
lower_bound_msat = channel->channel_info.their_config.max_htlc_value_in_flight;
|
|
|
|
|
|
|
|
return lower_bound_msat;
|
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
struct channel *new_channel(struct peer *peer, u64 dbid,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct wallet_shachain *their_shachain,
|
2018-02-19 02:06:14 +01:00
|
|
|
enum channel_state state,
|
2019-09-09 18:11:24 +02:00
|
|
|
enum side opener,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* NULL or stolen */
|
2023-07-16 07:56:52 +02:00
|
|
|
struct logger *log,
|
2018-02-23 06:53:44 +01:00
|
|
|
const char *transient_billboard TAKES,
|
2018-02-19 02:06:14 +01:00
|
|
|
u8 channel_flags,
|
2023-01-12 20:14:47 +01:00
|
|
|
bool req_confirmed_ins_local,
|
2023-01-10 21:59:55 +01:00
|
|
|
bool req_confirmed_ins_remote,
|
2018-02-19 02:06:14 +01:00
|
|
|
const struct channel_config *our_config,
|
|
|
|
u32 minimum_depth,
|
|
|
|
u64 next_index_local,
|
|
|
|
u64 next_index_remote,
|
|
|
|
u64 next_htlc_id,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *funding,
|
|
|
|
struct amount_sat funding_sats,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat push,
|
2019-09-30 23:47:16 +02:00
|
|
|
struct amount_sat our_funds,
|
2022-09-10 04:10:31 +02:00
|
|
|
bool remote_channel_ready,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* NULL or stolen */
|
|
|
|
struct short_channel_id *scid,
|
2024-01-31 04:16:17 +01:00
|
|
|
struct short_channel_id *alias_local TAKES,
|
2022-04-22 12:57:01 +02:00
|
|
|
struct short_channel_id *alias_remote STEALS,
|
2020-09-09 09:20:53 +02:00
|
|
|
struct channel_id *cid,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat our_msat,
|
|
|
|
struct amount_msat msat_to_us_min,
|
|
|
|
struct amount_msat msat_to_us_max,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* Stolen */
|
|
|
|
struct bitcoin_tx *last_tx,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *last_sig,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* NULL or stolen */
|
2020-08-13 19:45:02 +02:00
|
|
|
const struct bitcoin_signature *last_htlc_sigs,
|
2018-02-19 02:06:14 +01:00
|
|
|
const struct channel_info *channel_info,
|
2020-09-17 03:58:59 +02:00
|
|
|
const struct fee_states *fee_states TAKES,
|
2018-02-19 02:06:14 +01:00
|
|
|
/* NULL or stolen */
|
|
|
|
u8 *remote_shutdown_scriptpubkey,
|
2019-10-15 03:08:33 +02:00
|
|
|
const u8 *local_shutdown_scriptpubkey,
|
2018-03-07 01:06:07 +01:00
|
|
|
u64 final_key_idx,
|
2018-02-19 02:06:14 +01:00
|
|
|
bool last_was_revoke,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct changed_htlc *last_sent_commit,
|
2018-04-03 09:19:39 +02:00
|
|
|
u32 first_blocknum,
|
|
|
|
u32 min_possible_feerate,
|
2018-04-26 06:51:01 +02:00
|
|
|
u32 max_possible_feerate,
|
2018-07-23 04:23:02 +02:00
|
|
|
const struct basepoints *local_basepoints,
|
2018-08-17 07:06:36 +02:00
|
|
|
const struct pubkey *local_funding_pubkey,
|
2019-02-21 01:01:33 +01:00
|
|
|
const struct pubkey *future_per_commitment_point,
|
|
|
|
u32 feerate_base,
|
2019-05-01 00:57:09 +02:00
|
|
|
u32 feerate_ppm,
|
2019-09-10 04:22:27 +02:00
|
|
|
const u8 *remote_upfront_shutdown_script,
|
2021-06-04 07:13:47 +02:00
|
|
|
u64 local_static_remotekey_start,
|
|
|
|
u64 remote_static_remotekey_start,
|
2021-09-09 07:29:35 +02:00
|
|
|
const struct channel_type *type STEALS,
|
2020-10-28 11:46:17 +01:00
|
|
|
enum side closer,
|
2021-03-15 21:25:41 +01:00
|
|
|
enum state_change reason,
|
|
|
|
/* NULL or stolen */
|
2021-06-17 03:28:18 +02:00
|
|
|
const struct bitcoin_outpoint *shutdown_wrong_funding,
|
2021-06-22 20:25:59 +02:00
|
|
|
const struct height_states *height_states TAKES,
|
2021-06-17 03:28:18 +02:00
|
|
|
u32 lease_expiry,
|
|
|
|
secp256k1_ecdsa_signature *lease_commit_sig STEALS,
|
|
|
|
u32 lease_chan_max_msat,
|
2022-03-21 01:58:23 +01:00
|
|
|
u16 lease_chan_max_ppt,
|
2022-03-21 01:58:54 +01:00
|
|
|
struct amount_msat htlc_minimum_msat,
|
2023-07-21 09:15:22 +02:00
|
|
|
struct amount_msat htlc_maximum_msat,
|
2023-12-06 21:14:05 +01:00
|
|
|
bool ignore_fee_limits,
|
|
|
|
/* NULL or stolen */
|
2024-01-31 04:16:19 +01:00
|
|
|
struct peer_update *peer_update STEALS,
|
|
|
|
u64 last_stable_connection)
|
2018-02-12 11:10:46 +01:00
|
|
|
{
|
2018-02-19 02:06:14 +01:00
|
|
|
struct channel *channel = tal(peer->ld, struct channel);
|
2022-03-21 01:58:54 +01:00
|
|
|
struct amount_msat htlc_min, htlc_max;
|
2018-02-12 11:10:46 +01:00
|
|
|
|
2023-07-10 21:59:41 +02:00
|
|
|
bool anysegwit = !chainparams->is_elements && feature_negotiated(peer->ld->our_features,
|
|
|
|
peer->their_features,
|
|
|
|
OPT_SHUTDOWN_ANYSEGWIT);
|
|
|
|
|
2018-02-18 13:53:46 +01:00
|
|
|
assert(dbid != 0);
|
2018-02-12 11:10:46 +01:00
|
|
|
channel->peer = peer;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->dbid = dbid;
|
2021-01-20 02:09:38 +01:00
|
|
|
channel->unsaved_dbid = 0;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->reestablished = false;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->error = NULL;
|
2021-01-20 02:07:04 +01:00
|
|
|
channel->open_attempt = NULL;
|
2020-11-24 01:54:56 +01:00
|
|
|
channel->openchannel_signed_cmd = NULL;
|
2018-02-19 02:06:14 +01:00
|
|
|
if (their_shachain)
|
|
|
|
channel->their_shachain = *their_shachain;
|
|
|
|
else {
|
|
|
|
channel->their_shachain.id = 0;
|
|
|
|
shachain_init(&channel->their_shachain.chain);
|
|
|
|
}
|
|
|
|
channel->state = state;
|
2019-09-09 18:11:24 +02:00
|
|
|
channel->opener = opener;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->owner = NULL;
|
2018-02-23 06:53:44 +01:00
|
|
|
memset(&channel->billboard, 0, sizeof(channel->billboard));
|
|
|
|
channel->billboard.transient = tal_strdup(channel, transient_billboard);
|
2023-05-30 06:27:56 +02:00
|
|
|
|
|
|
|
/* If it's a unix domain socket connection, we don't save it */
|
|
|
|
if (peer->addr.itype == ADDR_INTERNAL_WIREADDR) {
|
|
|
|
channel->scb = tal(channel, struct scb_chan);
|
|
|
|
channel->scb->id = dbid;
|
|
|
|
channel->scb->unused = 0;
|
2023-05-30 06:28:18 +02:00
|
|
|
channel->scb->addr = peer->addr.u.wireaddr.wireaddr;
|
2023-05-30 06:27:56 +02:00
|
|
|
channel->scb->node_id = peer->id;
|
|
|
|
channel->scb->funding = *funding;
|
|
|
|
channel->scb->cid = *cid;
|
|
|
|
channel->scb->funding_sats = funding_sats;
|
|
|
|
channel->scb->type = channel_type_dup(channel->scb, type);
|
|
|
|
} else
|
|
|
|
channel->scb = NULL;
|
2018-02-23 06:53:44 +01:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
if (!log) {
|
2023-07-16 07:56:52 +02:00
|
|
|
channel->log = new_logger(channel,
|
|
|
|
peer->ld->log_book,
|
|
|
|
&channel->peer->id,
|
|
|
|
"chan#%"PRIu64,
|
|
|
|
dbid);
|
2018-02-19 02:06:14 +01:00
|
|
|
} else
|
|
|
|
channel->log = tal_steal(channel, log);
|
2023-01-12 20:14:47 +01:00
|
|
|
channel->req_confirmed_ins[LOCAL] = req_confirmed_ins_local;
|
|
|
|
channel->req_confirmed_ins[REMOTE] = req_confirmed_ins_remote;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->channel_flags = channel_flags;
|
|
|
|
channel->our_config = *our_config;
|
|
|
|
channel->minimum_depth = minimum_depth;
|
2022-06-02 01:39:53 +02:00
|
|
|
channel->depth = 0;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->next_index[LOCAL] = next_index_local;
|
|
|
|
channel->next_index[REMOTE] = next_index_remote;
|
|
|
|
channel->next_htlc_id = next_htlc_id;
|
2021-10-13 05:45:36 +02:00
|
|
|
channel->funding = *funding;
|
|
|
|
channel->funding_sats = funding_sats;
|
2023-10-02 00:59:52 +02:00
|
|
|
channel->funding_spend_watch = NULL;
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->push = push;
|
2019-09-30 23:47:16 +02:00
|
|
|
channel->our_funds = our_funds;
|
2022-09-10 04:10:31 +02:00
|
|
|
channel->remote_channel_ready = remote_channel_ready;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->scid = tal_steal(channel, scid);
|
2024-01-31 04:16:17 +01:00
|
|
|
channel->alias[LOCAL] = tal_dup_or_null(channel, struct short_channel_id, alias_local);
|
2022-04-22 12:57:01 +02:00
|
|
|
channel->alias[REMOTE] = tal_steal(channel, alias_remote); /* Haven't gotten one yet. */
|
2020-09-09 09:20:53 +02:00
|
|
|
channel->cid = *cid;
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->our_msat = our_msat;
|
|
|
|
channel->msat_to_us_min = msat_to_us_min;
|
|
|
|
channel->msat_to_us_max = msat_to_us_max;
|
2022-06-24 02:48:18 +02:00
|
|
|
channel->last_tx = tal_steal(channel, last_tx);
|
|
|
|
if (channel->last_tx) {
|
|
|
|
channel->last_tx->chainparams = chainparams;
|
|
|
|
}
|
2023-10-31 02:40:52 +01:00
|
|
|
if (last_sig)
|
|
|
|
channel->last_sig = *last_sig;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs);
|
|
|
|
channel->channel_info = *channel_info;
|
2020-09-17 03:58:59 +02:00
|
|
|
channel->fee_states = dup_fee_states(channel, fee_states);
|
2019-09-29 09:35:45 +02:00
|
|
|
channel->shutdown_scriptpubkey[REMOTE]
|
2018-02-19 02:06:14 +01:00
|
|
|
= tal_steal(channel, remote_shutdown_scriptpubkey);
|
2018-03-07 01:06:07 +01:00
|
|
|
channel->final_key_idx = final_key_idx;
|
2020-01-03 14:08:29 +01:00
|
|
|
channel->closing_fee_negotiation_step = 50;
|
|
|
|
channel->closing_fee_negotiation_step_unit
|
|
|
|
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
|
2021-03-15 21:25:41 +01:00
|
|
|
channel->shutdown_wrong_funding
|
|
|
|
= tal_steal(channel, shutdown_wrong_funding);
|
2021-09-08 06:41:46 +02:00
|
|
|
channel->closing_feerate_range = NULL;
|
2023-07-10 21:59:41 +02:00
|
|
|
if (local_shutdown_scriptpubkey) {
|
2019-09-29 09:35:45 +02:00
|
|
|
channel->shutdown_scriptpubkey[LOCAL]
|
|
|
|
= tal_steal(channel, local_shutdown_scriptpubkey);
|
2023-07-10 21:59:41 +02:00
|
|
|
} else if (anysegwit) {
|
|
|
|
channel->shutdown_scriptpubkey[LOCAL]
|
|
|
|
= p2tr_for_keyidx(channel, channel->peer->ld,
|
|
|
|
channel->final_key_idx);
|
|
|
|
} else {
|
2019-09-29 09:35:45 +02:00
|
|
|
channel->shutdown_scriptpubkey[LOCAL]
|
|
|
|
= p2wpkh_for_keyidx(channel, channel->peer->ld,
|
2023-07-10 21:59:41 +02:00
|
|
|
channel->final_key_idx);
|
|
|
|
}
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->last_was_revoke = last_was_revoke;
|
|
|
|
channel->last_sent_commit = tal_steal(channel, last_sent_commit);
|
2018-02-12 11:10:46 +01:00
|
|
|
channel->first_blocknum = first_blocknum;
|
2018-04-03 09:19:39 +02:00
|
|
|
channel->min_possible_feerate = min_possible_feerate;
|
|
|
|
channel->max_possible_feerate = max_possible_feerate;
|
2018-07-23 04:23:02 +02:00
|
|
|
channel->local_basepoints = *local_basepoints;
|
|
|
|
channel->local_funding_pubkey = *local_funding_pubkey;
|
2018-08-17 07:06:36 +02:00
|
|
|
channel->future_per_commitment_point
|
|
|
|
= tal_steal(channel, future_per_commitment_point);
|
2019-02-21 01:01:33 +01:00
|
|
|
channel->feerate_base = feerate_base;
|
|
|
|
channel->feerate_ppm = feerate_ppm;
|
2021-09-23 04:42:47 +02:00
|
|
|
channel->old_feerate_timeout.ts.tv_sec = 0;
|
|
|
|
channel->old_feerate_timeout.ts.tv_nsec = 0;
|
2019-05-01 00:57:09 +02:00
|
|
|
channel->remote_upfront_shutdown_script
|
|
|
|
= tal_steal(channel, remote_upfront_shutdown_script);
|
2021-06-04 07:13:47 +02:00
|
|
|
channel->static_remotekey_start[LOCAL] = local_static_remotekey_start;
|
|
|
|
channel->static_remotekey_start[REMOTE] = remote_static_remotekey_start;
|
2021-09-09 07:29:35 +02:00
|
|
|
channel->type = tal_steal(channel, type);
|
2019-08-24 23:06:08 +02:00
|
|
|
channel->forgets = tal_arr(channel, struct command *, 0);
|
2018-02-19 02:06:14 +01:00
|
|
|
|
2021-06-17 03:28:18 +02:00
|
|
|
channel->lease_expiry = lease_expiry;
|
|
|
|
channel->lease_commit_sig = tal_steal(channel, lease_commit_sig);
|
|
|
|
channel->lease_chan_max_msat = lease_chan_max_msat;
|
|
|
|
channel->lease_chan_max_ppt = lease_chan_max_ppt;
|
2021-06-22 20:25:59 +02:00
|
|
|
channel->blockheight_states = dup_height_states(channel, height_states);
|
2021-06-17 03:28:18 +02:00
|
|
|
|
2022-03-21 01:58:54 +01:00
|
|
|
/* DB migration, for example, sets min to 0, max to large: fixup */
|
|
|
|
htlc_min = channel->channel_info.their_config.htlc_minimum;
|
|
|
|
if (amount_msat_greater(htlc_min, htlc_minimum_msat))
|
|
|
|
channel->htlc_minimum_msat = htlc_min;
|
|
|
|
else
|
|
|
|
channel->htlc_minimum_msat = htlc_minimum_msat;
|
2022-03-21 01:58:23 +01:00
|
|
|
htlc_max = htlc_max_possible_send(channel);
|
|
|
|
if (amount_msat_less(htlc_max, htlc_maximum_msat))
|
|
|
|
channel->htlc_maximum_msat = htlc_max;
|
|
|
|
else
|
|
|
|
channel->htlc_maximum_msat = htlc_maximum_msat;
|
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
list_add_tail(&peer->channels, &channel->list);
|
2020-09-08 06:57:53 +02:00
|
|
|
channel->rr_number = peer->ld->rr_counter++;
|
2018-02-12 11:10:46 +01:00
|
|
|
tal_add_destructor(channel, destroy_channel);
|
|
|
|
|
2021-02-04 22:14:44 +01:00
|
|
|
list_head_init(&channel->inflights);
|
|
|
|
|
2020-10-28 11:46:17 +01:00
|
|
|
channel->closer = closer;
|
2023-04-06 01:33:24 +02:00
|
|
|
channel->close_blockheight = NULL;
|
2020-10-28 11:46:17 +01:00
|
|
|
channel->state_change_cause = reason;
|
2023-07-21 09:15:22 +02:00
|
|
|
channel->ignore_fee_limits = ignore_fee_limits;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->last_stable_connection = last_stable_connection;
|
2024-01-31 04:16:19 +01:00
|
|
|
channel->stable_conn_timer = NULL;
|
2024-01-31 04:16:17 +01:00
|
|
|
/* Populate channel->channel_gossip */
|
|
|
|
channel_gossip_init(channel, take(peer_update));
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* Make sure we see any spends using this key */
|
2023-07-10 21:59:41 +02:00
|
|
|
if (!local_shutdown_scriptpubkey) {
|
|
|
|
if (anysegwit) {
|
|
|
|
txfilter_add_scriptpubkey(peer->ld->owned_txfilter,
|
|
|
|
take(p2tr_for_keyidx(NULL, peer->ld,
|
|
|
|
channel->final_key_idx)));
|
|
|
|
} else {
|
|
|
|
txfilter_add_scriptpubkey(peer->ld->owned_txfilter,
|
|
|
|
take(p2wpkh_for_keyidx(NULL, peer->ld,
|
|
|
|
channel->final_key_idx)));
|
|
|
|
}
|
|
|
|
}
|
2022-06-24 03:14:11 +02:00
|
|
|
/* scid is NULL when opening a new channel so we don't
|
|
|
|
* need to set error in that case as well */
|
2024-03-20 02:59:51 +01:00
|
|
|
if (scid && is_stub_scid(*scid))
|
2022-06-24 03:14:11 +02:00
|
|
|
channel->error = towire_errorfmt(peer->ld,
|
|
|
|
&channel->cid,
|
|
|
|
"We can't be together anymore.");
|
2018-03-07 01:06:07 +01:00
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *channel_state_name(const struct channel *channel)
|
|
|
|
{
|
2018-02-19 02:06:14 +01:00
|
|
|
return channel_state_str(channel->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *channel_state_str(enum channel_state state)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; enum_channel_state_names[i].name; i++)
|
|
|
|
if (enum_channel_state_names[i].v == state)
|
|
|
|
return enum_channel_state_names[i].name;
|
|
|
|
return "unknown";
|
2018-02-12 11:10:46 +01:00
|
|
|
}
|
|
|
|
|
2023-10-02 00:59:49 +02:00
|
|
|
struct channel *peer_any_channel(struct peer *peer,
|
2023-10-02 00:59:51 +02:00
|
|
|
bool (*channel_state_filter)(enum channel_state),
|
2023-10-02 00:59:49 +02:00
|
|
|
bool *others)
|
2021-01-20 02:07:04 +01:00
|
|
|
{
|
2022-03-22 23:59:20 +01:00
|
|
|
struct channel *channel, *ret = NULL;
|
2021-01-20 02:07:04 +01:00
|
|
|
|
|
|
|
list_for_each(&peer->channels, channel, list) {
|
2023-10-02 00:59:51 +02:00
|
|
|
if (channel_state_filter && !channel_state_filter(channel->state))
|
2022-03-22 23:59:20 +01:00
|
|
|
continue;
|
|
|
|
/* Already found one? */
|
|
|
|
if (ret) {
|
|
|
|
if (others)
|
|
|
|
*others = true;
|
|
|
|
} else {
|
|
|
|
if (others)
|
|
|
|
*others = false;
|
|
|
|
ret = channel;
|
|
|
|
}
|
2021-01-20 02:07:04 +01:00
|
|
|
}
|
2022-03-22 23:59:20 +01:00
|
|
|
return ret;
|
2021-01-20 02:07:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-07 20:03:06 +01:00
|
|
|
struct channel_inflight *channel_inflight_find(struct channel *channel,
|
|
|
|
const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
struct channel_inflight *inflight;
|
|
|
|
list_for_each(&channel->inflights, inflight, list) {
|
2021-10-13 05:45:36 +02:00
|
|
|
if (bitcoin_txid_eq(txid, &inflight->funding->outpoint.txid))
|
2021-01-07 20:03:06 +01:00
|
|
|
return inflight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:42:47 +01:00
|
|
|
struct channel *any_channel_by_scid(struct lightningd *ld,
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id scid,
|
2022-08-08 21:58:09 +02:00
|
|
|
bool privacy_leak_ok)
|
2020-02-27 05:26:36 +01:00
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *chan;
|
2023-01-18 06:04:32 +01:00
|
|
|
struct peer_node_id_map_iter it;
|
|
|
|
|
|
|
|
/* FIXME: Support lookup by scid directly! */
|
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
p;
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
2020-02-27 05:26:36 +01:00
|
|
|
list_for_each(&p->channels, chan, list) {
|
2023-04-07 08:32:47 +02:00
|
|
|
/* BOLT #2:
|
2022-08-08 21:58:09 +02:00
|
|
|
* - MUST always recognize the `alias` as a
|
|
|
|
* `short_channel_id` for incoming HTLCs to this
|
|
|
|
* channel.
|
|
|
|
*/
|
2022-04-28 17:57:55 +02:00
|
|
|
if (chan->alias[LOCAL] &&
|
2024-03-20 02:59:51 +01:00
|
|
|
short_channel_id_eq(scid, *chan->alias[LOCAL]))
|
2022-04-28 17:57:55 +02:00
|
|
|
return chan;
|
2023-04-07 08:32:47 +02:00
|
|
|
/* BOLT #2:
|
2022-08-08 21:58:09 +02:00
|
|
|
* - if `channel_type` has `option_scid_alias` set:
|
|
|
|
* - MUST NOT allow incoming HTLCs to this channel
|
|
|
|
* using the real `short_channel_id`
|
|
|
|
*/
|
|
|
|
if (!privacy_leak_ok
|
2023-04-10 02:23:56 +02:00
|
|
|
&& channel_type_has(chan->type, OPT_SCID_ALIAS))
|
2022-08-08 21:58:09 +02:00
|
|
|
continue;
|
|
|
|
if (chan->scid
|
2024-03-20 02:59:51 +01:00
|
|
|
&& short_channel_id_eq(scid, *chan->scid))
|
2022-08-08 21:58:09 +02:00
|
|
|
return chan;
|
2020-02-27 05:26:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:33:14 +02:00
|
|
|
struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid)
|
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *chan;
|
2023-01-18 06:04:32 +01:00
|
|
|
struct peer_node_id_map_iter it;
|
|
|
|
|
|
|
|
/* FIXME: Support lookup by id directly! */
|
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
p;
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
2018-04-05 18:33:14 +02:00
|
|
|
list_for_each(&p->channels, chan, list) {
|
|
|
|
if (chan->dbid == dbid)
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:28:46 +02:00
|
|
|
struct channel *channel_by_cid(struct lightningd *ld,
|
2021-01-20 01:55:59 +01:00
|
|
|
const struct channel_id *cid)
|
2020-09-17 22:28:46 +02:00
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *channel;
|
2023-01-18 06:04:32 +01:00
|
|
|
struct peer_node_id_map_iter it;
|
2020-09-17 22:28:46 +02:00
|
|
|
|
2023-01-18 06:04:32 +01:00
|
|
|
/* FIXME: Support lookup by cid directly! */
|
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
p;
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
2020-09-17 22:28:46 +02:00
|
|
|
if (p->uncommitted_channel) {
|
2021-01-20 01:55:59 +01:00
|
|
|
/* We can't use this method for old, uncommitted
|
|
|
|
* channels; there's no "channel" struct here! */
|
|
|
|
if (channel_id_eq(&p->uncommitted_channel->cid, cid))
|
2020-09-17 22:28:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_for_each(&p->channels, channel, list) {
|
|
|
|
if (channel_id_eq(&channel->cid, cid)) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-22 23:59:20 +01:00
|
|
|
struct channel *find_channel_by_id(const struct peer *peer,
|
|
|
|
const struct channel_id *cid)
|
|
|
|
{
|
|
|
|
struct channel *c;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, c, list) {
|
|
|
|
if (channel_id_eq(&c->cid, cid))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct channel *find_channel_by_scid(const struct peer *peer,
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id scid)
|
2022-03-22 23:59:20 +01:00
|
|
|
{
|
|
|
|
struct channel *c;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, c, list) {
|
2024-03-20 02:59:51 +01:00
|
|
|
if (c->scid && short_channel_id_eq(*c->scid, scid))
|
2022-04-28 17:55:15 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct channel *find_channel_by_alias(const struct peer *peer,
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id alias,
|
2022-04-28 17:55:15 +02:00
|
|
|
enum side side)
|
|
|
|
{
|
|
|
|
struct channel *c;
|
|
|
|
list_for_each(&peer->channels, c, list) {
|
2024-03-20 02:59:51 +01:00
|
|
|
if (c->alias[side] && short_channel_id_eq(*c->alias[side], alias))
|
2022-03-22 23:59:20 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-09-17 22:28:46 +02:00
|
|
|
|
2023-06-29 02:14:10 +02:00
|
|
|
bool have_anchor_channel(struct lightningd *ld)
|
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *channel;
|
|
|
|
struct peer_node_id_map_iter it;
|
|
|
|
|
|
|
|
for (p = peer_node_id_map_first(ld->peers, &it);
|
|
|
|
p;
|
|
|
|
p = peer_node_id_map_next(ld->peers, &it)) {
|
|
|
|
if (p->uncommitted_channel) {
|
|
|
|
/* FIXME: Assume anchors if supported */
|
|
|
|
if (feature_negotiated(ld->our_features,
|
|
|
|
p->their_features,
|
|
|
|
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
list_for_each(&p->channels, channel, list) {
|
|
|
|
if (channel_type_has(channel->type,
|
|
|
|
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_last_tx(struct channel *channel,
|
|
|
|
struct bitcoin_tx *tx,
|
2023-01-30 07:06:03 +01:00
|
|
|
const struct bitcoin_signature *sig)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
2019-07-30 22:04:55 +02:00
|
|
|
assert(tx->chainparams);
|
2018-02-19 02:06:12 +01:00
|
|
|
channel->last_sig = *sig;
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_free(channel->last_tx);
|
|
|
|
channel->last_tx = tal_steal(channel, tx);
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_state(struct channel *channel,
|
2018-02-19 02:06:14 +01:00
|
|
|
enum channel_state old_state,
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
enum channel_state state,
|
|
|
|
enum state_change reason,
|
|
|
|
char *why)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
2020-10-28 11:46:21 +01:00
|
|
|
struct timeabs timestamp;
|
2020-09-08 16:11:05 +02:00
|
|
|
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
/* set closer, if known */
|
2023-10-02 00:59:49 +02:00
|
|
|
if (channel_state_closing(state) && channel->closer == NUM_SIDES) {
|
2020-11-12 17:29:50 +01:00
|
|
|
if (reason == REASON_LOCAL) channel->closer = LOCAL;
|
|
|
|
if (reason == REASON_USER) channel->closer = LOCAL;
|
|
|
|
if (reason == REASON_REMOTE) channel->closer = REMOTE;
|
|
|
|
if (reason == REASON_ONCHAIN) channel->closer = REMOTE;
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use or update state_change_cause, if known */
|
|
|
|
if (reason != REASON_UNKNOWN)
|
|
|
|
channel->state_change_cause = reason;
|
|
|
|
else
|
|
|
|
reason = channel->state_change_cause;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_info(channel->log, "State changed from %s to %s",
|
2018-02-19 02:06:14 +01:00
|
|
|
channel_state_name(channel), channel_state_str(state));
|
2018-02-12 11:13:04 +01:00
|
|
|
if (channel->state != old_state)
|
|
|
|
fatal("channel state %s should be %s",
|
2018-02-19 02:06:14 +01:00
|
|
|
channel_state_name(channel), channel_state_str(old_state));
|
2018-02-12 11:13:04 +01:00
|
|
|
|
|
|
|
channel->state = state;
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
/* TODO(cdecker) Selectively save updated fields to DB */
|
|
|
|
wallet_channel_save(channel->peer->ld->wallet, channel);
|
2020-09-08 16:11:05 +02:00
|
|
|
|
2020-10-28 11:46:22 +01:00
|
|
|
/* plugin notification channel_state_changed and DB entry */
|
2020-09-09 13:28:45 +02:00
|
|
|
if (state != old_state) { /* see issue #4029 */
|
2020-10-28 11:46:21 +01:00
|
|
|
timestamp = time_now();
|
2020-10-28 11:46:22 +01:00
|
|
|
wallet_state_change_add(channel->peer->ld->wallet,
|
|
|
|
channel->dbid,
|
2023-10-02 00:59:50 +02:00
|
|
|
timestamp,
|
2020-10-28 11:46:22 +01:00
|
|
|
old_state,
|
|
|
|
state,
|
|
|
|
reason,
|
|
|
|
why);
|
2020-09-09 13:28:45 +02:00
|
|
|
notify_channel_state_changed(channel->peer->ld,
|
|
|
|
&channel->peer->id,
|
2020-12-15 01:43:05 +01:00
|
|
|
&channel->cid,
|
2020-09-09 13:28:45 +02:00
|
|
|
channel->scid,
|
2023-10-02 00:59:50 +02:00
|
|
|
timestamp,
|
2020-09-09 13:28:45 +02:00
|
|
|
old_state,
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
state,
|
|
|
|
reason,
|
|
|
|
why);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *channel_change_state_reason_str(enum state_change reason)
|
|
|
|
{
|
|
|
|
switch (reason) {
|
|
|
|
case REASON_UNKNOWN: return "unknown";
|
|
|
|
case REASON_LOCAL: return "local";
|
|
|
|
case REASON_USER: return "user";
|
|
|
|
case REASON_REMOTE: return "remote";
|
|
|
|
case REASON_PROTOCOL: return "protocol";
|
|
|
|
case REASON_ONCHAIN: return "onchain";
|
2020-09-09 13:28:45 +02:00
|
|
|
}
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
abort();
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
void channel_fail_permanent(struct channel *channel,
|
|
|
|
enum state_change reason,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
2022-06-24 03:14:11 +02:00
|
|
|
/* Don't do anything if it's an stub channel because
|
|
|
|
* peer has already closed it unilatelrally. */
|
2024-03-20 02:59:51 +01:00
|
|
|
if (channel->scid && is_stub_scid(*channel->scid))
|
2022-06-24 03:14:11 +02:00
|
|
|
return;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
va_list ap;
|
|
|
|
char *why;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-08-26 20:31:25 +02:00
|
|
|
why = tal_vfmt(tmpctx, fmt, ap);
|
2018-02-12 11:13:04 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_unusual(channel->log, "Peer permanent failure in %s: %s",
|
|
|
|
channel_state_name(channel), why);
|
|
|
|
|
|
|
|
/* We can have multiple errors, eg. onchaind failures. */
|
2020-09-09 09:20:53 +02:00
|
|
|
if (!channel->error)
|
|
|
|
channel->error = towire_errorfmt(channel,
|
|
|
|
&channel->cid, "%s", why);
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
2018-04-10 08:03:15 +02:00
|
|
|
/* Drop non-cooperatively (unilateral) to chain. */
|
|
|
|
drop_to_chain(ld, channel, false);
|
2018-08-23 03:08:48 +02:00
|
|
|
|
2023-10-02 00:59:51 +02:00
|
|
|
if (channel_state_wants_onchain_fail(channel->state))
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
channel_set_state(channel,
|
|
|
|
channel->state,
|
|
|
|
AWAITING_UNILATERAL,
|
|
|
|
reason,
|
|
|
|
why);
|
2018-08-23 03:08:48 +02:00
|
|
|
|
2023-10-26 18:38:58 +02:00
|
|
|
if (channel_state_open_uncommitted(channel->state))
|
|
|
|
delete_channel(channel);
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
tal_free(why);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 20:31:25 +02:00
|
|
|
void channel_fail_forget(struct channel *channel, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *why;
|
|
|
|
|
2019-09-09 18:11:24 +02:00
|
|
|
assert(channel->opener == REMOTE &&
|
2019-08-26 20:31:25 +02:00
|
|
|
channel->state == CHANNELD_AWAITING_LOCKIN);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
why = tal_vfmt(tmpctx, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
log_unusual(channel->log, "Peer permanent failure in %s: %s, "
|
|
|
|
"forget channel",
|
|
|
|
channel_state_name(channel), why);
|
|
|
|
|
2020-09-09 09:20:53 +02:00
|
|
|
if (!channel->error)
|
|
|
|
channel->error = towire_errorfmt(channel,
|
|
|
|
&channel->cid, "%s", why);
|
2019-08-26 20:31:25 +02:00
|
|
|
|
|
|
|
delete_channel(channel);
|
|
|
|
tal_free(why);
|
|
|
|
}
|
|
|
|
|
2021-02-19 19:44:36 +01:00
|
|
|
struct channel_inflight *
|
|
|
|
channel_current_inflight(const struct channel *channel)
|
|
|
|
{
|
|
|
|
/* The last inflight should always be the one in progress */
|
2021-05-20 23:47:22 +02:00
|
|
|
return list_tail(&channel->inflights,
|
|
|
|
struct channel_inflight, list);
|
2021-02-19 19:44:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 channel_last_funding_feerate(const struct channel *channel)
|
|
|
|
{
|
|
|
|
struct channel_inflight *inflight;
|
|
|
|
inflight = channel_current_inflight(channel);
|
|
|
|
if (!inflight)
|
|
|
|
return 0;
|
|
|
|
return inflight->funding->feerate;
|
|
|
|
}
|
|
|
|
|
2021-02-16 20:23:53 +01:00
|
|
|
void channel_cleanup_commands(struct channel *channel, const char *why)
|
|
|
|
{
|
|
|
|
if (channel->open_attempt) {
|
|
|
|
struct open_attempt *oa = channel->open_attempt;
|
2021-03-09 22:14:08 +01:00
|
|
|
if (oa->cmd) {
|
|
|
|
/* If we requested this be aborted, it's a success */
|
|
|
|
if (oa->aborted) {
|
|
|
|
struct json_stream *response;
|
|
|
|
response = json_stream_success(oa->cmd);
|
|
|
|
json_add_channel_id(response,
|
|
|
|
"channel_id",
|
|
|
|
&channel->cid);
|
|
|
|
json_add_bool(response, "channel_canceled",
|
|
|
|
list_empty(&channel->inflights));
|
|
|
|
json_add_string(response, "reason", why);
|
|
|
|
was_pending(command_success(oa->cmd, response));
|
|
|
|
} else
|
|
|
|
was_pending(command_fail(oa->cmd, LIGHTNINGD,
|
|
|
|
"%s", why));
|
|
|
|
}
|
2021-02-16 20:23:53 +01:00
|
|
|
notify_channel_open_failed(channel->peer->ld, &channel->cid);
|
|
|
|
channel->open_attempt = tal_free(channel->open_attempt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel->openchannel_signed_cmd) {
|
|
|
|
was_pending(command_fail(channel->openchannel_signed_cmd,
|
|
|
|
LIGHTNINGD, "%s", why));
|
|
|
|
channel->openchannel_signed_cmd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_internal_error(struct channel *channel, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2018-02-20 10:51:44 +01:00
|
|
|
char *why;
|
2018-02-12 11:13:04 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-02-20 10:51:44 +01:00
|
|
|
why = tal_vfmt(channel, fmt, ap);
|
2018-02-12 11:13:04 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2021-02-16 20:23:53 +01:00
|
|
|
log_broken(channel->log, "Internal error %s: %s",
|
2018-02-20 10:51:44 +01:00
|
|
|
channel_state_name(channel), why);
|
2018-02-21 04:35:15 +01:00
|
|
|
|
2021-02-16 20:23:53 +01:00
|
|
|
channel_cleanup_commands(channel, why);
|
|
|
|
|
2023-10-02 00:59:50 +02:00
|
|
|
/* Nothing ventured, nothing lost! */
|
2023-10-02 00:59:51 +02:00
|
|
|
if (channel_state_uncommitted(channel->state)) {
|
2021-12-28 00:21:09 +01:00
|
|
|
channel_set_owner(channel, NULL);
|
2021-02-16 20:23:53 +01:00
|
|
|
delete_channel(channel);
|
|
|
|
tal_free(why);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-21 04:35:15 +01:00
|
|
|
/* Don't expose internal error causes to remove unless doing dev */
|
2023-09-21 07:36:28 +02:00
|
|
|
if (channel->peer->ld->developer)
|
|
|
|
channel_fail_permanent(channel,
|
|
|
|
REASON_LOCAL, "Internal error: %s", why);
|
|
|
|
else
|
|
|
|
channel_fail_permanent(channel, REASON_LOCAL, "Internal error");
|
2018-02-21 04:35:15 +01:00
|
|
|
tal_free(why);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 06:53:44 +01:00
|
|
|
void channel_set_billboard(struct channel *channel, bool perm, const char *str)
|
|
|
|
{
|
|
|
|
const char **p;
|
|
|
|
|
|
|
|
if (perm)
|
|
|
|
p = &channel->billboard.permanent[channel->state];
|
|
|
|
else
|
|
|
|
p = &channel->billboard.transient;
|
2018-02-23 06:53:47 +01:00
|
|
|
*p = tal_free(*p);
|
2018-02-23 06:53:44 +01:00
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
if (str) {
|
|
|
|
*p = tal_fmt(channel, "%s:%s", channel_state_name(channel), str);
|
|
|
|
if (taken(str))
|
|
|
|
tal_free(str);
|
|
|
|
}
|
2018-02-23 06:53:44 +01:00
|
|
|
}
|
|
|
|
|
2023-10-02 00:59:48 +02:00
|
|
|
static void channel_err(struct channel *channel, bool disconnect, const char *why)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
2023-04-03 06:49:05 +02:00
|
|
|
/* Nothing to do if channel isn't actually owned! */
|
|
|
|
if (!channel->owner)
|
|
|
|
return;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_info(channel->log, "Peer transient failure in %s: %s",
|
|
|
|
channel_state_name(channel), why);
|
|
|
|
|
|
|
|
if (dev_disconnect_permanent(channel->peer->ld)) {
|
feat: adds state change cause and message
This adds a `state_change` 'cause' to a channel.
A 'cause' is some initial 'reason' a channel was created or closed by:
/* Anything other than the reasons below. Should not happen. */
REASON_UNKNOWN,
/* Unconscious internal reasons, e.g. dev fail of a channel. */
REASON_LOCAL,
/* The operator or a plugin opened or closed a channel by intention. */
REASON_USER,
/* The remote closed or funded a channel with us by intention. */
REASON_REMOTE,
/* E.g. We need to close a channel because of bad signatures and such. */
REASON_PROTOCOL,
/* A channel was closed onchain, while we were offline. */
/* Note: This is very likely a conscious remote decision. */
REASON_ONCHAIN
If a 'cause' is known and a subsequent state change is made with
`REASON_UNKNOWN` the preceding cause will be used as reason, since a lot
(all `REASON_UNKNOWN`) state changes are a subsequent consequences of a prior
cause: local, user, remote, protocol or onchain.
Changelog-Added: Plugins: Channel closure resaon/cause to channel_state_changed notification
2020-10-28 11:46:12 +01:00
|
|
|
channel_fail_permanent(channel,
|
|
|
|
REASON_LOCAL,
|
|
|
|
"dev_disconnect permfail");
|
2018-02-12 11:13:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-21 07:36:28 +02:00
|
|
|
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
2023-10-02 00:59:48 +02:00
|
|
|
|
|
|
|
/* Force a disconnect in case the issue is with TCP */
|
2023-10-22 06:07:31 +02:00
|
|
|
if (disconnect) {
|
|
|
|
force_peer_disconnect(channel->peer->ld, channel->peer,
|
|
|
|
"One channel had an error");
|
2023-10-02 00:59:48 +02:00
|
|
|
}
|
2019-07-26 04:11:18 +02:00
|
|
|
}
|
|
|
|
|
2023-10-02 00:59:48 +02:00
|
|
|
void channel_fail_transient(struct channel *channel, bool disconnect, const char *fmt, ...)
|
2019-07-26 04:11:18 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2023-10-02 00:59:48 +02:00
|
|
|
channel_err(channel, disconnect, tal_vfmt(tmpctx, fmt, ap));
|
2019-07-26 04:11:18 +02:00
|
|
|
va_end(ap);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
2022-05-06 15:38:34 +02:00
|
|
|
|
2022-07-16 06:49:29 +02:00
|
|
|
bool channel_is_connected(const struct channel *channel)
|
|
|
|
{
|
|
|
|
return channel->owner && channel->owner->talks_to_peer;
|
|
|
|
}
|
|
|
|
|
2024-03-20 02:59:51 +01:00
|
|
|
struct short_channel_id
|
2022-05-06 15:38:34 +02:00
|
|
|
channel_scid_or_local_alias(const struct channel *chan)
|
|
|
|
{
|
|
|
|
assert(chan->scid != NULL || chan->alias[LOCAL] != NULL);
|
|
|
|
if (chan->scid != NULL)
|
2024-03-20 02:59:51 +01:00
|
|
|
return *chan->scid;
|
2022-05-06 15:38:34 +02:00
|
|
|
else
|
2024-03-20 02:59:51 +01:00
|
|
|
return *chan->alias[LOCAL];
|
2022-05-06 15:38:34 +02:00
|
|
|
}
|
2024-01-31 04:16:17 +01:00
|
|
|
|
|
|
|
const u8 *channel_update_for_error(const tal_t *ctx,
|
|
|
|
struct channel *channel)
|
|
|
|
{
|
|
|
|
/* FIXME: Call directly from callers */
|
|
|
|
return channel_gossip_update_for_error(ctx, channel);
|
|
|
|
}
|
|
|
|
|