diff --git a/common/Makefile b/common/Makefile index a309d5c5ec76..192ea4e81226 100644 --- a/common/Makefile +++ b/common/Makefile @@ -68,7 +68,14 @@ COMMON_SRC_NOGEN := \ 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 := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN) diff --git a/common/errcode.h b/common/errcode.h new file mode 100644 index 000000000000..78aa8dd38592 --- /dev/null +++ b/common/errcode.h @@ -0,0 +1,13 @@ +#ifndef LIGHTNING_COMMON_ERRCODE_H +#define LIGHTNING_COMMON_ERRCODE_H + +#include "config.h" + +#include +#include + +typedef s32 errcode_t; + +#define PRIerrcode PRId32 + +#endif /* LIGHTNING_COMMON_ERRCODE_H */ diff --git a/common/json.c b/common/json.c index af99b2e82574..cd504a5f3d41 100644 --- a/common/json.c +++ b/common/json.c @@ -65,6 +65,30 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, 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) { 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) { - char *end; - long l; + s64 tmp; - l = strtol(buffer + tok->start, &end, 0); - if (end != buffer + tok->end) + if (!json_to_s64(buffer, tok, &tmp)) return false; + *num = tmp; - BUILD_ASSERT(sizeof(l) >= sizeof(*num)); - *num = l; + /* Just in case it doesn't fit. */ + if (*num != tmp) + return false; - /* Check for overflow/underflow */ - if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) + return true; +} + +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; + *errcode = tmp; - /* Check for truncation */ - if (*num != l) + /* Just in case it doesn't fit. */ + if (*errcode != tmp) return false; return true; diff --git a/common/json.h b/common/json.h index 80548671af5c..857686d259f3 100644 --- a/common/json.h +++ b/common/json.h @@ -3,7 +3,9 @@ #include "config.h" #include #include +#include #include +#include #include #include #include @@ -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, 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) */ bool json_to_u32(const char *buffer, const jsmntok_t *tok, 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) */ 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 */ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); diff --git a/common/json_command.h b/common/json_command.h index f6dfc0314f5f..711f256438fb 100644 --- a/common/json_command.h +++ b/common/json_command.h @@ -4,13 +4,14 @@ #define LIGHTNING_COMMON_JSON_COMMAND_H #include "config.h" #include +#include #include struct command; struct command_result; /* 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, ...) PRINTF_FMT(3, 4) WARN_UNUSED_RESULT; diff --git a/common/jsonrpc_errors.h b/common/jsonrpc_errors.h index 08478e0000e7..791cdf3fccac 100644 --- a/common/jsonrpc_errors.h +++ b/common/jsonrpc_errors.h @@ -3,55 +3,59 @@ */ #ifndef LIGHTNING_COMMON_JSONRPC_ERRORS_H #define LIGHTNING_COMMON_JSONRPC_ERRORS_H + #include "config.h" +#include + /* Standard errors defined by JSON-RPC 2.0 standard */ -#define JSONRPC2_INVALID_REQUEST -32600 -#define JSONRPC2_METHOD_NOT_FOUND -32601 -#define JSONRPC2_INVALID_PARAMS -32602 +static const errcode_t JSONRPC2_INVALID_REQUEST = -32600; +static const errcode_t JSONRPC2_METHOD_NOT_FOUND = -32601; +static const errcode_t JSONRPC2_INVALID_PARAMS = -32602; /* Uncategorized error. * FIXME: This should be replaced in all places * 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 */ -#define PARAM_DEV_ERROR -2 +static const errcode_t PARAM_DEV_ERROR = -2; /* Plugin returned an error */ -#define PLUGIN_ERROR -3 +static const errcode_t PLUGIN_ERROR = -3; /* Errors from `pay`, `sendpay`, or `waitsendpay` commands */ -#define PAY_IN_PROGRESS 200 -#define PAY_RHASH_ALREADY_USED 201 -#define PAY_UNPARSEABLE_ONION 202 -#define PAY_DESTINATION_PERM_FAIL 203 -#define PAY_TRY_OTHER_ROUTE 204 -#define PAY_ROUTE_NOT_FOUND 205 -#define PAY_ROUTE_TOO_EXPENSIVE 206 -#define PAY_INVOICE_EXPIRED 207 -#define PAY_NO_SUCH_PAYMENT 208 -#define PAY_UNSPECIFIED_ERROR 209 -#define PAY_STOPPED_RETRYING 210 +static const errcode_t PAY_IN_PROGRESS = 200; +static const errcode_t PAY_RHASH_ALREADY_USED = 201; +static const errcode_t PAY_UNPARSEABLE_ONION = 202; +static const errcode_t PAY_DESTINATION_PERM_FAIL = 203; +static const errcode_t PAY_TRY_OTHER_ROUTE = 204; +static const errcode_t PAY_ROUTE_NOT_FOUND = 205; +static const errcode_t PAY_ROUTE_TOO_EXPENSIVE = 206; +static const errcode_t PAY_INVOICE_EXPIRED = 207; +static const errcode_t PAY_NO_SUCH_PAYMENT = 208; +static const errcode_t PAY_UNSPECIFIED_ERROR = 209; +static const errcode_t PAY_STOPPED_RETRYING = 210; /* `fundchannel` or `withdraw` errors */ -#define FUND_MAX_EXCEEDED 300 -#define FUND_CANNOT_AFFORD 301 -#define FUND_OUTPUT_IS_DUST 302 -#define FUNDING_BROADCAST_FAIL 303 -#define FUNDING_STILL_SYNCING_BITCOIN 304 -#define FUNDING_PEER_NOT_CONNECTED 305 -#define FUNDING_UNKNOWN_PEER 306 +static const errcode_t FUND_MAX_EXCEEDED = 300; +static const errcode_t FUND_CANNOT_AFFORD = 301; +static const errcode_t FUND_OUTPUT_IS_DUST = 302; +static const errcode_t FUNDING_BROADCAST_FAIL = 303; +static const errcode_t FUNDING_STILL_SYNCING_BITCOIN = 304; +static const errcode_t FUNDING_PEER_NOT_CONNECTED = 305; +static const errcode_t FUNDING_UNKNOWN_PEER = 306; /* `connect` errors */ -#define CONNECT_NO_KNOWN_ADDRESS 400 -#define CONNECT_ALL_ADDRESSES_FAILED 401 +static const errcode_t CONNECT_NO_KNOWN_ADDRESS = 400; +static const errcode_t CONNECT_ALL_ADDRESSES_FAILED = 401; /* Errors from `invoice` command */ -#define INVOICE_LABEL_ALREADY_EXISTS 900 -#define INVOICE_PREIMAGE_ALREADY_EXISTS 901 -#define INVOICE_HINTS_GAVE_NO_ROUTES 902 -#define INVOICE_WAIT_TIMED_OUT 904 +static const errcode_t INVOICE_LABEL_ALREADY_EXISTS = 900; +static const errcode_t INVOICE_PREIMAGE_ALREADY_EXISTS = 901; +static const errcode_t INVOICE_HINTS_GAVE_NO_ROUTES = 902; +static const errcode_t INVOICE_EXPIRED_DURING_WAIT = 903; +static const errcode_t INVOICE_WAIT_TIMED_OUT = 904; #endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */ diff --git a/common/test/run-param.c b/common/test/run-param.c index ab9006299326..93872d1f1c9e 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -5,6 +5,7 @@ #include "../param.c" #include #include +#include #include #include #include @@ -28,7 +29,7 @@ struct command_result { static struct command_result cmd_failed; struct command_result *command_fail(struct command *cmd, - int code, const char *fmt, ...) + errcode_t code, const char *fmt, ...) { failed = true; va_list ap; diff --git a/connectd/connect_wire.csv b/connectd/connect_wire.csv index bfc98aff5d86..9dec1992e2b8 100644 --- a/connectd/connect_wire.csv +++ b/connectd/connect_wire.csv @@ -44,7 +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,int, +msgdata,connectctl_connect_failed,failcode,errcode_t, msgdata,connectctl_connect_failed,failreason,wirestring, msgdata,connectctl_connect_failed,seconds_to_delay,u32, msgdata,connectctl_connect_failed,addrhint,?wireaddr_internal, diff --git a/connectd/connectd.c b/connectd/connectd.c index e8bc769448ca..0157eb325446 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -566,7 +567,7 @@ static void connect_failed(struct daemon *daemon, const struct node_id *id, u32 seconds_waited, const struct wireaddr_internal *addrhint, - int errcode, + errcode_t errcode, const char *errfmt, ...) PRINTF_FMT(6,7); @@ -574,7 +575,7 @@ static void connect_failed(struct daemon *daemon, const struct node_id *id, u32 seconds_waited, const struct wireaddr_internal *addrhint, - int errcode, + errcode_t errcode, const char *errfmt, ...) { u8 *msg; diff --git a/doc/lightning-waitinvoice.7 b/doc/lightning-waitinvoice.7 index be1939feda61..7d2766b7f7ff 100644 --- a/doc/lightning-waitinvoice.7 +++ b/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\. -If the invoice is deleted while unpaid, or the invoice does not exist, -this command will return with an error with code -1\. - - -If the invoice expires before being paid, or is already expired, this -command will return with an error with code -2, with the data being the -invoice data as per \fBlistinvoice\fR\. - +On error the returned object will contain \fBcode\fR and \fBmessage\fR properties, +with \fBcode\fR being one of the following: + +.RS +.IP \[bu] +-32602: If the given parameters are wrong\. +.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 Christian Decker \fI is mainly @@ -38,7 +42,3 @@ responsible\. Main web site: \fIhttps://github.com/ElementsProject/lightning\fR -.HL - -Last updated 2019-04-07 14:23:17 CEST - diff --git a/doc/lightning-waitinvoice.7.md b/doc/lightning-waitinvoice.7.md index 46a16c1baba0..2f632d1be16c 100644 --- a/doc/lightning-waitinvoice.7.md +++ b/doc/lightning-waitinvoice.7.md @@ -18,12 +18,12 @@ RETURN VALUE On success, an invoice description will be returned as per lightning-listinvoice(7). The *status* field will be *paid*. -If the invoice is deleted while unpaid, or the invoice does not exist, -this command will return with an error with code -1. +On error the returned object will contain `code` and `message` properties, +with `code` being one of the following: -If the invoice expires before being paid, or is already expired, this -command will return with an error with code -2, with the data being the -invoice data as per **listinvoice**. +- -32602: If the given parameters are wrong. +- -1: If the invoice is deleted while unpaid, or the invoice does not exist. +- 903: If the invoice expires before being paid, or is already expired. AUTHOR ------ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 542e5b304dcc..bf35d3557d12 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -231,7 +232,7 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay, static void connect_failed(struct lightningd *ld, const u8 *msg) { struct node_id id; - int errcode; + errcode_t errcode; char *errmsg; struct connect *c; u32 seconds_to_delay; diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 3cc5073be6eb..8d9a9ae21f92 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -82,8 +82,7 @@ static struct command_result *tell_waiter(struct command *cmd, json_add_invoice(response, details); return command_success(cmd, response); } else { - /* FIXME: -2 should be a constant in jsonrpc_errors.h. */ - response = json_stream_fail(cmd, -2, + response = json_stream_fail(cmd, INVOICE_EXPIRED_DURING_WAIT, "invoice expired during wait"); json_add_invoice(response, details); json_object_end(response); diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 938452121286..bde211189cd7 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -464,7 +464,7 @@ struct command_result *command_failed(struct command *cmd, 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 *errmsg; @@ -502,7 +502,7 @@ static void json_command_malformed(struct json_connection *jcon, json_add_string(js, "jsonrpc", "2.0"); json_add_literal(js, "id", id, strlen(id)); 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_object_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, - int code, + errcode_t code, const char *errmsg) { struct json_stream *js = json_start(cmd); @@ -561,14 +561,14 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd, assert(code); 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); return js; } struct json_stream *json_stream_fail(struct command *cmd, - int code, + errcode_t code, const char *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"); if (custom_return) { - int code; + errcode_t code; const char *errmsg; - if (!json_to_int(buffer, json_get_member(buffer, custom_return, "code"), - &code)) + if (!json_to_errcode(buffer, + json_get_member(buffer, custom_return, "code"), + &code)) return was_pending(command_fail(p->cmd, JSONRPC2_INVALID_REQUEST, "Bad response to 'rpc_command' hook: " "'error' object does not contain a code.")); diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index 19f9af80bb9d..2d84a8a02a42 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -96,7 +97,7 @@ struct json_stream *json_stream_success(struct command *cmd); * You need to json_object_end() once you're done! */ struct json_stream *json_stream_fail(struct command *cmd, - int code, + errcode_t code, 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. */ struct json_stream *json_stream_fail_nodata(struct command *cmd, - int code, + errcode_t code, const char *errmsg); /* These returned values are never NULL. */ diff --git a/lightningd/notification.c b/lightningd/notification.c index 8eff67dbfe06..790164d86cc3 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -254,7 +254,7 @@ void notify_sendpay_success(struct lightningd *ld, static void sendpay_failure_notification_serialize(struct json_stream *stream, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail, 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 * 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_object_start(stream, "data"); @@ -282,14 +282,14 @@ REGISTER_NOTIFICATION(sendpay_failure, void notify_sendpay_failure(struct lightningd *ld, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail, const char *errmsg) { void (*serialize)(struct json_stream *, const struct wallet_payment *, - int, + errcode_t, const struct onionreply *, const struct routing_failure *, const char *) = sendpay_failure_notification_gen.serialize; diff --git a/lightningd/notification.h b/lightningd/notification.h index 24557862b205..df89bbd63456 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -62,7 +63,7 @@ void notify_sendpay_success(struct lightningd *ld, void notify_sendpay_failure(struct lightningd *ld, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail, const char *errmsg); diff --git a/lightningd/pay.c b/lightningd/pay.c index da4340f1d631..db819f06219f 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -172,7 +172,7 @@ json_add_routefail_info(struct json_stream *js, void json_sendpay_fail_fields(struct json_stream *js, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail) { @@ -191,7 +191,7 @@ void json_sendpay_fail_fields(struct json_stream *js, 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 char *details) { @@ -210,7 +210,7 @@ static const char *sendpay_errmsg_fmt(const tal_t *ctx, int pay_errcode, static struct command_result * sendpay_fail(struct command *cmd, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail, const char *errmsg) @@ -243,7 +243,7 @@ json_sendpay_in_progress(struct command *cmd, static void tell_waiters_failed(struct lightningd *ld, const struct sha256 *payment_hash, const struct wallet_payment *payment, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail, const char *details) @@ -377,7 +377,7 @@ remote_routing_failure(const tal_t *ctx, const u8 *failuremsg, int origin_index, struct log *log, - int *pay_errcode) + errcode_t *pay_errcode) { enum onion_type failcode = fromwire_peektype(failuremsg); struct routing_failure *routing_failure; @@ -515,7 +515,7 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout, struct wallet_payment *payment; struct routing_failure* fail = NULL; const char *failmsg; - int pay_errcode; + errcode_t pay_errcode; payment = wallet_payment_by_hash(tmpctx, ld->wallet, &hout->payment_hash, @@ -631,7 +631,7 @@ static struct command_result *wait_payment(struct lightningd *ld, char *faildetail; struct routing_failure *fail; int faildirection; - int rpcerrorcode; + errcode_t rpcerrorcode; payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash, partid); diff --git a/lightningd/pay.h b/lightningd/pay.h index 1854454554ff..588dceab213d 100644 --- a/lightningd/pay.h +++ b/lightningd/pay.h @@ -2,6 +2,7 @@ #define LIGHTNING_LIGHTNINGD_PAY_H #include "config.h" #include +#include struct htlc_out; 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. */ void json_sendpay_fail_fields(struct json_stream *js, const struct wallet_payment *t, - int pay_errcode, + errcode_t pay_errcode, const struct onionreply *onionreply, const struct routing_failure *fail); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 367b79b4fe5b..d8fd8aaa72a4 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -2,6 +2,7 @@ #include "../invoice.c" #include "../peer_control.c" #include +#include 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) { fprintf(stderr, "command_check_only called!\n"); abort(); } /* 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, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } diff --git a/lightningd/test/run-log-pruning.c b/lightningd/test/run-log-pruning.c index 99f372e72778..1a99d4f1219b 100644 --- a/lightningd/test/run-log-pruning.c +++ b/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) { fprintf(stderr, "bigsize_put called!\n"); abort(); } /* 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, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 820f8dcb85cb..9674b98b8ab1 100644 --- a/plugins/libplugin.c +++ b/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, - int code, + errcode_t code, const char *errmsg, const struct json_out *data) { struct json_out *jout = start_json_rpc(cmd, *cmd->id); 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); if (data) @@ -305,7 +305,7 @@ struct command_result *forward_result(struct command *cmd, /* Called by param() directly if it's malformed. */ struct command_result *command_fail(struct command *cmd, - int code, const char *fmt, ...) + errcode_t code, const char *fmt, ...) { va_list ap; struct command_result *res; diff --git a/plugins/libplugin.h b/plugins/libplugin.h index 62b7c630d57e..63c9a7a3625a 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -4,6 +4,7 @@ #include "config.h" #include +#include #include #include #include @@ -74,7 +75,7 @@ void NORETURN plugin_err(const char *fmt, ...); * NULL, data can be NULL; otherwise it must be a JSON object. */ struct command_result *WARN_UNUSED_RESULT command_done_err(struct command *cmd, - int code, + errcode_t code, const char *errmsg, const struct json_out *data); diff --git a/plugins/pay.c b/plugins/pay.c index bc7a706ec308..97f3a40883e8 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -425,13 +426,14 @@ static struct command_result *waitsendpay_error(struct command *cmd, { struct pay_attempt *attempt = current_attempt(pc); const jsmntok_t *codetok, *failcodetok, *nodeidtok, *scidtok, *dirtok; - int code, failcode; + errcode_t code; + int failcode; bool node_err = false; attempt_failed_tok(pc, "waitsendpay", buf, error); 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'", error->end - error->start, buf + error->start); @@ -849,13 +851,13 @@ static struct command_result *getroute_error(struct command *cmd, const jsmntok_t *error, struct pay_command *pc) { - int code; + errcode_t code; const jsmntok_t *codetok; attempt_failed_tok(pc, "getroute", buf, error); 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'", error->end - error->start, buf + error->start); diff --git a/tools/generate-wire.py b/tools/generate-wire.py index acecb2258efa..1ff6e9e788af 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -185,7 +185,7 @@ class Type(FieldSet): 'bool', 'amount_sat', 'amount_msat', - 'int', + 'errcode_t', 'bigsize', 'varint' ] @@ -200,7 +200,7 @@ class Type(FieldSet): 'secp256k1_ecdsa_recoverable_signature', 'wirestring', 'double', - 'int', + 'errcode_t', 'bigsize', 'varint', ] diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index b6f5fcbef58c..05efeee3be98 100644 --- a/wallet/test/run-wallet.c +++ b/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 #include #include +#include #include #include #include @@ -61,7 +62,7 @@ bool channel_tell_depth(struct lightningd *ld UNNEEDED, bool command_check_only(const struct command *cmd UNNEEDED) { fprintf(stderr, "command_check_only called!\n"); abort(); } /* 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, ...) { fprintf(stderr, "command_fail called!\n"); abort(); } diff --git a/wire/fromwire.c b/wire/fromwire.c index 879b755d3fa7..1d17c02f2cb5 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -9,8 +9,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -165,12 +167,12 @@ bool fromwire_bool(const u8 **cursor, size_t *max) 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; } diff --git a/wire/towire.c b/wire/towire.c index b38736374868..1c85c865c16b 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -87,9 +88,9 @@ void towire_bool(u8 **pptr, bool v) 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) diff --git a/wire/wire.h b/wire/wire.h index 302a4af98702..4d77a1c563d0 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -79,7 +80,7 @@ void towire_tu64(u8 **pptr, u64 v); void towire_double(u8 **pptr, const double *v); void towire_pad(u8 **pptr, size_t num); 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_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); void fromwire_double(const u8 **cursor, size_t *max, double *v); 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); void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret); void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);