Browse Source

Use dedicated type for error codes

Before this patch we used `int` for error codes. The problem with
`int` is that we try to pass it to/from wire and the size of `int` is
not defined by the standard. So a sender with 4-byte `int` would write
4 bytes to the wire and a receiver with 2-byte `int` (for example) would
read just 2 bytes from the wire.

To resolve this:

* Introduce an error code type with a known size:
  `typedef s32 errcode_t`.

* Change all error code macros to constants of type `errcode_t`.
  Constants also play better with gdb - it would visualize the name of
  the constant instead of the numeric value.

* Change all functions that take error codes to take the new type
  `errcode_t` instead of `int`.

* Introduce towire / fromwire functions to send / receive the newly added
  type `errcode_t` and use it instead of `towire_int()`.

In addition:

* Remove the now unneeded `towire_int()`.

* Replace a hardcoded error code `-2` with a new constant
  `INVOICE_EXPIRED_DURING_WAIT` (903).

Changelog-Changed: The waitinvoice command would now return error code 903 to designate that the invoice expired during wait, instead of the previous -2
travis-debug
Vasil Dimov 5 years ago
committed by ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent
commit
55173a56b7
  1. 9
      common/Makefile
  2. 13
      common/errcode.h
  3. 51
      common/json.c
  4. 8
      common/json.h
  5. 3
      common/json_command.h
  6. 64
      common/jsonrpc_errors.h
  7. 3
      common/test/run-param.c
  8. 2
      connectd/connect_wire.csv
  9. 5
      connectd/connectd.c
  10. 24
      doc/lightning-waitinvoice.7
  11. 10
      doc/lightning-waitinvoice.7.md
  12. 3
      lightningd/connect_control.c
  13. 3
      lightningd/invoice.c
  14. 17
      lightningd/jsonrpc.c
  15. 5
      lightningd/jsonrpc.h
  16. 8
      lightningd/notification.c
  17. 3
      lightningd/notification.h
  18. 14
      lightningd/pay.c
  19. 3
      lightningd/pay.h
  20. 3
      lightningd/test/run-invoice-select-inchan.c
  21. 2
      lightningd/test/run-log-pruning.c
  22. 6
      plugins/libplugin.c
  23. 3
      plugins/libplugin.h
  24. 10
      plugins/pay.c
  25. 4
      tools/generate-wire.py
  26. 3
      wallet/test/run-wallet.c
  27. 10
      wire/fromwire.c
  28. 5
      wire/towire.c
  29. 5
      wire/wire.h

9
common/Makefile

@ -68,7 +68,14 @@ COMMON_SRC_NOGEN := \
COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h common/gossip_constants.h COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \
common/errcode.h \
common/gossip_constants.h \
common/htlc.h \
common/json_command.h \
common/jsonrpc_errors.h \
common/overflows.h \
common/status_levels.h
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h
COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN) COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)

13
common/errcode.h

@ -0,0 +1,13 @@
#ifndef LIGHTNING_COMMON_ERRCODE_H
#define LIGHTNING_COMMON_ERRCODE_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <inttypes.h>
typedef s32 errcode_t;
#define PRIerrcode PRId32
#endif /* LIGHTNING_COMMON_ERRCODE_H */

51
common/json.c

@ -65,6 +65,30 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok,
return true; return true;
} }
bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num)
{
char *end;
long long l;
l = strtoll(buffer + tok->start, &end, 0);
if (end != buffer + tok->end)
return false;
BUILD_ASSERT(sizeof(l) >= sizeof(*num));
*num = l;
/* Check for overflow/underflow */
if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
return false;
/* Check if the number did not fit in `s64` (in case `long long`
is a bigger type). */
if (*num != l)
return false;
return true;
}
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num) bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num)
{ {
char *end; char *end;
@ -122,22 +146,29 @@ bool json_to_u32(const char *buffer, const jsmntok_t *tok,
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num) bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
{ {
char *end; s64 tmp;
long l;
l = strtol(buffer + tok->start, &end, 0); if (!json_to_s64(buffer, tok, &tmp))
if (end != buffer + tok->end)
return false; return false;
*num = tmp;
BUILD_ASSERT(sizeof(l) >= sizeof(*num)); /* Just in case it doesn't fit. */
*num = l; if (*num != tmp)
return false;
/* Check for overflow/underflow */ return true;
if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) }
bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcode)
{
s64 tmp;
if (!json_to_s64(buffer, tok, &tmp))
return false; return false;
*errcode = tmp;
/* Check for truncation */ /* Just in case it doesn't fit. */
if (*num != l) if (*errcode != tmp)
return false; return false;
return true; return true;

8
common/json.h

@ -3,7 +3,9 @@
#include "config.h" #include "config.h"
#include <bitcoin/preimage.h> #include <bitcoin/preimage.h>
#include <bitcoin/privkey.h> #include <bitcoin/privkey.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/errcode.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -37,6 +39,9 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok,
bool json_to_u64(const char *buffer, const jsmntok_t *tok, bool json_to_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num); uint64_t *num);
/* Extract signed 64 bit integer from this (may be a string, or a number literal) */
bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num);
/* Extract number from this (may be a string, or a number literal) */ /* Extract number from this (may be a string, or a number literal) */
bool json_to_u32(const char *buffer, const jsmntok_t *tok, bool json_to_u32(const char *buffer, const jsmntok_t *tok,
uint32_t *num); uint32_t *num);
@ -51,6 +56,9 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);
/* Extract signed integer from this (may be a string, or a number literal) */ /* Extract signed integer from this (may be a string, or a number literal) */
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num); bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num);
/* Extract an error code from this (may be a string, or a number literal) */
bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcode);
/* Extract boolean from this */ /* Extract boolean from this */
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b);

