2018-05-06 15:32:01 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <bitcoin/script.h>
|
2019-09-10 05:27:51 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <channeld/gen_channel_wire.h>
|
2019-09-10 04:23:27 +02:00
|
|
|
#include <common/features.h>
|
2019-09-22 04:07:43 +02:00
|
|
|
#include <common/gossip_constants.h>
|
2019-08-23 23:34:52 +02:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-05-06 15:32:01 +02:00
|
|
|
#include <common/memleak.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2018-05-06 15:32:01 +02:00
|
|
|
#include <common/timeout.h>
|
|
|
|
#include <common/utils.h>
|
2019-08-23 23:34:52 +02:00
|
|
|
#include <common/wallet_tx.h>
|
2018-08-17 07:06:35 +02:00
|
|
|
#include <common/wire_error.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <errno.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/channel_control.h>
|
|
|
|
#include <lightningd/closing_control.h>
|
|
|
|
#include <lightningd/hsm_control.h>
|
2019-08-23 23:34:52 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/log.h>
|
2020-04-01 05:53:22 +02:00
|
|
|
#include <lightningd/onion_message.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/subd.h>
|
2019-11-28 19:07:52 +01:00
|
|
|
#include <wire/gen_common_wire.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
msg = towire_channel_feerates(NULL, feerate,
|
|
|
|
feerate_min(ld, NULL),
|
|
|
|
feerate_max(ld, NULL));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
struct channel *channel = peer_active_channel(peer);
|
|
|
|
|
|
|
|
if (!channel)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* FIXME: We choose not to drop to chain if we can't contact
|
|
|
|
* peer. We *could* do so, however. */
|
|
|
|
try_update_feerates(ld, channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:08:01 +02:00
|
|
|
channel_set_state(channel, CHANNELD_AWAITING_LOCKIN, CHANNELD_NORMAL);
|
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);
|
2018-04-23 12:08:01 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_got_funding_locked(msg,
|
2018-02-20 21:59:09 +01:00
|
|
|
&next_per_commitment_point)) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channel_got_funding_locked %s",
|
|
|
|
tal_hex(channel, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel->remote_funding_locked) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"channel_got_funding_locked twice");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update_per_commit_point(channel, &next_per_commitment_point);
|
|
|
|
|
|
|
|
log_debug(channel->log, "Got funding_locked");
|
|
|
|
channel->remote_funding_locked = true;
|
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;
|
|
|
|
|
|
|
|
if (!fromwire_channel_got_announcement(msg,
|
|
|
|
&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;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_got_shutdown(channel, msg, &scriptpubkey)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "bad channel_got_shutdown %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Add to spec that we must allow repeated shutdown! */
|
2019-09-29 09:35:45 +02:00
|
|
|
tal_free(channel->shutdown_scriptpubkey[REMOTE]);
|
|
|
|
channel->shutdown_scriptpubkey[REMOTE] = scriptpubkey;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG`
|
|
|
|
* (pay to pubkey hash), OR
|
|
|
|
* 2. `OP_HASH160` `20` 20-bytes `OP_EQUAL` (pay to script hash), OR
|
|
|
|
* 3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), OR
|
|
|
|
* 4. `OP_0` `32` 32-bytes (version 0 pay to witness script hash)
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
*...
|
|
|
|
* - if the `scriptpubkey` is not in one of the above forms:
|
|
|
|
* - SHOULD fail the connection.
|
|
|
|
*/
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!is_p2pkh(scriptpubkey, NULL) && !is_p2sh(scriptpubkey, NULL)
|
|
|
|
&& !is_p2wpkh(scriptpubkey, NULL) && !is_p2wsh(scriptpubkey, NULL)) {
|
|
|
|
channel_fail_permanent(channel, "Bad shutdown scriptpubkey %s",
|
|
|
|
tal_hex(channel, scriptpubkey));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-04-03 09:08:53 +02:00
|
|
|
channel->state, CHANNELD_SHUTTING_DOWN);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* TODO(cdecker) Selectively save updated fields to DB */
|
|
|
|
wallet_channel_save(ld->wallet, channel);
|
|
|
|
}
|
|
|
|
|
2018-08-17 07:06:35 +02:00
|
|
|
static void channel_fail_fallen_behind(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
2019-09-10 05:27:51 +02:00
|
|
|
if (!fromwire_channel_fail_fallen_behind(channel, msg,
|
|
|
|
cast_const2(struct pubkey **,
|
|
|
|
&channel->future_per_commitment_point))) {
|
2018-08-17 07:06:35 +02:00
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channel_fail_fallen_behind %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 (!channel->option_static_remotekey) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad channel_fail_fallen_behind %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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. */
|
|
|
|
channel_fail_permanent(channel, "Awaiting unilateral close");
|
2018-08-17 07:06:35 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
static void peer_start_closingd_after_shutdown(struct channel *channel,
|
|
|
|
const u8 *msg,
|
|
|
|
const int *fds)
|
|
|
|
{
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
if (!fromwire_channel_shutdown_complete(tmpctx, msg, &pps)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "bad shutdown_complete: %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
2019-06-03 20:11:25 +02:00
|
|
|
per_peer_state_set_fds_arr(pps, fds);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* This sets channel->owner, closes down channeld. */
|
2019-06-03 20:11:25 +02:00
|
|
|
peer_start_closingd(channel, pps, false, NULL);
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_set_state(channel, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE);
|
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
json_add_string(response, "cancelled", "Channel open canceled by RPC(after fundchannel_complete)");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!fromwire_channel_send_error_reply(msg)) {
|
|
|
|
channel_internal_error(channel, "bad send_error_reply: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
forget(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void forget_channel(struct channel *channel, const char *why)
|
|
|
|
{
|
|
|
|
struct channel_id cid;
|
|
|
|
|
|
|
|
derive_channel_id(&cid, &channel->funding_txid,
|
|
|
|
channel->funding_outnum);
|
|
|
|
channel->error = towire_errorfmt(channel, &cid, "%s", why);
|
|
|
|
|
|
|
|
/* If the peer is connected, we let them know. Otherwise
|
|
|
|
* we just directly remove the channel */
|
|
|
|
if (channel->owner)
|
|
|
|
subd_send_msg(channel->owner,
|
|
|
|
take(towire_channel_send_error(NULL, why)));
|
|
|
|
else
|
|
|
|
forget(channel);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
|
|
|
|
{
|
|
|
|
enum channel_wire_type t = fromwire_peektype(msg);
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_CHANNEL_SENDING_COMMITSIG:
|
|
|
|
peer_sending_commitsig(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNEL_GOT_COMMITSIG:
|
|
|
|
peer_got_commitsig(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNEL_GOT_REVOKE:
|
|
|
|
peer_got_revoke(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNEL_GOT_FUNDING_LOCKED:
|
|
|
|
peer_got_funding_locked(sd->channel, msg);
|
|
|
|
break;
|
2019-04-25 13:58:07 +02:00
|
|
|
case WIRE_CHANNEL_GOT_ANNOUNCEMENT:
|
|
|
|
peer_got_announcement(sd->channel, msg);
|
|
|
|
break;
|
2018-02-20 21:59:09 +01:00
|
|
|
case WIRE_CHANNEL_GOT_SHUTDOWN:
|
|
|
|
peer_got_shutdown(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
case WIRE_CHANNEL_SHUTDOWN_COMPLETE:
|
2019-05-04 07:53:13 +02:00
|
|
|
/* We expect 3 fds. */
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fds)
|
2019-05-04 07:53:13 +02:00
|
|
|
return 3;
|
2018-02-20 21:59:09 +01:00
|
|
|
peer_start_closingd_after_shutdown(sd->channel, msg, fds);
|
|
|
|
break;
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_CHANNEL_FAIL_FALLEN_BEHIND:
|
|
|
|
channel_fail_fallen_behind(sd->channel, msg);
|
|
|
|
break;
|
2019-08-23 23:34:52 +02:00
|
|
|
case WIRE_CHANNEL_SEND_ERROR_REPLY:
|
|
|
|
handle_error_channel(sd->channel, msg);
|
|
|
|
break;
|
2020-04-01 05:53:22 +02:00
|
|
|
#if EXPERIMENTAL_FEATURES
|
2020-04-01 05:53:09 +02:00
|
|
|
case WIRE_GOT_ONIONMSG_TO_US:
|
2020-04-01 05:53:22 +02:00
|
|
|
handle_onionmsg_to_us(sd->channel, msg);
|
|
|
|
break;
|
2020-04-01 05:53:09 +02:00
|
|
|
case WIRE_GOT_ONIONMSG_FORWARD:
|
2020-04-01 05:53:22 +02:00
|
|
|
handle_onionmsg_forward(sd->channel, msg);
|
2020-04-01 05:53:09 +02:00
|
|
|
break;
|
2020-04-01 05:53:22 +02:00
|
|
|
#else
|
|
|
|
case WIRE_GOT_ONIONMSG_TO_US:
|
|
|
|
case WIRE_GOT_ONIONMSG_FORWARD:
|
|
|
|
#endif
|
2018-02-20 21:59:09 +01:00
|
|
|
/* And we never get these from channeld. */
|
|
|
|
case WIRE_CHANNEL_INIT:
|
2019-02-26 17:57:19 +01:00
|
|
|
case WIRE_CHANNEL_FUNDING_DEPTH:
|
2018-02-20 21:59:09 +01:00
|
|
|
case WIRE_CHANNEL_OFFER_HTLC:
|
|
|
|
case WIRE_CHANNEL_FULFILL_HTLC:
|
|
|
|
case WIRE_CHANNEL_FAIL_HTLC:
|
|
|
|
case WIRE_CHANNEL_GOT_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNEL_GOT_REVOKE_REPLY:
|
|
|
|
case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNEL_SEND_SHUTDOWN:
|
|
|
|
case WIRE_CHANNEL_DEV_REENABLE_COMMIT:
|
|
|
|
case WIRE_CHANNEL_FEERATES:
|
2019-03-09 21:29:39 +01:00
|
|
|
case WIRE_CHANNEL_SPECIFIC_FEERATES:
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CHANNEL_DEV_MEMLEAK:
|
2020-04-01 05:53:09 +02:00
|
|
|
case WIRE_SEND_ONIONMSG:
|
|
|
|
/* Replies go to requests. */
|
2018-02-20 21:59:09 +01:00
|
|
|
case WIRE_CHANNEL_OFFER_HTLC_REPLY:
|
|
|
|
case WIRE_CHANNEL_DEV_REENABLE_COMMIT_REPLY:
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CHANNEL_DEV_MEMLEAK_REPLY:
|
2019-08-23 23:34:52 +02:00
|
|
|
case WIRE_CHANNEL_SEND_ERROR:
|
2018-02-20 21:59:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:07:52 +01:00
|
|
|
switch ((enum common_wire_type)t) {
|
|
|
|
#if DEVELOPER
|
|
|
|
case WIRE_CUSTOMMSG_IN:
|
2019-12-05 00:11:28 +01:00
|
|
|
handle_custommsg_in(sd->ld, sd->node_id, msg);
|
2019-11-28 19:07:52 +01:00
|
|
|
break;
|
|
|
|
#else
|
|
|
|
case WIRE_CUSTOMMSG_IN:
|
|
|
|
#endif
|
|
|
|
/* We send these. */
|
|
|
|
case WIRE_CUSTOMMSG_OUT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:26:30 +02:00
|
|
|
void peer_start_channeld(struct channel *channel,
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps,
|
2018-02-20 21:59:09 +01:00
|
|
|
const u8 *funding_signed,
|
|
|
|
bool reconnected)
|
|
|
|
{
|
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
|
|
|
|
| HSM_CAP_SIGN_REMOTE_TX);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
channel_set_owner(channel,
|
|
|
|
new_channel_subd(ld,
|
2018-02-20 21:59:09 +01:00
|
|
|
"lightning_channeld", channel,
|
2019-11-17 12:40:33 +01:00
|
|
|
&channel->peer->id,
|
2018-04-26 06:51:01 +02:00
|
|
|
channel->log, true,
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_wire_type_name,
|
|
|
|
channel_msg,
|
|
|
|
channel_errmsg,
|
2018-02-23 06:53:47 +01:00
|
|
|
channel_set_billboard,
|
2019-06-03 20:11:25 +02:00
|
|
|
take(&pps->peer_fd),
|
|
|
|
take(&pps->gossip_fd),
|
|
|
|
take(&pps->gossip_store_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,
|
|
|
|
"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-01-05 16:52:34 +01:00
|
|
|
if (!wallet_remote_ann_sigs_load(tmpctx, channel->peer->ld->wallet, channel->dbid,
|
2019-05-14 11:36:05 +02:00
|
|
|
&remote_ann_node_sig, &remote_ann_bitcoin_sig)) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"Could not load remote announcement signatures");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:49:43 +02:00
|
|
|
pbases = wallet_penalty_base_load_for_channel(
|
|
|
|
tmpctx, channel->peer->ld->wallet, channel->dbid);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
initmsg = towire_channel_init(tmpctx,
|
2019-09-25 22:38:45 +02:00
|
|
|
chainparams,
|
2020-04-03 02:03:59 +02:00
|
|
|
ld->our_features,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->funding_txid,
|
|
|
|
channel->funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->funding,
|
2019-02-26 17:57:19 +01:00
|
|
|
channel->minimum_depth,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->our_config,
|
|
|
|
&channel->channel_info.their_config,
|
2019-12-12 18:18:25 +01:00
|
|
|
channel->channel_info.fee_states,
|
2018-08-23 01:27:25 +02:00
|
|
|
feerate_min(ld, NULL),
|
|
|
|
feerate_max(ld, NULL),
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->last_sig,
|
2019-06-03 20:11:25 +02:00
|
|
|
pps,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->channel_info.remote_fundingkey,
|
2018-07-09 13:17:59 +02:00
|
|
|
&channel->channel_info.theirbase,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->channel_info.remote_per_commit,
|
|
|
|
&channel->channel_info.old_remote_per_commit,
|
2019-09-09 18:11:24 +02:00
|
|
|
channel->opener,
|
2019-02-21 01:01:33 +01:00
|
|
|
channel->feerate_base,
|
|
|
|
channel->feerate_ppm,
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->our_msat,
|
2018-07-23 04:23:03 +02:00
|
|
|
&channel->local_basepoints,
|
|
|
|
&channel->local_funding_pubkey,
|
2018-02-20 21:59:09 +01:00
|
|
|
&ld->id,
|
|
|
|
&channel->peer->id,
|
2018-05-17 06:46:22 +02:00
|
|
|
cfg->commit_time_ms,
|
2018-02-20 21:59:09 +01:00
|
|
|
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,
|
2020-04-03 05:14:07 +02:00
|
|
|
htlcs,
|
2018-02-20 21:59:09 +01:00
|
|
|
channel->scid != NULL,
|
|
|
|
channel->remote_funding_locked,
|
2019-06-03 14:05:18 +02:00
|
|
|
&scid,
|
2018-02-20 21:59:09 +01:00
|
|
|
reconnected,
|
2018-03-07 01:06:07 +01:00
|
|
|
channel->state == CHANNELD_SHUTTING_DOWN,
|
2019-09-29 09:35:45 +02:00
|
|
|
channel->shutdown_scriptpubkey[REMOTE] != NULL,
|
|
|
|
channel->shutdown_scriptpubkey[LOCAL],
|
2018-02-20 21:59:09 +01:00
|
|
|
channel->channel_flags,
|
2018-04-26 06:51:02 +02:00
|
|
|
funding_signed,
|
2018-08-17 06:16:34 +02:00
|
|
|
reached_announce_depth,
|
2018-12-10 02:03:42 +01:00
|
|
|
&last_remote_per_commit_secret,
|
2020-04-03 02:03:59 +02:00
|
|
|
channel->peer->their_features,
|
2019-05-14 11:36:05 +02:00
|
|
|
channel->remote_upfront_shutdown_script,
|
|
|
|
remote_ann_node_sig,
|
2019-08-02 05:31:00 +02:00
|
|
|
remote_ann_bitcoin_sig,
|
2019-09-10 04:23:27 +02:00
|
|
|
/* Set at channel open, even if not
|
|
|
|
* negotiated now! */
|
2019-09-18 03:05:05 +02:00
|
|
|
channel->option_static_remotekey,
|
2020-01-23 06:48:42 +01:00
|
|
|
IFDEV(ld->dev_fast_gossip, false),
|
2020-05-07 02:49:43 +02:00
|
|
|
IFDEV(dev_fail_process_onionpacket, false),
|
|
|
|
pbases);
|
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
|
|
|
|
|
|
|
/* On restart, feerate might not be what we expect: adjust now. */
|
2019-09-09 18:11:24 +02:00
|
|
|
if (channel->opener == LOCAL)
|
2018-08-22 04:33:32 +02:00
|
|
|
try_update_feerates(ld, channel);
|
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,
|
2018-04-23 12:08:01 +02:00
|
|
|
struct channel *channel,
|
2018-05-17 07:08:11 +02:00
|
|
|
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-05-17 07:08:11 +02:00
|
|
|
/* If not awaiting lockin/announce, it doesn't care any more */
|
|
|
|
if (channel->state != CHANNELD_AWAITING_LOCKIN
|
|
|
|
&& channel->state != CHANNELD_NORMAL) {
|
2018-04-23 12:08:01 +02:00
|
|
|
log_debug(channel->log,
|
2019-02-26 17:57:19 +01:00
|
|
|
"Funding tx %s confirmed, but peer in state %s",
|
|
|
|
txidstr, channel_state_name(channel));
|
2018-04-23 12:08:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
subd_send_msg(channel->owner,
|
2019-02-26 17:57:19 +01:00
|
|
|
take(towire_channel_funding_depth(NULL, channel->scid,
|
2018-05-17 07:08:11 +02:00
|
|
|
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)
|
|
|
|
{
|
2018-05-07 01:01:49 +02:00
|
|
|
u32 max_funding_unconfirmed = ld->max_funding_unconfirmed;
|
2018-05-06 15:32:01 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A non-funding node (fundee):
|
|
|
|
* - SHOULD forget the channel if it does not see the
|
2019-11-22 05:41:25 +01:00
|
|
|
* correct funding transaction after a reasonable timeout.
|
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;
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
list_for_each (&peer->channels, channel, list)
|
|
|
|
if (is_fundee_should_forget(ld, channel, block_height)) {
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&to_forget, channel);
|
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,
|
|
|
|
&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:31:30 +02:00
|
|
|
static struct channel *find_channel_by_id(const struct peer *peer,
|
|
|
|
const struct channel_id *cid)
|
|
|
|
{
|
|
|
|
struct channel *c;
|
|
|
|
|
|
|
|
list_for_each(&peer->channels, c, list) {
|
|
|
|
struct channel_id this_cid;
|
|
|
|
|
|
|
|
derive_channel_id(&this_cid,
|
|
|
|
&c->funding_txid, c->funding_outnum);
|
|
|
|
if (channel_id_eq(&this_cid, cid))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
cancel = find_channel_by_id(peer, &cc->cid);
|
|
|
|
if (!cancel)
|
|
|
|
return;
|
2019-08-23 23:34:52 +02:00
|
|
|
|
|
|
|
if (txout != NULL) {
|
|
|
|
for (size_t i = 0; i < tal_count(cancel->forgets); i++)
|
|
|
|
was_pending(command_fail(cancel->forgets[i], LIGHTNINGD,
|
|
|
|
"The funding transaction has been broadcast, "
|
|
|
|
"please consider `close` or `dev-fail`! "));
|
|
|
|
tal_free(cancel->forgets);
|
|
|
|
cancel->forgets = tal_arr(cancel, struct command *, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-08-23 23:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *cancel_channel_before_broadcast(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
struct peer *peer,
|
|
|
|
const jsmntok_t *cidtok)
|
|
|
|
{
|
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);
|
2019-08-23 23:34:52 +02:00
|
|
|
|
2019-09-19 08:44:24 +02:00
|
|
|
cc->peer = peer->id;
|
2019-08-23 23:34:52 +02:00
|
|
|
if (!cidtok) {
|
2019-09-19 08:31:30 +02:00
|
|
|
struct channel *channel;
|
|
|
|
|
|
|
|
cancel_channel = NULL;
|
2019-08-23 23:34:52 +02:00
|
|
|
list_for_each(&peer->channels, channel, list) {
|
|
|
|
if (cancel_channel) {
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Multiple channels:"
|
|
|
|
" please specify channel_id");
|
|
|
|
}
|
|
|
|
cancel_channel = channel;
|
|
|
|
}
|
|
|
|
if (!cancel_channel)
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"No channels matching that peer_id");
|
2019-09-19 08:44:24 +02:00
|
|
|
derive_channel_id(&cc->cid,
|
|
|
|
&cancel_channel->funding_txid,
|
|
|
|
cancel_channel->funding_outnum);
|
2019-08-23 23:34:52 +02:00
|
|
|
} else {
|
2019-09-19 08:44:24 +02:00
|
|
|
if (!json_tok_channel_id(buffer, cidtok, &cc->cid))
|
2019-08-23 23:34:52 +02:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Invalid channel_id parameter.");
|
|
|
|
|
2019-09-19 08:44:24 +02:00
|
|
|
cancel_channel = find_channel_by_id(peer, &cc->cid);
|
2019-08-23 23:34:52 +02:00
|
|
|
if (!cancel_channel)
|
2019-09-19 08:31:30 +02:00
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
2019-08-23 23:34:52 +02:00
|
|
|
"Channel ID not found: '%.*s'",
|
|
|
|
cidtok->end - cidtok->start,
|
|
|
|
buffer + cidtok->start);
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:11:24 +02:00
|
|
|
if (cancel_channel->opener == REMOTE)
|
2019-12-12 01:46:08 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Cannot cancel channel that was "
|
|
|
|
"initiated by peer");
|
|
|
|
|
2019-08-23 23:34:52 +02:00
|
|
|
/* Check if we broadcast the transaction. (We store the transaction type into DB
|
|
|
|
* before broadcast). */
|
|
|
|
enum wallet_tx_type type;
|
2020-01-05 16:52:34 +01:00
|
|
|
if (wallet_transaction_type(cmd->ld->wallet,
|
2019-08-23 23:34:52 +02:00
|
|
|
&cancel_channel->funding_txid,
|
|
|
|
&type))
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Has the funding transaction been broadcast? "
|
|
|
|
"Please use `close` or `dev-fail` instead.");
|
|
|
|
|
|
|
|
if (channel_has_htlc_out(cancel_channel) ||
|
|
|
|
channel_has_htlc_in(cancel_channel)) {
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"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.");
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
* 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,
|
|
|
|
&cancel_channel->funding_txid,
|
|
|
|
cancel_channel->funding_outnum,
|
|
|
|
process_check_funding_broadcast,
|
|
|
|
notleak(cc));
|
2019-08-23 23:34:52 +02:00
|
|
|
return command_still_pending(cmd);
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
channel = peer_active_channel(peer);
|
|
|
|
if (!channel || !channel->owner || channel->state != CHANNELD_NORMAL)
|
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
|
|
|
|
|
|
|
|
msg = towire_channel_feerates(NULL, *feerate,
|
|
|
|
feerate_min(cmd->ld, NULL),
|
|
|
|
feerate_max(cmd->ld, NULL));
|
|
|
|
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);
|
|
|
|
#endif /* DEVELOPER */
|