Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions closingd/simpleclosed.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <common/per_peer_state.h>
#include <common/read_peer_msg.h>
#include <common/shutdown_scriptpubkey.h>
#include <common/simple_close_weight.h>
#include <common/status.h>
#include <common/subdaemon.h>
#include <common/utils.h>
Expand All @@ -34,11 +35,6 @@
#define PEER_FD 3
#define HSM_FD 4

/* Approx weight of a simple-close tx with both outputs (vbytes * 4 for weight).
* Input: 41vb, witness: ~222wu/4=55.5vb, outputs: ~65vb each, overhead: 11vb
* Total ~236 vbytes = ~704 weight + witness ~222 = ~926wu, round to 900. */
#define SIMPLE_CLOSE_WEIGHT 900

static const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES)
{
u8 *msg;
Expand All @@ -57,13 +53,13 @@ static const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES)
static struct per_peer_state *pps;

/* Tell master we got peer's closing_sig for our tx; block for txid reply. */
static struct bitcoin_txid master_got_sig(struct bitcoin_tx *tx,
const struct bitcoin_signature *sig)
static struct bitcoin_txid master_got_sig_ourtx(const struct bitcoin_tx *tx,
const struct bitcoin_signature *sig)
{
struct bitcoin_txid txid;
u8 *msg;

msg = towire_simpleclosed_got_sig(tmpctx, tx, sig);
msg = towire_simpleclosed_our_closing_tx(tmpctx, tx, sig);
if (!wire_sync_write(REQ_FD, take(msg)))
status_failed(STATUS_FAIL_MASTER_IO,
"Writing got_sig: %s",
Expand All @@ -74,7 +70,7 @@ static struct bitcoin_txid master_got_sig(struct bitcoin_tx *tx,
status_failed(STATUS_FAIL_MASTER_IO,
"Reading got_sig_reply: %s",
strerror(errno));
if (!fromwire_simpleclosed_got_sig_reply(msg, &txid))
if (!fromwire_simpleclosed_our_closing_tx_reply(msg, &txid))
status_failed(STATUS_FAIL_MASTER_IO,
"Bad got_sig_reply: %s",
tal_hex(tmpctx, msg));
Expand Down Expand Up @@ -462,8 +458,8 @@ static struct bitcoin_tx *handle_closing_complete(

/* Tell master to validate, store, and handle broadcast. */
wire_sync_write(REQ_FD,
take(towire_simpleclosed_closee_broadcast(NULL,
chosen_tx, &their_sig)));
take(towire_simpleclosed_their_closing_tx(NULL,
chosen_tx, &their_sig)));
return chosen_tx;
}

Expand Down Expand Up @@ -686,7 +682,7 @@ int main(int argc, char *argv[])
local_wallet_index, local_wallet_ext_key, local_sat,
remote_sat, dust_limit, sent_closer_script, sent_closee_script,
sent_fee, sent_locktime, sent_tlvs, msg, &their_sig);
txid = master_got_sig(closing_tx, &their_sig);
txid = master_got_sig_ourtx(closing_tx, &their_sig);
status_debug("Closer tx stored by master: %s",
fmt_bitcoin_txid(tmpctx, &txid));
got_our_sig = true;
Expand Down
16 changes: 8 additions & 8 deletions closingd/simpleclosed_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ msgdata,simpleclosed_init,opener,enum side,

# simpleclosed tells master it got a valid closing_sig for our closing_complete;
# master should broadcast this tx.
msgtype,simpleclosed_got_sig,3002
msgdata,simpleclosed_got_sig,tx,bitcoin_tx,
msgdata,simpleclosed_got_sig,sig,bitcoin_signature,
msgtype,simpleclosed_our_closing_tx,3002
msgdata,simpleclosed_our_closing_tx,tx,bitcoin_tx,
msgdata,simpleclosed_our_closing_tx,their_sig,bitcoin_signature,

# Master replies with the txid (after storing/broadcasting).
msgtype,simpleclosed_got_sig_reply,3102
msgdata,simpleclosed_got_sig_reply,closing_txid,bitcoin_txid,
msgtype,simpleclosed_our_closing_tx_reply,3102
msgdata,simpleclosed_our_closing_tx_reply,closing_txid,bitcoin_txid,

# simpleclosed tells master it signed the peer's closing tx (as the closee);
# master validates and stores, drop_to_chain handles broadcast.
msgtype,simpleclosed_closee_broadcast,3003
msgdata,simpleclosed_closee_broadcast,tx,bitcoin_tx,
msgdata,simpleclosed_closee_broadcast,sig,bitcoin_signature,
msgtype,simpleclosed_their_closing_tx,3003
msgdata,simpleclosed_their_closing_tx,tx,bitcoin_tx,
msgdata,simpleclosed_their_closing_tx,their_sig,bitcoin_signature,

# Negotiations complete, exiting.
msgtype,simpleclosed_complete,3004
Expand Down
3 changes: 2 additions & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \
common/hsm_version.h \
common/htlc.h \
common/jsonrpc_errors.h \
common/overflows.h
common/overflows.h \
common/simple_close_weight.h

COMMON_HEADERS_GEN := common/htlc_state_names_gen.h common/status_wiregen.h common/peer_status_wiregen.h common/scb_wiregen.h common/gossip_store_wiregen.h

Expand Down
12 changes: 12 additions & 0 deletions common/simple_close_weight.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef LIGHTNING_COMMON_SIMPLE_CLOSE_WEIGHT_H
#define LIGHTNING_COMMON_SIMPLE_CLOSE_WEIGHT_H
#include "config.h"
/* When lightningd checks the closing tx, it must use the same weight
* approximation as simpleclosed, otherwise it might reject it, so we share
* this constant. */

/* Approx weight of a simple-close tx with both outputs (vbytes * 4 for weight).
* Input: 41vb, witness: ~222wu/4=55.5vb, outputs: ~65vb each, overhead: 11vb
* Total ~236 vbytes = ~704 weight + witness ~222 = ~926wu, round to 900. */
#define SIMPLE_CLOSE_WEIGHT 900
#endif /* LIGHTNING_COMMON_SIMPLE_CLOSE_WEIGHT_H */
9 changes: 0 additions & 9 deletions common/test/run-htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_u8_array */
void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); }
/* Generated stub for memleak_add_helper_ */
void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED,
const tal_t *)){ }
/* Generated stub for memleak_scan_htable */
void memleak_scan_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED)
{ fprintf(stderr, "memleak_scan_htable called!\n"); abort(); }
/* Generated stub for notleak_ */
void *notleak_(void *ptr UNNEEDED, bool plus_children UNNEEDED)
{ fprintf(stderr, "notleak_ called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
Expand Down
9 changes: 0 additions & 9 deletions common/test/run-route-infloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
void *record UNNEEDED, struct tlv_field **fields UNNEEDED,
const u64 *extra_types UNNEEDED, size_t *err_off UNNEEDED, u64 *err_type UNNEEDED)
{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); }
/* Generated stub for memleak_add_helper_ */
void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED,
const tal_t *)){ }
/* Generated stub for memleak_scan_htable */
void memleak_scan_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED)
{ fprintf(stderr, "memleak_scan_htable called!\n"); abort(); }
/* Generated stub for notleak_ */
void *notleak_(void *ptr UNNEEDED, bool plus_children UNNEEDED)
{ fprintf(stderr, "notleak_ called!\n"); abort(); }
/* Generated stub for sciddir_or_pubkey_from_node_id */
bool sciddir_or_pubkey_from_node_id(struct sciddir_or_pubkey *sciddpk UNNEEDED,
const struct node_id *node_id UNNEEDED)
Expand Down
23 changes: 23 additions & 0 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,29 @@ static struct bitcoin_tx *sign_and_send_last(const tal_t *ctx,
return tx;
}

