Skip to content

Commit

Permalink
connectd: add own err codes instead of generic -1
Browse files Browse the repository at this point in the history
Make it possible for connectd to send an error code to lightningd in
addition to the error message. Introduce two new error codes, replacing
the catch-all -1.

This change, together with
ElementsProject#3395
will implement ElementsProject#3366

Changelog-Changed: The `connect` command now returns its own error codes instead of a generic -1.
  • Loading branch information
vasild committed Jan 13, 2020
1 parent 2d45b13 commit c602a9f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
4 changes: 4 additions & 0 deletions common/jsonrpc_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#define FUNDING_BROADCAST_FAIL 303
#define FUNDING_STILL_SYNCING_BITCOIN 304

/* `connect` errors */
#define CONNECT_NO_KNOWN_ADDRESS 400
#define CONNECT_ALL_ADDRESSES_FAILED 401

/* Errors from `invoice` command */
#define INVOICE_LABEL_ALREADY_EXISTS 900
#define INVOICE_PREIMAGE_ALREADY_EXISTS 901
Expand Down
1 change: 1 addition & 0 deletions connectd/connect_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ msgdata,connectctl_connect_to_peer,addrhint,?wireaddr_internal,
# Connectd->master: connect failed.
msgtype,connectctl_connect_failed,2020
msgdata,connectctl_connect_failed,id,node_id,
msgdata,connectctl_connect_failed,failcode,u32,
msgdata,connectctl_connect_failed,failreason,wirestring,
msgdata,connectctl_connect_failed,seconds_to_delay,u32,
msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal,
Expand Down
19 changes: 12 additions & 7 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <common/daemon_conn.h>
#include <common/decode_array.h>
#include <common/features.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.h>
#include <common/ping.h>
#include <common/pseudorand.h>
Expand Down Expand Up @@ -565,22 +566,24 @@ static void connect_failed(struct daemon *daemon,
const struct node_id *id,
u32 seconds_waited,
const struct wireaddr_internal *addrhint,
u32 errcode,
const char *errfmt, ...)
PRINTF_FMT(5,6);
PRINTF_FMT(6,7);

static void connect_failed(struct daemon *daemon,
const struct node_id *id,
u32 seconds_waited,
const struct wireaddr_internal *addrhint,
u32 errcode,
const char *errfmt, ...)
{
u8 *msg;
va_list ap;
char *err;
char *errmsg;
u32 wait_seconds;

va_start(ap, errfmt);
err = tal_vfmt(tmpctx, errfmt, ap);
errmsg = tal_vfmt(tmpctx, errfmt, ap);
va_end(ap);

/* Wait twice as long to reconnect, between min and max. */
Expand All @@ -594,11 +597,11 @@ static void connect_failed(struct daemon *daemon,
* happened. We leave it to lightningd to decide if it wants to try
* again, with the wait_seconds as a hint of how long before
* asking. */
msg = towire_connectctl_connect_failed(NULL, id, err, wait_seconds,
addrhint);
msg = towire_connectctl_connect_failed(NULL, id, errcode, errmsg,
wait_seconds, addrhint);
daemon_conn_send(daemon->master, take(msg));

status_peer_debug(id, "Failed connected out: %s", err);
status_peer_debug(id, "Failed connected out: %s", errmsg);
}

/*~ This is the destructor for the (unsuccessful) connection. We accumulate
Expand Down Expand Up @@ -717,7 +720,8 @@ static void try_connect_one_addr(struct connecting *connect)
if (connect->addrnum == tal_count(connect->addrs)) {
connect_failed(connect->daemon, &connect->id,
connect->seconds_waited,
connect->addrhint, "%s", connect->errors);
connect->addrhint, CONNECT_ALL_ADDRESSES_FAILED,
"%s", connect->errors);
tal_free(connect);
return;
}
Expand Down Expand Up @@ -1428,6 +1432,7 @@ static void try_connect_peer(struct daemon *daemon,
* to retry; an address may get gossiped or appear on the DNS seed. */
if (tal_count(addrs) == 0) {
connect_failed(daemon, id, seconds_waited, addrhint,
CONNECT_NO_KNOWN_ADDRESS,
"Unable to connect, no address known for peer");
return;
}
Expand Down
37 changes: 32 additions & 5 deletions doc/lightning-connect.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions doc/lightning-connect.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ RETURN VALUE

On success the peer *id* is returned.

The following error codes may occur:
- -1: Catchall nonspecific error. This may occur if the host is not
valid or there are problems communicating with the peer. **connect**
will make up to 10 attempts to connect to the peer before giving up.
ERRORS
------

On failure, one of the following errors will be returned:

{ "code" : 400, "message" : "Unable to connect, no address known for peer" }

If some addresses are known but connecting to all of them failed, the message
will contain details about the failures:

{ "code" : 401, "message" : "..." }

If the given parameters are wrong:

{ "code" : -32602, "message" : "..." }

AUTHOR
------
Expand Down
11 changes: 7 additions & 4 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <errno.h>
#include <hsmd/capabilities.h>
#include <hsmd/gen_hsm_wire.h>
#include <inttypes.h>
#include <lightningd/channel.h>
#include <lightningd/connect_control.h>
#include <lightningd/hsm_control.h>
Expand Down Expand Up @@ -231,21 +232,23 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay,
static void connect_failed(struct lightningd *ld, const u8 *msg)
{
struct node_id id;
char *err;
u32 errcode;
char *errmsg;
struct connect *c;
u32 seconds_to_delay;
struct wireaddr_internal *addrhint;
struct channel *channel;

if (!fromwire_connectctl_connect_failed(tmpctx, msg, &id, &err,
&seconds_to_delay, &addrhint))
if (!fromwire_connectctl_connect_failed(tmpctx, msg, &id, &errcode, &errmsg,
&seconds_to_delay, &addrhint) ||
errcode > INT_MAX)
fatal("Connect gave bad CONNECTCTL_CONNECT_FAILED message %s",
tal_hex(msg, msg));

/* We can have multiple connect commands: fail them all */
while ((c = find_connect(ld, &id)) != NULL) {
/* They delete themselves from list */
was_pending(command_fail(c->cmd, LIGHTNINGD, "%s", err));
was_pending(command_fail(c->cmd, (int)errcode, "%s", errmsg));
}

/* If we have an active channel, then reconnect. */
Expand Down

0 comments on commit c602a9f

Please sign in to comment.