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
This commit is contained in:
Vasil Dimov
2020-01-26 13:52:29 +01:00
committed by ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent 295ca2a436
commit 55173a56b7
29 changed files with 187 additions and 108 deletions

View File

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

View File

@@ -4,6 +4,7 @@
#include "config.h"
#include <ccan/time/time.h>
#include <common/errcode.h>
#include <common/json.h>
#include <common/json_command.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. */
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);

View File

@@ -7,6 +7,7 @@
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/bolt11.h>
#include <common/errcode.h>
#include <common/features.h>
#include <common/gossip_constants.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);
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);