3
common/json_command.h

@ -4,13 +4,14 @@
#define LIGHTNING_COMMON_JSON_COMMAND_H #define LIGHTNING_COMMON_JSON_COMMAND_H
#include "config.h" #include "config.h"
#include <ccan/compiler/compiler.h> #include <ccan/compiler/compiler.h>
#include <common/errcode.h>
#include <stdbool.h> #include <stdbool.h>
struct command; struct command;
struct command_result; struct command_result;
/* Caller supplied this: param assumes it can call it. */ /* Caller supplied this: param assumes it can call it. */
struct command_result *command_fail(struct command *cmd, int code, struct command_result *command_fail(struct command *cmd, errcode_t code,
const char *fmt, ...) const char *fmt, ...)
PRINTF_FMT(3, 4) WARN_UNUSED_RESULT; PRINTF_FMT(3, 4) WARN_UNUSED_RESULT;

64
common/jsonrpc_errors.h

@ -3,55 +3,59 @@
*/ */
#ifndef LIGHTNING_COMMON_JSONRPC_ERRORS_H #ifndef LIGHTNING_COMMON_JSONRPC_ERRORS_H
#define LIGHTNING_COMMON_JSONRPC_ERRORS_H #define LIGHTNING_COMMON_JSONRPC_ERRORS_H
#include "config.h" #include "config.h"
#include <common/errcode.h>
/* Standard errors defined by JSON-RPC 2.0 standard */ /* Standard errors defined by JSON-RPC 2.0 standard */
#define JSONRPC2_INVALID_REQUEST -32600 static const errcode_t JSONRPC2_INVALID_REQUEST = -32600;
#define JSONRPC2_METHOD_NOT_FOUND -32601 static const errcode_t JSONRPC2_METHOD_NOT_FOUND = -32601;
#define JSONRPC2_INVALID_PARAMS -32602 static const errcode_t JSONRPC2_INVALID_PARAMS = -32602;
/* Uncategorized error. /* Uncategorized error.
* FIXME: This should be replaced in all places * FIXME: This should be replaced in all places
* with a specific error code, and then removed. * with a specific error code, and then removed.
*/ */
#define LIGHTNINGD -1 static const errcode_t LIGHTNINGD = -1;
/* Developer error in the parameters to param() call */ /* Developer error in the parameters to param() call */
#define PARAM_DEV_ERROR -2 static const errcode_t PARAM_DEV_ERROR = -2;
/* Plugin returned an error */ /* Plugin returned an error */
#define PLUGIN_ERROR -3 static const errcode_t PLUGIN_ERROR = -3;
/* Errors from `pay`, `sendpay`, or `waitsendpay` commands */ /* Errors from `pay`, `sendpay`, or `waitsendpay` commands */
#define PAY_IN_PROGRESS 200 static const errcode_t PAY_IN_PROGRESS = 200;
#define PAY_RHASH_ALREADY_USED 201 static const errcode_t PAY_RHASH_ALREADY_USED = 201;
#define PAY_UNPARSEABLE_ONION 202 static const errcode_t PAY_UNPARSEABLE_ONION = 202;
#define PAY_DESTINATION_PERM_FAIL 203 static const errcode_t PAY_DESTINATION_PERM_FAIL = 203;
#define PAY_TRY_OTHER_ROUTE 204 static const errcode_t PAY_TRY_OTHER_ROUTE = 204;
#define PAY_ROUTE_NOT_FOUND 205 static const errcode_t PAY_ROUTE_NOT_FOUND = 205;
#define PAY_ROUTE_TOO_EXPENSIVE 206 static const errcode_t PAY_ROUTE_TOO_EXPENSIVE = 206;
#define PAY_INVOICE_EXPIRED 207 static const errcode_t PAY_INVOICE_EXPIRED = 207;
#define PAY_NO_SUCH_PAYMENT 208 static const errcode_t PAY_NO_SUCH_PAYMENT = 208;
#define PAY_UNSPECIFIED_ERROR 209 static const errcode_t PAY_UNSPECIFIED_ERROR = 209;
#define PAY_STOPPED_RETRYING 210 static const errcode_t PAY_STOPPED_RETRYING = 210;
/* `fundchannel` or `withdraw` errors */ /* `fundchannel` or `withdraw` errors */
#define FUND_MAX_EXCEEDED 300 static const errcode_t FUND_MAX_EXCEEDED = 300;
#define FUND_CANNOT_AFFORD 301 static const errcode_t FUND_CANNOT_AFFORD = 301;
#define FUND_OUTPUT_IS_DUST 302 static const errcode_t FUND_OUTPUT_IS_DUST = 302;
#define FUNDING_BROADCAST_FAIL 303 static const errcode_t FUNDING_BROADCAST_FAIL = 303;
#define FUNDING_STILL_SYNCING_BITCOIN 304 static const errcode_t FUNDING_STILL_SYNCING_BITCOIN = 304;
#define FUNDING_PEER_NOT_CONNECTED 305 static const errcode_t FUNDING_PEER_NOT_CONNECTED = 305;
#define FUNDING_UNKNOWN_PEER 306 static const errcode_t FUNDING_UNKNOWN_PEER = 306;
/* `connect` errors */ /* `connect` errors */
#define CONNECT_NO_KNOWN_ADDRESS 400 static const errcode_t CONNECT_NO_KNOWN_ADDRESS = 400;
#define CONNECT_ALL_ADDRESSES_FAILED 401 static const errcode_t CONNECT_ALL_ADDRESSES_FAILED = 401;
/* Errors from `invoice` command */ /* Errors from `invoice` command */
#define INVOICE_LABEL_ALREADY_EXISTS 900 static const errcode_t INVOICE_LABEL_ALREADY_EXISTS = 900;
#define INVOICE_PREIMAGE_ALREADY_EXISTS 901 static const errcode_t INVOICE_PREIMAGE_ALREADY_EXISTS = 901;
#define INVOICE_HINTS_GAVE_NO_ROUTES 902 static const errcode_t INVOICE_HINTS_GAVE_NO_ROUTES = 902;
#define INVOICE_WAIT_TIMED_OUT 904 static const errcode_t INVOICE_EXPIRED_DURING_WAIT = 903;
static const errcode_t INVOICE_WAIT_TIMED_OUT = 904;
#endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */ #endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */

