diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 4a644f1e7..fcd802c5c 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -461,7 +461,7 @@ static u8 *fundee_channel(struct state *state, secp256k1_ecdsa_signature theirsig, sig; struct bitcoin_tx **txs; struct sha256_double chain_hash; - u8 *msg; + u8 *msg, *encmsg; const u8 **wscripts; state->remoteconf = tal(state, struct channel_config); @@ -601,20 +601,6 @@ static u8 *fundee_channel(struct state *state, peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_BAD_PARAM, "could not create channel with given config"); - /* Now, ask master to watch. */ - status_trace("asking master to watch funding %s", - type_to_string(trc, struct sha256_double, &state->funding_txid)); - msg = towire_opening_fundee_reply(state, &state->funding_txid); - wire_sync_write(REQ_FD, msg); - - msg = wire_sync_read(state, REQ_FD); - if (!fromwire_opening_fundee_finish(msg, NULL)) - status_failed(WIRE_OPENING_BAD_PARAM, - "Expected valid opening_fundee_finish: %s", - tal_hex(trc, msg)); - - status_trace("master said to finish"); - /* BOLT #2: * * The recipient MUST fail the channel if `signature` is incorrect. @@ -658,23 +644,25 @@ 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. */ msg = towire_funding_signed(state, &channel_id, &sig); - if (!sync_crypto_write(&state->cs, PEER_FD, msg)) - peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED, - "Writing funding_signed"); + encmsg = cryptomsg_encrypt_msg(state, &state->cs, msg); - return towire_opening_fundee_finish_reply(state, - state->funding_txout, - state->remoteconf, - &theirsig, - &state->cs, - &their_funding_pubkey, - &theirs.revocation, - &theirs.payment, - &theirs.delayed_payment, - &state->next_per_commit[REMOTE], - state->funding_satoshis, - state->push_msat); + return towire_opening_fundee_reply(state, + state->remoteconf, + &theirsig, + &state->cs, + &theirs.revocation, + &theirs.payment, + &theirs.delayed_payment, + &state->next_per_commit[REMOTE], + &their_funding_pubkey, + &state->funding_txid, + state->funding_txout, + state->funding_satoshis, + state->push_msat, + encmsg); } #ifndef TESTING diff --git a/lightningd/opening/opening_wire.csv b/lightningd/opening/opening_wire.csv index 5d4edbabf..3b346ebc3 100644 --- a/lightningd/opening/opening_wire.csv +++ b/lightningd/opening/opening_wire.csv @@ -58,22 +58,20 @@ opening_fundee,4,max_feerate,4 opening_fundee,8,len,2 opening_fundee,10,msg,len*u8 -# This gives the txid of their funding tx to watch. +# This gives their txid and info, means we can send funding_signed: we're done. opening_fundee_reply,103 +opening_fundee_reply,0,their_config,struct channel_config +opening_fundee_reply,36,first_commit_sig,secp256k1_ecdsa_signature +opening_fundee_reply,100,crypto_state,struct crypto_state +opening_fundee_reply,244,revocation_basepoint,33 +opening_fundee_reply,277,payment_basepoint,33 +opening_fundee_reply,310,delayed_payment_basepoint,33 +opening_fundee_reply,343,their_per_commit_point,33 +opening_fundee_reply,0,remote_fundingkey,33 opening_fundee_reply,0,funding_txid,struct sha256_double - -# Acknowledge watch is in place, now can send sig. -opening_fundee_finish,4 - -opening_fundee_finish_reply,104 -opening_fundee_finish_reply,32,funding_txout,u16 -opening_fundee_finish_reply,0,their_config,struct channel_config -opening_fundee_finish_reply,36,first_commit_sig,secp256k1_ecdsa_signature -opening_fundee_finish_reply,100,crypto_state,struct crypto_state -opening_fundee_finish_reply,244,remote_fundingkey,33 -opening_fundee_finish_reply,277,revocation_basepoint,33 -opening_fundee_finish_reply,310,payment_basepoint,33 -opening_fundee_finish_reply,343,delayed_payment_basepoint,33 -opening_fundee_finish_reply,377,their_per_commit_point,33 -opening_fundee_finish_reply,410,funding_satoshis,8 -opening_fundee_finish_reply,418,push_msat,8 +opening_fundee_reply,0,funding_txout,u16 +opening_fundee_reply,0,funding_satoshis,8 +opening_fundee_reply,0,push_msat,8 +# The (encrypted) funding signed message: send this and we're committed. +opening_fundee_reply,0,msglen,u16 +opening_fundee_reply,0,funding_signed_msg,msglen*u8 diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index a88732ff6..2310e32ce 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1482,7 +1482,7 @@ static bool opening_funder_finished(struct subd *opening, const u8 *resp, return false; } -static bool opening_fundee_finish_response(struct subd *opening, +static bool opening_fundee_finished(struct subd *opening, const u8 *reply, const int *fds, struct peer *peer) @@ -1492,6 +1492,7 @@ static bool opening_fundee_finish_response(struct subd *opening, struct basepoints theirbase; struct pubkey remote_fundingkey, their_per_commit_point; struct config *cfg = &peer->ld->dstate.config; + u8 *funding_msg_enc; u8 *initmsg; log_debug(peer->log, "Got opening_fundee_finish_response"); @@ -1499,23 +1500,39 @@ static bool opening_fundee_finish_response(struct subd *opening, peer->fd = fds[0]; peer->cs = tal(peer, struct crypto_state); - if (!fromwire_opening_fundee_finish_reply(reply, NULL, - &peer->funding_outnum, - &their_config, - &first_commit_sig, - peer->cs, - &remote_fundingkey, - &theirbase.revocation, - &theirbase.payment, - &theirbase.delayed_payment, - &their_per_commit_point, - &peer->funding_satoshi, - &peer->push_msat)) { - log_broken(peer->log, "bad OPENING_FUNDEE_FINISH_REPLY %s", + peer->funding_txid = tal(peer, struct sha256_double); + if (!fromwire_opening_fundee_reply(reply, reply, NULL, + &their_config, + &first_commit_sig, + peer->cs, + &theirbase.revocation, + &theirbase.payment, + &theirbase.delayed_payment, + &their_per_commit_point, + &remote_fundingkey, + peer->funding_txid, + &peer->funding_outnum, + &peer->funding_satoshi, + &peer->push_msat, + &funding_msg_enc)) { + log_broken(peer->log, "bad OPENING_FUNDEE_REPLY %s", tal_hex(reply, reply)); return false; } + log_debug(peer->log, "Watching funding tx %s", + type_to_string(reply, struct sha256_double, + peer->funding_txid)); + 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; + } + initmsg = towire_channel_init(peer, peer->funding_txid, peer->funding_outnum, @@ -1541,39 +1558,12 @@ static bool opening_fundee_finish_response(struct subd *opening, /* On to normal operation! */ peer->owner = NULL; - peer_start_channeld(peer, initmsg, OPENINGD_AWAITING_LOCKIN); + peer_start_channeld(peer, initmsg, OPENINGD); /* Tell opening daemon to exit. */ return false; } -static bool opening_fundee_reply(struct subd *opening, const u8 *reply, - const int *fds, - struct peer *peer) -{ - peer->funding_txid = tal(peer, struct sha256_double); - if (!fromwire_opening_fundee_reply(reply, NULL, peer->funding_txid)) { - log_broken(peer->log, "bad OPENING_FUNDEE_REPLY %s", - tal_hex(reply, reply)); - return false; - } - - log_debug(peer->log, "Watching funding tx %s", - type_to_string(reply, struct sha256_double, - peer->funding_txid)); - watch_txid(peer, peer->ld->topology, peer, peer->funding_txid, - funding_lockin_cb, NULL); - - /* It's about to send out funding_signed, so set this now. */ - peer_set_condition(peer, OPENINGD, OPENINGD_AWAITING_LOCKIN); - - /* Tell it we're watching. */ - subd_req(peer, opening, towire_opening_fundee_finish(reply), - -1, 1, - opening_fundee_finish_response, peer); - return true; -} - static void channel_config(struct lightningd *ld, struct channel_config *ours, u32 *max_to_self_delay, @@ -1679,7 +1669,8 @@ void peer_fundee_open(struct peer *peer, const u8 *from_peer) peer_fail(peer, "Unacceptably long open_channel"); return; } - subd_req(peer, peer->owner, take(msg), -1, 0, opening_fundee_reply, peer); + subd_req(peer, peer->owner, take(msg), -1, 1, + opening_fundee_finished, peer); } /* Peer has been released from gossip. Start opening. */ diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 16b9ed3a7..94a3288d9 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -109,7 +109,7 @@ static inline bool peer_on_chain(const struct peer *peer) */ static inline bool peer_persists(const struct peer *peer) { - return peer->state >= CHANNELD_AWAITING_LOCKIN; + return peer->state > GETTING_SIG_FROM_HSM; } struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id); diff --git a/lightningd/peer_state.h b/lightningd/peer_state.h index 29fb777e2..6f6db94f4 100644 --- a/lightningd/peer_state.h +++ b/lightningd/peer_state.h @@ -14,10 +14,6 @@ enum peer_state { /* Getting signature from HSM for funding tx (funder only). */ GETTING_SIG_FROM_HSM, - /* Waiting for funding tx to lock in: either have broadcast, or - * have sent `funding_signed`. */ - OPENINGD_AWAITING_LOCKIN, - /* Getting HSM fd for channeld. */ GETTING_HSMFD,