/* Normally we only sign and broadcast our last_tx, but in the case of
* simple close, we want to broadcast theirs, but we don't bother
* saving it. We'll close the channel if/when we see it onchain. */
void sign_and_broadcast_their_closing(struct channel *channel,
struct bitcoin_tx *tx,
const struct bitcoin_signature *their_sig)
{
struct lightningd *ld = channel->peer->ld;
struct bitcoin_tx *signed_tx;

/* We shouldn't get here, but in case we do. */
if (channel->withheld) {
log_broken(channel->log,
"Withheld channel: should not have mutual close!");
return;
}

signed_tx = sign_last_tx(NULL, channel, tx, their_sig);
broadcast_tx(channel, ld->topology, channel, take(signed_tx),
cmd_id_from_close_command(tmpctx, ld, channel),
false, 0, NULL, NULL, NULL);
}

/* FIXME: reorder! */
static enum watch_result funding_spent(struct channel *channel,
const struct bitcoin_tx *tx,
Expand Down
5 changes: 5 additions & 0 deletions lightningd/peer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ void drop_to_chain(struct lightningd *ld, struct channel *channel,
bool cooperative,
const struct bitcoin_tx *unilateral_tx);

/* Special case of sending their mutual close */
void sign_and_broadcast_their_closing(struct channel *channel,
struct bitcoin_tx *tx,
const struct bitcoin_signature *their_sig);

void update_channel_from_inflight(struct lightningd *ld,
struct channel *channel,
const struct channel_inflight *inflight,
Expand Down
Loading
Loading