From 7b1a4fc765c99f08adc4cf155510efe064ee5c21 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 10 Jan 2017 15:19:25 +1030 Subject: [PATCH] utils: add tal_hex() helper. This is a shortcut when the data being dumped is a tal array. Signed-off-by: Rusty Russell --- bitcoin/tx.c | 2 +- daemon/chaintopology.c | 2 +- daemon/commit_tx.c | 3 +-- daemon/db.c | 16 +++++++--------- daemon/netaddr.c | 2 +- daemon/peer.c | 3 +-- utils.c | 5 +++++ utils.h | 3 +++ 8 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 98e89c118..d30bba218 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -497,7 +497,7 @@ bool bitcoin_txid_to_hex(const struct sha256_double *txid, 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)); + char *s = tal_hex(ctx, lin); tal_free(lin); return s; } diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index 95831b8b3..57eb8454e 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -324,7 +324,7 @@ void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx, otx->peer = peer; bitcoin_txid(tx, &otx->txid); - otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx)); + otx->hextx = tal_hex(otx, rawtx); otx->failed = failed; tal_free(rawtx); diff --git a/daemon/commit_tx.c b/daemon/commit_tx.c index fd285d2f5..1de37fe19 100644 --- a/daemon/commit_tx.c +++ b/daemon/commit_tx.c @@ -211,8 +211,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, log_add_struct(peer->log, " rhash %s", struct sha256, &h->rhash); log_debug(peer->log, "Script: %s", - tal_hexstr(tmpctx, - wscript, tal_count(wscript))); + tal_hex(tmpctx, wscript)); } else log_debug(peer->log, "DOES NOT pay %"PRIu64" to htlc %"PRIu64, h->msatoshi / 1000, h->id); diff --git a/daemon/db.c b/daemon/db.c index d557653ce..d6d3705d4 100644 --- a/daemon/db.c +++ b/daemon/db.c @@ -772,7 +772,7 @@ static const char *linearize_shachain(const tal_t *ctx, } assert(tal_count(p) == SHACHAIN_SIZE); - str = tal_hexstr(ctx, p, tal_count(p)); + str = tal_hex(ctx, p); tal_free(p); return str; } @@ -1011,7 +1011,7 @@ static const char *pubkeys_to_hex(const tal_t *ctx, const struct pubkey *ids) for (i = 0; i < tal_count(ids); i++) pubkey_to_der(ders + i * PUBKEY_DER_LEN, &ids[i]); - return tal_hexstr(ctx, ders, tal_count(ders)); + return tal_hex(ctx, ders); } static struct pubkey *pubkeys_from_arr(const tal_t *ctx, const void *blob, size_t len) @@ -1518,7 +1518,7 @@ void db_new_htlc(struct peer *peer, const struct htlc *htlc) htlc->msatoshi, abs_locktime_to_blocks(&htlc->expiry), tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)), - tal_hexstr(ctx, htlc->routing, tal_count(htlc->routing)), + tal_hex(ctx, htlc->routing), peerid, htlc->src->id); } else { @@ -1531,7 +1531,7 @@ void db_new_htlc(struct peer *peer, const struct htlc *htlc) htlc->msatoshi, abs_locktime_to_blocks(&htlc->expiry), tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)), - tal_hexstr(ctx, htlc->routing, tal_count(htlc->routing))); + tal_hex(ctx, htlc->routing)); } tal_free(ctx); @@ -1798,8 +1798,7 @@ void db_set_our_closing_script(struct peer *peer) assert(peer->dstate->db->in_transaction); db_exec(__func__, peer->dstate, "UPDATE closing SET our_script=x'%s',shutdown_order=%"PRIu64" WHERE peer=x'%s';", - tal_hexstr(ctx, peer->closing.our_script, - tal_count(peer->closing.our_script)), + tal_hex(ctx, peer->closing.our_script), peer->closing.shutdown_order, peerid); tal_free(ctx); @@ -1815,8 +1814,7 @@ void db_set_their_closing_script(struct peer *peer) assert(peer->dstate->db->in_transaction); db_exec(__func__, peer->dstate, "UPDATE closing SET their_script=x'%s' WHERE peer=x'%s';", - tal_hexstr(ctx, peer->closing.their_script, - tal_count(peer->closing.their_script)), + tal_hex(ctx, peer->closing.their_script), peerid); tal_free(ctx); } @@ -1923,7 +1921,7 @@ void db_complete_pay_command(struct lightningd_state *dstate, else db_exec(__func__, dstate, "UPDATE pay SET fail=x'%s', htlc_peer=NULL WHERE rhash=x'%s';", - tal_hexstr(ctx, htlc->fail, tal_count(htlc->fail)), + tal_hex(ctx, htlc->fail), tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash))); tal_free(ctx); diff --git a/daemon/netaddr.c b/daemon/netaddr.c index f390af992..3eb50714f 100644 --- a/daemon/netaddr.c +++ b/daemon/netaddr.c @@ -61,7 +61,7 @@ char *netaddr_to_hex(const tal_t *ctx, const struct netaddr *a) assert(a->addrlen <= sizeof(a->saddr)); push(&a->saddr, a->addrlen, &blob); - hex = tal_hexstr(ctx, blob, tal_count(blob)); + hex = tal_hex(ctx, blob); tal_free(blob); return hex; } diff --git a/daemon/peer.c b/daemon/peer.c index bceacf366..abe79f0fb 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -5156,8 +5156,7 @@ static void json_signcommit(struct command *cmd, = tal_free(peer->local.commit->tx->input[0].witness); json_object_start(response, NULL); - json_add_string(response, "tx", - tal_hexstr(cmd, linear, tal_count(linear))); + json_add_string(response, "tx", tal_hex(cmd, linear)); json_object_end(response); command_success(cmd, response); } diff --git a/utils.c b/utils.c index 1770fcbbe..9707b1b8b 100644 --- a/utils.c +++ b/utils.c @@ -10,6 +10,11 @@ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) return str; } +char *tal_hex(const tal_t *ctx, const tal_t *data) +{ + return tal_hexstr(ctx, data, tal_len(data)); +} + u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len) { u8 *data = tal_arr(ctx, u8, hex_data_size(len)); diff --git a/utils.h b/utils.h index 90f02ff6d..53b13f81c 100644 --- a/utils.h +++ b/utils.h @@ -10,6 +10,9 @@ extern secp256k1_context *secp256k1_ctx; /* Allocate and fill in a hex-encoded string of this data. */ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); +/* Allocate and fill a hex-encoding of this tal pointer. */ +char *tal_hex(const tal_t *ctx, const tal_t *data); + /* Allocate and fill a buffer with the data of this hex string. */ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len);