connectd: add own err codes instead of generic -1

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
https://github.com/ElementsProject/lightning/pull/3395
will implement https://github.com/ElementsProject/lightning/issues/3366

Changelog-Changed: The `connect` command now returns its own error codes instead of a generic -1.
This commit is contained in:
Vasil Dimov
2020-01-05 18:17:25 +01:00
committed by Christian Decker
parent 3cf91b23b9
commit fc75d8a9e6
6 changed files with 71 additions and 20 deletions

View File

@@ -44,6 +44,10 @@
#define FUNDING_PEER_NOT_CONNECTED 305
#define FUNDING_UNKNOWN_PEER 306
/* `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

View File

@@ -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,
1 #include <common/cryptomsg.h>
44 msgtype,connect_peer_connected,2002 # Connectd -> master: we got a peer. Three fds: peer, gossip and gossip_store
45 msgdata,connect_peer_connected,id,node_id, msgtype,connect_peer_connected,2002
46 msgdata,connect_peer_connected,addr,wireaddr_internal, msgdata,connect_peer_connected,id,node_id,
47 msgdata,connect_peer_connected,addr,wireaddr_internal,
48 msgdata,connect_peer_connected,pps,per_peer_state,
49 msgdata,connect_peer_connected,flen,u16,
50 msgdata,connect_peer_connected,features,u8,flen

View File

@@ -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>
@@ -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. */
@@ -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
@@ -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;
}
@@ -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;
}

View File

@@ -40,14 +40,41 @@ another node\. Once the peer is connected a channel can be opened with
On success the peer \fIid\fR is returned\.
.SH ERRORS
The following error codes may occur:
On failure, one of the following errors will be returned:
.IP \[bu]
-1: Catchall nonspecific error\. This may occur if the host is not
valid or there are problems communicating with the peer\. \fBconnect\fR
will make up to 10 attempts to connect to the peer before giving up\.
.nf
.RS
{ "code" : 400, "message" : "Unable to connect, no address known for peer" }
.RE
.fi
If some addresses are known but connecting to all of them failed, the message
will contain details about the failures:
.nf
.RS
{ "code" : 401, "message" : "..." }
.RE
.fi
If the given parameters are wrong:
.nf
.RS
{ "code" : -32602, "message" : "..." }
.RE
.fi
.SH AUTHOR
Rusty Russell \fI<rusty@rustcorp.com.au\fR> is mainly responsible\.

View File

@@ -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
------

View File

@@ -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>
@@ -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. */