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>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/gen_connect_wire.h>
|
2018-07-23 04:23:03 +02:00
|
|
|
#include <errno.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2018-02-12 11:10:46 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/channel.h>
|
2018-08-09 02:25:29 +02:00
|
|
|
#include <lightningd/connect_control.h>
|
2018-02-19 02:06:14 +01:00
|
|
|
#include <lightningd/gen_channel_state_names.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>
|
2018-08-09 02:25:29 +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;
|
|
|
|
msg = towire_connectctl_peer_disconnected(
|
|
|
|
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;
|
2019-06-22 15:08:21 +02:00
|
|
|
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);
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = towire_hsm_get_channel_basepoints(NULL, peer_id, dbid);
|
|
|
|
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);
|
|
|
|
if (!fromwire_hsm_get_channel_basepoints_reply(msg, local_basepoints,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
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 */
|
|
|
|
secp256k1_ecdsa_signature *last_htlc_sigs,
|
|
|
|
const struct channel_info *channel_info,
|
|
|
|
/* 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,
|
|
|
|
bool option_static_remotekey)
|
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;
|
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);
|
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;
|
2019-12-12 18:18:25 +01:00
|
|
|
channel->channel_info.fee_states
|
|
|
|
= dup_fee_states(channel, channel_info->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;
|
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);
|
|
|
|
tal_add_destructor(channel, destroy_channel);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
struct channel *chan;
|
|
|
|
list_for_each(&ld->peers, p, list) {
|
|
|
|
list_for_each(&p->channels, chan, list) {
|
|
|
|
if (channel_active(chan)
|
|
|
|
&& chan->scid
|
|
|
|
&& 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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
enum channel_state state)
|
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);
|
2018-02-12 11:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void channel_fail_permanent(struct channel *channel, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
va_list ap;
|
|
|
|
char *why;
|
2018-03-16 15:19:11 +01:00
|
|
|
struct channel_id cid;
|
2018-02-12 11:13:04 +01:00
|
|
|
|
|
|
|
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. */
|
|
|
|
if (!channel->error) {
|
2018-03-16 15:19:11 +01:00
|
|
|
derive_channel_id(&cid,
|
|
|
|
&channel->funding_txid,
|
|
|
|
channel->funding_outnum);
|
|
|
|
channel->error = towire_errorfmt(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))
|
|
|
|
channel_set_state(channel, channel->state, AWAITING_UNILATERAL);
|
|
|
|
|
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;
|
|
|
|
struct channel_id cid;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!channel->error) {
|
|
|
|
derive_channel_id(&cid,
|
|
|
|
&channel->funding_txid,
|
|
|
|
channel->funding_outnum);
|
|
|
|
channel->error = towire_errorfmt(channel, &cid, "%s", why);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-02-20 10:51:44 +01:00
|
|
|
channel_fail_permanent(channel, "Internal error: %s", why);
|
2018-02-21 04:35:15 +01:00
|
|
|
#else
|
|
|
|
channel_fail_permanent(channel, "Internal error");
|
|
|
|
#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)) {
|
2019-06-30 02:42:44 +02:00
|
|
|
channel_fail_permanent(channel, "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
|
|
|
}
|