From eae9b81099d3554ac0bb9dfb195ba57af9dc6751 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 28 Jul 2018 15:23:33 +0930 Subject: [PATCH] json: json_add_hex_talarr for common case of dumping a tal object in hex. Signed-off-by: Rusty Russell --- common/json.c | 7 +++++++ common/json.h | 4 ++++ lightningd/gossip_control.c | 5 ++--- lightningd/invoice.c | 2 +- lightningd/jsonrpc.c | 2 +- lightningd/log.c | 2 +- lightningd/opening_control.c | 2 +- lightningd/pay.c | 8 +++----- lightningd/payalgo.c | 13 +++++-------- lightningd/peer_control.c | 24 ++++++++++-------------- wallet/test/run-wallet.c | 9 +++++---- wallet/walletrpc.c | 8 ++++---- 12 files changed, 44 insertions(+), 42 deletions(-) diff --git a/common/json.c b/common/json.c index 072d397b1..5c7d36742 100644 --- a/common/json.c +++ b/common/json.c @@ -419,6 +419,13 @@ void json_add_hex(struct json_result *result, const char *fieldname, tal_free(hex); } +void json_add_hex_talarr(struct json_result *result, + const char *fieldname, + const tal_t *data) +{ + json_add_hex(result, fieldname, data, tal_len(data)); +} + void json_add_object(struct json_result *result, ...) { va_list ap; diff --git a/common/json.h b/common/json.h index 2c4fa4f3c..15a00dd1a 100644 --- a/common/json.h +++ b/common/json.h @@ -110,6 +110,10 @@ void json_add_bool(struct json_result *result, const char *fieldname, /* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */ void json_add_hex(struct json_result *result, const char *fieldname, const void *data, size_t len); +/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */ +void json_add_hex_talarr(struct json_result *result, + const char *fieldname, + const tal_t *data); void json_add_object(struct json_result *result, ...); const char *json_result_string(const struct json_result *result); diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index c72b80d52..0f2d1a494 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -212,9 +212,8 @@ static void json_getnodes_reply(struct subd *gossip UNUSED, const u8 *reply, nodes[i]->color, ARRAY_SIZE(nodes[i]->color)); json_add_u64(response, "last_timestamp", nodes[i]->last_timestamp); - json_add_hex(response, "global_features", - nodes[i]->global_features, - tal_len(nodes[i]->global_features)); + json_add_hex_talarr(response, "global_features", + nodes[i]->global_features); json_array_start(response, "addresses"); for (j=0; jaddresses); j++) { json_add_address(response, NULL, &nodes[i]->addresses[j]); diff --git a/lightningd/invoice.c b/lightningd/invoice.c index b2494b041..7cb026576 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -638,7 +638,7 @@ static void json_add_fallback(struct json_result *response, (const u8 *)&wsh, sizeof(wsh))) json_add_string(response, "addr", out); } - json_add_hex(response, "hex", fallback, tal_len(fallback)); + json_add_hex_talarr(response, "hex", fallback); json_object_end(response); } diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index f205087b1..6dc1e4ff8 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -151,7 +151,7 @@ static void json_getinfo(struct command *cmd, json_object_start(response, NULL); json_add_pubkey(response, "id", &cmd->ld->id); json_add_string(response, "alias", (const char *)cmd->ld->alias); - json_add_hex(response, "color", (const void *)cmd->ld->rgb, tal_len(cmd->ld->rgb)); + json_add_hex_talarr(response, "color", cmd->ld->rgb); if (cmd->ld->listen) { /* These are the addresses we're announcing */ json_array_start(response, "address"); diff --git a/lightningd/log.c b/lightningd/log.c index a839bf3e4..b04612a0a 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -615,7 +615,7 @@ static void log_to_json(unsigned int skipped, json_add_string(info->response, "source", prefix); json_add_string(info->response, "log", log); if (io) - json_add_hex(info->response, "data", io, tal_count(io)); + json_add_hex_talarr(info->response, "data", io); json_object_end(info->response); } diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 1f839a0f6..9ab4421a6 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -439,7 +439,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, response = new_json_result(fc->cmd); json_object_start(response, NULL); linear = linearize_tx(response, fundingtx); - json_add_hex(response, "tx", linear, tal_len(linear)); + json_add_hex_talarr(response, "tx", linear); json_add_txid(response, "txid", &channel->funding_txid); derive_channel_id(&cid, &channel->funding_txid, funding_outnum); json_add_string(response, "channel_id", diff --git a/lightningd/pay.c b/lightningd/pay.c index a75036b7c..e9c6d9990 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -852,8 +852,7 @@ static void json_waitsendpay_on_resolve(const struct sendpay_result *r, case PAY_UNPARSEABLE_ONION: data = new_json_result(cmd); json_object_start(data, NULL); - json_add_hex(data, "onionreply", - r->onionreply, tal_len(r->onionreply)); + json_add_hex_talarr(data, "onionreply", r->onionreply); json_object_end(data); assert(r->details != NULL); @@ -878,9 +877,8 @@ static void json_waitsendpay_on_resolve(const struct sendpay_result *r, json_add_short_channel_id(data, "erring_channel", &fail->erring_channel); if (fail->channel_update) - json_add_hex(data, "channel_update", - fail->channel_update, - tal_len(fail->channel_update)); + json_add_hex_talarr(data, "channel_update", + fail->channel_update); json_object_end(data); assert(r->details != NULL); diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index df5dd0a83..1dc425668 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -56,8 +56,7 @@ json_add_failure(struct json_result *r, char const *n, switch (f->type) { case FAIL_UNPARSEABLE_ONION: json_add_string(r, "type", "FAIL_UNPARSEABLE_ONION"); - json_add_hex(r, "onionreply", f->onionreply, - tal_len(f->onionreply)); + json_add_hex_talarr(r, "onionreply", f->onionreply); break; case FAIL_PAYMENT_REPLY: @@ -69,9 +68,8 @@ json_add_failure(struct json_result *r, char const *n, json_add_short_channel_id(r, "erring_channel", &rf->erring_channel); if (rf->channel_update) - json_add_hex(r, "channel_update", - rf->channel_update, - tal_len(rf->channel_update)); + json_add_hex_talarr(r, "channel_update", + rf->channel_update); break; } json_add_route(r, "route", f->route, tal_count(f->route)); @@ -259,9 +257,8 @@ static void json_pay_failure(struct pay *pay, json_add_short_channel_id(data, "erring_channel", &fail->erring_channel); if (fail->channel_update) - json_add_hex(data, "channel_update", - fail->channel_update, - tal_len(fail->channel_update)); + json_add_hex_talarr(data, "channel_update", + fail->channel_update); json_add_failures(data, "failures", &pay->pay_failures); json_object_end(data); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 7bbf681fd..8b9e6fa7c 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -248,7 +248,7 @@ resolve_one_close_command(struct close_command *cc, bool cooperative) bitcoin_txid(cc->channel->last_tx, &txid); json_object_start(result, NULL); - json_add_hex(result, "tx", tx, tal_len(tx)); + json_add_hex_talarr(result, "tx", tx); json_add_txid(result, "txid", &txid); if (cooperative) json_add_string(result, "type", "mutual"); @@ -761,13 +761,11 @@ static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg, struct wireaddr_internal, &p->addr)); json_array_end(response); - json_add_hex(response, "global_features", - p->global_features, - tal_len(p->global_features)); + json_add_hex_talarr(response, "global_features", + p->global_features); - json_add_hex(response, "local_features", - p->local_features, - tal_len(p->local_features)); + json_add_hex_talarr(response, "local_features", + p->local_features); } json_array_start(response, "channels"); @@ -894,13 +892,11 @@ static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg, /* Fake state. */ json_add_string(response, "state", "GOSSIPING"); json_add_pubkey(response, "id", ids+i); - json_add_hex(response, "global_features", - pf[i]->global_features, - tal_len(pf[i]->global_features)); + json_add_hex_talarr(response, "global_features", + pf[i]->global_features); - json_add_hex(response, "local_features", - pf[i]->local_features, - tal_len(pf[i]->local_features)); + json_add_hex_talarr(response, "local_features", + pf[i]->local_features); json_array_start(response, "netaddr"); if (addrs[i].itype != ADDR_INTERNAL_WIREADDR || addrs[i].u.wireaddr.type != ADDR_TYPE_PADDING) @@ -1190,7 +1186,7 @@ static void json_sign_last_tx(struct command *cmd, remove_sig(channel->last_tx); json_object_start(response, NULL); - json_add_hex(response, "tx", linear, tal_len(linear)); + json_add_hex_talarr(response, "tx", linear); json_object_end(response); command_success(cmd, response); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 858dfd518..09932a744 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -182,10 +182,11 @@ void invoices_waitone(const tal_t *ctx UNNEEDED, void json_add_bool(struct json_result *result UNNEEDED, const char *fieldname UNNEEDED, bool value UNNEEDED) { fprintf(stderr, "json_add_bool called!\n"); abort(); } -/* Generated stub for json_add_hex */ -void json_add_hex(struct json_result *result UNNEEDED, const char *fieldname UNNEEDED, - const void *data UNNEEDED, size_t len UNNEEDED) -{ fprintf(stderr, "json_add_hex called!\n"); abort(); } +/* Generated stub for json_add_hex_talarr */ +void json_add_hex_talarr(struct json_result *result UNNEEDED, + const char *fieldname UNNEEDED, + const tal_t *data UNNEEDED) +{ fprintf(stderr, "json_add_hex_talarr called!\n"); abort(); } /* Generated stub for json_add_log */ void json_add_log(struct json_result *result UNNEEDED, const struct log_book *lr UNNEEDED, enum log_level minlevel UNNEEDED) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index ba9da2fd7..cf881b5ae 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -334,11 +334,11 @@ static void json_listaddrs(struct command *cmd, json_add_u64(response, "keyidx", keyidx); json_add_pubkey(response, "pubkey", &pubkey); json_add_string(response, "p2sh", out_p2sh); - json_add_hex(response, "p2sh_redeemscript", - redeemscript_p2sh, tal_count(redeemscript_p2sh)); + json_add_hex_talarr(response, "p2sh_redeemscript", + redeemscript_p2sh); json_add_string(response, "bech32", out_p2wpkh); - json_add_hex(response, "bech32_redeemscript", - redeemscript_p2wpkh, tal_count(redeemscript_p2wpkh)); + json_add_hex_talarr(response, "bech32_redeemscript", + redeemscript_p2wpkh); json_object_end(response); } json_array_end(response);