2018-08-23 01:27:25 +02:00
|
|
|
#include <bitcoin/feerate.h>
|
2018-03-07 01:06:07 +01:00
|
|
|
#include <bitcoin/script.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <closingd/gen_closing_wire.h>
|
|
|
|
#include <common/close_tx.h>
|
|
|
|
#include <common/initial_commit_tx.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <errno.h>
|
2018-04-25 14:37:38 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/chaintopology.h>
|
|
|
|
#include <lightningd/channel.h>
|
|
|
|
#include <lightningd/closing_control.h>
|
2018-07-23 04:23:03 +02:00
|
|
|
#include <lightningd/hsm_control.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/log.h>
|
|
|
|
#include <lightningd/options.h>
|
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/subd.h>
|
|
|
|
|
|
|
|
/* Is this better than the last tx we were holding? This can happen
|
|
|
|
* even without closingd misbehaving, if we have multiple,
|
|
|
|
* interrupted, rounds of negotiation. */
|
|
|
|
static bool better_closing_fee(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
|
|
|
const struct bitcoin_tx *tx)
|
|
|
|
{
|
closing_control: always prefer lower fee, not closest to ideal.
We had an intermittant test failure, where the fee we negotiated was
further from our ideal than the final commitment transaction. It worked
fine if the other side sent the mutual close first, but not if we sent
our unilateral close first.
ERROR: test_closing_different_fees (__main__.LightningDTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/test_lightningd.py", line 1319, in test_closing_different_fees
wait_for(lambda: p.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status'][1] == 'ONCHAIN:Tracking mutual close transaction')
File "tests/test_lightningd.py", line 74, in wait_for
raise ValueError("Error waiting for {}", success)
ValueError: ('Error waiting for {}', <function LightningDTests.test_closing_different_fees.<locals>.<lambda> at 0x7f4b43e31a60>)
Really, if we're prepared to negotiate it, we should be prepared to
accept it ourselves. Simply take the cheapest tx which is above our
minimum.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-04-12 05:03:54 +02:00
|
|
|
u64 weight, fee, last_fee, min_fee;
|
2018-08-23 01:27:25 +02:00
|
|
|
u32 min_feerate;
|
2018-02-20 21:59:09 +01:00
|
|
|
size_t i;
|
2018-08-23 01:27:25 +02:00
|
|
|
bool feerate_unknown;
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* Calculate actual fee (adds in eliminated outputs) */
|
|
|
|
fee = channel->funding_satoshi;
|
|
|
|
for (i = 0; i < tal_count(tx->output); i++)
|
|
|
|
fee -= tx->output[i].amount;
|
|
|
|
|
|
|
|
last_fee = channel->funding_satoshi;
|
2018-04-10 13:21:30 +02:00
|
|
|
for (i = 0; i < tal_count(channel->last_tx->output); i++)
|
2018-02-20 21:59:09 +01:00
|
|
|
last_fee -= channel->last_tx->output[i].amount;
|
|
|
|
|
|
|
|
log_debug(channel->log, "Their actual closing tx fee is %"PRIu64
|
|
|
|
" vs previous %"PRIu64, fee, last_fee);
|
|
|
|
|
|
|
|
/* Weight once we add in sigs. */
|
|
|
|
weight = measure_tx_weight(tx) + 74 * 2;
|
|
|
|
|
2018-08-23 01:27:25 +02:00
|
|
|
/* If we don't have a feerate estimate, this gives feerate_floor */
|
|
|
|
min_feerate = feerate_min(ld, &feerate_unknown);
|
|
|
|
|
|
|
|
min_fee = min_feerate * weight / 1000;
|
2018-02-20 21:59:09 +01:00
|
|
|
if (fee < min_fee) {
|
|
|
|
log_debug(channel->log, "... That's below our min %"PRIu64
|
|
|
|
" for weight %"PRIu64" at feerate %u",
|
2018-08-23 01:27:25 +02:00
|
|
|
min_fee, weight, min_feerate);
|
2018-02-20 21:59:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:27:25 +02:00
|
|
|
/* In case of a tie, prefer new over old: this covers the preference
|
|
|
|
* for a mutual close over a unilateral one. */
|
|
|
|
|
|
|
|
/* If we don't know the feerate, prefer higher fee. */
|
|
|
|
if (feerate_unknown)
|
|
|
|
return fee >= last_fee;
|
|
|
|
|
|
|
|
/* Otherwise prefer lower fee. */
|
closing_control: always prefer lower fee, not closest to ideal.
We had an intermittant test failure, where the fee we negotiated was
further from our ideal than the final commitment transaction. It worked
fine if the other side sent the mutual close first, but not if we sent
our unilateral close first.
ERROR: test_closing_different_fees (__main__.LightningDTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/test_lightningd.py", line 1319, in test_closing_different_fees
wait_for(lambda: p.rpc.listpeers(l1.info['id'])['peers'][0]['channels'][0]['status'][1] == 'ONCHAIN:Tracking mutual close transaction')
File "tests/test_lightningd.py", line 74, in wait_for
raise ValueError("Error waiting for {}", success)
ValueError: ('Error waiting for {}', <function LightningDTests.test_closing_different_fees.<locals>.<lambda> at 0x7f4b43e31a60>)
Really, if we're prepared to negotiate it, we should be prepared to
accept it ourselves. Simply take the cheapest tx which is above our
minimum.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-04-12 05:03:54 +02:00
|
|
|
return fee <= last_fee;
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_received_closing_signature(struct channel *channel,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature sig;
|
2018-02-20 21:59:09 +01:00
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_closing_received_signature(msg, msg, &sig, &tx)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "Bad closing_received_signature %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Make sure signature is correct! */
|
|
|
|
if (better_closing_fee(ld, channel, tx)) {
|
|
|
|
channel_set_last_tx(channel, tx, &sig);
|
|
|
|
/* TODO(cdecker) Selectively save updated fields to DB */
|
|
|
|
wallet_channel_save(ld->wallet, channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK, you can continue now. */
|
|
|
|
subd_send_msg(channel->owner,
|
|
|
|
take(towire_closing_received_signature_reply(channel)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_closing_complete(struct channel *channel, const u8 *msg)
|
|
|
|
{
|
2018-04-26 06:51:01 +02:00
|
|
|
if (!fromwire_closing_complete(msg)) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel, "Bad closing_complete %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
/* Don't report spurious failure when closingd exits. */
|
2018-08-09 02:25:29 +02:00
|
|
|
channel_set_owner(channel, NULL, false);
|
2018-04-26 06:51:01 +02:00
|
|
|
/* Clear any transient negotiation messages */
|
|
|
|
channel_set_billboard(channel, false, NULL);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* Retransmission only, ignore closing. */
|
|
|
|
if (channel->state == CLOSINGD_COMPLETE)
|
|
|
|
return;
|
|
|
|
|
2018-04-10 08:03:15 +02:00
|
|
|
/* Channel gets dropped to chain cooperatively. */
|
|
|
|
drop_to_chain(channel->peer->ld, channel, true);
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_set_state(channel, CLOSINGD_SIGEXCHANGE, CLOSINGD_COMPLETE);
|
|
|
|
}
|
|
|
|
|
2018-03-05 17:40:50 +01:00
|
|
|
static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds UNUSED)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
|
|
|
enum closing_wire_type t = fromwire_peektype(msg);
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_CLOSING_RECEIVED_SIGNATURE:
|
|
|
|
peer_received_closing_signature(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIRE_CLOSING_COMPLETE:
|
|
|
|
peer_closing_complete(sd->channel, msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We send these, not receive them */
|
|
|
|
case WIRE_CLOSING_INIT:
|
|
|
|
case WIRE_CLOSING_RECEIVED_SIGNATURE_REPLY:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_start_closingd(struct channel *channel,
|
2018-04-23 12:08:02 +02:00
|
|
|
const struct crypto_state *cs,
|
2018-02-20 21:59:09 +01:00
|
|
|
int peer_fd, int gossip_fd,
|
2018-04-23 12:08:02 +02:00
|
|
|
bool reconnected,
|
|
|
|
const u8 *channel_reestablish)
|
2018-02-20 21:59:09 +01:00
|
|
|
{
|
2018-03-07 01:06:07 +01:00
|
|
|
u8 *initmsg;
|
2018-08-23 01:27:25 +02:00
|
|
|
u32 feerate;
|
2018-02-20 21:59:09 +01:00
|
|
|
u64 minfee, startfee, feelimit;
|
|
|
|
u64 num_revocations;
|
|
|
|
u64 funding_msatoshi, our_msatoshi, their_msatoshi;
|
2018-07-23 04:23:03 +02:00
|
|
|
int hsmfd;
|
2018-02-20 21:59:09 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
if (!channel->remote_shutdown_scriptpubkey) {
|
2018-02-20 21:59:09 +01:00
|
|
|
channel_internal_error(channel,
|
2018-03-07 01:06:07 +01:00
|
|
|
"Can't start closing: no remote info");
|
2018-02-20 21:59:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
hsmfd = hsm_get_client_fd(ld, &channel->peer->id, channel->dbid,
|
|
|
|
HSM_CAP_SIGN_CLOSING_TX);
|
|
|
|
|
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_closingd",
|
2018-04-26 06:51:01 +02:00
|
|
|
channel, channel->log, true,
|
2018-02-20 21:59:09 +01:00
|
|
|
closing_wire_type_name, closing_msg,
|
|
|
|
channel_errmsg,
|
2018-02-23 06:53:47 +01:00
|
|
|
channel_set_billboard,
|
2018-02-20 21:59:09 +01:00
|
|
|
take(&peer_fd), take(&gossip_fd),
|
2018-07-23 04:23:03 +02:00
|
|
|
take(&hsmfd),
|
2018-08-09 02:25:29 +02:00
|
|
|
NULL),
|
|
|
|
false);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!channel->owner) {
|
|
|
|
log_unusual(channel->log, "Could not subdaemon closing: %s",
|
|
|
|
strerror(errno));
|
|
|
|
channel_fail_transient(channel, "Failed to subdaemon closing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node:
|
|
|
|
* - MUST set `fee_satoshis` less than or equal to the base
|
|
|
|
* fee of the final commitment transaction, as calculated in
|
|
|
|
* [BOLT #3](03-transactions.md#fee-calculation).
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
2018-12-15 01:09:45 +01:00
|
|
|
feelimit = commit_tx_base_fee_sat(channel->channel_info.feerate_per_kw[LOCAL],
|
|
|
|
0);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
2018-08-23 01:27:25 +02:00
|
|
|
/* Pick some value above slow feerate (or min possible if unknown) */
|
2018-12-15 01:09:45 +01:00
|
|
|
minfee = commit_tx_base_fee_sat(feerate_min(ld, NULL), 0);
|
2018-08-23 01:27:25 +02:00
|
|
|
|
|
|
|
/* If we can't determine feerate, start at half unilateral feerate. */
|
2018-08-24 04:22:48 +02:00
|
|
|
feerate = mutual_close_feerate(ld->topology);
|
2018-08-23 01:27:25 +02:00
|
|
|
if (!feerate) {
|
|
|
|
feerate = channel->channel_info.feerate_per_kw[LOCAL] / 2;
|
|
|
|
if (feerate < feerate_floor())
|
|
|
|
feerate = feerate_floor();
|
|
|
|
}
|
2018-12-15 01:09:45 +01:00
|
|
|
startfee = commit_tx_base_fee_sat(feerate, 0);
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
if (startfee > feelimit)
|
|
|
|
startfee = feelimit;
|
|
|
|
if (minfee > feelimit)
|
|
|
|
minfee = feelimit;
|
|
|
|
|
|
|
|
num_revocations
|
|
|
|
= revocations_received(&channel->their_shachain.chain);
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* Each node offering a signature:
|
|
|
|
* - MUST round each output down to whole satoshis.
|
2018-02-20 21:59:09 +01:00
|
|
|
*/
|
|
|
|
/* Convert unit */
|
|
|
|
funding_msatoshi = channel->funding_satoshi * 1000;
|
|
|
|
/* What is not ours is theirs */
|
|
|
|
our_msatoshi = channel->our_msatoshi;
|
|
|
|
their_msatoshi = funding_msatoshi - our_msatoshi;
|
|
|
|
initmsg = towire_closing_init(tmpctx,
|
|
|
|
cs,
|
|
|
|
&channel->funding_txid,
|
|
|
|
channel->funding_outnum,
|
|
|
|
channel->funding_satoshi,
|
2018-07-23 04:23:03 +02:00
|
|
|
&channel->local_funding_pubkey,
|
2018-02-20 21:59:09 +01:00
|
|
|
&channel->channel_info.remote_fundingkey,
|
|
|
|
channel->funder,
|
|
|
|
our_msatoshi / 1000, /* Rounds down */
|
|
|
|
their_msatoshi / 1000, /* Rounds down */
|
|
|
|
channel->our_config.dust_limit_satoshis,
|
|
|
|
minfee, feelimit, startfee,
|
2018-03-07 01:06:07 +01:00
|
|
|
p2wpkh_for_keyidx(tmpctx, ld,
|
|
|
|
channel->final_key_idx),
|
2018-02-20 21:59:09 +01:00
|
|
|
channel->remote_shutdown_scriptpubkey,
|
|
|
|
reconnected,
|
|
|
|
channel->next_index[LOCAL],
|
|
|
|
channel->next_index[REMOTE],
|
|
|
|
num_revocations,
|
2018-10-29 04:37:11 +01:00
|
|
|
channel_reestablish,
|
|
|
|
p2wpkh_for_keyidx(tmpctx, ld,
|
|
|
|
channel->final_key_idx));
|
2018-02-20 21:59:09 +01:00
|
|
|
|
|
|
|
/* We don't expect a response: it will give us feedback on
|
|
|
|
* signatures sent and received, then closing_complete. */
|
|
|
|
subd_send_msg(channel->owner, take(initmsg));
|
2018-05-26 15:47:02 +02:00
|
|
|
|
|
|
|
/* Now tell gossipd that we're closing and that neither direction should
|
|
|
|
* be used. */
|
|
|
|
if (channel->scid)
|
|
|
|
subd_send_msg(channel->peer->ld->gossip,
|
|
|
|
take(towire_gossip_local_channel_close(
|
|
|
|
tmpctx, channel->scid)));
|
2018-02-20 21:59:09 +01:00
|
|
|
}
|