Browse Source

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.
travis-debug
Vasil Dimov 5 years ago
committed by Christian Decker
parent
commit
fc75d8a9e6
  1. 4
      common/jsonrpc_errors.h
  2. 1
      connectd/connect_wire.csv
  3. 19
      connectd/connectd.c
  4. 37
      doc/lightning-connect.7
  5. 19
      doc/lightning-connect.7.md
  6. 11
      lightningd/connect_control.c

4
common/jsonrpc_errors.h

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

1
connectd/connect_wire.csv

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

Can't render this file because it has a wrong number of fields in line 6.

19
connectd/connectd.c

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

37
doc/lightning-connect.7

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

19
doc/lightning-connect.7.md

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

11
lightningd/connect_control.c

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

Loading…
Cancel
Save