2021-02-04 22:14:44 +01:00
|
|
|
#include <bitcoin/psbt.h>
|
2018-03-07 01:06:07 +01:00
|
|
|
#include <bitcoin/script.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
|
|
|
#include <ccan/tal/str/str.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>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
2019-10-15 12:58:30 +02:00
|
|
|
#include <common/utils.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 <inttypes.h>
|
|
|
|
#include <lightningd/channel.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>
|
2018-07-23 04:23:03 +02:00
|
|
|
#include <lightningd/hsm_control.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <lightningd/jsonrpc.h>
|
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/log.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>
|
2020-10-22 02:57:07 +02:00
|
|
|
#include <lightningd/opening_control.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/subd.h>
|
2018-07-23 04:23:03 +02:00
|
|
|
#include <wire/wire_sync.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
static bool connects_to_peer(struct subd *owner)
|
|
|
|
{
|
|
|
|
return owner && owner->talks_to_peer;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
if (old_owner) {
|
2018-02-12 11:13:04 +01:00
|
|
|
subd_release_channel(old_owner, channel);
|
2018-04-26 06:51:01 +02:00
|
|
|
if (channel->connected && !connects_to_peer(owner)) {
|
2019-05-30 16:59:23 +02:00
|
|
|
/* If shutting down, connectd no longer exists,
|
|
|
|
* and we should not transfer peer to connectd.
|
|
|
|
* Only transfer to connectd if connectd is
|
|
|
|
* there to be transferred to.
|
|
|
|
*/
|
|
|
|
if (channel->peer->ld->connectd) {
|
|
|
|
u8 *msg;
|
2020-08-25 04:16:22 +02:00
|
|
|
msg = towire_connectd_peer_disconnected(
|
2019-05-30 16:59:23 +02:00
|
|
|
NULL,
|
|
|
|
&channel->peer->id);
|
|
|
|
subd_send_msg(channel->peer->ld->connectd,
|
|
|
|
take(msg));
|
|
|
|
}
|
2018-08-09 02:25:29 +02:00
|
|
|
}
|
2018-04-26 06:51:01 +02:00
|
|
|
}
|
|
|
|
channel->connected = connects_to_peer(owner);
|
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;
|
|
|
|
|
|
|
|
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
|
|
|
|
hout;
|
|
|
|
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;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
|
|
|
|
hin;
|
|
|
|
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
|
|
|
{
|
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);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-07-23 04:23:03 +02:00
|
|
|
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);
|
2018-07-23 04:23:03 +02:00
|
|
|
if (!wire_sync_write(ld->hsm_fd, take(msg)))
|
|
|
|
fatal("Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, ld->hsm_fd);
|
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,
|
|
|
|
const struct bitcoin_txid funding_txid,
|
|
|
|
u16 funding_outnum,
|
|
|
|
u32 funding_feerate,
|
|
|
|
struct amount_sat total_funds,
|
|
|
|
struct amount_sat our_funds,
|
|
|
|
struct wally_psbt *psbt STEALS,
|
|
|
|
struct bitcoin_tx *last_tx STEALS,
|
|
|
|
const struct bitcoin_signature last_sig)
|
|
|
|
{
|
|
|
|
struct channel_inflight *inflight
|
|
|
|
= tal(channel, struct channel_inflight);
|
|
|
|
struct funding_info *funding
|
|
|
|
= tal(inflight, struct funding_info);
|
|
|
|
|
|
|
|
funding->txid = funding_txid;
|
|
|
|
funding->total_funds = total_funds;
|
|
|
|
funding->outnum = funding_outnum;
|
|
|
|
funding->feerate = funding_feerate;
|
|
|
|
funding->our_funds = our_funds;
|
|
|
|
|
|
|
|
inflight->funding = funding;
|
|
|
|
inflight->channel = channel,
|
|
|
|
inflight->remote_tx_sigs = false,
|
|
|
|
inflight->funding_psbt = tal_steal(inflight, psbt);
|
|
|
|
inflight->last_tx = tal_steal(inflight, last_tx);
|
|
|
|
inflight->last_sig = last_sig;
|
|
|
|
|
|
|
|
list_add_tail(&channel->inflights, &inflight->list);
|
|
|
|
tal_add_destructor(inflight, destroy_inflight);
|
|
|
|
|
|
|
|
return inflight;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return oa;
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
struct log *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,
|
|
|
|
const struct channel_config *our_config,
|
|
|
|
u32 minimum_depth,
|
|
|
|
u64 next_index_local,
|
|
|
|
u64 next_index_remote,
|
|
|
|
u64 next_htlc_id,
|
|
|
|
const struct bitcoin_txid *funding_txid,
|
|
|
|
u16 funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding,
|
|
|
|
struct amount_msat push,
|
2019-09-30 23:47:16 +02:00
|
|
|
struct amount_sat our_funds,
|
2018-02-19 02:06:14 +01:00
|
|
|
bool remote_funding_locked,
|
|
|
|
/* NULL or stolen */
|
|
|
|
struct short_channel_id *scid,
|
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
|
|
|
bool connected,
|
|
|
|
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,
|
2020-08-13 19:41:02 +02:00
|
|
|
bool option_static_remotekey,
|
2020-09-11 22:28:15 +02:00
|
|
|
bool option_anchor_outputs,
|
2020-10-28 11:46:17 +01:00
|
|
|
enum side closer,
|
|
|
|
enum state_change reason)
|
2018-02-12 11:10:46 +01:00
|
|
|
{
|
2018-02-19 02:06:14 +01:00
|
|
|
struct channel *channel = tal(peer->ld, struct channel);
|
2018-02-12 11:10:46 +01:00
|
|
|
|
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;
|
|
|
|
channel->error = NULL;
|
2018-08-09 04:53:04 +02:00
|
|
|
channel->htlc_timeout = 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);
|
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
if (!log) {
|
|
|
|
channel->log = new_log(channel,
|
2019-11-18 01:27:15 +01:00
|
|
|
peer->ld->log_book,
|
2019-11-17 12:41:33 +01:00
|
|
|
&channel->peer->id,
|
2019-11-18 01:27:18 +01:00
|
|
|
"chan#%"PRIu64,
|
2019-11-17 12:41:33 +01:00
|
|
|
dbid);
|
2018-02-19 02:06:14 +01:00
|
|
|
} else
|
|
|
|
channel->log = tal_steal(channel, log);
|
|
|
|
channel->channel_flags = channel_flags;
|
|
|
|
channel->our_config = *our_config;
|
|
|
|
channel->minimum_depth = minimum_depth;
|
|
|
|
channel->next_index[LOCAL] = next_index_local;
|
|
|
|
channel->next_index[REMOTE] = next_index_remote;
|
|
|
|
channel->next_htlc_id = next_htlc_id;
|
|
|
|
channel->funding_txid = *funding_txid;
|
|
|
|
channel->funding_outnum = funding_outnum;
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->funding = funding;
|
|
|
|
channel->push = push;
|
2019-09-30 23:47:16 +02:00
|
|
|
channel->our_funds = our_funds;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->remote_funding_locked = remote_funding_locked;
|
|
|
|
channel->scid = tal_steal(channel, scid);
|
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;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->last_tx = tal_steal(channel, last_tx);
|
2019-10-15 12:58:30 +02:00
|
|
|
channel->last_tx->chainparams = chainparams;
|
2019-05-28 18:06:39 +02:00
|
|
|
channel->last_tx_type = TX_UNKNOWN;
|
2018-02-19 02:06:14 +01:00
|
|
|
channel->last_sig = *last_sig;
|
|
|
|
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;
|
2019-09-29 09:35:45 +02:00
|
|
|
if (local_shutdown_scriptpubkey)
|
|
|
|
channel->shutdown_scriptpubkey[LOCAL]
|
|
|
|
= tal_steal(channel, local_shutdown_scriptpubkey);
|
|
|
|
else
|
|
|
|
channel->shutdown_scriptpubkey[LOCAL]
|
|
|
|
= p2wpkh_for_keyidx(channel, channel->peer->ld,
|
|
|
|
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-04-26 06:51:01 +02:00
|
|
|
channel->connected = connected;
|
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;
|
2019-05-01 00:57:09 +02:00
|
|
|
channel->remote_upfront_shutdown_script
|
|
|
|
= tal_steal(channel, remote_upfront_shutdown_script);
|
2019-09-10 04:22:27 +02:00
|
|
|
channel->option_static_remotekey = option_static_remotekey;
|
2020-08-13 19:41:02 +02:00
|
|
|
channel->option_anchor_outputs = option_anchor_outputs;
|
2019-08-24 23:06:08 +02:00
|
|
|
channel->forgets = tal_arr(channel, struct command *, 0);
|
2018-02-19 02:06:14 +01:00
|
|
|
|
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;
|
|
|
|
channel->state_change_cause = reason;
|
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 */
|
|
|
|
txfilter_add_scriptpubkey(peer->ld->owned_txfilter,
|
|
|
|
take(p2wpkh_for_keyidx(NULL, peer->ld,
|
|
|
|
channel->final_key_idx)));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-20 02:07:04 +01:00
|
|
|
struct channel *peer_unsaved_channel(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct channel *channel;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, channel, list) {
|
|
|
|
if (channel_unsaved(channel))
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:10:46 +01:00
|
|
|
struct channel *peer_active_channel(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct channel *channel;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, channel, list) {
|
|
|
|
if (channel_active(channel))
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (bitcoin_txid_eq(txid, &inflight->funding->txid))
|
|
|
|
return inflight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
json-invoice: add routeboost, warnings.
We split json_invoice(), as it now needs to round-trip to the gossipd,
and uniqueness checks need to happen *after* gossipd replies to avoid
a race.
For every candidate channel gossipd gives us, we check that it's in
state NORMAL (not shutting down, not still waiting for lockin), that
it's connected, and that it has capacity. We then choose one with
probability weighted by excess capacity, so larger channels are more
likely.
As a side effect of this, we can tell if an invoice is unpayble (no
channels have sufficient incoming capacity) or difficuly (no *online*
channels have sufficient capacity), so we add those warnings.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-09-27 07:29:17 +02:00
|
|
|
struct channel *peer_normal_channel(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct channel *channel;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, channel, list) {
|
|
|
|
if (channel->state == CHANNELD_NORMAL)
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *active_channel_by_id(struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2018-02-19 02:06:02 +01:00
|
|
|
struct uncommitted_channel **uc)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
|
|
|
struct peer *peer = peer_by_id(ld, id);
|
2018-02-19 02:06:02 +01:00
|
|
|
if (!peer) {
|
|
|
|
if (uc)
|
|
|
|
*uc = NULL;
|
2018-02-12 11:13:04 +01:00
|
|
|
return NULL;
|
2018-02-19 02:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uc)
|
|
|
|
*uc = peer->uncommitted_channel;
|
2018-02-12 11:13:04 +01:00
|
|
|
return peer_active_channel(peer);
|
|
|
|
}
|
|
|
|
|
2020-02-27 05:26:36 +01:00
|
|
|
struct channel *active_channel_by_scid(struct lightningd *ld,
|
|
|
|
const struct short_channel_id *scid)
|
2021-01-07 19:42:47 +01:00
|
|
|
{
|
|
|
|
struct channel *chan = any_channel_by_scid(ld, scid);
|
|
|
|
if (chan && !channel_active(chan))
|
|
|
|
chan = NULL;
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct channel *any_channel_by_scid(struct lightningd *ld,
|
|
|
|
const struct short_channel_id *scid)
|
2020-02-27 05:26:36 +01:00
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *chan;
|
|
|
|
list_for_each(&ld->peers, p, list) {
|
|
|
|
list_for_each(&p->channels, chan, list) {
|
2021-01-07 19:42:47 +01:00
|
|
|
if (chan->scid
|
2020-02-27 05:26:36 +01:00
|
|
|
&& short_channel_id_eq(scid, chan->scid))
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
list_for_each(&ld->peers, p, list) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
list_for_each(&ld->peers, p, list) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_set_last_tx(struct channel *channel,
|
|
|
|
struct bitcoin_tx *tx,
|
2019-05-28 18:06:39 +02:00
|
|
|
const struct bitcoin_signature *sig,
|
2019-06-07 11:38:20 +02:00
|
|
|
enum wallet_tx_type txtypes)
|
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);
|
2019-05-28 18:06:39 +02:00
|
|
|
channel->last_tx_type = txtypes;
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
if (state > CHANNELD_NORMAL && 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,
|
|
|
|
×tamp,
|
|
|
|
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,
|
2020-10-28 11:46:21 +01:00
|
|
|
×tamp,
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if (channel_active(channel))
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2018-02-20 10:51:44 +01:00
|
|
|
log_broken(channel->log, "Peer internal error %s: %s",
|
|
|
|
channel_state_name(channel), why);
|
2018-02-21 04:35:15 +01:00
|
|
|
|
|
|
|
/* Don't expose internal error causes to remove unless doing dev */
|
|
|
|
#if DEVELOPER
|
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, "Internal error: %s", why);
|
2018-02-21 04:35:15 +01:00
|
|
|
#else
|
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, "Internal error");
|
2018-02-21 04:35:15 +01:00
|
|
|
#endif
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-26 04:11:18 +02:00
|
|
|
static void err_and_reconnect(struct channel *channel,
|
|
|
|
const char *why,
|
|
|
|
u32 seconds_before_reconnect)
|
2018-02-12 11:13:04 +01:00
|
|
|
{
|
|
|
|
log_info(channel->log, "Peer transient failure in %s: %s",
|
|
|
|
channel_state_name(channel), why);
|
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-25 04:47:34 +02:00
|
|
|
channel_set_owner(channel, NULL);
|
|
|
|
|
2019-07-26 04:11:18 +02:00
|
|
|
delay_then_reconnect(channel, seconds_before_reconnect,
|
|
|
|
&channel->peer->addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_fail_reconnect_later(struct channel *channel, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
err_and_reconnect(channel, tal_vfmt(tmpctx, fmt, ap), 60);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_fail_reconnect(struct channel *channel, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
err_and_reconnect(channel, tal_vfmt(tmpctx, fmt, ap), 1);
|
|
|
|
va_end(ap);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|