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

@@ -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,
1 #include <common/cryptomsg.h>
44 # Connectd -> master: we got a peer. Three fds: peer, gossip and gossip_store
45 msgtype,connect_peer_connected,2002
46 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

@@ -29,6 +29,7 @@
#include <common/cryptomsg.h>
#include <common/daemon_conn.h>
#include <common/decode_array.h>
#include <common/errcode.h>
#include <common/features.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.h>
@@ -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;