2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-09-10 05:27:51 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2020-08-25 03:33:16 +02:00
|
|
|
#include <channeld/channeld_wiregen.h>
|
2019-08-23 23:34:52 +02:00
|
|
|
#include <common/json_command.h>
|
2020-05-15 12:30:25 +02:00
|
|
|
#include <common/json_helpers.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/json_tok.h>
|
2018-05-06 15:32:01 +02:00
|
|
|
#include <common/memleak.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/param.h>
|
2021-02-19 05:22:01 +01:00
|
|
|
#include <common/shutdown_scriptpubkey.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/type_to_string.h>
|
2018-08-17 07:06:35 +02:00
|
|
|
#include <common/wire_error.h>
|
2022-03-31 11:10:50 +02:00
|
|
|
#include <connectd/connectd_wiregen.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <errno.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <hsmd/capabilities.h>
|
2021-09-17 00:31:38 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <lightningd/channel.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/channel_control.h>
|
|
|
|
#include <lightningd/closing_control.h>
|
2020-04-02 03:58:07 +02:00
|
|
|
#include <lightningd/coin_mvts.h>
|
2021-03-12 01:19:40 +01:00
|
|
|
#include <lightningd/dual_open_control.h>
|
2022-01-24 21:03:52 +01:00
|
|
|
#include <lightningd/gossip_control.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/hsm_control.h>
|
2020-10-09 00:21:20 +02:00
|
|
|
#include <lightningd/notification.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/peer_control.h>
|
2022-01-11 02:13:59 +01:00
|
|
|
#include <lightningd/peer_fd.h>
|
2021-12-23 21:16:35 +01:00
|
|
|
#include <wally_bip32.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-08-23 01:27:22 +02:00
|
|
|
static void update_feerates(struct lightningd *ld, struct channel *channel)
|
|
|
|
{
|
2018-08-23 01:27:25 +02:00
|
|
|
u8 *msg;
|
2018-08-24 04:22:48 +02:00
|
|
|
u32 feerate = unilateral_feerate(ld->topology);
|
2018-08-23 01:27:25 +02:00
|
|
|
|
|
|
|
/* Nothing to do if we don't know feerate. */
|
|
|
|
if (!feerate)
|
|
|
|
return;
|
|
|
|
|
2021-02-18 05:15:54 +01:00
|
|
|
log_debug(ld->log,
|
|
|
|
"update_feerates: feerate = %u, min=%u, max=%u, penalty=%u",
|
|
|
|
feerate,
|
|
|
|
feerate_min(ld, NULL),
|
|
|
|
feerate_max(ld, NULL),
|
|
|
|
try_get_feerate(ld->topology, FEERATE_PENALTY));
|
|
|
|
|
2020-08-25 03:33:16 +02:00
|
|
|
msg = towire_channeld_feerates(NULL, feerate,
|
|
|
|
feerate_min(ld, NULL),
|
|
|
|
feerate_max(ld, NULL),
|
|
|
|
try_get_feerate(ld->topology, FEERATE_PENALTY));
|
2018-08-23 01:27:22 +02:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void try_update_feerates(struct lightningd *ld, struct channel *channel)
|
|
|
|
{
|
|
|
|
/* No point until funding locked in */
|
|
|
|
if (!channel_fees_can_change(channel))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Can't if no daemon listening. */
|
|
|
|
if (!channel->owner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
update_feerates(ld, channel);
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:25:59 +02:00
|
|
|
static void try_update_blockheight(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
|
|
|
u32 blockheight)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
2021-07-02 22:25:40 +02:00
|
|
|
log_debug(channel->log, "attempting update blockheight %s",
|
|
|
|
type_to_string(tmpctx, struct channel_id, &channel->cid));
|
|
|
|
|
|
|
|
/* If they're offline, check that we're not too far behind anyway */
|
|
|
|
if (!channel->owner) {
|
|
|
|
if (channel->opener == REMOTE
|
|
|
|
&& channel->lease_expiry > 0) {
|
|
|
|
u32 peer_height
|
|
|
|
= get_blockheight(channel->blockheight_states,
|
|
|
|
channel->opener, REMOTE);
|
|
|
|
|
|
|
|
/* Lease no longer active, we don't (really) care */
|
|
|
|
if (peer_height >= channel->lease_expiry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(peer_height + 1008 > peer_height);
|
|
|
|
if (peer_height + 1008 < blockheight)
|
|
|
|
channel_fail_permanent(channel,
|
|
|
|
REASON_PROTOCOL,
|
|
|
|
"Offline peer is too"
|
|
|
|
" far behind,"
|
|
|
|
" terminating leased"
|
|
|
|
" channel. Our current"
|
|
|
|
" %u, theirs %u",
|
|
|
|
blockheight,
|
|
|
|
peer_height);
|
|
|
|
}
|
2021-06-22 20:25:59 +02:00
|
|
|
return;
|
2021-07-02 22:25:40 +02:00
|
|
|
}
|
2021-06-22 20:25:59 +02:00
|
|
|
|
2021-07-02 22:25:40 +02:00
|
|
|
/* If we're not opened/locked in yet, don't send update */
|
|
|
|
if (!channel_fees_can_change(channel))
|
2021-06-22 20:25:59 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* We don't update the blockheight for non-leased chans */
|
|
|
|
if (channel->lease_expiry == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_debug(ld->log, "update_blockheight: height = %u", blockheight);
|
|
|
|
|
|
|
|
msg = towire_channeld_blockheight(NULL, blockheight);
|
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:27:22 +02:00
|
|
|
void notify_feerate_change(struct lightningd *ld)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
/* FIXME: We should notify onchaind about NORMAL fee change in case
|
|
|
|
* it's going to generate more txs. */
|
|
|
|
list_for_each(&ld->peers, peer, list) {
|
2022-03-22 21:30:54 +01:00
|
|
|
struct channel *channel;
|
2018-08-23 01:27:22 +02:00
|
|
|
|
2022-03-22 21:30:54 +01:00
|
|
|
list_for_each(&peer->channels, channel, list)
|
|
|
|
try_update_feerates(ld, channel);
|
2018-08-23 01:27:22 +02:00
|
|
|
}
|
2022-03-22 21:30:54 +01:00
|
|
|
|
|
|
|
/* FIXME: We choose not to drop to chain if we can't contact
|
|
|
|
* peer. We *could* do so, however. */
|
2018-08-23 01:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:25:54 +01:00
|
|
|
void channel_record_open(struct channel *channel)
|
2020-04-02 03:58:07 +02:00
|
|
|
{
|
|
|
|
struct chain_coin_mvt *mvt;
|
2020-04-03 23:52:35 +02:00
|
|
|
u32 blockheight;
|
2021-12-01 16:32:55 +01:00
|
|
|
struct amount_msat start_balance;
|
2021-12-08 18:42:07 +01:00
|
|
|
bool is_pushed = !amount_msat_zero(channel->push);
|
|
|
|
bool is_leased = channel->lease_expiry > 0;
|
2020-04-02 03:58:07 +02:00
|
|
|
|
2020-04-03 23:52:35 +02:00
|
|
|
blockheight = short_channel_id_blocknum(channel->scid);
|
|
|
|
|
2021-12-08 18:42:07 +01:00
|
|
|
/* If funds were pushed, add/sub them from the starting balance */
|
2022-02-08 21:39:50 +01:00
|
|
|
if (channel->opener == LOCAL) {
|
|
|
|
if (!amount_msat_add(&start_balance,
|
|
|
|
channel->our_msat, channel->push))
|
|
|
|
fatal("Unable to add push_msat (%s) + our_msat (%s)",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->push),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->our_msat));
|
|
|
|
} else {
|
|
|
|
if (!amount_msat_sub(&start_balance,
|
|
|
|
channel->our_msat, channel->push))
|
|
|
|
fatal("Unable to sub our_msat (%s) - push (%s)",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->our_msat),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->push));
|
|
|
|
}
|
2021-12-01 16:32:55 +01:00
|
|
|
|
|
|
|
mvt = new_coin_channel_open(tmpctx,
|
|
|
|
&channel->cid,
|
|
|
|
&channel->funding,
|
|
|
|
blockheight,
|
|
|
|
start_balance,
|
2021-12-06 19:24:20 +01:00
|
|
|
channel->funding_sats,
|
2021-12-08 18:42:07 +01:00
|
|
|
channel->opener == LOCAL,
|
|
|
|
is_leased);
|
2020-04-02 03:58:07 +02:00
|
|
|
|
|
|
|
notify_chain_mvt(channel->peer->ld, mvt);
|
2021-12-01 16:32:55 +01:00
|
|
|
|
2021-12-08 18:42:07 +01:00
|
|
|
/* If we pushed sats, *now* record them */
|
|
|
|
if (is_pushed)
|
2021-12-01 16:32:55 +01:00
|
|
|
notify_channel_mvt(channel->peer->ld,
|
2021-12-08 18:42:07 +01:00
|
|
|
new_coin_channel_push(tmpctx, &channel->cid,
|
|
|
|
channel->push,
|
|
|
|
is_leased ? LEASE_FEE : PUSHED,
|
|
|
|
channel->opener == REMOTE));
|
2020-04-02 03:58:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:08:01 +02:00
|
|
|
static void lockin_complete(struct channel *channel)
|
|
|
|
{
|
|
|
|
/* We set this once we're locked in. */
|
|
|
|
assert(channel->scid);
|
|
|
|
/* We set this once they're locked in. */
|
|
|
|
assert(channel->remote_funding_locked);
|
2019-04-16 05:23:57 +02:00
|
|
|
|
|
|
|
/* We might have already started shutting down */
|
|
|
|
if (channel->state != CHANNELD_AWAITING_LOCKIN) {
|
|
|
|
log_debug(channel->log, "Lockin complete, but state %s",
|
|
|
|
channel_state_name(channel));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
CHANNELD_AWAITING_LOCKIN,
|
|
|
|
CHANNELD_NORMAL,
|
|
|
|
REASON_UNKNOWN,
|
|
|
|
"Lockin complete");
|
2018-08-23 01:27:17 +02:00
|
|
|
|
|
|
|
/* Fees might have changed (and we use IMMEDIATE once we're funded),
|
|
|
|
* so update now. */
|
|
|
|
try_update_feerates(channel->peer->ld, channel);
|
2021-06-22 20:25:59 +02:00
|
|
|
|
|
|
|
try_update_blockheight(channel->peer->ld, channel,
|
|
|
|
get_block_height(channel->peer->ld->topology));
|
2020-11-24 02:25:54 +01:00
|
|
|
channel_record_open(channel);
|
2018-04-23 12:08:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 02:24:50 +01:00
|
|
|
bool channel_on_funding_locked(struct channel *channel,
|
|
|
|
struct pubkey *next_per_commitment_point)
|
|
|
|
{
|
|
|
|
if (channel->remote_funding_locked) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"channel_got_funding_locked twice");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
update_per_commit_point(channel, next_per_commitment_point);
|
|
|
|
|
|
|
|
log_debug(channel->log, "Got funding_locked");
|
|
|
|
channel->remote_funding_locked = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* We were informed by channeld that it announced the channel and sent
|
|
|
|
* an update, so we can now start sending a node_announcement. The
|
|
|
|
* first step is to build the provisional announcement and ask the HSM
|
|
|
|
* to sign it. */
|
|
|
|
|
|
|
|
static void peer_got_funding_locked(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey next_per_commitment_point;
|
2022-04-22 16:15:49 +02:00
|
|
|
struct short_channel_id *alias_remote;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2022-04-22 16:15:49 +02:00
|
|
|
if (!fromwire_channeld_got_funding_locked(tmpctx,
|
|
|
|
msg, &next_per_commitment_point, &alias_remote)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channel_got_funding_locked %s",
|
|
|
|
tal_hex(channel, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:24:50 +01:00
|
|
|
if (!channel_on_funding_locked(channel, &next_per_commitment_point))
|
2018-02-20 21:59:09 +01:00
|
|
|
return;
|
2018-04-23 12:08:01 +02:00
|
|
|
|
2022-04-22 16:15:49 +02:00
|
|
|
if (channel->alias[REMOTE] == NULL)
|
|
|
|
channel->alias[REMOTE] = tal_steal(channel, alias_remote);
|
|
|
|
|
|
|
|
/* Remember that we got the lockin */
|
|
|
|
wallet_channel_save(channel->peer->ld->wallet, channel);
|
2018-04-23 12:08:01 +02:00
|
|
|
if (channel->scid)
|
|
|
|
lockin_complete(channel);
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 13:58:07 +02:00
|
|
|
static void peer_got_announcement(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature remote_ann_node_sig;
|
|
|
|
secp256k1_ecdsa_signature remote_ann_bitcoin_sig;
|
|
|
|
|
2020-08-25 03:33:16 +02:00
|
|
|
if (!fromwire_channeld_got_announcement(msg,
|
2019-04-25 13:58:07 +02:00
|
|
|
&remote_ann_node_sig,
|
|
|
|
&remote_ann_bitcoin_sig)) {
|
|
|
|
channel_internal_error(channel,
|
2019-06-03 18:40:15 +02:00
|
|
|
"bad channel_got_announcement %s",
|
2019-04-25 13:58:07 +02:00
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wallet_announcement_save(channel->peer->ld->wallet, channel->dbid,
|
|
|
|
&remote_ann_node_sig,
|
|
|
|
&remote_ann_bitcoin_sig);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
static void peer_got_shutdown(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *scriptpubkey;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2021-03-15 21:25:41 +01:00
|
|
|
struct bitcoin_outpoint *wrong_funding;
|
2021-02-24 03:53:12 +01:00
|
|
|
bool anysegwit = feature_negotiated(ld->our_features,
|
|
|
|
channel->peer->their_features,
|
|
|
|
OPT_SHUTDOWN_ANYSEGWIT);
|
2022-03-31 11:10:50 +02:00
|
|
|
bool anchors = feature_negotiated(ld->our_features,
|
|
|
|
channel->peer->their_features,
|
|
|
|
OPT_ANCHOR_OUTPUTS)
|
|
|
|
|| feature_negotiated(ld->our_features,
|
|
|
|
channel->peer->their_features,
|
|
|
|
OPT_ANCHORS_ZERO_FEE_HTLC_TX);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2021-03-15 21:25:41 +01:00
|
|
|
if (!fromwire_channeld_got_shutdown(channel, msg, &scriptpubkey,
|
|
|
|
&wrong_funding)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "bad channel_got_shutdown %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-31 11:10:50 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
* A receiving node:
|
|
|
|
*...
|
|
|
|
* - if the `scriptpubkey` is not in one of the above forms:
|
|
|
|
* - SHOULD send a `warning`.
|
|
|
|
*/
|
2022-03-31 11:10:50 +02:00
|
|
|
if (!valid_shutdown_scriptpubkey(scriptpubkey, anysegwit, anchors)) {
|
2022-03-31 11:10:50 +02:00
|
|
|
u8 *warning = towire_warningfmt(NULL,
|
|
|
|
&channel->cid,
|
|
|
|
"Bad shutdown scriptpubkey %s",
|
|
|
|
tal_hex(tmpctx, scriptpubkey));
|
|
|
|
|
|
|
|
/* Get connectd to send warning, and then allow reconnect. */
|
|
|
|
subd_send_msg(ld->connectd,
|
|
|
|
take(towire_connectd_peer_final_msg(NULL,
|
|
|
|
&channel->peer->id,
|
|
|
|
warning)));
|
|
|
|
channel_fail_reconnect(channel, "Bad shutdown scriptpubkey %s",
|
2021-06-10 05:51:12 +02:00
|
|
|
tal_hex(tmpctx, scriptpubkey));
|
2018-02-20 21:59:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-31 11:10:50 +02:00
|
|
|
/* FIXME: Add to spec that we must allow repeated shutdown! */
|
|
|
|
tal_free(channel->shutdown_scriptpubkey[REMOTE]);
|
|
|
|
channel->shutdown_scriptpubkey[REMOTE] = scriptpubkey;
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* If we weren't already shutting down, we are now */
|
2018-04-03 09:08:53 +02:00
|
|
|
if (channel->state != CHANNELD_SHUTTING_DOWN)
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_set_state(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->state,
|
|
|
|
CHANNELD_SHUTTING_DOWN,
|
|
|
|
REASON_REMOTE,
|
|
|
|
"Peer closes channel");
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2021-03-15 21:25:41 +01:00
|
|
|
/* If we set it, that's what we want. Otherwise use their preference.
|
|
|
|
* We can't have both, since only opener can set this! */
|
|
|
|
if (!channel->shutdown_wrong_funding)
|
|
|
|
channel->shutdown_wrong_funding = wrong_funding;
|
|
|
|
|
2021-03-15 21:26:13 +01:00
|
|
|
/* We now watch the "wrong" funding, in case we spend it. */
|
|
|
|
channel_watch_wrong_funding(ld, channel);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* TODO(cdecker) Selectively save updated fields to DB */
|
|
|
|
wallet_channel_save(ld->wallet, channel);
|
|
|
|
}
|
|
|
|
|
2020-12-10 21:02:02 +01:00
|
|
|
void channel_fallen_behind(struct channel *channel, const u8 *msg)
|
2018-08-17 07:06:35 +02:00
|
|
|
{
|
|
|
|
|
2019-09-10 05:27:51 +02:00
|
|
|
/* per_commitment_point is NULL if option_static_remotekey, but we
|
|
|
|
* use its presence as a flag so set it any valid key in that case. */
|
|
|
|
if (!channel->future_per_commitment_point) {
|
|
|
|
struct pubkey *any = tal(channel, struct pubkey);
|
|
|
|
if (!pubkey_from_node_id(any, &channel->peer->ld->id))
|
|
|
|
fatal("Our own id invalid?");
|
|
|
|
channel->future_per_commitment_point = any;
|
|
|
|
}
|
2018-08-17 07:06:35 +02:00
|
|
|
|
2018-08-23 03:08:48 +02:00
|
|
|
/* Peer sees this, so send a generic msg about unilateral close. */
|
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,
|
|
|
|
"Awaiting unilateral close");
|
2018-08-17 07:06:35 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 21:02:02 +01:00
|
|
|
static void
|
|
|
|
channel_fail_fallen_behind(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
if (!fromwire_channeld_fail_fallen_behind(channel, msg,
|
|
|
|
cast_const2(struct pubkey **,
|
|
|
|
&channel->future_per_commitment_point))) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channel_fail_fallen_behind %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel_fallen_behind(channel, msg);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
static void peer_start_closingd_after_shutdown(struct channel *channel,
|
|
|
|
const u8 *msg,
|
|
|
|
const int *fds)
|
|
|
|
{
|
2022-01-11 02:13:59 +01:00
|
|
|
struct peer_fd *peer_fd;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2022-01-08 14:29:29 +01:00
|
|
|
if (!fromwire_channeld_shutdown_complete(msg)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "bad shutdown_complete: %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
2022-01-11 02:13:59 +01:00
|
|
|
peer_fd = new_peer_fd_arr(msg, fds);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* This sets channel->owner, closes down channeld. */
|
2022-01-11 02:13:59 +01:00
|
|
|
peer_start_closingd(channel, peer_fd);
|
2021-06-14 23:09:49 +02:00
|
|
|
|
|
|
|
/* We might have reconnected, so already be here. */
|
2021-06-15 07:07:15 +02:00
|
|
|
if (!channel_closed(channel)
|
|
|
|
&& channel->state != CLOSINGD_SIGEXCHANGE)
|
2021-06-14 23:09:49 +02:00
|
|
|
channel_set_state(channel,
|
|
|
|
CHANNELD_SHUTTING_DOWN,
|
|
|
|
CLOSINGD_SIGEXCHANGE,
|
|
|
|
REASON_UNKNOWN,
|
|
|
|
"Start closingd");
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 01:23:19 +01:00
|
|
|
static void forget(struct channel *channel)
|
2019-08-23 23:34:52 +02:00
|
|
|
{
|
|
|
|
struct command **forgets = tal_steal(tmpctx, channel->forgets);
|
|
|
|
channel->forgets = tal_arr(channel, struct command *, 0);
|
|
|
|
|
|
|
|
/* Forget the channel. */
|
|
|
|
delete_channel(channel);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(forgets); i++) {
|
|
|
|
assert(!forgets[i]->json_stream);
|
|
|
|
|
|
|
|
struct json_stream *response;
|
|
|
|
response = json_stream_success(forgets[i]);
|
2020-11-24 01:36:22 +01:00
|
|
|
json_add_string(response, "cancelled",
|
|
|
|
"Channel open canceled by RPC(after"
|
|
|
|
" fundchannel_complete)");
|
2019-08-23 23:34:52 +02:00
|
|
|
was_pending(command_success(forgets[i], response));
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(forgets);
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:23:19 +01:00
|
|
|
static void handle_error_channel(struct channel *channel,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
2020-08-25 03:33:16 +02:00
|
|
|
if (!fromwire_channeld_send_error_reply(msg)) {
|
2019-12-12 01:23:19 +01:00
|
|
|
channel_internal_error(channel, "bad send_error_reply: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
forget(channel);
|
|
|
|
}
|
|
|
|
|
2022-01-24 21:03:52 +01:00
|
|
|
static void handle_local_private_channel(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct amount_sat capacity;
|
|
|
|
u8 *features;
|
|
|
|
|
|
|
|
if (!fromwire_channeld_local_private_channel(msg, msg, &capacity,
|
|
|
|
&features)) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channeld_local_private_channel %s",
|
|
|
|
tal_hex(channel, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tell_gossipd_local_private_channel(channel->peer->ld, channel,
|
|
|
|
capacity, features);
|
|
|
|
}
|
|
|
|
|
2021-12-04 12:27:06 +01:00
|
|
|
static void forget_channel(struct channel *channel, const char *why)
|
2019-12-12 01:23:19 +01:00
|
|
|
{
|
2020-09-09 09:20:53 +02:00
|
|
|
channel->error = towire_errorfmt(channel, &channel->cid, "%s", why);
|
2019-12-12 01:23:19 +01:00
|
|
|
|
|
|
|
/* If the peer is connected, we let them know. Otherwise
|
|
|
|
* we just directly remove the channel */
|
|
|
|
if (channel->owner)
|
|
|
|
subd_send_msg(channel->owner,
|
2020-08-25 03:33:16 +02:00
|
|
|
take(towire_channeld_send_error(NULL, why)));
|
2019-12-12 01:23:19 +01:00
|
|
|
else
|
|
|
|
forget(channel);
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:13:47 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES
|
|
|
|
static void handle_channel_upgrade(struct channel *channel,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
2021-09-09 07:29:35 +02:00
|
|
|
struct channel_type *newtype;
|
2021-06-04 07:13:47 +02:00
|
|
|
|
2021-09-09 07:29:35 +02:00
|
|
|
if (!fromwire_channeld_upgraded(msg, msg, &newtype)) {
|
2021-06-04 07:13:47 +02:00
|
|
|
channel_internal_error(channel, "bad handle_channel_upgrade: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-09 07:29:35 +02:00
|
|
|
/* You can currently only upgrade to turn on option_static_remotekey:
|
|
|
|
* if they somehow thought anything else we need to close channel! */
|
|
|
|
if (channel->static_remotekey_start[LOCAL] != 0x7FFFFFFFFFFFFFFFULL) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"channel_upgrade already static_remotekey? %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!channel_type_eq(newtype, channel_type_static_remotekey(tmpctx))) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"channel_upgrade must be static_remotekey, not %s",
|
|
|
|
fmt_featurebits(tmpctx, newtype->features));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(channel->type);
|
|
|
|
channel->type = channel_type_dup(channel, newtype);
|
2021-06-04 07:13:47 +02:00
|
|
|
channel->static_remotekey_start[LOCAL] = channel->next_index[LOCAL];
|
|
|
|
channel->static_remotekey_start[REMOTE] = channel->next_index[REMOTE];
|
|
|
|
log_debug(channel->log,
|
|
|
|
"option_static_remotekey enabled at %"PRIu64"/%"PRIu64,
|
|
|
|
channel->static_remotekey_start[LOCAL],
|
|
|
|
channel->static_remotekey_start[REMOTE]);
|
|
|
|
|
|
|
|
wallet_channel_save(channel->peer->ld->wallet, channel);
|
|
|
|
}
|
|
|
|
#endif /* EXPERIMENTAL_FEATURES */
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
|
|
|
|
{
|
2020-08-25 03:33:16 +02:00
|
|
|
enum channeld_wire t = fromwire_peektype(msg);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
switch (t) {
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_SENDING_COMMITSIG:
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_sending_commitsig(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_GOT_COMMITSIG:
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_got_commitsig(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_GOT_REVOKE:
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_got_revoke(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_GOT_FUNDING_LOCKED:
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_got_funding_locked(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_GOT_ANNOUNCEMENT:
|
2019-04-25 13:58:07 +02:00
|
|
|
peer_got_announcement(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_GOT_SHUTDOWN:
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_got_shutdown(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_SHUTDOWN_COMPLETE:
|
2022-01-29 04:33:05 +01:00
|
|
|
/* We expect 1 fd. */
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fds)
|
2022-01-29 04:33:05 +01:00
|
|
|
return 1;
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_start_closingd_after_shutdown(sd->channel, msg, fds);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_FAIL_FALLEN_BEHIND:
|
2018-08-17 07:06:35 +02:00
|
|
|
channel_fail_fallen_behind(sd->channel, msg);
|
|
|
|
break;
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_SEND_ERROR_REPLY:
|
2019-08-23 23:34:52 +02:00
|
|
|
handle_error_channel(sd->channel, msg);
|
|
|
|
break;
|
2022-01-24 21:02:52 +01:00
|
|
|
case WIRE_CHANNELD_USED_CHANNEL_UPDATE:
|
|
|
|
/* This tells gossipd we used it. */
|
|
|
|
get_channel_update(sd->channel);
|
|
|
|
break;
|
2022-01-24 21:03:52 +01:00
|
|
|
case WIRE_CHANNELD_LOCAL_CHANNEL_UPDATE:
|
|
|
|
tell_gossipd_local_channel_update(sd->ld, sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNELD_LOCAL_CHANNEL_ANNOUNCEMENT:
|
|
|
|
tell_gossipd_local_channel_announce(sd->ld, sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNELD_LOCAL_PRIVATE_CHANNEL:
|
|
|
|
handle_local_private_channel(sd->channel, msg);
|
|
|
|
break;
|
2021-06-04 07:13:47 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES
|
|
|
|
case WIRE_CHANNELD_UPGRADED:
|
|
|
|
handle_channel_upgrade(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
case WIRE_CHANNELD_UPGRADED:
|
|
|
|
#endif
|
2018-02-20 21:59:09 +01:00
|
|
|
/* And we never get these from channeld. */
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_INIT:
|
|
|
|
case WIRE_CHANNELD_FUNDING_DEPTH:
|
|
|
|
case WIRE_CHANNELD_OFFER_HTLC:
|
|
|
|
case WIRE_CHANNELD_FULFILL_HTLC:
|
|
|
|
case WIRE_CHANNELD_FAIL_HTLC:
|
|
|
|
case WIRE_CHANNELD_GOT_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNELD_GOT_REVOKE_REPLY:
|
|
|
|
case WIRE_CHANNELD_SENDING_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNELD_SEND_SHUTDOWN:
|
|
|
|
case WIRE_CHANNELD_DEV_REENABLE_COMMIT:
|
|
|
|
case WIRE_CHANNELD_FEERATES:
|
2021-06-22 20:25:59 +02:00
|
|
|
case WIRE_CHANNELD_BLOCKHEIGHT:
|
2022-03-21 01:58:28 +01:00
|
|
|
case WIRE_CHANNELD_CONFIG_CHANNEL:
|
2022-01-24 21:02:52 +01:00
|
|
|
case WIRE_CHANNELD_CHANNEL_UPDATE:
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_DEV_MEMLEAK:
|
2021-05-31 05:08:04 +02:00
|
|
|
case WIRE_CHANNELD_DEV_QUIESCE:
|
2020-04-01 05:53:09 +02:00
|
|
|
/* Replies go to requests. */
|
2020-08-25 03:33:16 +02:00
|
|
|
case WIRE_CHANNELD_OFFER_HTLC_REPLY:
|
|
|
|
case WIRE_CHANNELD_DEV_REENABLE_COMMIT_REPLY:
|
|
|
|
case WIRE_CHANNELD_DEV_MEMLEAK_REPLY:
|
|
|
|
case WIRE_CHANNELD_SEND_ERROR:
|
2021-05-31 05:08:04 +02:00
|
|
|
case WIRE_CHANNELD_DEV_QUIESCE_REPLY:
|
2018-02-20 21:59:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:26:30 +02:00
|
|
|
void peer_start_channeld(struct channel *channel,
|
2022-01-11 02:13:59 +01:00
|
|
|
struct peer_fd *peer_fd,
|
2020-09-10 21:34:18 +02:00
|
|
|
const u8 *fwd_msg,
|
2021-06-15 07:07:10 +02:00
|
|
|
bool reconnected,
|
2022-03-22 21:27:30 +01:00
|
|
|
bool reestablish_only)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2018-07-09 13:17:59 +02:00
|
|
|
u8 *initmsg;
|
2018-02-20 21:59:09 +01:00
|
|
|
int hsmfd;
|
2020-04-03 05:14:07 +02:00
|
|
|
const struct existing_htlc **htlcs;
|
2019-06-03 14:05:18 +02:00
|
|
|
struct short_channel_id scid;
|
2018-02-20 21:59:09 +01:00
|
|
|
u64 num_revocations;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
const struct config *cfg = &ld->config;
|
2018-04-26 06:51:02 +02:00
|
|
|
bool reached_announce_depth;
|
2018-08-17 06:16:34 +02:00
|
|
|
struct secret last_remote_per_commit_secret;
|
2019-05-14 11:36:05 +02:00
|
|
|
secp256k1_ecdsa_signature *remote_ann_node_sig, *remote_ann_bitcoin_sig;
|
2020-05-07 02:49:43 +02:00
|
|
|
struct penalty_base *pbases;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
hsmfd = hsm_get_client_fd(ld, &channel->peer->id,
|
|
|
|
channel->dbid,
|
2018-07-23 04:23:03 +02:00
|
|
|
HSM_CAP_SIGN_GOSSIP
|
|
|
|
| HSM_CAP_ECDH
|
2018-07-23 04:23:03 +02:00
|
|
|
| HSM_CAP_COMMITMENT_POINT
|
2020-05-07 02:50:43 +02:00
|
|
|
| HSM_CAP_SIGN_REMOTE_TX
|
|
|
|
| HSM_CAP_SIGN_ONCHAIN_TX);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
channel_set_owner(channel,
|
2022-03-29 01:49:23 +02:00
|
|
|
new_channel_subd(channel, ld,
|
2020-12-01 21:49:35 +01:00
|
|
|
"lightning_channeld",
|
2021-01-20 02:51:15 +01:00
|
|
|
channel,
|
2019-11-17 12:40:33 +01:00
|
|
|
&channel->peer->id,
|
2018-04-26 06:51:01 +02:00
|
|
|
channel->log, true,
|
2020-08-25 03:33:16 +02:00
|
|
|
channeld_wire_name,
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_msg,
|
|
|
|
channel_errmsg,
|
2018-02-23 06:53:47 +01:00
|
|
|
channel_set_billboard,
|
2022-01-11 02:13:59 +01:00
|
|
|
take(&peer_fd->fd),
|
2019-07-25 04:47:34 +02:00
|
|
|
take(&hsmfd), NULL));
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
if (!channel->owner) {
|
2020-03-25 05:26:44 +01:00
|
|
|
log_broken(channel->log, "Could not subdaemon channel: %s",
|
|
|
|
strerror(errno));
|
2019-07-26 04:11:18 +02:00
|
|
|
channel_fail_reconnect_later(channel,
|
|
|
|
"Failed to subdaemon channel");
|
2018-08-09 02:26:30 +02:00
|
|
|
return;
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 05:14:07 +02:00
|
|
|
htlcs = peer_htlcs(tmpctx, channel);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
if (channel->scid) {
|
2019-06-03 14:05:18 +02:00
|
|
|
scid = *channel->scid;
|
2019-09-22 04:08:43 +02:00
|
|
|
reached_announce_depth
|
|
|
|
= is_scid_depth_announceable(&scid,
|
|
|
|
get_block_height(ld->topology));
|
2018-04-26 06:51:02 +02:00
|
|
|
log_debug(channel->log, "Already have funding locked in%s",
|
|
|
|
reached_announce_depth
|
|
|
|
? " (and ready to announce)" : "");
|
2018-02-20 21:59:09 +01:00
|
|
|
} else {
|
|
|
|
log_debug(channel->log, "Waiting for funding confirmations");
|
2019-06-03 14:05:18 +02:00
|
|
|
memset(&scid, 0, sizeof(scid));
|
2018-04-26 06:51:02 +02:00
|
|
|
reached_announce_depth = false;
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
num_revocations = revocations_received(&channel->their_shachain.chain);
|
|
|
|
|
2018-08-17 06:16:34 +02:00
|
|
|
/* BOLT #2:
|
2019-08-02 05:24:25 +02:00
|
|
|
* - if `next_revocation_number` equals 0:
|
2018-08-17 06:16:34 +02:00
|
|
|
* - MUST set `your_last_per_commitment_secret` to all zeroes
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST set `your_last_per_commitment_secret` to the last
|
|
|
|
* `per_commitment_secret` it received
|
|
|
|
*/
|
|
|
|
if (num_revocations == 0)
|
|
|
|
memset(&last_remote_per_commit_secret, 0,
|
|
|
|
sizeof(last_remote_per_commit_secret));
|
|
|
|
else if (!shachain_get_secret(&channel->their_shachain.chain,
|
|
|
|
num_revocations-1,
|
|
|
|
&last_remote_per_commit_secret)) {
|
|
|
|
channel_fail_permanent(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
|
|
|
REASON_LOCAL,
|
2018-08-17 06:16:34 +02:00
|
|
|
"Could not get revocation secret %"PRIu64,
|
|
|
|
num_revocations-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* Warn once. */
|
|
|
|
if (ld->config.ignore_fee_limits)
|
|
|
|
log_debug(channel->log, "Ignoring fee limits!");
|
|
|
|
|
2020-11-24 01:36:22 +01:00
|
|
|
if (!wallet_remote_ann_sigs_load(tmpctx, channel->peer->ld->wallet,
|
|
|
|
channel->dbid,
|
|
|
|
&remote_ann_node_sig,
|
|
|
|
&remote_ann_bitcoin_sig)) {
|
2019-05-14 11:36:05 +02:00
|
|
|
channel_internal_error(channel,
|
2020-11-24 01:36:22 +01:00
|
|
|
"Could not load remote announcement"
|
|
|
|
" signatures");
|
2019-05-14 11:36:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:49:43 +02:00
|
|
|
pbases = wallet_penalty_base_load_for_channel(
|
|
|
|
tmpctx, channel->peer->ld->wallet, channel->dbid);
|
|
|
|
|
2021-12-23 21:16:35 +01:00
|
|
|
struct ext_key final_ext_key;
|
|
|
|
if (bip32_key_from_parent(
|
|
|
|
ld->wallet->bip32_base,
|
|
|
|
channel->final_key_idx,
|
|
|
|
BIP32_FLAG_KEY_PUBLIC,
|
|
|
|
&final_ext_key) != WALLY_OK) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"Could not derive final_ext_key %"PRIu64,
|
|
|
|
channel->final_key_idx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-25 03:33:16 +02:00
|
|
|
initmsg = towire_channeld_init(tmpctx,
|
2021-12-29 04:26:40 +01:00
|
|
|
chainparams,
|
|
|
|
ld->our_features,
|
|
|
|
&channel->cid,
|
|
|
|
&channel->funding,
|
|
|
|
channel->funding_sats,
|
|
|
|
channel->minimum_depth,
|
|
|
|
get_block_height(ld->topology),
|
|
|
|
channel->blockheight_states,
|
|
|
|
channel->lease_expiry,
|
|
|
|
&channel->our_config,
|
|
|
|
&channel->channel_info.their_config,
|
|
|
|
channel->fee_states,
|
|
|
|
feerate_min(ld, NULL),
|
|
|
|
feerate_max(ld, NULL),
|
|
|
|
try_get_feerate(ld->topology, FEERATE_PENALTY),
|
|
|
|
&channel->last_sig,
|
|
|
|
&channel->channel_info.remote_fundingkey,
|
|
|
|
&channel->channel_info.theirbase,
|
|
|
|
&channel->channel_info.remote_per_commit,
|
|
|
|
&channel->channel_info.old_remote_per_commit,
|
|
|
|
channel->opener,
|
|
|
|
channel->feerate_base,
|
|
|
|
channel->feerate_ppm,
|
2022-03-21 01:58:54 +01:00
|
|
|
channel->htlc_minimum_msat,
|
2022-03-21 01:58:27 +01:00
|
|
|
channel->htlc_maximum_msat,
|
2021-12-29 04:26:40 +01:00
|
|
|
channel->our_msat,
|
|
|
|
&channel->local_basepoints,
|
|
|
|
&channel->local_funding_pubkey,
|
|
|
|
&ld->id,
|
|
|
|
&channel->peer->id,
|
|
|
|
cfg->commit_time_ms,
|
|
|
|
cfg->cltv_expiry_delta,
|
|
|
|
channel->last_was_revoke,
|
|
|
|
channel->last_sent_commit,
|
|
|
|
channel->next_index[LOCAL],
|
|
|
|
channel->next_index[REMOTE],
|
|
|
|
num_revocations,
|
|
|
|
channel->next_htlc_id,
|
|
|
|
htlcs,
|
|
|
|
channel->scid != NULL,
|
|
|
|
channel->remote_funding_locked,
|
|
|
|
&scid,
|
|
|
|
reconnected,
|
2021-06-15 07:07:10 +02:00
|
|
|
/* Anything that indicates we are or have
|
|
|
|
* shut down */
|
2021-12-29 04:26:40 +01:00
|
|
|
channel->state == CHANNELD_SHUTTING_DOWN
|
2021-06-15 07:07:10 +02:00
|
|
|
|| channel->state == CLOSINGD_SIGEXCHANGE
|
|
|
|
|| channel_closed(channel),
|
2021-12-29 04:26:40 +01:00
|
|
|
channel->shutdown_scriptpubkey[REMOTE] != NULL,
|
2021-12-23 21:16:35 +01:00
|
|
|
channel->final_key_idx,
|
|
|
|
&final_ext_key,
|
2021-12-29 04:26:40 +01:00
|
|
|
channel->shutdown_scriptpubkey[LOCAL],
|
|
|
|
channel->channel_flags,
|
|
|
|
fwd_msg,
|
|
|
|
reached_announce_depth,
|
|
|
|
&last_remote_per_commit_secret,
|
|
|
|
channel->peer->their_features,
|
|
|
|
channel->remote_upfront_shutdown_script,
|
|
|
|
remote_ann_node_sig,
|
|
|
|
remote_ann_bitcoin_sig,
|
|
|
|
channel->type,
|
|
|
|
IFDEV(ld->dev_fast_gossip, false),
|
|
|
|
IFDEV(dev_fail_process_onionpacket, false),
|
|
|
|
IFDEV(ld->dev_disable_commit == -1
|
|
|
|
? NULL
|
|
|
|
: (u32 *)&ld->dev_disable_commit,
|
|
|
|
NULL),
|
|
|
|
pbases,
|
2022-01-24 21:02:52 +01:00
|
|
|
reestablish_only,
|
|
|
|
channel->channel_update);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* We don't expect a response: we are triggered by funding_depth_cb. */
|
|
|
|
subd_send_msg(channel->owner, take(initmsg));
|
2018-08-22 04:33:32 +02:00
|
|
|
|
2021-06-22 20:25:59 +02:00
|
|
|
/* On restart, feerate and blockheight
|
|
|
|
* might not be what we expect: adjust now. */
|
|
|
|
if (channel->opener == LOCAL) {
|
2018-08-22 04:33:32 +02:00
|
|
|
try_update_feerates(ld, channel);
|
2021-06-22 20:25:59 +02:00
|
|
|
try_update_blockheight(ld, channel,
|
|
|
|
get_block_height(ld->topology));
|
|
|
|
}
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
2018-04-23 12:08:01 +02:00
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
bool channel_tell_depth(struct lightningd *ld,
|
2020-11-24 01:36:22 +01:00
|
|
|
struct channel *channel,
|
|
|
|
const struct bitcoin_txid *txid,
|
|
|
|
u32 depth)
|
2018-04-23 12:08:01 +02:00
|
|
|
{
|
2019-02-26 17:57:19 +01:00
|
|
|
const char *txidstr;
|
|
|
|
|
|
|
|
txidstr = type_to_string(tmpctx, struct bitcoin_txid, txid);
|
|
|
|
|
2018-04-23 12:08:01 +02:00
|
|
|
if (!channel->owner) {
|
|
|
|
log_debug(channel->log,
|
2019-02-26 17:57:19 +01:00
|
|
|
"Funding tx %s confirmed, but peer disconnected",
|
|
|
|
txidstr);
|
2018-04-23 12:08:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-24 02:44:02 +01:00
|
|
|
if (streq(channel->owner->name, "dualopend")) {
|
2020-12-10 21:05:52 +01:00
|
|
|
if (channel->state != DUALOPEND_AWAITING_LOCKIN) {
|
2020-11-24 02:44:02 +01:00
|
|
|
log_debug(channel->log,
|
|
|
|
"Funding tx %s confirmed, but peer in"
|
|
|
|
" state %s",
|
|
|
|
txidstr, channel_state_name(channel));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-20 23:54:34 +02:00
|
|
|
log_debug(channel->log,
|
|
|
|
"Funding tx %s confirmed, telling peer", txidstr);
|
2021-01-07 20:03:06 +01:00
|
|
|
dualopen_tell_depth(channel->owner, channel,
|
|
|
|
txid, depth);
|
2020-11-24 22:10:19 +01:00
|
|
|
return true;
|
2020-11-24 02:44:02 +01:00
|
|
|
} else if (channel->state != CHANNELD_AWAITING_LOCKIN
|
|
|
|
&& channel->state != CHANNELD_NORMAL) {
|
|
|
|
/* If not awaiting lockin/announce, it doesn't
|
|
|
|
* care any more */
|
|
|
|
log_debug(channel->log,
|
|
|
|
"Funding tx %s confirmed, but peer in state %s",
|
|
|
|
txidstr, channel_state_name(channel));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:08:01 +02:00
|
|
|
subd_send_msg(channel->owner,
|
2022-04-22 16:15:49 +02:00
|
|
|
take(towire_channeld_funding_depth(
|
|
|
|
NULL, channel->scid, channel->alias[LOCAL], depth)));
|
2018-04-23 12:08:01 +02:00
|
|
|
|
2018-05-17 07:08:11 +02:00
|
|
|
if (channel->remote_funding_locked
|
2019-02-26 17:57:19 +01:00
|
|
|
&& channel->state == CHANNELD_AWAITING_LOCKIN
|
|
|
|
&& depth >= channel->minimum_depth)
|
2018-04-23 12:08:01 +02:00
|
|
|
lockin_complete(channel);
|
2018-05-17 07:08:11 +02:00
|
|
|
|
2018-04-23 12:08:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-06 15:32:01 +02:00
|
|
|
|
|
|
|
/* Check if we are the fundee of this channel, the channel
|
|
|
|
* funding transaction is still not yet seen onchain, and
|
|
|
|
* it has been too long since the channel was first opened.
|
|
|
|
* If so, we should forget the channel. */
|
|
|
|
static bool
|
|
|
|
is_fundee_should_forget(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
|
|
|
u32 block_height)
|
|
|
|
{
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A non-funding node (fundee):
|
|
|
|
* - SHOULD forget the channel if it does not see the
|
2021-04-05 23:12:11 +02:00
|
|
|
* correct funding transaction after a timeout of 2016 blocks.
|
2018-05-06 15:32:01 +02:00
|
|
|
*/
|
2021-04-05 23:12:11 +02:00
|
|
|
u32 max_funding_unconfirmed = IFDEV(ld->dev_max_funding_unconfirmed, 2016);
|
2018-05-06 15:32:01 +02:00
|
|
|
|
|
|
|
/* Only applies if we are fundee. */
|
2019-09-09 18:11:24 +02:00
|
|
|
if (channel->opener == LOCAL)
|
2018-05-06 15:32:01 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Does not apply if we already saw the funding tx. */
|
|
|
|
if (channel->scid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Not even reached previous starting blocknum.
|
|
|
|
* (e.g. if --rescan option is used) */
|
|
|
|
if (block_height < channel->first_blocknum)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Timeout in blocks not yet reached. */
|
2018-05-07 01:01:49 +02:00
|
|
|
if (block_height - channel->first_blocknum < max_funding_unconfirmed)
|
2018-05-06 15:32:01 +02:00
|
|
|
return false;
|
|
|
|
|
2021-07-02 22:21:54 +02:00
|
|
|
/* If we've got funds in the channel, don't forget it */
|
|
|
|
if (!amount_sat_zero(channel->our_funds))
|
|
|
|
return false;
|
|
|
|
|
2018-05-06 15:32:01 +02:00
|
|
|
/* Ah forget it! */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify all channels of new blocks. */
|
|
|
|
void channel_notify_new_block(struct lightningd *ld,
|
|
|
|
u32 block_height)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
struct channel *channel;
|
|
|
|
struct channel **to_forget = tal_arr(NULL, struct channel *, 0);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
list_for_each (&ld->peers, peer, list) {
|
2021-01-22 01:55:23 +01:00
|
|
|
list_for_each (&peer->channels, channel, list) {
|
|
|
|
if (channel_unsaved(channel))
|
|
|
|
continue;
|
2018-05-06 15:32:01 +02:00
|
|
|
if (is_fundee_should_forget(ld, channel, block_height)) {
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&to_forget, channel);
|
2021-06-22 20:25:59 +02:00
|
|
|
} else
|
|
|
|
/* Let channels know about new blocks,
|
|
|
|
* required for lease updates */
|
|
|
|
try_update_blockheight(ld, channel,
|
|
|
|
block_height);
|
2021-01-22 01:55:23 +01:00
|
|
|
}
|
2018-05-06 15:32:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Need to forget in a separate loop, else the above
|
|
|
|
* nested loops may crash due to the last channel of
|
|
|
|
* a peer also deleting the peer, making the inner
|
|
|
|
* loop crash.
|
|
|
|
* list_for_each_safe does not work because it is not
|
|
|
|
* just the freeing of the channel that occurs, but the
|
|
|
|
* potential destruction of the peer that invalidates
|
|
|
|
* memory the inner loop is accessing. */
|
|
|
|
for (i = 0; i < tal_count(to_forget); ++i) {
|
|
|
|
channel = to_forget[i];
|
|
|
|
/* Report it first. */
|
|
|
|
log_unusual(channel->log,
|
|
|
|
"Forgetting channel: "
|
|
|
|
"It has been %"PRIu32" blocks without the "
|
|
|
|
"funding transaction %s getting deeply "
|
|
|
|
"confirmed. "
|
|
|
|
"We are fundee and can forget channel without "
|
|
|
|
"loss of funds.",
|
|
|
|
block_height - channel->first_blocknum,
|
|
|
|
type_to_string(tmpctx, struct bitcoin_txid,
|
2021-10-13 05:45:36 +02:00
|
|
|
&channel->funding.txid));
|
2018-08-02 08:49:56 +02:00
|
|
|
/* FIXME: Send an error packet for this case! */
|
2018-05-06 15:32:01 +02:00
|
|
|
/* And forget it. */
|
|
|
|
delete_channel(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(to_forget);
|
|
|
|
}
|
2019-08-23 23:34:52 +02:00
|
|
|
|
2019-09-19 08:44:24 +02:00
|
|
|
/* Since this could vanish while we're checking with bitcoind, we need to save
|
|
|
|
* the details and re-lookup.
|
|
|
|
*
|
|
|
|
* channel_id *should* be unique, but it can be set by the counterparty, so
|
|
|
|
* we cannot rely on that! */
|
|
|
|
struct channel_to_cancel {
|
|
|
|
struct node_id peer;
|
|
|
|
struct channel_id cid;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void process_check_funding_broadcast(struct bitcoind *bitcoind,
|
2019-08-23 23:34:52 +02:00
|
|
|
const struct bitcoin_tx_output *txout,
|
|
|
|
void *arg)
|
|
|
|
{
|
2019-09-19 08:44:24 +02:00
|
|
|
struct channel_to_cancel *cc = arg;
|
|
|
|
struct peer *peer;
|
|
|
|
struct channel *cancel;
|
|
|
|
|
|
|
|
/* Peer could have errored out while we were waiting */
|
|
|
|
peer = peer_by_id(bitcoind->ld, &cc->peer);
|
|
|
|
if (!peer)
|
2020-09-08 11:39:52 +02:00
|
|
|
goto cleanup;
|
2019-09-19 08:44:24 +02:00
|
|
|
cancel = find_channel_by_id(peer, &cc->cid);
|
|
|
|
if (!cancel)
|
2020-09-08 11:39:52 +02:00
|
|
|
goto cleanup;
|
2019-08-23 23:34:52 +02:00
|
|
|
|
|
|
|
if (txout != NULL) {
|
|
|
|
for (size_t i = 0; i < tal_count(cancel->forgets); i++)
|
2020-06-24 06:34:26 +02:00
|
|
|
was_pending(command_fail(cancel->forgets[i],
|
|
|
|
FUNDING_CANCEL_NOT_SAFE,
|
2019-08-23 23:34:52 +02:00
|
|
|
"The funding transaction has been broadcast, "
|
|
|
|
"please consider `close` or `dev-fail`! "));
|
|
|
|
tal_free(cancel->forgets);
|
|
|
|
cancel->forgets = tal_arr(cancel, struct command *, 0);
|
2020-09-08 11:39:52 +02:00
|
|
|
goto cleanup;
|
2019-08-23 23:34:52 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 01:50:30 +01:00
|
|
|
char *error_reason = "Cancel channel by our RPC "
|
|
|
|
"command before funding "
|
|
|
|
"transaction broadcast.";
|
|
|
|
forget_channel(cancel, error_reason);
|
2020-09-08 11:39:52 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
tal_free(cc);
|
|
|
|
return;
|
2019-08-23 23:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *cancel_channel_before_broadcast(struct command *cmd,
|
2020-06-24 06:34:26 +02:00
|
|
|
struct peer *peer)
|
2019-08-23 23:34:52 +02:00
|
|
|
{
|
2019-09-19 08:31:30 +02:00
|
|
|
struct channel *cancel_channel;
|
2019-09-19 08:44:24 +02:00
|
|
|
struct channel_to_cancel *cc = tal(cmd, struct channel_to_cancel);
|
2020-06-24 06:34:26 +02:00
|
|
|
struct channel *channel;
|
2019-08-23 23:34:52 +02:00
|
|
|
|
2019-09-19 08:44:24 +02:00
|
|
|
cc->peer = peer->id;
|
2020-06-24 06:34:26 +02:00
|
|
|
cancel_channel = NULL;
|
|
|
|
list_for_each(&peer->channels, channel, list) {
|
|
|
|
/* After `fundchannel_complete`, channel is in
|
|
|
|
* `CHANNELD_AWAITING_LOCKIN` state.
|
|
|
|
*
|
|
|
|
* TODO: This assumes only one channel at a time
|
|
|
|
* can be in this state, which is true at the
|
|
|
|
* time of this writing, but may change *if* we
|
|
|
|
* ever implement multiple channels per peer.
|
|
|
|
*/
|
|
|
|
if (channel->state != CHANNELD_AWAITING_LOCKIN)
|
|
|
|
continue;
|
|
|
|
cancel_channel = channel;
|
|
|
|
break;
|
2019-08-23 23:34:52 +02:00
|
|
|
}
|
2020-06-24 06:34:26 +02:00
|
|
|
if (!cancel_channel)
|
|
|
|
return command_fail(cmd, FUNDING_NOTHING_TO_CANCEL,
|
|
|
|
"No channels being opened or "
|
|
|
|
"awaiting lock-in for "
|
|
|
|
"peer_id %s",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&peer->id));
|
2020-09-09 09:20:53 +02:00
|
|
|
cc->cid = cancel_channel->cid;
|
2019-08-23 23:34:52 +02:00
|
|
|
|
2019-09-09 18:11:24 +02:00
|
|
|
if (cancel_channel->opener == REMOTE)
|
2020-06-24 06:34:26 +02:00
|
|
|
return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE,
|
2019-12-12 01:46:08 +01:00
|
|
|
"Cannot cancel channel that was "
|
|
|
|
"initiated by peer");
|
|
|
|
|
2020-11-24 01:36:22 +01:00
|
|
|
/* Check if we broadcast the transaction. (We store the transaction
|
|
|
|
* type into DB before broadcast). */
|
2019-08-23 23:34:52 +02:00
|
|
|
enum wallet_tx_type type;
|
2020-01-05 16:52:34 +01:00
|
|
|
if (wallet_transaction_type(cmd->ld->wallet,
|
2021-10-13 05:45:36 +02:00
|
|
|
&cancel_channel->funding.txid,
|
2019-08-23 23:34:52 +02:00
|
|
|
&type))
|
2020-06-24 06:34:26 +02:00
|
|
|
return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE,
|
2020-11-24 01:36:22 +01:00
|
|
|
"Has the funding transaction been"
|
|
|
|
" broadcast? Please use `close` or"
|
|
|
|
" `dev-fail` instead.");
|
2019-08-23 23:34:52 +02:00
|
|
|
|
|
|
|
if (channel_has_htlc_out(cancel_channel) ||
|
|
|
|
channel_has_htlc_in(cancel_channel)) {
|
2020-06-24 06:34:26 +02:00
|
|
|
return command_fail(cmd, FUNDING_CANCEL_NOT_SAFE,
|
2020-11-24 01:36:22 +01:00
|
|
|
"This channel has HTLCs attached and it"
|
|
|
|
" is not safe to cancel. Has the funding"
|
|
|
|
" transaction been broadcast? Please use"
|
|
|
|
" `close` or `dev-fail` instead.");
|
2019-08-23 23:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tal_arr_expand(&cancel_channel->forgets, cmd);
|
|
|
|
|
|
|
|
/* Check if the transaction is onchain. */
|
|
|
|
/* Note: The above check and this check can't completely ensure that
|
|
|
|
* the funding transaction isn't broadcast. We can't know if the funding
|
2020-11-24 01:36:22 +01:00
|
|
|
* is broadcast by external wallet and the transaction hasn't
|
|
|
|
* been onchain. */
|
2020-01-09 16:38:12 +01:00
|
|
|
bitcoind_getutxout(cmd->ld->topology->bitcoind,
|
2021-10-13 05:45:36 +02:00
|
|
|
&cancel_channel->funding,
|
2020-01-09 16:38:12 +01:00
|
|
|
process_check_funding_broadcast,
|
2021-11-24 05:06:05 +01:00
|
|
|
/* Freed by callback */
|
|
|
|
tal_steal(NULL, cc));
|
2019-08-23 23:34:52 +02:00
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
2019-10-28 04:33:42 +01:00
|
|
|
|
2022-01-24 21:02:52 +01:00
|
|
|
void channel_replace_update(struct channel *channel, u8 *update TAKES)
|
|
|
|
{
|
|
|
|
tal_free(channel->channel_update);
|
|
|
|
channel->channel_update = tal_dup_talarr(channel, u8, update);
|
|
|
|
|
|
|
|
/* Keep channeld up-to-date */
|
|
|
|
if (!channel->owner || !streq(channel->owner->name, "channeld"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
subd_send_msg(channel->owner,
|
|
|
|
take(towire_channeld_channel_update(NULL,
|
|
|
|
channel->channel_update)));
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:33:42 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
static struct command_result *json_dev_feerate(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
u32 *feerate;
|
|
|
|
struct node_id *id;
|
|
|
|
struct peer *peer;
|
|
|
|
struct json_stream *response;
|
|
|
|
struct channel *channel;
|
|
|
|
const u8 *msg;
|
2022-03-22 23:59:20 +01:00
|
|
|
bool more_than_one;
|
2019-10-28 04:33:42 +01:00
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("id", param_node_id, &id),
|
|
|
|
p_req("feerate", param_number, &feerate),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
peer = peer_by_id(cmd->ld, id);
|
|
|
|
if (!peer)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
|
|
|
|
2022-03-22 23:59:20 +01:00
|
|
|
channel = peer_any_active_channel(peer, &more_than_one);
|
2019-10-28 04:33:42 +01:00
|
|
|
if (!channel || !channel->owner || channel->state != CHANNELD_NORMAL)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
|
2022-03-22 23:59:20 +01:00
|
|
|
/* This is a dev command: fix the api if you need this! */
|
|
|
|
if (more_than_one)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "More than one channel");
|
2019-10-28 04:33:42 +01:00
|
|
|
|
2020-08-25 03:33:16 +02:00
|
|
|
msg = towire_channeld_feerates(NULL, *feerate,
|
2020-11-24 01:36:22 +01:00
|
|
|
feerate_min(cmd->ld, NULL),
|
|
|
|
feerate_max(cmd->ld, NULL),
|
|
|
|
try_get_feerate(cmd->ld->topology,
|
|
|
|
FEERATE_PENALTY));
|
2019-10-28 04:33:42 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
|
|
|
json_add_node_id(response, "id", id);
|
|
|
|
json_add_u32(response, "feerate", *feerate);
|
|
|
|
|
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command dev_feerate_command = {
|
|
|
|
"dev-feerate",
|
|
|
|
"developer",
|
|
|
|
json_dev_feerate,
|
|
|
|
"Set feerate for {id} to {feerate}"
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &dev_feerate_command);
|
2021-05-31 05:08:04 +02:00
|
|
|
|
|
|
|
#if EXPERIMENTAL_FEATURES
|
|
|
|
static void quiesce_reply(struct subd *channeld UNUSED,
|
|
|
|
const u8 *reply,
|
|
|
|
const int *fds UNUSED,
|
|
|
|
struct command *cmd)
|
|
|
|
{
|
|
|
|
struct json_stream *response;
|
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
|
|
|
was_pending(command_success(cmd, response));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *json_dev_quiesce(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct node_id *id;
|
|
|
|
struct peer *peer;
|
|
|
|
struct channel *channel;
|
|
|
|
const u8 *msg;
|
2022-03-22 23:59:20 +01:00
|
|
|
bool more_than_one;
|
2021-05-31 05:08:04 +02:00
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("id", param_node_id, &id),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
peer = peer_by_id(cmd->ld, id);
|
|
|
|
if (!peer)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
|
|
|
|
2022-03-22 23:59:20 +01:00
|
|
|
channel = peer_any_active_channel(peer, &more_than_one);
|
2021-05-31 05:08:04 +02:00
|
|
|
if (!channel || !channel->owner || channel->state != CHANNELD_NORMAL)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
|
2022-03-22 23:59:20 +01:00
|
|
|
/* This is a dev command: fix the api if you need this! */
|
|
|
|
if (more_than_one)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "More than one channel");
|
2021-05-31 05:08:04 +02:00
|
|
|
|
|
|
|
msg = towire_channeld_dev_quiesce(NULL);
|
|
|
|
subd_req(channel->owner, channel->owner, take(msg), -1, 0,
|
|
|
|
quiesce_reply, cmd);
|
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command dev_quiesce_command = {
|
|
|
|
"dev-quiesce",
|
|
|
|
"developer",
|
|
|
|
json_dev_quiesce,
|
|
|
|
"Initiate quiscence protocol with peer"
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &dev_quiesce_command);
|
|
|
|
#endif /* EXPERIMENTAL_FEATURES */
|
2019-10-28 04:33:42 +01:00
|
|
|
#endif /* DEVELOPER */
|