diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 862e66295..ff44385fd 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -1027,8 +1027,9 @@ static void init_channel(struct peer *peer, const u8 *msg) struct pubkey funding_pubkey[NUM_SIDES]; struct sha256_double funding_txid; bool am_funder; + u8 *funding_signed; - if (!fromwire_channel_init(msg, NULL, + if (!fromwire_channel_init(msg, msg, NULL, &funding_txid, &funding_txout, &peer->conf[LOCAL], &peer->conf[REMOTE], &peer->their_commit_sig, @@ -1046,7 +1047,8 @@ static void init_channel(struct peer *peer, const u8 *msg) &peer->node_ids[LOCAL], &peer->node_ids[REMOTE], &peer->commit_msec, - &peer->cltv_delta)) + &peer->cltv_delta, + &funding_signed)) status_failed(WIRE_CHANNEL_BAD_COMMAND, "Init: %s", tal_hex(msg, msg)); @@ -1076,6 +1078,10 @@ static void init_channel(struct peer *peer, const u8 *msg) /* OK, now we can process peer messages. */ peer->peer_conn = io_new_conn(peer, PEER_FD, setup_peer_conn, peer); io_set_finish(peer->peer_conn, peer_conn_broken, peer); + + /* If we have a funding_signed message, we send that immediately */ + if (tal_len(funding_signed) != 0) + msg_enqueue(&peer->peer_out, take(funding_signed)); } static void handle_funding_locked(struct peer *peer, const u8 *msg) diff --git a/lightningd/channel/channel_wire.csv b/lightningd/channel/channel_wire.csv index 319e5d61c..25d71a8de 100644 --- a/lightningd/channel/channel_wire.csv +++ b/lightningd/channel/channel_wire.csv @@ -40,6 +40,8 @@ channel_init,533,local_node_id,struct pubkey channel_init,566,remote_node_id,struct pubkey channel_init,599,commit_msec,4 channel_init,603,cltv_delta,u16 +channel_init,605,init_peer_pkt_len,u16 +channel_init,607,init_peer_pkt,init_peer_pkt_len*u8 # Tx is deep enough, go! channel_funding_locked,2 diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 68e3f3441..54daa3bb5 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -465,7 +465,7 @@ static u8 *fundee_channel(struct state *state, secp256k1_ecdsa_signature theirsig, sig; struct bitcoin_tx **txs; struct sha256_double chain_hash; - u8 *msg, *encmsg; + u8 *msg; const u8 **wscripts; state->remoteconf = tal(state, struct channel_config); @@ -648,10 +648,9 @@ static u8 *fundee_channel(struct state *state, &state->our_secrets.funding_privkey, our_funding_pubkey, &sig); - /* We don't send this ourselves: master does, because it needs to save - * state to disk before doing so. */ + /* We don't send this ourselves: channeld does, because master needs + * to save state to disk before doing so. */ msg = towire_funding_signed(state, &channel_id, &sig); - encmsg = cryptomsg_encrypt_msg(state, &state->cs, msg); return towire_opening_fundee_reply(state, state->remoteconf, @@ -666,7 +665,7 @@ static u8 *fundee_channel(struct state *state, state->funding_txout, state->funding_satoshis, state->push_msat, - encmsg); + msg); } #ifndef TESTING diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 638c4bd28..4138cf7b0 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -721,7 +721,8 @@ static enum watch_result funding_lockin_cb(struct peer *peer, } /* FIXME: Reshuffle. */ -static void peer_start_channeld(struct peer *peer, enum peer_state oldstate); +static void peer_start_channeld(struct peer *peer, enum peer_state oldstate, + const u8 *funding_signed); static bool opening_got_hsm_funding_sig(struct subd *hsm, const u8 *resp, const int *fds, @@ -764,7 +765,7 @@ static bool opening_got_hsm_funding_sig(struct subd *hsm, const u8 *resp, command_success(fc->cmd, null_response(fc->cmd)); /* Start normal channel daemon. */ - peer_start_channeld(fc->peer, GETTING_SIG_FROM_HSM); + peer_start_channeld(fc->peer, GETTING_SIG_FROM_HSM, NULL); tal_free(fc); return true; @@ -1542,7 +1543,11 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp, &peer->ld->dstate.id, peer->id, time_to_msec(cfg->commit_time), - cfg->deadline_blocks); + cfg->deadline_blocks, + peer->funding_signed); + + /* Don't need this any more (we never re-transmit it) */ + peer->funding_signed = tal_free(peer->funding_signed); /* We don't expect a response: we are triggered by funding_depth_cb. */ subd_send_msg(peer->owner, take(initmsg)); @@ -1553,7 +1558,8 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp, } /* opening is done, start lightningd_channel for peer. */ -static void peer_start_channeld(struct peer *peer, enum peer_state oldstate) +static void peer_start_channeld(struct peer *peer, enum peer_state oldstate, + const u8 *funding_signed) { /* Unowned: back to being owned by main daemon. */ peer->owner = NULL; @@ -1568,6 +1574,9 @@ static void peer_start_channeld(struct peer *peer, enum peer_state oldstate) peer_set_condition(peer, oldstate, GETTING_HSMFD); + /* Save this for when we get HSM fd. */ + peer->funding_signed = funding_signed; + /* Get fd from hsm. */ subd_req(peer, peer->ld->hsm, take(towire_hsmctl_hsmfd_channeld(peer, peer->unique_id)), @@ -1665,7 +1674,7 @@ static bool opening_fundee_finished(struct subd *opening, const int *fds, struct peer *peer) { - u8 *funding_msg_enc; + u8 *funding_signed; struct channel_info *channel_info; log_debug(peer->log, "Got opening_fundee_finish_response"); @@ -1677,7 +1686,7 @@ static bool opening_fundee_finished(struct subd *opening, peer->channel_info = channel_info = tal(peer, struct channel_info); peer->funding_txid = tal(peer, struct sha256_double); - if (!fromwire_opening_fundee_reply(reply, reply, NULL, + if (!fromwire_opening_fundee_reply(peer, reply, NULL, &channel_info->their_config, &channel_info->commit_sig, peer->cs, @@ -1690,7 +1699,7 @@ static bool opening_fundee_finished(struct subd *opening, &peer->funding_outnum, &peer->funding_satoshi, &peer->push_msat, - &funding_msg_enc)) { + &funding_signed)) { log_broken(peer->log, "bad OPENING_FUNDEE_REPLY %s", tal_hex(reply, reply)); return false; @@ -1702,16 +1711,9 @@ static bool opening_fundee_finished(struct subd *opening, watch_txid(peer, peer->ld->topology, peer, peer->funding_txid, funding_lockin_cb, NULL); - /* FIXME: Remove synchronous write! */ - if (write(peer->fd, funding_msg_enc, tal_len(funding_msg_enc)) - != tal_len(funding_msg_enc)) { - log_broken(peer->log, "Could not write funding_signed msg"); - return false; - } - /* On to normal operation! */ peer->owner = NULL; - peer_start_channeld(peer, OPENINGD); + peer_start_channeld(peer, OPENINGD, funding_signed); /* Tell opening daemon to exit. */ return false; diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 94a3288d9..dbbd4e256 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -51,6 +51,9 @@ struct peer { /* Our channel config. */ struct channel_config our_config; + /* funding_signed packet for fundee, waiting to send. */ + const u8 *funding_signed; + /* Channel if locked. */ struct short_channel_id *scid;