diff --git a/lightningd/opening/Makefile b/lightningd/opening/Makefile index bf25cabef..4e8126bbc 100644 --- a/lightningd/opening/Makefile +++ b/lightningd/opening/Makefile @@ -10,8 +10,7 @@ lightningd/opening-all: lightningd/lightningd_opening # lightningd/opening needs these: LIGHTNINGD_OPENING_HEADERS_GEN := \ - lightningd/opening/gen_opening_control_wire.h \ - lightningd/opening/gen_opening_status_wire.h + lightningd/opening/gen_opening_wire.h LIGHTNINGD_OPENING_HEADERS_NOGEN := @@ -36,17 +35,11 @@ LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_OPENING_HEADERS_NOGEN) $(LIGHTNINGD_OPENING_OBJS): $(LIGHTNINGD_HEADERS) -lightningd/opening/gen_opening_control_wire.h: $(WIRE_GEN) lightningd/opening/opening_control_wire.csv - $(WIRE_GEN) --header $@ opening_control_wire_type < lightningd/opening/opening_control_wire.csv > $@ +lightningd/opening/gen_opening_wire.h: $(WIRE_GEN) lightningd/opening/opening_wire.csv + $(WIRE_GEN) --header $@ opening_wire_type < lightningd/opening/opening_wire.csv > $@ -lightningd/opening/gen_opening_control_wire.c: $(WIRE_GEN) lightningd/opening/opening_control_wire.csv - $(WIRE_GEN) ${@:.c=.h} opening_control_wire_type < lightningd/opening/opening_control_wire.csv > $@ - -lightningd/opening/gen_opening_status_wire.h: $(WIRE_GEN) lightningd/opening/opening_status_wire.csv - $(WIRE_GEN) --header $@ opening_status_wire_type < lightningd/opening/opening_status_wire.csv > $@ - -lightningd/opening/gen_opening_status_wire.c: $(WIRE_GEN) lightningd/opening/opening_status_wire.csv - $(WIRE_GEN) ${@:.c=.h} opening_status_wire_type < lightningd/opening/opening_status_wire.csv > $@ +lightningd/opening/gen_opening_wire.c: $(WIRE_GEN) lightningd/opening/opening_wire.csv + $(WIRE_GEN) ${@:.c=.h} opening_wire_type < lightningd/opening/opening_wire.csv > $@ LIGHTNINGD_OPENING_OBJS := $(LIGHTNINGD_OPENING_SRC:.c=.o) $(LIGHTNINGD_OPENING_GEN_SRC:.c=.o) diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 770c1908f..899452ccd 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -13,8 +13,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -26,8 +25,7 @@ #include #include -/* Stdout == status, stdin == requests, 3 == peer */ -#define STATUS_FD STDOUT_FILENO +/* stdin == requests, 3 == peer */ #define REQ_FD STDIN_FILENO #define PEER_FD 3 @@ -325,8 +323,8 @@ static u8 *open_channel(struct state *state, check_config_bounds(state, state->remoteconf); /* Now, ask master create a transaction to pay those two addresses. */ - msg = towire_opening_open_resp(state, our_funding_pubkey, - &their_funding_pubkey); + msg = towire_opening_open_reply(state, our_funding_pubkey, + &their_funding_pubkey); wire_sync_write(REQ_FD, msg); /* Expect funding tx. */ @@ -436,14 +434,14 @@ static u8 *open_channel(struct state *state, * Once the channel funder receives the `funding_signed` message, they * must broadcast the funding transaction to the Bitcoin network. */ - return towire_opening_open_funding_resp(state, - state->remoteconf, - &sig, - &state->cs, - &theirs.revocation, - &theirs.payment, - &theirs.delayed_payment, - &state->next_per_commit[REMOTE]); + return towire_opening_open_funding_reply(state, + state->remoteconf, + &sig, + &state->cs, + &theirs.revocation, + &theirs.payment, + &theirs.delayed_payment, + &state->next_per_commit[REMOTE]); } /* This is handed the message the peer sent which caused gossip to stop: @@ -584,7 +582,7 @@ static u8 *recv_channel(struct state *state, /* 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_accept_resp(state, &state->funding_txid); + msg = towire_opening_accept_reply(state, &state->funding_txid); wire_sync_write(REQ_FD, msg); msg = wire_sync_read(state, REQ_FD); @@ -642,18 +640,18 @@ static u8 *recv_channel(struct state *state, peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED, "Writing funding_signed"); - return towire_opening_accept_finish_resp(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_accept_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); } #ifndef TESTING @@ -678,7 +676,7 @@ int main(int argc, char *argv[]) signal(SIGCHLD, SIG_IGN); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - status_setup(STATUS_FD); + status_setup(REQ_FD); msg = wire_sync_read(state, REQ_FD); if (!msg) @@ -718,15 +716,7 @@ int main(int argc, char *argv[]) wire_sync_write(REQ_FD, msg); fdpass_send(REQ_FD, PEER_FD); status_trace("Sent %s with fd", - opening_control_wire_type_name(fromwire_peektype(msg))); - - /* Wait for exit command (avoid state close being read before reqfd) */ - msg = wire_sync_read(state, REQ_FD); - if (!msg) - status_failed(WIRE_OPENING_BAD_COMMAND, "%s", strerror(errno)); - if (!fromwire_opening_exit_req(msg, NULL)) - status_failed(WIRE_OPENING_BAD_COMMAND, "Expected exit req not %i", - fromwire_peektype(msg)); + opening_wire_type_name(fromwire_peektype(msg))); tal_free(state); return 0; } diff --git a/lightningd/opening/opening_control_wire.csv b/lightningd/opening/opening_control_wire.csv deleted file mode 100644 index 4956f6680..000000000 --- a/lightningd/opening/opening_control_wire.csv +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -opening_init,0 -# Base configuration we'll offer (channel reserve will vary with amount) -opening_init,0,our_config,36,struct channel_config -# Minimum/maximum configuration values we'll accept -opening_init,36,max_to_self_delay,4 -opening_init,40,min_effective_htlc_capacity_msat,8 -opening_init,48,crypto_state,144,struct crypto_state -# Seed to generate all the keys from -opening_init,196,seed,32,struct privkey - -# This means we offer the open. -opening_open,1 -opening_open,0,funding_satoshis,8 -opening_open,8,push_msat,8 -opening_open,16,feerate_per_kw,4 -opening_open,20,max_minimum_depth,4 - -# Response asks for txid of funding transaction. -opening_open_resp,101 -opening_open_resp,0,local_fundingkey,33 -opening_open_resp,0,remote_fundingkey,33 - -# Now we give the funding txid and outnum. -opening_open_funding,2 -opening_open_funding,0,txid,32,struct sha256_double -opening_open_funding,32,txout,2,u16 - -# This gives their sig, means we can broadcast tx: we're done. -opening_open_funding_resp,102 -opening_open_funding_resp,0,their_config,36,struct channel_config -opening_open_funding_resp,36,first_commit_sig,64,secp256k1_ecdsa_signature -opening_open_funding_resp,100,crypto_state,144,struct crypto_state -opening_open_funding_resp,244,revocation_basepoint,33 -opening_open_funding_resp,277,payment_basepoint,33 -opening_open_funding_resp,310,delayed_payment_basepoint,33 -opening_open_funding_resp,343,their_per_commit_point,33 - -# This means they offer the open (contains their offer packet) -opening_accept,3 -opening_accept,0,min_feerate,4 -opening_accept,4,max_feerate,4 -opening_accept,8,len,2 -opening_accept,10,msg,len,u8 - -# This gives the txid of their funding tx to watch. -opening_accept_resp,103 -opening_accept_resp,0,funding_txid,32,struct sha256_double - -# Acknowledge watch is in place, now can send sig. -opening_accept_finish,4 - -opening_accept_finish_resp,104 -opening_accept_finish_resp,32,funding_txout,2,u16 -opening_accept_finish_resp,0,their_config,36,struct channel_config -opening_accept_finish_resp,36,first_commit_sig,64,secp256k1_ecdsa_signature -opening_accept_finish_resp,100,crypto_state,144,struct crypto_state -opening_accept_finish_resp,244,remote_fundingkey,33 -opening_accept_finish_resp,277,revocation_basepoint,33 -opening_accept_finish_resp,310,payment_basepoint,33 -opening_accept_finish_resp,343,delayed_payment_basepoint,33 -opening_accept_finish_resp,377,their_per_commit_point,33 -opening_accept_finish_resp,410,funding_satoshis,8 -opening_accept_finish_resp,418,push_msat,8 - -# You're OK to exit. -opening_exit_req,99 diff --git a/lightningd/opening/opening_status_wire.csv b/lightningd/opening/opening_status_wire.csv deleted file mode 100644 index 1616058d8..000000000 --- a/lightningd/opening/opening_status_wire.csv +++ /dev/null @@ -1,15 +0,0 @@ -# Shouldn't happen -opening_bad_command,0x8000 -# Also shouldn't happen -opening_key_derivation_failed,0x8001 -# Also shouldn't happen -opening_bad_param,0x8002 -# Also shouldn't happen -opening_hsm_failed,0x8003 - -# These are due to peer. -opening_peer_write_failed,0x8010 -opening_peer_read_failed,0x8011 -opening_peer_bad_funding,0x8012 -opening_peer_bad_config,0x8013 -opening_peer_bad_initial_message,0x8014 diff --git a/lightningd/opening/opening_wire.csv b/lightningd/opening/opening_wire.csv new file mode 100644 index 000000000..ea7280e63 --- /dev/null +++ b/lightningd/opening/opening_wire.csv @@ -0,0 +1,78 @@ +# These shouldn't happen +opening_bad_command,0x8000 +opening_key_derivation_failed,0x8001 +opening_bad_param,0x8002 +opening_hsm_failed,0x8003 + +# These are due to peer. +opening_peer_write_failed,0x8010 +opening_peer_read_failed,0x8011 +opening_peer_bad_funding,0x8012 +opening_peer_bad_config,0x8013 +opening_peer_bad_initial_message,0x8014 + +#include +#include +opening_init,0 +# Base configuration we'll offer (channel reserve will vary with amount) +opening_init,0,our_config,36,struct channel_config +# Minimum/maximum configuration values we'll accept +opening_init,36,max_to_self_delay,4 +opening_init,40,min_effective_htlc_capacity_msat,8 +opening_init,48,crypto_state,144,struct crypto_state +# Seed to generate all the keys from +opening_init,196,seed,32,struct privkey + +# This means we offer the open. +opening_open,1 +opening_open,0,funding_satoshis,8 +opening_open,8,push_msat,8 +opening_open,16,feerate_per_kw,4 +opening_open,20,max_minimum_depth,4 + +# Reply asks for txid of funding transaction. +opening_open_reply,101 +opening_open_reply,0,local_fundingkey,33 +opening_open_reply,0,remote_fundingkey,33 + +# Now we give the funding txid and outnum. +opening_open_funding,2 +opening_open_funding,0,txid,32,struct sha256_double +opening_open_funding,32,txout,2,u16 + +# This gives their sig, means we can broadcast tx: we're done. +opening_open_funding_reply,102 +opening_open_funding_reply,0,their_config,36,struct channel_config +opening_open_funding_reply,36,first_commit_sig,64,secp256k1_ecdsa_signature +opening_open_funding_reply,100,crypto_state,144,struct crypto_state +opening_open_funding_reply,244,revocation_basepoint,33 +opening_open_funding_reply,277,payment_basepoint,33 +opening_open_funding_reply,310,delayed_payment_basepoint,33 +opening_open_funding_reply,343,their_per_commit_point,33 + +# This means they offer the open (contains their offer packet) +opening_accept,3 +opening_accept,0,min_feerate,4 +opening_accept,4,max_feerate,4 +opening_accept,8,len,2 +opening_accept,10,msg,len,u8 + +# This gives the txid of their funding tx to watch. +opening_accept_reply,103 +opening_accept_reply,0,funding_txid,32,struct sha256_double + +# Acknowledge watch is in place, now can send sig. +opening_accept_finish,4 + +opening_accept_finish_reply,104 +opening_accept_finish_reply,32,funding_txout,2,u16 +opening_accept_finish_reply,0,their_config,36,struct channel_config +opening_accept_finish_reply,36,first_commit_sig,64,secp256k1_ecdsa_signature +opening_accept_finish_reply,100,crypto_state,144,struct crypto_state +opening_accept_finish_reply,244,remote_fundingkey,33 +opening_accept_finish_reply,277,revocation_basepoint,33 +opening_accept_finish_reply,310,payment_basepoint,33 +opening_accept_finish_reply,343,delayed_payment_basepoint,33 +opening_accept_finish_reply,377,their_per_commit_point,33 +opening_accept_finish_reply,410,funding_satoshis,8 +opening_accept_finish_reply,418,push_msat,8 diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index b3098db28..65488a1bb 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -23,8 +23,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -635,10 +634,6 @@ static void peer_start_channeld(struct peer *peer, bool am_funder, { u8 *msg; - /* Tell opening daemon to exit. */ - subdaemon_req(peer->owner, take(towire_opening_exit_req(peer)), - -1, NULL, NULL, NULL); - /* Normal channel daemon. */ peer->owner = new_subdaemon(peer->ld, peer->ld, "lightningd_channel", peer, @@ -679,7 +674,7 @@ static void peer_start_channeld(struct peer *peer, bool am_funder, subdaemon_req(peer->owner, take(msg), -1, NULL, NULL, NULL); } -static void opening_release_tx(struct subdaemon *opening, const u8 *resp, +static bool opening_release_tx(struct subd *opening, const u8 *resp, struct funding_channel *fc) { u8 *msg; @@ -692,18 +687,18 @@ static void opening_release_tx(struct subdaemon *opening, const u8 *resp, /* FIXME: marshal code wants array, not array of pointers. */ struct utxo *utxos = tal_arr(fc, struct utxo, tal_count(fc->utxomap)); - if (!fromwire_opening_open_funding_resp(resp, NULL, - &their_config, - &commit_sig, - &crypto_state, - &theirbase.revocation, - &theirbase.payment, - &theirbase.delayed_payment, - &their_per_commit_point)) { - log_broken(fc->peer->log, "bad OPENING_OPEN_FUNDING_RESP %s", + if (!fromwire_opening_open_funding_reply(resp, NULL, + &their_config, + &commit_sig, + &crypto_state, + &theirbase.revocation, + &theirbase.payment, + &theirbase.delayed_payment, + &their_per_commit_point)) { + log_broken(fc->peer->log, "bad OPENING_OPEN_FUNDING_REPLY %s", tal_hex(resp, resp)); tal_free(fc->peer); - return; + return false; } peer_set_condition(fc->peer, "Getting HSM to sign funding tx"); @@ -725,22 +720,25 @@ static void opening_release_tx(struct subdaemon *opening, const u8 *resp, &their_config, &crypto_state, &commit_sig, &fc->remote_fundingkey, &theirbase, &their_per_commit_point); + + /* Tell opening daemon to exit. */ + return false; } -static void opening_gen_funding(struct subdaemon *opening, const u8 *resp, +static bool opening_gen_funding(struct subd *opening, const u8 *reply, struct funding_channel *fc) { u8 *msg; struct pubkey changekey; peer_set_condition(fc->peer, "Created funding transaction for channel"); - if (!fromwire_opening_open_resp(resp, NULL, - &fc->local_fundingkey, - &fc->remote_fundingkey)) { - log_broken(fc->peer->log, "Bad opening_open_resp %s", - tal_hex(fc, resp)); - tal_free(fc->peer); - return; + if (!fromwire_opening_open_reply(reply, NULL, + &fc->local_fundingkey, + &fc->remote_fundingkey)) { + log_broken(fc->peer->log, "Bad opening_open_reply %s", + tal_hex(fc, reply)); + /* Free openingd and peer */ + return false; } if (fc->change @@ -759,12 +757,14 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp, msg = towire_opening_open_funding(fc, fc->peer->funding_txid, fc->peer->funding_outnum); - subdaemon_req(fc->peer->owner, take(msg), -1, &fc->peer->fd, - opening_release_tx, fc); + /* FIXME: subdaemon */ + subd_req((struct subd *)fc->peer->owner, take(msg), -1, &fc->peer->fd, + opening_release_tx, fc); + return true; } -static void opening_accept_finish_response(struct subdaemon *opening, - const u8 *resp, +static bool opening_accept_finish_response(struct subd *opening, + const u8 *reply, struct peer *peer) { struct channel_config their_config; @@ -774,51 +774,53 @@ static void opening_accept_finish_response(struct subdaemon *opening, struct pubkey remote_fundingkey, their_per_commit_point; log_debug(peer->log, "Got opening_accept_finish_response"); - if (!fromwire_opening_accept_finish_resp(resp, NULL, - &peer->funding_outnum, - &their_config, - &first_commit_sig, - &crypto_state, - &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_ACCEPT_FINISH_RESP %s", - tal_hex(resp, resp)); - tal_free(peer); - return; + if (!fromwire_opening_accept_finish_reply(reply, NULL, + &peer->funding_outnum, + &their_config, + &first_commit_sig, + &crypto_state, + &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_ACCEPT_FINISH_REPLY %s", + tal_hex(reply, reply)); + return false; } /* On to normal operation! */ peer_start_channeld(peer, false, &their_config, &crypto_state, &first_commit_sig, &remote_fundingkey, &theirbase, &their_per_commit_point); + + /* Tell opening daemon to exit. */ + return false; } -static void opening_accept_response(struct subdaemon *opening, const u8 *resp, - struct peer *peer) +static bool opening_accept_reply(struct subd *opening, const u8 *reply, + struct peer *peer) { peer->funding_txid = tal(peer, struct sha256_double); - if (!fromwire_opening_accept_resp(resp, NULL, peer->funding_txid)) { - log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s", - tal_hex(resp, resp)); - tal_free(peer); - return; + if (!fromwire_opening_accept_reply(reply, NULL, peer->funding_txid)) { + log_broken(peer->log, "bad OPENING_ACCEPT_REPLY %s", + tal_hex(reply, reply)); + return false; } log_debug(peer->log, "Watching funding tx %s", - type_to_string(resp, struct sha256_double, + type_to_string(reply, struct sha256_double, peer->funding_txid)); watch_txid(peer, peer->ld->topology, peer, peer->funding_txid, funding_depth_cb, NULL); /* Tell it we're watching. */ - subdaemon_req(peer->owner, towire_opening_accept_finish(resp), - -1, &peer->fd, - opening_accept_finish_response, peer); + subd_req(opening, towire_opening_accept_finish(reply), + -1, &peer->fd, + opening_accept_finish_response, peer); + return true; } static void channel_config(struct lightningd *ld, @@ -879,6 +881,7 @@ void peer_accept_open(struct peer *peer, u32 max_to_self_delay, max_minimum_depth; u64 min_effective_htlc_capacity_msat; u8 *msg; + struct subd *opening; /* Note: gossipd handles unknown packets, so we don't have to worry * about ignoring odd ones here. */ @@ -891,12 +894,12 @@ void peer_accept_open(struct peer *peer, } peer_set_condition(peer, "Starting opening daemon"); - peer->owner = new_subdaemon(ld, ld, - "lightningd_opening", peer, - opening_status_wire_type_name, - opening_control_wire_type_name, - NULL, NULL, - peer->fd, -1); + opening = new_subd(ld, ld, "lightningd_opening", peer, + opening_wire_type_name, + NULL, NULL, + peer->fd, -1); + + peer->owner = (struct subdaemon *)opening; if (!peer->owner) { log_unusual(ld->log, "Could not subdaemon opening: %s", strerror(errno)); @@ -918,7 +921,7 @@ void peer_accept_open(struct peer *peer, min_effective_htlc_capacity_msat, cs, peer->seed); - subdaemon_req(peer->owner, take(msg), -1, NULL, NULL, NULL); + subd_send_msg(opening, take(msg)); /* FIXME: Real feerates! */ msg = towire_opening_accept(peer, 7500, 150000, from_peer); @@ -928,8 +931,7 @@ void peer_accept_open(struct peer *peer, tal_free(peer); return; } - subdaemon_req(peer->owner, take(msg), -1, NULL, - opening_accept_response, peer); + subd_req(opening, take(msg), -1, NULL, opening_accept_reply, peer); } /* Peer has been released from gossip. Start opening. */ @@ -942,6 +944,7 @@ static bool gossip_peer_released(struct subd *gossip, u64 min_effective_htlc_capacity_msat; u64 id; u8 *msg; + struct subd *opening; fc->cs = tal(fc, struct crypto_state); if (!fromwire_gossipctl_release_peer_reply(resp, NULL, &id, fc->cs)) @@ -953,19 +956,20 @@ static bool gossip_peer_released(struct subd *gossip, id, fc->peer->unique_id); peer_set_condition(fc->peer, "Starting opening daemon"); - fc->peer->owner = new_subdaemon(fc->peer->ld, ld, - "lightningd_opening", fc->peer, - opening_status_wire_type_name, - opening_control_wire_type_name, - NULL, NULL, - fc->peer->fd, -1); - if (!fc->peer->owner) { + opening = new_subd(fc->peer->ld, ld, + "lightningd_opening", fc->peer, + opening_wire_type_name, + NULL, NULL, + fc->peer->fd, -1); + if (!opening) { log_unusual(ld->log, "Could not subdaemon opening: %s", strerror(errno)); peer_set_condition(fc->peer, "Failed to subdaemon opening"); tal_free(fc->peer); return true; } + fc->peer->owner = (struct subdaemon *)opening; + /* They took our fd. */ fc->peer->fd = -1; @@ -984,13 +988,12 @@ static bool gossip_peer_released(struct subd *gossip, /* FIXME: Support push_msat? */ fc->peer->push_msat = 0; - subdaemon_req(fc->peer->owner, take(msg), -1, NULL, NULL, NULL); + subd_send_msg(opening, take(msg)); /* FIXME: Real feerate! */ msg = towire_opening_open(fc, fc->peer->funding_satoshi, fc->peer->push_msat, 15000, max_minimum_depth); - subdaemon_req(fc->peer->owner, take(msg), -1, NULL, - opening_gen_funding, fc); + subd_req(opening, take(msg), -1, NULL, opening_gen_funding, fc); return true; }