3
common/test/run-param.c

@ -5,6 +5,7 @@
#include "../param.c" #include "../param.c"
#include <ccan/array_size/array_size.h> #include <ccan/array_size/array_size.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <common/errcode.h>
#include <common/json.h> #include <common/json.h>
#include <setjmp.h> #include <setjmp.h>
#include <signal.h> #include <signal.h>
@ -28,7 +29,7 @@ struct command_result {
static struct command_result cmd_failed; static struct command_result cmd_failed;
struct command_result *command_fail(struct command *cmd, struct command_result *command_fail(struct command *cmd,
int code, const char *fmt, ...) errcode_t code, const char *fmt, ...)
{ {
failed = true; failed = true;
va_list ap; va_list ap;

2
connectd/connect_wire.csv

@ -44,7 +44,7 @@ msgdata,connectctl_connect_to_peer,addrhint,?wireaddr_internal,
# Connectd->master: connect failed. # Connectd->master: connect failed.
msgtype,connectctl_connect_failed,2020 msgtype,connectctl_connect_failed,2020
msgdata,connectctl_connect_failed,id,node_id, msgdata,connectctl_connect_failed,id,node_id,
msgdata,connectctl_connect_failed,failcode,int, msgdata,connectctl_connect_failed,failcode,errcode_t,
msgdata,connectctl_connect_failed,failreason,wirestring, msgdata,connectctl_connect_failed,failreason,wirestring,
msgdata,connectctl_connect_failed,seconds_to_delay,u32, msgdata,connectctl_connect_failed,seconds_to_delay,u32,
msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal, msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal,

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

5
connectd/connectd.c

@ -29,6 +29,7 @@
#include <common/cryptomsg.h> #include <common/cryptomsg.h>
#include <common/daemon_conn.h> #include <common/daemon_conn.h>
#include <common/decode_array.h> #include <common/decode_array.h>
#include <common/errcode.h>
#include <common/features.h> #include <common/features.h>
#include <common/jsonrpc_errors.h> #include <common/jsonrpc_errors.h>
#include <common/memleak.h> #include <common/memleak.h>
@ -566,7 +567,7 @@ static void connect_failed(struct daemon *daemon,
const struct node_id *id, const struct node_id *id,
u32 seconds_waited, u32 seconds_waited,
const struct wireaddr_internal *addrhint, const struct wireaddr_internal *addrhint,
int errcode, errcode_t errcode,
const char *errfmt, ...) const char *errfmt, ...)
PRINTF_FMT(6,7); PRINTF_FMT(6,7);
@ -574,7 +575,7 @@ static void connect_failed(struct daemon *daemon,
const struct node_id *id, const struct node_id *id,
u32 seconds_waited, u32 seconds_waited,
const struct wireaddr_internal *addrhint, const struct wireaddr_internal *addrhint,
int errcode, errcode_t errcode,
const char *errfmt, ...) const char *errfmt, ...)
{ {
u8 *msg; u8 *msg;

24
doc/lightning-waitinvoice.7

@ -16,14 +16,18 @@ On success, an invoice description will be returned as per
\fBlightning-listinvoice\fR(7)\. The \fIstatus\fR field will be \fIpaid\fR\. \fBlightning-listinvoice\fR(7)\. The \fIstatus\fR field will be \fIpaid\fR\.
If the invoice is deleted while unpaid, or the invoice does not exist, On error the returned object will contain \fBcode\fR and \fBmessage\fR properties,
this command will return with an error with code -1\. with \fBcode\fR being one of the following:
.RS
If the invoice expires before being paid, or is already expired, this .IP \[bu]
command will return with an error with code -2, with the data being the -32602: If the given parameters are wrong\.
invoice data as per \fBlistinvoice\fR\. .IP \[bu]
-1: If the invoice is deleted while unpaid, or the invoice does not exist\.
.IP \[bu]
903: If the invoice expires before being paid, or is already expired\.
.RE
.SH AUTHOR .SH AUTHOR
Christian Decker \fI<decker.christian@gmail.com\fR> is mainly Christian Decker \fI<decker.christian@gmail.com\fR> is mainly
@ -38,7 +42,3 @@ responsible\.
Main web site: \fIhttps://github.com/ElementsProject/lightning\fR Main web site: \fIhttps://github.com/ElementsProject/lightning\fR
.HL
Last updated 2019-04-07 14:23:17 CEST

10
doc/lightning-waitinvoice.7.md

@ -18,12 +18,12 @@ RETURN VALUE
On success, an invoice description will be returned as per On success, an invoice description will be returned as per
lightning-listinvoice(7). The *status* field will be *paid*. lightning-listinvoice(7). The *status* field will be *paid*.
If the invoice is deleted while unpaid, or the invoice does not exist, On error the returned object will contain `code` and `message` properties,
this command will return with an error with code -1. with `code` being one of the following:
If the invoice expires before being paid, or is already expired, this - -32602: If the given parameters are wrong.
command will return with an error with code -2, with the data being the - -1: If the invoice is deleted while unpaid, or the invoice does not exist.
invoice data as per **listinvoice**. - 903: If the invoice expires before being paid, or is already expired.
AUTHOR AUTHOR
------ ------

3
lightningd/connect_control.c

@ -2,6 +2,7 @@
#include <ccan/fdpass/fdpass.h> #include <ccan/fdpass/fdpass.h>
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/errcode.h>
#include <common/features.h> #include <common/features.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_helpers.h> #include <common/json_helpers.h>
@ -231,7 +232,7 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay,
static void connect_failed(struct lightningd *ld, const u8 *msg) static void connect_failed(struct lightningd *ld, const u8 *msg)
{ {
struct node_id id; struct node_id id;
int errcode; errcode_t errcode;
char *errmsg; char *errmsg;
struct connect *c; struct connect *c;
u32 seconds_to_delay; u32 seconds_to_delay;

3
lightningd/invoice.c

@ -82,8 +82,7 @@ static struct command_result *tell_waiter(struct command *cmd,
json_add_invoice(response, details); json_add_invoice(response, details);
return command_success(cmd, response); return command_success(cmd, response);
} else { } else {
/* FIXME: -2 should be a constant in jsonrpc_errors.h. */ response = json_stream_fail(cmd, INVOICE_EXPIRED_DURING_WAIT,
response = json_stream_fail(cmd, -2,
"invoice expired during wait"); "invoice expired during wait");
json_add_invoice(response, details); json_add_invoice(response, details);
json_object_end(response); json_object_end(response);

17
lightningd/jsonrpc.c

@ -464,7 +464,7 @@ struct command_result *command_failed(struct command *cmd,
return command_raw_complete(cmd, result); return command_raw_complete(cmd, result);
} }
struct command_result *command_fail(struct command *cmd, int code, struct command_result *command_fail(struct command *cmd, errcode_t code,
const char *fmt, ...) const char *fmt, ...)
{ {
const char *errmsg; const char *errmsg;
@ -502,7 +502,7 @@ static void json_command_malformed(struct json_connection *jcon,
json_add_string(js, "jsonrpc", "2.0"); json_add_string(js, "jsonrpc", "2.0");
json_add_literal(js, "id", id, strlen(id)); json_add_literal(js, "id", id, strlen(id));
json_object_start(js, "error"); json_object_start(js, "error");
json_add_member(js, "code", false, "%d", JSONRPC2_INVALID_REQUEST); json_add_member(js, "code", false, "%" PRIerrcode, JSONRPC2_INVALID_REQUEST);
json_add_string(js, "message", error); json_add_string(js, "message", error);
json_object_end(js); json_object_end(js);
json_object_compat_end(js); json_object_compat_end(js);
@ -553,7 +553,7 @@ struct json_stream *json_stream_success(struct command *cmd)
} }
struct json_stream *json_stream_fail_nodata(struct command *cmd, struct json_stream *json_stream_fail_nodata(struct command *cmd,
int code, errcode_t code,
const char *errmsg) const char *errmsg)
{ {
struct json_stream *js = json_start(cmd); struct json_stream *js = json_start(cmd);
@ -561,14 +561,14 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd,
assert(code); assert(code);
json_object_start(js, "error"); json_object_start(js, "error");
json_add_member(js, "code", false, "%d", code); json_add_member(js, "code", false, "%" PRIerrcode, code);
json_add_string(js, "message", errmsg); json_add_string(js, "message", errmsg);
return js; return js;
} }
struct json_stream *json_stream_fail(struct command *cmd, struct json_stream *json_stream_fail(struct command *cmd,
int code, errcode_t code,
const char *errmsg) const char *errmsg)
{ {
struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg); struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg);
@ -704,10 +704,11 @@ rpc_command_hook_callback(struct rpc_command_hook_payload *p,
custom_return = json_get_member(buffer, tok, "error"); custom_return = json_get_member(buffer, tok, "error");
if (custom_return) { if (custom_return) {
int code; errcode_t code;
const char *errmsg; const char *errmsg;
if (!json_to_int(buffer, json_get_member(buffer, custom_return, "code"), if (!json_to_errcode(buffer,
&code)) json_get_member(buffer, custom_return, "code"),
&code))
return was_pending(command_fail(p->cmd, JSONRPC2_INVALID_REQUEST, return was_pending(command_fail(p->cmd, JSONRPC2_INVALID_REQUEST,
"Bad response to 'rpc_command' hook: " "Bad response to 'rpc_command' hook: "
"'error' object does not contain a code.")); "'error' object does not contain a code."));

5
lightningd/jsonrpc.h

@ -4,6 +4,7 @@
#include <bitcoin/chainparams.h> #include <bitcoin/chainparams.h>
#include <ccan/autodata/autodata.h> #include <ccan/autodata/autodata.h>
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include <common/errcode.h>
#include <common/json.h> #include <common/json.h>
#include <lightningd/json_stream.h> #include <lightningd/json_stream.h>
#include <stdarg.h> #include <stdarg.h>
@ -96,7 +97,7 @@ struct json_stream *json_stream_success(struct command *cmd);
* You need to json_object_end() once you're done! * You need to json_object_end() once you're done!
*/ */
struct json_stream *json_stream_fail(struct command *cmd, struct json_stream *json_stream_fail(struct command *cmd,
int code, errcode_t code,
const char *errmsg); const char *errmsg);
/** /**
@ -108,7 +109,7 @@ struct json_stream *json_stream_fail(struct command *cmd,
* This is used by command_fail(), which doesn't add any JSON data. * This is used by command_fail(), which doesn't add any JSON data.
*/ */
struct json_stream *json_stream_fail_nodata(struct command *cmd, struct json_stream *json_stream_fail_nodata(struct command *cmd,
int code, errcode_t code,
const char *errmsg); const char *errmsg);
/* These returned values are never NULL. */ /* These returned values are never NULL. */

8
lightningd/notification.c

@ -254,7 +254,7 @@ void notify_sendpay_success(struct lightningd *ld,
static void sendpay_failure_notification_serialize(struct json_stream *stream, static void sendpay_failure_notification_serialize(struct json_stream *stream,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail, const struct routing_failure *fail,
char *errmsg) char *errmsg)
@ -263,7 +263,7 @@ static void sendpay_failure_notification_serialize(struct json_stream *stream,
/* In line with the format of json error returned /* In line with the format of json error returned
* by sendpay_fail(). */ * by sendpay_fail(). */
json_add_member(stream, "code", false, "%d", pay_errcode); json_add_member(stream, "code", false, "%" PRIerrcode, pay_errcode);
json_add_string(stream, "message", errmsg); json_add_string(stream, "message", errmsg);
json_object_start(stream, "data"); json_object_start(stream, "data");
@ -282,14 +282,14 @@ REGISTER_NOTIFICATION(sendpay_failure,
void notify_sendpay_failure(struct lightningd *ld, void notify_sendpay_failure(struct lightningd *ld,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail, const struct routing_failure *fail,
const char *errmsg) const char *errmsg)
{ {
void (*serialize)(struct json_stream *, void (*serialize)(struct json_stream *,
const struct wallet_payment *, const struct wallet_payment *,
int, errcode_t,
const struct onionreply *, const struct onionreply *,
const struct routing_failure *, const struct routing_failure *,
const char *) = sendpay_failure_notification_gen.serialize; const char *) = sendpay_failure_notification_gen.serialize;

3
lightningd/notification.h

@ -7,6 +7,7 @@
#include <ccan/json_escape/json_escape.h> #include <ccan/json_escape/json_escape.h>
#include <ccan/time/time.h> #include <ccan/time/time.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/errcode.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <lightningd/htlc_end.h> #include <lightningd/htlc_end.h>
#include <lightningd/jsonrpc.h> #include <lightningd/jsonrpc.h>
@ -62,7 +63,7 @@ void notify_sendpay_success(struct lightningd *ld,
void notify_sendpay_failure(struct lightningd *ld, void notify_sendpay_failure(struct lightningd *ld,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail, const struct routing_failure *fail,
const char *errmsg); const char *errmsg);

14
lightningd/pay.c

@ -172,7 +172,7 @@ json_add_routefail_info(struct json_stream *js,
void json_sendpay_fail_fields(struct json_stream *js, void json_sendpay_fail_fields(struct json_stream *js,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail) const struct routing_failure *fail)
{ {
@ -191,7 +191,7 @@ void json_sendpay_fail_fields(struct json_stream *js,
fail->msg); fail->msg);
} }
static const char *sendpay_errmsg_fmt(const tal_t *ctx, int pay_errcode, static const char *sendpay_errmsg_fmt(const tal_t *ctx, errcode_t pay_errcode,
const struct routing_failure *fail, const struct routing_failure *fail,
const char *details) const char *details)
{ {
@ -210,7 +210,7 @@ static const char *sendpay_errmsg_fmt(const tal_t *ctx, int pay_errcode,
static struct command_result * static struct command_result *
sendpay_fail(struct command *cmd, sendpay_fail(struct command *cmd,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail, const struct routing_failure *fail,
const char *errmsg) const char *errmsg)
@ -243,7 +243,7 @@ json_sendpay_in_progress(struct command *cmd,
static void tell_waiters_failed(struct lightningd *ld, static void tell_waiters_failed(struct lightningd *ld,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
const struct wallet_payment *payment, const struct wallet_payment *payment,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail, const struct routing_failure *fail,
const char *details) const char *details)
@ -377,7 +377,7 @@ remote_routing_failure(const tal_t *ctx,
const u8 *failuremsg, const u8 *failuremsg,
int origin_index, int origin_index,
struct log *log, struct log *log,
int *pay_errcode) errcode_t *pay_errcode)
{ {
enum onion_type failcode = fromwire_peektype(failuremsg); enum onion_type failcode = fromwire_peektype(failuremsg);
struct routing_failure *routing_failure; struct routing_failure *routing_failure;
@ -539,7 +539,7 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
struct wallet_payment *payment; struct wallet_payment *payment;
struct routing_failure* fail = NULL; struct routing_failure* fail = NULL;
const char *failmsg; const char *failmsg;
int pay_errcode; errcode_t pay_errcode;
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash, &hout->payment_hash,
@ -662,7 +662,7 @@ static struct command_result *wait_payment(struct lightningd *ld,
char *faildetail; char *faildetail;
struct routing_failure *fail; struct routing_failure *fail;
int faildirection; int faildirection;
int rpcerrorcode; errcode_t rpcerrorcode;
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
payment_hash, partid); payment_hash, partid);

3
lightningd/pay.h

@ -2,6 +2,7 @@
#define LIGHTNING_LIGHTNINGD_PAY_H #define LIGHTNING_LIGHTNINGD_PAY_H
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <common/errcode.h>
struct htlc_out; struct htlc_out;
struct lightningd; struct lightningd;
@ -28,7 +29,7 @@ void json_add_payment_fields(struct json_stream *response,
/* This json will be also used in 'sendpay_failure' notifictaion. */ /* This json will be also used in 'sendpay_failure' notifictaion. */
void json_sendpay_fail_fields(struct json_stream *js, void json_sendpay_fail_fields(struct json_stream *js,
const struct wallet_payment *t, const struct wallet_payment *t,
int pay_errcode, errcode_t pay_errcode,
const struct onionreply *onionreply, const struct onionreply *onionreply,
const struct routing_failure *fail); const struct routing_failure *fail);

3
lightningd/test/run-invoice-select-inchan.c

@ -2,6 +2,7 @@
#include "../invoice.c" #include "../invoice.c"
#include "../peer_control.c" #include "../peer_control.c"
#include <ccan/alignof/alignof.h> #include <ccan/alignof/alignof.h>
#include <common/errcode.h>
bool deprecated_apis = false; bool deprecated_apis = false;
@ -50,7 +51,7 @@ bool channel_tell_depth(struct lightningd *ld UNNEEDED,
bool command_check_only(const struct command *cmd UNNEEDED) bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); } { fprintf(stderr, "command_check_only called!\n"); abort(); }
/* Generated stub for command_fail */ /* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, int code UNNEEDED, struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "command_fail called!\n"); abort(); } { fprintf(stderr, "command_fail called!\n"); abort(); }

2
lightningd/test/run-log-pruning.c

@ -8,7 +8,7 @@ size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNN
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED) size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); } { fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for command_fail */ /* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, int code UNNEEDED, struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "command_fail called!\n"); abort(); } { fprintf(stderr, "command_fail called!\n"); abort(); }

6
plugins/libplugin.c

@ -251,14 +251,14 @@ command_success_str(struct command *cmd, const char *str)
} }
struct command_result *command_done_err(struct command *cmd, struct command_result *command_done_err(struct command *cmd,
int code, errcode_t code,
const char *errmsg, const char *errmsg,
const struct json_out *data) const struct json_out *data)
{ {
struct json_out *jout = start_json_rpc(cmd, *cmd->id); struct json_out *jout = start_json_rpc(cmd, *cmd->id);
json_out_start(jout, "error", '{'); json_out_start(jout, "error", '{');
json_out_add(jout, "code", false, "%d", code); json_out_add(jout, "code", false, "%" PRIerrcode, code);
json_out_addstr(jout, "message", errmsg); json_out_addstr(jout, "message", errmsg);
if (data) if (data)
@ -305,7 +305,7 @@ struct command_result *forward_result(struct command *cmd,
/* Called by param() directly if it's malformed. */ /* Called by param() directly if it's malformed. */
struct command_result *command_fail(struct command *cmd, struct command_result *command_fail(struct command *cmd,
int code, const char *fmt, ...) errcode_t code, const char *fmt, ...)
{ {
va_list ap; va_list ap;
struct command_result *res; struct command_result *res;

3
plugins/libplugin.h

@ -4,6 +4,7 @@
#include "config.h" #include "config.h"
#include <ccan/time/time.h> #include <ccan/time/time.h>
#include <common/errcode.h>
#include <common/json.h> #include <common/json.h>
#include <common/json_command.h> #include <common/json_command.h>
#include <common/json_helpers.h> #include <common/json_helpers.h>
@ -74,7 +75,7 @@ void NORETURN plugin_err(const char *fmt, ...);
* NULL, data can be NULL; otherwise it must be a JSON object. */ * NULL, data can be NULL; otherwise it must be a JSON object. */
struct command_result *WARN_UNUSED_RESULT struct command_result *WARN_UNUSED_RESULT
command_done_err(struct command *cmd, command_done_err(struct command *cmd,
int code, errcode_t code,
const char *errmsg, const char *errmsg,
const struct json_out *data); const struct json_out *data);

10
plugins/pay.c

@ -7,6 +7,7 @@
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/bolt11.h> #include <common/bolt11.h>
#include <common/errcode.h>
#include <common/features.h> #include <common/features.h>
#include <common/gossip_constants.h> #include <common/gossip_constants.h>
#include <common/pseudorand.h> #include <common/pseudorand.h>
@ -425,13 +426,14 @@ static struct command_result *waitsendpay_error(struct command *cmd,
{ {
struct pay_attempt *attempt = current_attempt(pc); struct pay_attempt *attempt = current_attempt(pc);
const jsmntok_t *codetok, *failcodetok, *nodeidtok, *scidtok, *dirtok; const jsmntok_t *codetok, *failcodetok, *nodeidtok, *scidtok, *dirtok;
int code, failcode; errcode_t code;
int failcode;
bool node_err = false; bool node_err = false;
attempt_failed_tok(pc, "waitsendpay", buf, error); attempt_failed_tok(pc, "waitsendpay", buf, error);
codetok = json_get_member(buf, error, "code"); codetok = json_get_member(buf, error, "code");
if (!json_to_int(buf, codetok, &code)) if (!json_to_errcode(buf, codetok, &code))
plugin_err("waitsendpay error gave no 'code'? '%.*s'", plugin_err("waitsendpay error gave no 'code'? '%.*s'",
error->end - error->start, buf + error->start); error->end - error->start, buf + error->start);
@ -849,13 +851,13 @@ static struct command_result *getroute_error(struct command *cmd,
const jsmntok_t *error, const jsmntok_t *error,
struct pay_command *pc) struct pay_command *pc)
{ {
int code; errcode_t code;
const jsmntok_t *codetok; const jsmntok_t *codetok;
attempt_failed_tok(pc, "getroute", buf, error); attempt_failed_tok(pc, "getroute", buf, error);
codetok = json_get_member(buf, error, "code"); codetok = json_get_member(buf, error, "code");
if (!json_to_int(buf, codetok, &code)) if (!json_to_errcode(buf, codetok, &code))
plugin_err("getroute error gave no 'code'? '%.*s'", plugin_err("getroute error gave no 'code'? '%.*s'",
error->end - error->start, buf + error->start); error->end - error->start, buf + error->start);

4
tools/generate-wire.py

@ -185,7 +185,7 @@ class Type(FieldSet):
'bool', 'bool',
'amount_sat', 'amount_sat',
'amount_msat', 'amount_msat',
'int', 'errcode_t',
'bigsize', 'bigsize',
'varint' 'varint'
] ]
@ -200,7 +200,7 @@ class Type(FieldSet):
'secp256k1_ecdsa_recoverable_signature', 'secp256k1_ecdsa_recoverable_signature',
'wirestring', 'wirestring',
'double', 'double',
'int', 'errcode_t',
'bigsize', 'bigsize',
'varint', 'varint',
] ]

3
wallet/test/run-wallet.c

@ -20,6 +20,7 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const s
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/errcode.h>
#include <common/memleak.h> #include <common/memleak.h>
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
@ -61,7 +62,7 @@ bool channel_tell_depth(struct lightningd *ld UNNEEDED,
bool command_check_only(const struct command *cmd UNNEEDED) bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); } { fprintf(stderr, "command_check_only called!\n"); abort(); }
/* Generated stub for command_fail */ /* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, int code UNNEEDED, struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
const char *fmt UNNEEDED, ...) const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "command_fail called!\n"); abort(); } { fprintf(stderr, "command_fail called!\n"); abort(); }

10
wire/fromwire.c

@ -9,8 +9,10 @@
#include <ccan/crypto/siphash24/siphash24.h> #include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/errcode.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <common/utils.h> #include <common/utils.h>
@ -165,12 +167,12 @@ bool fromwire_bool(const u8 **cursor, size_t *max)
return ret; return ret;
} }
int fromwire_int(const u8 **cursor, size_t *max) errcode_t fromwire_errcode_t(const u8 **cursor, size_t *max)
{ {
int ret; errcode_t ret;
ret = (s32)fromwire_u32(cursor, max);
if (!fromwire(cursor, max, &ret, sizeof(ret)))
return 0;
return ret; return ret;
} }

5
wire/towire.c

@ -10,6 +10,7 @@
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/errcode.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <common/utils.h> #include <common/utils.h>
@ -87,9 +88,9 @@ void towire_bool(u8 **pptr, bool v)
towire(pptr, &val, sizeof(val)); towire(pptr, &val, sizeof(val));
} }
void towire_int(u8 **pptr, int v) void towire_errcode_t(u8 **pptr, errcode_t v)
{ {
towire(pptr, &v, sizeof(v)); towire_u32(pptr, (u32)v);
} }
void towire_bigsize(u8 **pptr, const bigsize_t val) void towire_bigsize(u8 **pptr, const bigsize_t val)

5
wire/wire.h

@ -13,6 +13,7 @@
#include <ccan/structeq/structeq.h> #include <ccan/structeq/structeq.h>
#include <common/amount.h> #include <common/amount.h>
#include <common/bigsize.h> #include <common/bigsize.h>
#include <common/errcode.h>
#include <common/node_id.h> #include <common/node_id.h>
#include <secp256k1_recovery.h> #include <secp256k1_recovery.h>
#include <stdlib.h> #include <stdlib.h>
@ -79,7 +80,7 @@ void towire_tu64(u8 **pptr, u64 v);
void towire_double(u8 **pptr, const double *v); void towire_double(u8 **pptr, const double *v);
void towire_pad(u8 **pptr, size_t num); void towire_pad(u8 **pptr, size_t num);
void towire_bool(u8 **pptr, bool v); void towire_bool(u8 **pptr, bool v);
void towire_int(u8 **pptr, int v); void towire_errcode_t(u8 **pptr, errcode_t v);
void towire_bigsize(u8 **pptr, const bigsize_t val); void towire_bigsize(u8 **pptr, const bigsize_t val);
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num);
@ -102,7 +103,7 @@ u32 fromwire_tu32(const u8 **cursor, size_t *max);
u64 fromwire_tu64(const u8 **cursor, size_t *max); u64 fromwire_tu64(const u8 **cursor, size_t *max);
void fromwire_double(const u8 **cursor, size_t *max, double *v); void fromwire_double(const u8 **cursor, size_t *max, double *v);
bool fromwire_bool(const u8 **cursor, size_t *max); bool fromwire_bool(const u8 **cursor, size_t *max);
int fromwire_int(const u8 **cursor, size_t *max); errcode_t fromwire_errcode_t(const u8 **cursor, size_t *max);
bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max); bigsize_t fromwire_bigsize(const u8 **cursor, size_t *max);
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret); void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey); void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);

Loading…
Cancel
Save