From 93865bb0f31c70722f5567f4617ed3c763ece2fb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Oct 2019 10:27:52 +1030 Subject: [PATCH] wallet: minor style fixes, and remove null JSON fields. Our policy is generally to omit fields which aren't sensible. Also, @niftynei points out the spacing in for loops. Signed-off-by: Rusty Russell --- tests/test_wallet.py | 12 ++++++++++-- wallet/walletrpc.c | 22 +++++----------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/tests/test_wallet.py b/tests/test_wallet.py index fa2caecd0..e9868fab7 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -472,8 +472,16 @@ def test_transaction_annotations(node_factory, bitcoind): assert(output['type'] == 'deposit' and output['satoshis'] == '1000000000msat') # ... and all other output should be change, and have no annotations - types = set([o['type'] for i, o in enumerate(tx['outputs']) if i != idx]) - assert(set([None]) == types) + types = [] + for i, o in enumerate(tx['outputs']): + if i == idx: + continue + if 'type' in o: + types.append(o['type']) + else: + types.append(None) + + assert(set([None]) == set(types)) ########################################################################## # Let's now open a channel. The opener should get the funding transaction diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index ef416ca94..64b1c9cd0 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -907,7 +907,7 @@ struct { #if EXPERIMENTAL_FEATURES static const char *txtype_to_string(enum wallet_tx_type t) { - for (size_t i=0; wallet_tx_type_display_names[i].name != NULL; i++) + for (size_t i = 0; wallet_tx_type_display_names[i].name != NULL; i++) if (t == wallet_tx_type_display_names[i].t) return wallet_tx_type_display_names[i].name; return NULL; @@ -916,7 +916,7 @@ static const char *txtype_to_string(enum wallet_tx_type t) static void json_add_txtypes(struct json_stream *result, const char *fieldname, enum wallet_tx_type value) { json_array_start(result, fieldname); - for (size_t i=0; wallet_tx_type_display_names[i].name != NULL; i++) { + for (size_t i = 0; wallet_tx_type_display_names[i].name != NULL; i++) { if (value & wallet_tx_type_display_names[i].t) json_add_string(result, NULL, wallet_tx_type_display_names[i].name); } @@ -936,19 +936,15 @@ static void json_transaction_details(struct json_stream *response, #if EXPERIMENTAL_FEATURES if (tx->annotation.type != 0) json_add_txtypes(response, "type", tx->annotation.type); - else - json_add_null(response, "type"); if (tx->annotation.channel.u64 != 0) json_add_short_channel_id(response, "channel", &tx->annotation.channel); - else - json_add_null(response, "channel"); #endif json_add_u32(response, "locktime", wtx->locktime); json_add_u32(response, "version", wtx->version); json_array_start(response, "inputs"); - for (size_t i=0; inum_inputs; i++) { + for (size_t i = 0; i < wtx->num_inputs; i++) { struct wally_tx_input *in = &wtx->inputs[i]; json_object_start(response, NULL); json_add_hex(response, "txid", in->txhash, sizeof(in->txhash)); @@ -959,12 +955,8 @@ static void json_transaction_details(struct json_stream *response, const char *txtype = txtype_to_string(ann->type); if (txtype != NULL) json_add_string(response, "type", txtype); - else - json_add_null(response, "type"); if (ann->channel.u64 != 0) json_add_short_channel_id(response, "channel", &ann->channel); - else - json_add_null(response, "channel"); #endif json_object_end(response); @@ -972,7 +964,7 @@ static void json_transaction_details(struct json_stream *response, json_array_end(response); json_array_start(response, "outputs"); - for (size_t i=0; inum_outputs; i++) { + for (size_t i = 0; i < wtx->num_outputs; i++) { struct wally_tx_output *out = &wtx->outputs[i]; struct amount_asset amt = bitcoin_tx_output_get_amount(tx->tx, i); struct amount_sat sat; @@ -993,13 +985,9 @@ static void json_transaction_details(struct json_stream *response, const char *txtype = txtype_to_string(ann->type); if (txtype != NULL) json_add_string(response, "type", txtype); - else - json_add_null(response, "type"); if (ann->channel.u64 != 0) json_add_short_channel_id(response, "channel", &ann->channel); - else - json_add_null(response, "channel"); #endif json_add_hex(response, "scriptPubKey", out->script, out->script_len); @@ -1025,7 +1013,7 @@ static struct command_result *json_listtransactions(struct command *cmd, response = json_stream_success(cmd); json_array_start(response, "transactions"); - for (size_t i=0; i