Browse Source

channeld: do init as sync IO.

Saves a special case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
a6b03dec15
  1. 140
      lightningd/channel/channel.c

140
lightningd/channel/channel.c

@ -1209,7 +1209,8 @@ static void peer_conn_broken(struct io_conn *conn, struct peer *peer)
"peer connection broken: %s", strerror(errno)); "peer connection broken: %s", strerror(errno));
} }
static void init_channel(struct peer *peer, const u8 *msg) /* We do this synchronously. */
static void init_channel(struct peer *peer)
{ {
struct privkey seed; struct privkey seed;
struct basepoints points[NUM_SIDES]; struct basepoints points[NUM_SIDES];
@ -1219,7 +1220,9 @@ static void init_channel(struct peer *peer, const u8 *msg)
struct sha256_double funding_txid; struct sha256_double funding_txid;
bool am_funder; bool am_funder;
u8 *funding_signed; u8 *funding_signed;
u8 *msg;
msg = wire_sync_read(peer, REQ_FD);
if (!fromwire_channel_init(msg, msg, NULL, if (!fromwire_channel_init(msg, msg, NULL,
&funding_txid, &funding_txout, &funding_txid, &funding_txout,
&peer->conf[LOCAL], &peer->conf[REMOTE], &peer->conf[LOCAL], &peer->conf[REMOTE],
@ -1273,6 +1276,8 @@ static void init_channel(struct peer *peer, const u8 *msg)
/* If we have a funding_signed message, we send that immediately */ /* If we have a funding_signed message, we send that immediately */
if (tal_len(funding_signed) != 0) if (tal_len(funding_signed) != 0)
msg_enqueue(&peer->peer_out, take(funding_signed)); msg_enqueue(&peer->peer_out, take(funding_signed));
tal_free(msg);
} }
static void handle_funding_locked(struct peer *peer, const u8 *msg) static void handle_funding_locked(struct peer *peer, const u8 *msg)
@ -1490,76 +1495,71 @@ static void handle_ping_cmd(struct peer *peer, const u8 *inmsg)
static struct io_plan *req_in(struct io_conn *conn, struct daemon_conn *master) static struct io_plan *req_in(struct io_conn *conn, struct daemon_conn *master)
{ {
struct peer *peer = container_of(master, struct peer, master); struct peer *peer = container_of(master, struct peer, master);
enum channel_wire_type t = fromwire_peektype(master->msg_in);
if (!peer->channel) /* Waiting for something specific? Defer others. */
init_channel(peer, master->msg_in); if (peer->handle_master_reply) {
else { void (*handle)(struct peer *peer, const u8 *msg);
enum channel_wire_type t = fromwire_peektype(master->msg_in);
/* Waiting for something specific? Defer others. */ if (t != peer->master_reply_type) {
if (peer->handle_master_reply) { msg_enqueue(&peer->master_deferred,
void (*handle)(struct peer *peer, const u8 *msg); take(master->msg_in));
master->msg_in = NULL;
if (t != peer->master_reply_type) { goto out_next;
msg_enqueue(&peer->master_deferred, }
take(master->msg_in));
master->msg_in = NULL;
goto out_next;
}
/* Just in case it resets this. */ /* Just in case it resets this. */
handle = peer->handle_master_reply; handle = peer->handle_master_reply;
peer->handle_master_reply = NULL; peer->handle_master_reply = NULL;
handle(peer, master->msg_in); handle(peer, master->msg_in);
goto out; goto out;
} }
switch (t) { switch (t) {
case WIRE_CHANNEL_FUNDING_LOCKED: case WIRE_CHANNEL_FUNDING_LOCKED:
handle_funding_locked(peer, master->msg_in); handle_funding_locked(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH: case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH:
handle_funding_announce_depth(peer, master->msg_in); handle_funding_announce_depth(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_OFFER_HTLC: case WIRE_CHANNEL_OFFER_HTLC:
handle_offer_htlc(peer, master->msg_in); handle_offer_htlc(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_FULFILL_HTLC: case WIRE_CHANNEL_FULFILL_HTLC:
handle_preimage(peer, master->msg_in); handle_preimage(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_FAIL_HTLC: case WIRE_CHANNEL_FAIL_HTLC:
handle_fail(peer, master->msg_in); handle_fail(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_PING: case WIRE_CHANNEL_PING:
handle_ping_cmd(peer, master->msg_in); handle_ping_cmd(peer, master->msg_in);
goto out; goto out;
case WIRE_CHANNEL_BAD_COMMAND: case WIRE_CHANNEL_BAD_COMMAND:
case WIRE_CHANNEL_HSM_FAILED: case WIRE_CHANNEL_HSM_FAILED:
case WIRE_CHANNEL_CRYPTO_FAILED: case WIRE_CHANNEL_CRYPTO_FAILED:
case WIRE_CHANNEL_GOSSIP_BAD_MESSAGE: case WIRE_CHANNEL_GOSSIP_BAD_MESSAGE:
case WIRE_CHANNEL_INTERNAL_ERROR: case WIRE_CHANNEL_INTERNAL_ERROR:
case WIRE_CHANNEL_PEER_WRITE_FAILED: case WIRE_CHANNEL_PEER_WRITE_FAILED:
case WIRE_CHANNEL_PEER_READ_FAILED: case WIRE_CHANNEL_PEER_READ_FAILED:
case WIRE_CHANNEL_NORMAL_OPERATION: case WIRE_CHANNEL_NORMAL_OPERATION:
case WIRE_CHANNEL_INIT: case WIRE_CHANNEL_INIT:
case WIRE_CHANNEL_OFFER_HTLC_REPLY: case WIRE_CHANNEL_OFFER_HTLC_REPLY:
case WIRE_CHANNEL_PING_REPLY: case WIRE_CHANNEL_PING_REPLY:
case WIRE_CHANNEL_PEER_BAD_MESSAGE: case WIRE_CHANNEL_PEER_BAD_MESSAGE:
case WIRE_CHANNEL_ANNOUNCED: case WIRE_CHANNEL_ANNOUNCED:
case WIRE_CHANNEL_SENDING_COMMITSIG: case WIRE_CHANNEL_SENDING_COMMITSIG:
case WIRE_CHANNEL_GOT_COMMITSIG: case WIRE_CHANNEL_GOT_COMMITSIG:
case WIRE_CHANNEL_GOT_REVOKE: case WIRE_CHANNEL_GOT_REVOKE:
case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY: case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY:
case WIRE_CHANNEL_GOT_COMMITSIG_REPLY: case WIRE_CHANNEL_GOT_COMMITSIG_REPLY:
case WIRE_CHANNEL_GOT_REVOKE_REPLY: case WIRE_CHANNEL_GOT_REVOKE_REPLY:
case WIRE_CHANNEL_GOT_FUNDING_LOCKED: case WIRE_CHANNEL_GOT_FUNDING_LOCKED:
break; break;
}
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%u %s", t,
channel_wire_type_name(t));
} }
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%u %s", t,
channel_wire_type_name(t));
out: out:
/* In case we've now processed reply, process packet backlog. */ /* In case we've now processed reply, process packet backlog. */
@ -1608,7 +1608,8 @@ int main(int argc, char *argv[])
| SECP256K1_CONTEXT_SIGN); | SECP256K1_CONTEXT_SIGN);
daemon_conn_init(peer, &peer->master, REQ_FD, req_in, master_gone); daemon_conn_init(peer, &peer->master, REQ_FD, req_in, master_gone);
peer->channel = NULL; status_setup_async(&peer->master);
peer->htlc_id = 0; peer->htlc_id = 0;
peer->num_pings_outstanding = 0; peer->num_pings_outstanding = 0;
timers_init(&peer->timers, time_mono()); timers_init(&peer->timers, time_mono());
@ -1618,6 +1619,7 @@ int main(int argc, char *argv[])
peer->handle_master_reply = NULL; peer->handle_master_reply = NULL;
peer->master_reply_type = 0; peer->master_reply_type = 0;
msg_queue_init(&peer->master_deferred, peer); msg_queue_init(&peer->master_deferred, peer);
msg_queue_init(&peer->peer_out, peer);
/* We send these to HSM to get real signatures; don't have valgrind /* We send these to HSM to get real signatures; don't have valgrind
* complain. */ * complain. */
@ -1628,15 +1630,15 @@ int main(int argc, char *argv[])
sizeof(peer->announcement_bitcoin_sigs[i])); sizeof(peer->announcement_bitcoin_sigs[i]));
} }
status_setup_async(&peer->master);
msg_queue_init(&peer->peer_out, peer);
daemon_conn_init(peer, &peer->gossip_client, GOSSIP_FD, daemon_conn_init(peer, &peer->gossip_client, GOSSIP_FD,
gossip_client_recv, gossip_gone); gossip_client_recv, gossip_gone);
init_peer_crypto_state(peer, &peer->pcs); init_peer_crypto_state(peer, &peer->pcs);
peer->funding_locked[LOCAL] = peer->funding_locked[REMOTE] = false; peer->funding_locked[LOCAL] = peer->funding_locked[REMOTE] = false;
/* Read init_channel message sync. */
init_channel(peer);
for (;;) { for (;;) {
struct timer *expired = NULL; struct timer *expired = NULL;
io_loop(&peer->timers, &expired); io_loop(&peer->timers, &expired);

Loading…
Cancel
Save