utils: add tal_hex() helper.

This is a shortcut when the data being dumped is a tal array.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-01-10 15:19:25 +10:30
parent 4a233090ae
commit 7b1a4fc765
8 changed files with 20 additions and 16 deletions

View File

@@ -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) static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
{ {
u8 *lin = linearize_tx(ctx, 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); tal_free(lin);
return s; return s;
} }

View File

@@ -324,7 +324,7 @@ void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx,
otx->peer = peer; otx->peer = peer;
bitcoin_txid(tx, &otx->txid); bitcoin_txid(tx, &otx->txid);
otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx)); otx->hextx = tal_hex(otx, rawtx);
otx->failed = failed; otx->failed = failed;
tal_free(rawtx); tal_free(rawtx);

View File

@@ -211,8 +211,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
log_add_struct(peer->log, " rhash %s", struct sha256, log_add_struct(peer->log, " rhash %s", struct sha256,
&h->rhash); &h->rhash);
log_debug(peer->log, "Script: %s", log_debug(peer->log, "Script: %s",
tal_hexstr(tmpctx, tal_hex(tmpctx, wscript));
wscript, tal_count(wscript)));
} else } else
log_debug(peer->log, "DOES NOT pay %"PRIu64" to htlc %"PRIu64, log_debug(peer->log, "DOES NOT pay %"PRIu64" to htlc %"PRIu64,
h->msatoshi / 1000, h->id); h->msatoshi / 1000, h->id);

View File

@@ -772,7 +772,7 @@ static const char *linearize_shachain(const tal_t *ctx,
} }
assert(tal_count(p) == SHACHAIN_SIZE); assert(tal_count(p) == SHACHAIN_SIZE);
str = tal_hexstr(ctx, p, tal_count(p)); str = tal_hex(ctx, p);
tal_free(p); tal_free(p);
return str; 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++) for (i = 0; i < tal_count(ids); i++)
pubkey_to_der(ders + i * PUBKEY_DER_LEN, &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, static struct pubkey *pubkeys_from_arr(const tal_t *ctx,
const void *blob, size_t len) const void *blob, size_t len)
@@ -1518,7 +1518,7 @@ void db_new_htlc(struct peer *peer, const struct htlc *htlc)
htlc->msatoshi, htlc->msatoshi,
abs_locktime_to_blocks(&htlc->expiry), abs_locktime_to_blocks(&htlc->expiry),
tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)), tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)),
tal_hexstr(ctx, htlc->routing, tal_count(htlc->routing)), tal_hex(ctx, htlc->routing),
peerid, peerid,
htlc->src->id); htlc->src->id);
} else { } else {
@@ -1531,7 +1531,7 @@ void db_new_htlc(struct peer *peer, const struct htlc *htlc)
htlc->msatoshi, htlc->msatoshi,
abs_locktime_to_blocks(&htlc->expiry), abs_locktime_to_blocks(&htlc->expiry),
tal_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)), 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); tal_free(ctx);
@@ -1798,8 +1798,7 @@ void db_set_our_closing_script(struct peer *peer)
assert(peer->dstate->db->in_transaction); 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';", 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_hex(ctx, peer->closing.our_script),
tal_count(peer->closing.our_script)),
peer->closing.shutdown_order, peer->closing.shutdown_order,
peerid); peerid);
tal_free(ctx); tal_free(ctx);
@@ -1815,8 +1814,7 @@ void db_set_their_closing_script(struct peer *peer)
assert(peer->dstate->db->in_transaction); assert(peer->dstate->db->in_transaction);
db_exec(__func__, peer->dstate, db_exec(__func__, peer->dstate,
"UPDATE closing SET their_script=x'%s' WHERE peer=x'%s';", "UPDATE closing SET their_script=x'%s' WHERE peer=x'%s';",
tal_hexstr(ctx, peer->closing.their_script, tal_hex(ctx, peer->closing.their_script),
tal_count(peer->closing.their_script)),
peerid); peerid);
tal_free(ctx); tal_free(ctx);
} }
@@ -1923,7 +1921,7 @@ void db_complete_pay_command(struct lightningd_state *dstate,
else else
db_exec(__func__, dstate, db_exec(__func__, dstate,
"UPDATE pay SET fail=x'%s', htlc_peer=NULL WHERE rhash=x'%s';", "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_hexstr(ctx, &htlc->rhash, sizeof(htlc->rhash)));
tal_free(ctx); tal_free(ctx);

View File

@@ -61,7 +61,7 @@ char *netaddr_to_hex(const tal_t *ctx, const struct netaddr *a)
assert(a->addrlen <= sizeof(a->saddr)); assert(a->addrlen <= sizeof(a->saddr));
push(&a->saddr, a->addrlen, &blob); push(&a->saddr, a->addrlen, &blob);
hex = tal_hexstr(ctx, blob, tal_count(blob)); hex = tal_hex(ctx, blob);
tal_free(blob); tal_free(blob);
return hex; return hex;
} }

View File

@@ -5156,8 +5156,7 @@ static void json_signcommit(struct command *cmd,
= tal_free(peer->local.commit->tx->input[0].witness); = tal_free(peer->local.commit->tx->input[0].witness);
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_string(response, "tx", json_add_string(response, "tx", tal_hex(cmd, linear));
tal_hexstr(cmd, linear, tal_count(linear)));
json_object_end(response); json_object_end(response);
command_success(cmd, response); command_success(cmd, response);
} }

View File

@@ -10,6 +10,11 @@ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
return str; 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 *tal_hexdata(const tal_t *ctx, const void *str, size_t len)
{ {
u8 *data = tal_arr(ctx, u8, hex_data_size(len)); u8 *data = tal_arr(ctx, u8, hex_data_size(len));

View File

@@ -10,6 +10,9 @@ extern secp256k1_context *secp256k1_ctx;
/* Allocate and fill in a hex-encoded string of this data. */ /* Allocate and fill in a hex-encoded string of this data. */
char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); 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. */ /* 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); u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len);