From e5994ad8c1b01d8a91b6d1f3c6eb2fe8dcb3e955 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Oct 2016 14:01:18 +1030 Subject: [PATCH] json_connect: don't return until we're in state normal. This gives much better errors, and allows us to return the peer id. Closes: #37 Reported-by: Glenn Willen Signed-off-by: Rusty Russell --- daemon/peer.c | 26 +++++++++++++++++++++----- daemon/peer.h | 3 +++ daemon/test/test.sh | 4 ++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 97a2c3a90..e8b824543 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -281,10 +281,26 @@ static void peer_update_complete(struct peer *peer) void peer_open_complete(struct peer *peer, const char *problem) { - if (problem) + if (problem) { log_unusual(peer->log, "peer open failed: %s", problem); - else + if (peer->open_jsoncmd) { + command_fail(peer->open_jsoncmd, "%s", problem); + peer->open_jsoncmd = NULL; + } + } else { log_debug(peer->log, "peer open complete"); + if (peer->open_jsoncmd) { + struct json_result *response; + response = new_json_result(peer->open_jsoncmd); + + json_object_start(response, NULL); + json_add_pubkey(response, peer->dstate->secpctx, + "id", peer->id); + json_object_end(response); + command_success(peer->open_jsoncmd, response); + peer->open_jsoncmd = NULL; + } + } } static void set_peer_state(struct peer *peer, enum state newstate, @@ -2354,6 +2370,7 @@ struct peer *new_peer(struct lightningd_state *dstate, peer->secrets = NULL; list_head_init(&peer->watches); peer->outpkt = tal_arr(peer, Pkt *, 0); + peer->open_jsoncmd = NULL; peer->commit_jsoncmd = NULL; list_head_init(&peer->outgoing_txs); list_head_init(&peer->their_commits); @@ -2654,8 +2671,7 @@ static struct io_plan *crypto_on_out(struct io_conn *conn, peer->io_data = tal_steal(peer, iod); peer->id = tal_dup(peer, struct pubkey, id); peer->anchor.input = tal_steal(peer, connect->input); - - command_success(connect->cmd, null_response(connect)); + peer->open_jsoncmd = connect->cmd; return peer_crypto_on(conn, peer); } @@ -2914,7 +2930,7 @@ const struct json_command connect_command = { "connect", json_connect, "Connect to a {host} at {port} using hex-encoded {tx} to fund", - "Returns an empty result on success" + "Returns the {id} on success (once channel established)" }; /* Have any of our HTLCs passed their deadline? */ diff --git a/daemon/peer.h b/daemon/peer.h index 5a199a13f..6a580447e 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -96,6 +96,9 @@ struct peer { /* Are we connected now? (Crypto handshake completed). */ bool connected; + /* If we're doing an open, this is the command which triggered it */ + struct command *open_jsoncmd; + /* If we're doing a commit, this is the command which triggered it */ struct command *commit_jsoncmd; diff --git a/daemon/test/test.sh b/daemon/test/test.sh index 332899f3d..c393da7b8 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -461,7 +461,7 @@ TXID=`$CLI sendtoaddress $P2SHADDR 0.01` TX=`$CLI getrawtransaction $TXID` $CLI generate 1 -lcli1 connect localhost $PORT2 $TX +lcli1 connect localhost $PORT2 $TX & # Expect them to be waiting for anchor. check_peerstate lcli1 STATE_OPEN_WAITING_OURANCHOR @@ -1032,7 +1032,7 @@ if [ ! -n "$MANUALCOMMIT" ]; then TX2=`$CLI getrawtransaction $TXID2` $CLI generate 1 - lcli2 connect localhost $PORT3 $TX2 + lcli2 connect localhost $PORT3 $TX2 & check_tx_spend lcli2 $CLI generate 1