Skip to content

Commit e5994ad

Browse files
committed
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 <rusty@rustcorp.com.au>
1 parent d1c30c4 commit e5994ad

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

daemon/peer.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,26 @@ static void peer_update_complete(struct peer *peer)
281281

282282
void peer_open_complete(struct peer *peer, const char *problem)
283283
{
284-
if (problem)
284+
if (problem) {
285285
log_unusual(peer->log, "peer open failed: %s", problem);
286-
else
286+
if (peer->open_jsoncmd) {
287+
command_fail(peer->open_jsoncmd, "%s", problem);
288+
peer->open_jsoncmd = NULL;
289+
}
290+
} else {
287291
log_debug(peer->log, "peer open complete");
292+
if (peer->open_jsoncmd) {
293+
struct json_result *response;
294+
response = new_json_result(peer->open_jsoncmd);
295+
296+
json_object_start(response, NULL);
297+
json_add_pubkey(response, peer->dstate->secpctx,
298+
"id", peer->id);
299+
json_object_end(response);
300+
command_success(peer->open_jsoncmd, response);
301+
peer->open_jsoncmd = NULL;
302+
}
303+
}
288304
}
289305

290306
static void set_peer_state(struct peer *peer, enum state newstate,
@@ -2354,6 +2370,7 @@ struct peer *new_peer(struct lightningd_state *dstate,
23542370
peer->secrets = NULL;
23552371
list_head_init(&peer->watches);
23562372
peer->outpkt = tal_arr(peer, Pkt *, 0);
2373+
peer->open_jsoncmd = NULL;
23572374
peer->commit_jsoncmd = NULL;
23582375
list_head_init(&peer->outgoing_txs);
23592376
list_head_init(&peer->their_commits);
@@ -2654,8 +2671,7 @@ static struct io_plan *crypto_on_out(struct io_conn *conn,
26542671
peer->io_data = tal_steal(peer, iod);
26552672
peer->id = tal_dup(peer, struct pubkey, id);
26562673
peer->anchor.input = tal_steal(peer, connect->input);
2657-
2658-
command_success(connect->cmd, null_response(connect));
2674+
peer->open_jsoncmd = connect->cmd;
26592675
return peer_crypto_on(conn, peer);
26602676
}
26612677

@@ -2914,7 +2930,7 @@ const struct json_command connect_command = {
29142930
"connect",
29152931
json_connect,
29162932
"Connect to a {host} at {port} using hex-encoded {tx} to fund",
2917-
"Returns an empty result on success"
2933+
"Returns the {id} on success (once channel established)"
29182934
};
29192935

29202936
/* Have any of our HTLCs passed their deadline? */

daemon/peer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ struct peer {
9696
/* Are we connected now? (Crypto handshake completed). */
9797
bool connected;
9898

99+
/* If we're doing an open, this is the command which triggered it */
100+
struct command *open_jsoncmd;
101+
99102
/* If we're doing a commit, this is the command which triggered it */
100103
struct command *commit_jsoncmd;
101104

daemon/test/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ TXID=`$CLI sendtoaddress $P2SHADDR 0.01`
461461
TX=`$CLI getrawtransaction $TXID`
462462
$CLI generate 1
463463

464-
lcli1 connect localhost $PORT2 $TX
464+
lcli1 connect localhost $PORT2 $TX &
465465

466466
# Expect them to be waiting for anchor.
467467
check_peerstate lcli1 STATE_OPEN_WAITING_OURANCHOR
@@ -1032,7 +1032,7 @@ if [ ! -n "$MANUALCOMMIT" ]; then
10321032
TX2=`$CLI getrawtransaction $TXID2`
10331033
$CLI generate 1
10341034

1035-
lcli2 connect localhost $PORT3 $TX2
1035+
lcli2 connect localhost $PORT3 $TX2 &
10361036
check_tx_spend lcli2
10371037
$CLI generate 1
10381038

0 commit comments

Comments
 (0)