mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
type_to_string: move formatting to appropriate files.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "bitcoin/locktime.h"
|
||||
#include "type_to_string.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
|
||||
#define SECONDS_POINT 500000000
|
||||
|
||||
@@ -94,3 +96,22 @@ u32 abs_locktime_to_blocks(const struct abs_locktime *abs)
|
||||
assert(!abs_locktime_is_seconds(abs));
|
||||
return abs->locktime;
|
||||
}
|
||||
|
||||
static char *fmt_rel_locktime(const tal_t *ctx, const struct rel_locktime *rl)
|
||||
{
|
||||
if (rel_locktime_is_seconds(rl))
|
||||
return tal_fmt(ctx, "+%usec", rel_locktime_to_seconds(rl));
|
||||
else
|
||||
return tal_fmt(ctx, "+%ublocks", rel_locktime_to_blocks(rl));
|
||||
}
|
||||
|
||||
static char *fmt_abs_locktime(const tal_t *ctx, const struct abs_locktime *al)
|
||||
{
|
||||
if (abs_locktime_is_seconds(al))
|
||||
return tal_fmt(ctx, "%usec", abs_locktime_to_seconds(al));
|
||||
else
|
||||
return tal_fmt(ctx, "%ublocks", abs_locktime_to_blocks(al));
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_STRING(rel_locktime, fmt_rel_locktime);
|
||||
REGISTER_TYPE_TO_STRING(abs_locktime, fmt_abs_locktime);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "privkey.h"
|
||||
#include "pubkey.h"
|
||||
#include "type_to_string.h"
|
||||
#include "utils.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
@@ -66,6 +67,8 @@ bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
|
||||
return structeq(&a->pubkey, &b->pubkey);
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_STRING(pubkey, pubkey_to_hexstr);
|
||||
|
||||
int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
|
||||
{
|
||||
u8 keya[33], keyb[33];
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "shadouble.h"
|
||||
#include "type_to_string.h"
|
||||
#include <ccan/mem/mem.h>
|
||||
|
||||
void sha256_double(struct sha256_double *shadouble, const void *p, size_t len)
|
||||
@@ -12,3 +13,5 @@ void sha256_double_done(struct sha256_ctx *shactx, struct sha256_double *res)
|
||||
sha256_done(shactx, &res->sha);
|
||||
sha256(&res->sha, &res->sha, sizeof(res->sha));
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_HEXSTR(sha256_double);
|
||||
|
||||
11
bitcoin/tx.c
11
bitcoin/tx.c
@@ -1,6 +1,7 @@
|
||||
#include "bitcoin/block.h"
|
||||
#include "bitcoin/pullpush.h"
|
||||
#include "bitcoin/tx.h"
|
||||
#include "type_to_string.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/cast/cast.h>
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
@@ -492,3 +493,13 @@ bool bitcoin_txid_to_hex(const struct sha256_double *txid,
|
||||
reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8));
|
||||
return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len);
|
||||
}
|
||||
|
||||
static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
|
||||
{
|
||||
u8 *lin = linearize_tx(ctx, tx);
|
||||
char *s = tal_hexstr(ctx, lin, tal_count(lin));
|
||||
tal_free(lin);
|
||||
return s;
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_STRING(bitcoin_tx, fmt_bitcoin_tx);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#include "channel.h"
|
||||
#include "htlc.h"
|
||||
#include "remove_dust.h"
|
||||
#include "type_to_string.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/array_size/array_size.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/structeq/structeq.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
uint64_t fee_by_feerate(size_t txsize, uint64_t fee_rate)
|
||||
@@ -325,3 +328,32 @@ bool balance_after_force(struct channel_state *cstate)
|
||||
adjust_fee(cstate, cstate->fee_rate);
|
||||
return true;
|
||||
}
|
||||
|
||||
static char *fmt_channel_oneside(const tal_t *ctx,
|
||||
const struct channel_oneside *co)
|
||||
{
|
||||
return tal_fmt(ctx, "{ pay_msat=%u"
|
||||
" fee_msat=%u"
|
||||
" num_htlcs=%u }",
|
||||
co->pay_msat,
|
||||
co->fee_msat,
|
||||
co->num_htlcs);
|
||||
}
|
||||
|
||||
static char *fmt_channel_state(const tal_t *ctx,
|
||||
const struct channel_state *cs)
|
||||
{
|
||||
return tal_fmt(ctx, "{ anchor=%"PRIu64
|
||||
" fee_rate=%"PRIu64
|
||||
" num_nondust=%u"
|
||||
" ours=%s"
|
||||
" theirs=%s }",
|
||||
cs->anchor,
|
||||
cs->fee_rate,
|
||||
cs->num_nondust,
|
||||
fmt_channel_oneside(ctx, &cs->side[LOCAL]),
|
||||
fmt_channel_oneside(ctx, &cs->side[REMOTE]));
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_STRING(channel_oneside, fmt_channel_oneside);
|
||||
REGISTER_TYPE_TO_STRING(channel_state, fmt_channel_state);
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
#include "htlc.h"
|
||||
#include "log.h"
|
||||
#include "peer.h"
|
||||
#include "type_to_string.h"
|
||||
#include "gen_htlc_state_names.h"
|
||||
#include <ccan/array_size/array_size.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
const char *htlc_state_name(enum htlc_state s)
|
||||
@@ -203,3 +205,22 @@ void htlc_undostate(struct htlc *h,
|
||||
|
||||
h->state = newstate;
|
||||
}
|
||||
|
||||
static char *fmt_htlc(const tal_t *ctx, const struct htlc *h)
|
||||
{
|
||||
return tal_fmt(ctx, "{ id=%"PRIu64
|
||||
" msatoshi=%"PRIu64
|
||||
" expiry=%s"
|
||||
" rhash=%s"
|
||||
" rval=%s"
|
||||
" src=%s }",
|
||||
h->id, h->msatoshi,
|
||||
type_to_string(ctx, struct abs_locktime, &h->expiry),
|
||||
type_to_string(ctx, struct sha256, &h->rhash),
|
||||
h->r ? tal_hexstr(ctx, h->r, sizeof(*h->r))
|
||||
: "UNKNOWN",
|
||||
h->src ? type_to_string(ctx, struct pubkey,
|
||||
h->src->peer->id)
|
||||
: "local");
|
||||
}
|
||||
REGISTER_TYPE_TO_STRING(htlc, fmt_htlc);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "bitcoin/pullpush.h"
|
||||
#include "netaddr.h"
|
||||
#include "type_to_string.h"
|
||||
#include "utils.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
@@ -86,3 +87,4 @@ bool netaddr_from_fd(int fd, int type, int protocol, struct netaddr *a)
|
||||
return getpeername(fd, &a->saddr.s, &a->addrlen) == 0;
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_STRING(netaddr, netaddr_name);
|
||||
|
||||
@@ -78,7 +78,7 @@ update-mocks-daemon/test/%: daemon/test/%
|
||||
|
||||
update-mocks: $(DAEMON_TEST_SRC:%=update-mocks-%)
|
||||
|
||||
$(DAEMON_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) libsecp256k1.a utils.o
|
||||
$(DAEMON_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_OBJS) $(CORE_OBJS) libsecp256k1.a utils.o
|
||||
|
||||
$(DAEMON_TEST_OBJS): $(DAEMON_HEADERS) $(DAEMON_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(DAEMON_GEN_HEADERS) $(CCAN_HEADERS)
|
||||
|
||||
|
||||
@@ -293,6 +293,11 @@ all_ok()
|
||||
if grep ^== $DIR1/errors; then exit 1; fi
|
||||
if grep ^== $DIR2/errors; then exit 1; fi
|
||||
[ $NUM_LIGHTNINGD = 2 ] || if grep ^== $DIR3/errors; then exit 1; fi
|
||||
|
||||
# Look for unknown logging types.
|
||||
if grep "UNKNOWN TYPE" $DIR1/output >&2; then exit 1; fi
|
||||
if grep "UNKNOWN TYPE" $DIR2/output >&2; then exit 1; fi
|
||||
[ $NUM_LIGHTNINGD = 2 ] || if grep "UNKNOWN TYPE" $DIR3/output >&2; then exit 1; fi
|
||||
$SHUTDOWN_BITCOIN
|
||||
|
||||
trap "rm -rf $DIR1 $DIR2 $DIR3" EXIT
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include "type_to_string.h"
|
||||
#include "utils.h"
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
|
||||
@@ -202,3 +203,5 @@ void steal_from_prototal(const tal_t *ctx, struct ProtobufCAllocator *prototal,
|
||||
tal_steal(pb, prototal->allocator_data);
|
||||
tal_free(prototal);
|
||||
}
|
||||
|
||||
REGISTER_TYPE_TO_HEXSTR(rval);
|
||||
|
||||
@@ -11,95 +11,32 @@
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
REGISTER_TYPE_TO_STRING(netaddr, netaddr_name);
|
||||
/* We need at least one, and this is in CCAN so register it here. */
|
||||
REGISTER_TYPE_TO_HEXSTR(sha256);
|
||||
|
||||
char *type_to_string_(const tal_t *ctx, const char *typename,
|
||||
union printable_types u)
|
||||
{
|
||||
char *s = NULL;
|
||||
size_t i;
|
||||
static size_t num_p;
|
||||
static struct type_to_string **t = NULL;
|
||||
|
||||
/* GCC checks we're one of these, so we should be. */
|
||||
if (streq(typename, "struct pubkey"))
|
||||
s = pubkey_to_hexstr(ctx, u.pubkey);
|
||||
else if (streq(typename, "struct sha256_double"))
|
||||
s = tal_hexstr(ctx, u.sha256_double, sizeof(*u.sha256_double));
|
||||
else if (streq(typename, "struct sha256"))
|
||||
s = tal_hexstr(ctx, u.sha256, sizeof(*u.sha256));
|
||||
else if (streq(typename, "struct rel_locktime")) {
|
||||
if (rel_locktime_is_seconds(u.rel_locktime))
|
||||
s = tal_fmt(ctx, "+%usec",
|
||||
rel_locktime_to_seconds(u.rel_locktime));
|
||||
else
|
||||
s = tal_fmt(ctx, "+%ublocks",
|
||||
rel_locktime_to_blocks(u.rel_locktime));
|
||||
} else if (streq(typename, "struct abs_locktime")) {
|
||||
if (abs_locktime_is_seconds(u.abs_locktime))
|
||||
s = tal_fmt(ctx, "%usec",
|
||||
abs_locktime_to_seconds(u.abs_locktime));
|
||||
else
|
||||
s = tal_fmt(ctx, "%ublocks",
|
||||
abs_locktime_to_blocks(u.abs_locktime));
|
||||
} else if (streq(typename, "struct bitcoin_tx")) {
|
||||
u8 *lin = linearize_tx(ctx, u.bitcoin_tx);
|
||||
s = tal_hexstr(ctx, lin, tal_count(lin));
|
||||
} else if (streq(typename, "struct htlc")) {
|
||||
const struct htlc *h = u.htlc;
|
||||
s = tal_fmt(ctx, "{ id=%"PRIu64
|
||||
" msatoshi=%"PRIu64
|
||||
" expiry=%s"
|
||||
" rhash=%s"
|
||||
" rval=%s"
|
||||
" src=%s }",
|
||||
h->id, h->msatoshi,
|
||||
type_to_string(ctx, struct abs_locktime, &h->expiry),
|
||||
type_to_string(ctx, struct sha256, &h->rhash),
|
||||
h->r ? tal_hexstr(ctx, h->r, sizeof(*h->r))
|
||||
: "UNKNOWN",
|
||||
h->src ? type_to_string(ctx, struct pubkey,
|
||||
h->src->peer->id)
|
||||
: "local");
|
||||
} else if (streq(typename, "struct rval")) {
|
||||
s = tal_hexstr(ctx, u.rval, sizeof(*u.rval));
|
||||
} else if (streq(typename, "struct channel_oneside")) {
|
||||
s = tal_fmt(ctx, "{ pay_msat=%u"
|
||||
" fee_msat=%u"
|
||||
" num_htlcs=%u }",
|
||||
u.channel_oneside->pay_msat,
|
||||
u.channel_oneside->fee_msat,
|
||||
u.channel_oneside->num_htlcs);
|
||||
} else if (streq(typename, "struct channel_state")) {
|
||||
s = tal_fmt(ctx, "{ anchor=%"PRIu64
|
||||
" fee_rate=%"PRIu64
|
||||
" num_nondust=%u"
|
||||
" ours=%s"
|
||||
" theirs=%s }",
|
||||
u.cstate->anchor,
|
||||
u.cstate->fee_rate,
|
||||
u.cstate->num_nondust,
|
||||
type_to_string(ctx, struct channel_oneside,
|
||||
&u.cstate->side[LOCAL]),
|
||||
type_to_string(ctx, struct channel_oneside,
|
||||
&u.cstate->side[REMOTE]));
|
||||
} else {
|
||||
size_t i;
|
||||
static size_t num_p;
|
||||
static struct type_to_string **t = NULL;
|
||||
if (!t)
|
||||
t = autodata_get(type_to_string, &num_p);
|
||||
|
||||
if (!t)
|
||||
t = autodata_get(type_to_string, &num_p);
|
||||
/* Typenames in registrations don't include "struct " */
|
||||
if (strstarts(typename, "struct "))
|
||||
typename += strlen("struct ");
|
||||
|
||||
/* Typenames in registrations don't include "struct " */
|
||||
if (strstarts(typename, "struct "))
|
||||
typename += strlen("struct ");
|
||||
|
||||
for (i = 0; i < num_p; i++) {
|
||||
if (streq(t[i]->typename, typename)) {
|
||||
s = t[i]->fmt(ctx, u);
|
||||
break;
|
||||
}
|
||||
for (i = 0; i < num_p; i++) {
|
||||
if (streq(t[i]->typename, typename)) {
|
||||
s = t[i]->fmt(ctx, u);
|
||||
break;
|
||||
}
|
||||
if (!s)
|
||||
s = tal_fmt(ctx, "UNKNOWN TYPE %s", typename);
|
||||
}
|
||||
if (!s)
|
||||
s = tal_fmt(ctx, "UNKNOWN TYPE %s", typename);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ union printable_types {
|
||||
const struct bitcoin_tx *bitcoin_tx;
|
||||
const struct htlc *htlc;
|
||||
const struct rval *rval;
|
||||
const struct channel_state *cstate;
|
||||
const struct channel_state *channel_state;
|
||||
const struct channel_oneside *channel_oneside;
|
||||
const struct netaddr *netaddr;
|
||||
const char *charp_;
|
||||
|
||||
Reference in New Issue
Block a user