From 865c9e9567aeb554f00013af782dc243df072383 Mon Sep 17 00:00:00 2001 From: niftynei Date: Fri, 11 Sep 2020 14:14:14 -0500 Subject: [PATCH] df-channeld: broadcast funding tx, once we get tx_sigs from peer We broadcast the funding tx, as soon as we get the tx_sigs from the peer, and check that it's valid --- channeld/channeld.c | 8 ++- channeld/channeld_wire.csv | 4 ++ channeld/channeld_wiregen.c | 25 ++++++++- channeld/channeld_wiregen.h | 8 ++- lightningd/channel_control.c | 100 +++++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+), 4 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index ac9d7459e..5c4584cff 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -2049,8 +2049,11 @@ static void handle_tx_sigs(struct peer *peer, const u8 *msg) "Unable to extract funding_tx from finalized PSBT %s", type_to_string(tmpctx, struct wally_psbt, peer->psbt)); - /* FIXME: when a channel gets locked, check if there's a PSBT - * or open_channel hanging out for it! */ + + /* We need the peer controller to broadcast the tx for us */ + wire_sync_write(MASTER_FD, + take(towire_channeld_funding_tx(NULL, wtx))); + peer->psbt = tal_free(peer->psbt); } #endif /* EXPERIMENTAL_FEATURES */ @@ -3256,6 +3259,7 @@ static void req_in(struct peer *peer, const u8 *msg) case WIRE_CHANNELD_DEV_MEMLEAK: #endif /* DEVELOPER */ case WIRE_CHANNELD_INIT: + case WIRE_CHANNELD_FUNDING_TX: case WIRE_CHANNELD_OFFER_HTLC_REPLY: case WIRE_CHANNELD_SENDING_COMMITSIG: case WIRE_CHANNELD_GOT_COMMITSIG: diff --git a/channeld/channeld_wire.csv b/channeld/channeld_wire.csv index 3d88d2c0f..fdaa6301b 100644 --- a/channeld/channeld_wire.csv +++ b/channeld/channeld_wire.csv @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -74,6 +75,9 @@ msgdata,channeld_init,num_penalty_bases,u32, msgdata,channeld_init,pbases,penalty_base,num_penalty_bases msgdata,channeld_init,psbt,wally_psbt, +msgtype,channeld_funding_tx,1010 +msgdata,channeld_funding_tx,funding_tx,wally_tx, + # master->channeld funding hit new depth(funding locked if >= lock depth) msgtype,channeld_funding_depth,1002 msgdata,channeld_funding_depth,short_channel_id,?short_channel_id, diff --git a/channeld/channeld_wiregen.c b/channeld/channeld_wiregen.c index 4e9dd1b44..f168f1807 100644 --- a/channeld/channeld_wiregen.c +++ b/channeld/channeld_wiregen.c @@ -21,6 +21,7 @@ const char *channeld_wire_name(int e) switch ((enum channeld_wire)e) { case WIRE_CHANNELD_INIT: return "WIRE_CHANNELD_INIT"; + case WIRE_CHANNELD_FUNDING_TX: return "WIRE_CHANNELD_FUNDING_TX"; case WIRE_CHANNELD_FUNDING_DEPTH: return "WIRE_CHANNELD_FUNDING_DEPTH"; case WIRE_CHANNELD_OFFER_HTLC: return "WIRE_CHANNELD_OFFER_HTLC"; case WIRE_CHANNELD_OFFER_HTLC_REPLY: return "WIRE_CHANNELD_OFFER_HTLC_REPLY"; @@ -59,6 +60,7 @@ bool channeld_wire_is_defined(u16 type) { switch ((enum channeld_wire)type) { case WIRE_CHANNELD_INIT:; + case WIRE_CHANNELD_FUNDING_TX:; case WIRE_CHANNELD_FUNDING_DEPTH:; case WIRE_CHANNELD_OFFER_HTLC:; case WIRE_CHANNELD_OFFER_HTLC_REPLY:; @@ -299,6 +301,27 @@ bool fromwire_channeld_init(const tal_t *ctx, const void *p, const struct chainp return cursor != NULL; } +/* WIRE: CHANNELD_FUNDING_TX */ +u8 *towire_channeld_funding_tx(const tal_t *ctx, const struct wally_tx *funding_tx) +{ + u8 *p = tal_arr(ctx, u8, 0); + + towire_u16(&p, WIRE_CHANNELD_FUNDING_TX); + towire_wally_tx(&p, funding_tx); + + return memcheck(p, tal_count(p)); +} +bool fromwire_channeld_funding_tx(const tal_t *ctx, const void *p, struct wally_tx **funding_tx) +{ + const u8 *cursor = p; + size_t plen = tal_count(p); + + if (fromwire_u16(&cursor, &plen) != WIRE_CHANNELD_FUNDING_TX) + return false; + *funding_tx = fromwire_wally_tx(ctx, &cursor, &plen); + return cursor != NULL; +} + /* WIRE: CHANNELD_FUNDING_DEPTH */ /* master->channeld funding hit new depth(funding locked if >= lock depth) */ u8 *towire_channeld_funding_depth(const tal_t *ctx, const struct short_channel_id *short_channel_id, u32 depth) @@ -1188,4 +1211,4 @@ bool fromwire_send_onionmsg(const tal_t *ctx, const void *p, u8 onion[1366], str } return cursor != NULL; } -// SHA256STAMP:8ba694ae5e240ca5622d6860d1fa2b5116e44b2b8d5bfe0a77d88d4aab979906 +// SHA256STAMP:5614b61d80352e94f974d018c391102579d76c5db11648d18ed0dd198ab6838a diff --git a/channeld/channeld_wiregen.h b/channeld/channeld_wiregen.h index b7db74950..5cb7899cf 100644 --- a/channeld/channeld_wiregen.h +++ b/channeld/channeld_wiregen.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ enum channeld_wire { /* Begin! (passes gossipd-client fd) */ WIRE_CHANNELD_INIT = 1000, + WIRE_CHANNELD_FUNDING_TX = 1010, /* master->channeld funding hit new depth(funding locked if >= lock depth) */ WIRE_CHANNELD_FUNDING_DEPTH = 1002, /* Tell channel to offer this htlc */ @@ -93,6 +95,10 @@ bool channeld_wire_is_defined(u16 type); u8 *towire_channeld_init(const tal_t *ctx, const struct chainparams *chainparams, const struct feature_set *our_features, const struct channel_id *channel_id, const struct bitcoin_txid *funding_txid, u16 funding_txout, struct amount_sat funding_satoshi, u32 minimum_depth, const struct channel_config *our_config, const struct channel_config *their_config, const struct fee_states *fee_states, u32 feerate_min, u32 feerate_max, u32 feerate_penalty, const struct bitcoin_signature *first_commit_sig, const struct per_peer_state *per_peer_state, const struct pubkey *remote_fundingkey, const struct basepoints *remote_basepoints, const struct pubkey *remote_per_commit, const struct pubkey *old_remote_per_commit, enum side opener, u32 fee_base, u32 fee_proportional, struct amount_msat local_msatoshi, const struct basepoints *our_basepoints, const struct pubkey *our_funding_pubkey, const struct node_id *local_node_id, const struct node_id *remote_node_id, u32 commit_msec, u16 cltv_delta, bool last_was_revoke, const struct changed_htlc *last_sent_commit, u64 next_index_local, u64 next_index_remote, u64 revocations_received, u64 next_htlc_id, const struct existing_htlc **htlcs, bool local_funding_locked, bool remote_funding_locked, const struct short_channel_id *funding_short_id, bool reestablish, bool send_shutdown, bool remote_shutdown_received, const u8 *final_scriptpubkey, u8 flags, const u8 *init_peer_pkt, bool reached_announce_depth, const struct secret *last_remote_secret, const u8 *their_features, const u8 *upfront_shutdown_script, const secp256k1_ecdsa_signature *remote_ann_node_sig, const secp256k1_ecdsa_signature *remote_ann_bitcoin_sig, bool option_static_remotekey, bool option_anchor_outputs, bool dev_fast_gossip, bool dev_fail_process_onionpacket, const struct penalty_base *pbases, const struct wally_psbt *psbt); bool fromwire_channeld_init(const tal_t *ctx, const void *p, const struct chainparams **chainparams, struct feature_set **our_features, struct channel_id *channel_id, struct bitcoin_txid *funding_txid, u16 *funding_txout, struct amount_sat *funding_satoshi, u32 *minimum_depth, struct channel_config *our_config, struct channel_config *their_config, struct fee_states **fee_states, u32 *feerate_min, u32 *feerate_max, u32 *feerate_penalty, struct bitcoin_signature *first_commit_sig, struct per_peer_state **per_peer_state, struct pubkey *remote_fundingkey, struct basepoints *remote_basepoints, struct pubkey *remote_per_commit, struct pubkey *old_remote_per_commit, enum side *opener, u32 *fee_base, u32 *fee_proportional, struct amount_msat *local_msatoshi, struct basepoints *our_basepoints, struct pubkey *our_funding_pubkey, struct node_id *local_node_id, struct node_id *remote_node_id, u32 *commit_msec, u16 *cltv_delta, bool *last_was_revoke, struct changed_htlc **last_sent_commit, u64 *next_index_local, u64 *next_index_remote, u64 *revocations_received, u64 *next_htlc_id, struct existing_htlc ***htlcs, bool *local_funding_locked, bool *remote_funding_locked, struct short_channel_id *funding_short_id, bool *reestablish, bool *send_shutdown, bool *remote_shutdown_received, u8 **final_scriptpubkey, u8 *flags, u8 **init_peer_pkt, bool *reached_announce_depth, struct secret *last_remote_secret, u8 **their_features, u8 **upfront_shutdown_script, secp256k1_ecdsa_signature **remote_ann_node_sig, secp256k1_ecdsa_signature **remote_ann_bitcoin_sig, bool *option_static_remotekey, bool *option_anchor_outputs, bool *dev_fast_gossip, bool *dev_fail_process_onionpacket, struct penalty_base **pbases, struct wally_psbt **psbt); +/* WIRE: CHANNELD_FUNDING_TX */ +u8 *towire_channeld_funding_tx(const tal_t *ctx, const struct wally_tx *funding_tx); +bool fromwire_channeld_funding_tx(const tal_t *ctx, const void *p, struct wally_tx **funding_tx); + /* WIRE: CHANNELD_FUNDING_DEPTH */ /* master->channeld funding hit new depth(funding locked if >= lock depth) */ u8 *towire_channeld_funding_depth(const tal_t *ctx, const struct short_channel_id *short_channel_id, u32 depth); @@ -231,4 +237,4 @@ bool fromwire_send_onionmsg(const tal_t *ctx, const void *p, u8 onion[1366], str #endif /* LIGHTNING_CHANNELD_CHANNELD_WIREGEN_H */ -// SHA256STAMP:8ba694ae5e240ca5622d6860d1fa2b5116e44b2b8d5bfe0a77d88d4aab979906 +// SHA256STAMP:5614b61d80352e94f974d018c391102579d76c5db11648d18ed0dd198ab6838a diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index faf352908..5ff539974 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -315,6 +315,103 @@ static void handle_error_channel(struct channel *channel, forget(channel); } +struct channel_send { + struct wally_tx *wtx; + struct channel *channel; +}; + +static void sendfunding_done(struct bitcoind *bitcoind UNUSED, + bool success, const char *msg, + struct channel_send *cs) +{ + struct lightningd *ld = cs->channel->peer->ld; + struct channel *channel = cs->channel; + struct wally_tx *wtx = cs->wtx; + struct json_stream *response; + struct bitcoin_txid txid; + struct open_command *oc; + struct amount_sat unused; + int num_utxos; + + oc = find_open_command(ld, channel); + if (!oc && channel->opener == LOCAL) { + log_broken(channel->log, + "No outstanding command for channel %s," + " funding sent was success? %d", + type_to_string(tmpctx, struct channel_id, + &channel->cid), + success); + } + + if (!success) { + if (oc) + was_pending(command_fail(oc->cmd, + FUNDING_BROADCAST_FAIL, + "Error broadcasting funding " + "tx: %s. Unsent tx discarded " + "%s.", + msg, + type_to_string(tmpctx, + struct wally_tx, + wtx))); + log_unusual(channel->log, + "Error broadcasting funding " + "tx: %s. Unsent tx discarded " + "%s.", + msg, + type_to_string(tmpctx, struct wally_tx, wtx)); + tal_free(cs); + return; + } + + /* This might have spent UTXOs from our wallet */ + num_utxos = wallet_extract_owned_outputs(ld->wallet, + wtx, NULL, + &unused); + if (num_utxos) { + wallet_transaction_add(ld->wallet, wtx, 0, 0); + } + + if (oc) { + response = json_stream_success(oc->cmd); + wally_txid(wtx, &txid); + json_add_hex_talarr(response, "tx", linearize_wtx(tmpctx, wtx)); + json_add_txid(response, "txid", &txid); + json_add_string(response, "channel_id", + type_to_string(tmpctx, struct channel_id, + &channel->cid)); + was_pending(command_success(oc->cmd, response)); + } + + tal_free(cs); +} + +static void handle_funding_tx(struct channel *channel, + const u8 *msg) +{ + struct channel_send *cs; + struct lightningd *ld = channel->peer->ld; + + cs = tal(channel, struct channel_send); + cs->channel = channel; + + if (!fromwire_channeld_funding_tx(tmpctx, msg, &cs->wtx)) { + channel_internal_error(channel, + "bad channeld_funding_tx: %s", + tal_hex(tmpctx, msg)); + return; + } + + log_debug(channel->log, + "Broadcasting funding tx for channel %s. %s", + type_to_string(tmpctx, struct channel_id, &channel->cid), + type_to_string(tmpctx, struct wally_tx, cs->wtx)); + + bitcoind_sendrawtx(ld->topology->bitcoind, + tal_hex(tmpctx, linearize_wtx(tmpctx, cs->wtx)), + sendfunding_done, cs); +} + void forget_channel(struct channel *channel, const char *why) { channel->error = towire_errorfmt(channel, &channel->cid, "%s", why); @@ -363,6 +460,9 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) case WIRE_CHANNELD_SEND_ERROR_REPLY: handle_error_channel(sd->channel, msg); break; + case WIRE_CHANNELD_FUNDING_TX: + handle_funding_tx(sd->channel, msg); + break; #if EXPERIMENTAL_FEATURES case WIRE_GOT_ONIONMSG_TO_US: handle_onionmsg_to_us(sd->channel, msg);