2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2018-06-28 03:34:45 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
|
|
|
#include <ccan/utf8/utf8.h>
|
2019-09-27 02:02:34 +02:00
|
|
|
#include <common/decode_array.h>
|
2018-02-01 01:08:37 +01:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <devtools/print_wire.h>
|
2018-06-28 03:34:45 +02:00
|
|
|
#include <errno.h>
|
2018-02-01 01:08:37 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-06-28 03:30:49 +02:00
|
|
|
void printwire_u8(const char *fieldname, const u8 *v)
|
2018-02-01 01:08:37 +01:00
|
|
|
{
|
2018-05-17 07:08:01 +02:00
|
|
|
printf("%u\n", *v);
|
2018-02-01 01:08:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 03:30:49 +02:00
|
|
|
void printwire_u16(const char *fieldname, const u16 *v)
|
2018-02-01 01:08:37 +01:00
|
|
|
{
|
2018-05-17 07:08:01 +02:00
|
|
|
printf("%u\n", *v);
|
2018-02-01 01:08:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 03:30:49 +02:00
|
|
|
void printwire_u32(const char *fieldname, const u32 *v)
|
2018-02-01 01:08:37 +01:00
|
|
|
{
|
2018-05-17 07:08:01 +02:00
|
|
|
printf("%u\n", *v);
|
2018-02-01 01:08:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 03:30:49 +02:00
|
|
|
void printwire_u64(const char *fieldname, const u64 *v)
|
2018-02-01 01:08:37 +01:00
|
|
|
{
|
2018-05-17 07:08:01 +02:00
|
|
|
printf("%"PRIu64"\n", *v);
|
2018-02-01 01:08:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 09:49:13 +01:00
|
|
|
void printwire_wireaddr(const char *fieldname, const struct wireaddr *wireaddr)
|
|
|
|
{
|
|
|
|
printf("%s\n", fmt_wireaddr(tmpctx, wireaddr));
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:34:45 +02:00
|
|
|
/* Returns false if we ran out of data. */
|
|
|
|
static bool print_hexstring(const u8 **cursor, size_t *plen, size_t len)
|
2018-02-01 01:08:37 +01:00
|
|
|
{
|
|
|
|
while (len) {
|
|
|
|
u8 v = fromwire_u8(cursor, plen);
|
|
|
|
if (!*cursor)
|
2018-06-28 03:34:45 +02:00
|
|
|
return false;
|
|
|
|
printf("%02x", v);
|
2018-02-01 01:08:37 +01:00
|
|
|
len--;
|
|
|
|
}
|
2018-06-28 03:34:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printwire_alias(const u8 **cursor, size_t *plen, size_t len)
|
|
|
|
{
|
|
|
|
struct utf8_state utf8 = UTF8_STATE_INIT;
|
|
|
|
const char *p = (const char *)*cursor;
|
|
|
|
bool char_done = true;
|
|
|
|
|
|
|
|
printf("[");
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (!p[i]) {
|
|
|
|
if (!memeqzero(p+i, len-i)) {
|
|
|
|
printf(" **INVALID PADDING** ");
|
|
|
|
goto hexdump;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (utf8_decode(&utf8, p[i])) {
|
|
|
|
if (errno != 0) {
|
|
|
|
printf(" **INVALID UTF-8** ");
|
|
|
|
goto hexdump;
|
|
|
|
}
|
|
|
|
char_done = true;
|
|
|
|
} else {
|
|
|
|
/* Don't allow unprintable characters */
|
|
|
|
if (utf8.total_len == 1 && !cisprint(utf8.c)) {
|
|
|
|
printf(" **UNPRINTABLE CHARACTER** ");
|
|
|
|
goto hexdump;
|
|
|
|
}
|
|
|
|
char_done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!char_done) {
|
|
|
|
printf(" **INCOMPLETE UTF-8** ");
|
|
|
|
goto hexdump;
|
|
|
|
}
|
|
|
|
printf("%.*s ", (int)len, p);
|
|
|
|
|
|
|
|
hexdump:
|
|
|
|
if (!print_hexstring(cursor, plen, len))
|
|
|
|
return;
|
|
|
|
printf(" ]\n");
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:34:47 +02:00
|
|
|
static void printwire_addresses(const u8 **cursor, size_t *plen, size_t len)
|
|
|
|
{
|
|
|
|
struct wireaddr addr;
|
2022-01-14 19:01:34 +01:00
|
|
|
size_t to_go = len;
|
|
|
|
const size_t len_ref = *plen;
|
2018-06-28 03:34:47 +02:00
|
|
|
|
|
|
|
printf("[");
|
2022-01-14 19:01:34 +01:00
|
|
|
while (to_go && fromwire_wireaddr(cursor, plen, &addr)) {
|
|
|
|
to_go = len - (len_ref - *plen);
|
2018-06-28 03:34:47 +02:00
|
|
|
printf(" %s", fmt_wireaddr(NULL, &addr));
|
2022-01-14 19:01:34 +01:00
|
|
|
}
|
2018-06-28 03:34:47 +02:00
|
|
|
if (!*cursor)
|
|
|
|
return;
|
|
|
|
|
2022-01-14 19:01:34 +01:00
|
|
|
if (to_go) {
|
2018-06-28 03:34:47 +02:00
|
|
|
printf(" UNKNOWN:");
|
|
|
|
if (!print_hexstring(cursor, plen, len))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf(" ]\n");
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:34:47 +02:00
|
|
|
static void printwire_encoded_short_ids(const u8 **cursor, size_t *plen, size_t len)
|
|
|
|
{
|
|
|
|
struct short_channel_id *scids;
|
2020-03-04 05:13:00 +01:00
|
|
|
u8 *arr = fromwire_tal_arrn(tmpctx, cursor, plen, len);
|
2018-06-28 03:34:47 +02:00
|
|
|
|
2020-03-04 05:13:00 +01:00
|
|
|
if (!arr)
|
2018-06-28 03:34:47 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
printf("[");
|
|
|
|
scids = decode_short_ids(tmpctx, arr);
|
|
|
|
if (scids) {
|
|
|
|
switch (arr[0]) {
|
2019-09-27 02:02:34 +02:00
|
|
|
case ARR_UNCOMPRESSED:
|
2018-06-28 03:34:47 +02:00
|
|
|
printf(" (UNCOMPRESSED)");
|
|
|
|
break;
|
2019-09-27 02:02:34 +02:00
|
|
|
case ARR_ZLIB:
|
2018-06-28 03:34:47 +02:00
|
|
|
printf(" (ZLIB)");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < tal_count(scids); i++)
|
|
|
|
printf(" %s",
|
|
|
|
short_channel_id_to_str(tmpctx, &scids[i]));
|
|
|
|
} else {
|
|
|
|
/* If it was unknown, that's different from corrupt */
|
|
|
|
if (len == 0
|
2019-09-27 02:02:34 +02:00
|
|
|
|| arr[0] == ARR_UNCOMPRESSED
|
|
|
|
|| arr[0] == ARR_ZLIB) {
|
2018-06-28 03:34:47 +02:00
|
|
|
printf(" **CORRUPT**");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
printf(" UNKNOWN:");
|
|
|
|
print_hexstring(cursor, plen, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(" ]\n");
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:34:45 +02:00
|
|
|
void printwire_u8_array(const char *fieldname, const u8 **cursor, size_t *plen, size_t len)
|
|
|
|
{
|
|
|
|
if (streq(fieldname, "node_announcement.alias")) {
|
|
|
|
printwire_alias(cursor, plen, len);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-28 03:34:47 +02:00
|
|
|
if (streq(fieldname, "node_announcement.addresses")) {
|
|
|
|
printwire_addresses(cursor, plen, len);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-28 03:34:47 +02:00
|
|
|
if (strends(fieldname, ".encoded_short_ids")) {
|
|
|
|
printwire_encoded_short_ids(cursor, plen, len);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-28 03:34:45 +02:00
|
|
|
|
|
|
|
printf("[");
|
|
|
|
if (!print_hexstring(cursor, plen, len))
|
|
|
|
return;
|
2018-02-01 01:08:37 +01:00
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
|
2019-07-21 05:04:59 +02:00
|
|
|
static const struct tlv_print_record_type *
|
|
|
|
find_print_record_type(u64 type,
|
|
|
|
const struct tlv_print_record_type types[],
|
|
|
|
size_t num_types)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < num_types; i++)
|
|
|
|
if (types[i].type == type)
|
|
|
|
return types + i;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:31:14 +01:00
|
|
|
bool printwire_tlvs(const char *fieldname, const u8 **cursor, size_t *plen,
|
2019-07-21 05:04:59 +02:00
|
|
|
const struct tlv_print_record_type types[],
|
|
|
|
size_t num_types)
|
|
|
|
{
|
|
|
|
while (*plen > 0) {
|
|
|
|
u64 type, length;
|
|
|
|
const struct tlv_print_record_type *ptype;
|
|
|
|
|
|
|
|
type = fromwire_bigsize(cursor, plen);
|
|
|
|
if (!*cursor)
|
|
|
|
goto fail;
|
|
|
|
length = fromwire_bigsize(cursor, plen);
|
|
|
|
if (!*cursor)
|
|
|
|
goto fail;
|
|
|
|
|
2020-11-10 04:38:22 +01:00
|
|
|
if (length > *plen) {
|
|
|
|
*plen = 0;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-07-21 05:04:59 +02:00
|
|
|
ptype = find_print_record_type(type, types, num_types);
|
|
|
|
if (ptype) {
|
|
|
|
size_t tlvlen = length;
|
|
|
|
printf("{\ntype=%"PRIu64"\nlen=%"PRIu64"\n", type, length);
|
|
|
|
ptype->print(fieldname, cursor, &tlvlen);
|
|
|
|
if (!*cursor)
|
|
|
|
goto fail;
|
|
|
|
printf("}\n");
|
|
|
|
} else
|
2019-07-25 04:19:21 +02:00
|
|
|
printf("**TYPE #%"PRIu64" UNKNOWN for TLV %s**\n", type, fieldname);
|
2020-11-10 04:38:22 +01:00
|
|
|
*plen -= length;
|
2019-07-21 05:04:59 +02:00
|
|
|
}
|
2022-03-23 00:31:14 +01:00
|
|
|
return true;
|
2019-07-21 05:04:59 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
printf("**TRUNCATED TLV %s**\n", fieldname);
|
2022-03-23 00:31:14 +01:00
|
|
|
return false;
|
2019-07-21 05:04:59 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 01:08:37 +01:00
|
|
|
#define PRINTWIRE_TYPE_TO_STRING(T, N) \
|
2018-06-28 03:30:49 +02:00
|
|
|
void printwire_##N(const char *fieldname, const T *v) \
|
2018-02-01 01:08:37 +01:00
|
|
|
{ \
|
|
|
|
const char *s = type_to_string(NULL, T, v); \
|
|
|
|
printf("%s\n", s); \
|
|
|
|
tal_free(s); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PRINTWIRE_STRUCT_TYPE_TO_STRING(T) \
|
|
|
|
PRINTWIRE_TYPE_TO_STRING(struct T, T)
|
|
|
|
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(bitcoin_blkid);
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(bitcoin_txid);
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(channel_id);
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(node_id);
|
2018-02-01 01:08:37 +01:00
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(preimage);
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(pubkey);
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(sha256);
|
2018-07-09 13:17:58 +02:00
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(secret);
|
2018-02-01 01:08:37 +01:00
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(short_channel_id);
|
2019-02-21 04:45:55 +01:00
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(amount_sat);
|
|
|
|
PRINTWIRE_STRUCT_TYPE_TO_STRING(amount_msat);
|
2018-02-01 01:08:37 +01:00
|
|
|
PRINTWIRE_TYPE_TO_STRING(secp256k1_ecdsa_signature, secp256k1_ecdsa_signature);
|