From 9efe123a0daf18a080e990e2d319d1a82c40b5fa Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Fri, 16 Mar 2018 03:45:08 +0000 Subject: [PATCH] lightningd/json: Move json helpers specific for lightningd to new module. --- lightningd/Makefile | 1 + lightningd/connect_control.c | 1 + lightningd/dev_ping.c | 1 + lightningd/gossip_control.c | 14 +--- lightningd/invoice.c | 1 + lightningd/json.c | 134 +++++++++++++++++++++++++++++++++++ lightningd/json.h | 58 +++++++++++++++ lightningd/jsonrpc.c | 66 +---------------- lightningd/jsonrpc.h | 26 ------- lightningd/opening_control.c | 1 + lightningd/pay.c | 36 +--------- lightningd/payalgo.c | 31 +------- lightningd/peer_control.c | 1 + wallet/walletrpc.c | 1 + 14 files changed, 204 insertions(+), 168 deletions(-) create mode 100644 lightningd/json.c create mode 100644 lightningd/json.h diff --git a/lightningd/Makefile b/lightningd/Makefile index 4780730c6..02b509f9d 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -59,6 +59,7 @@ LIGHTNINGD_SRC := \ lightningd/hsm_control.c \ lightningd/htlc_end.c \ lightningd/invoice.c \ + lightningd/json.c \ lightningd/jsonrpc.c \ lightningd/lightningd.c \ lightningd/log.c \ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 0a01bd940..f4934f157 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/dev_ping.c b/lightningd/dev_ping.c index 001f273dc..5f2d93588 100644 --- a/lightningd/dev_ping.c +++ b/lightningd/dev_ping.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 475128034..1d00ebefb 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -289,7 +290,6 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con { struct json_result *response; struct route_hop *hops; - size_t i; fromwire_gossip_getroute_reply(reply, reply, &hops); @@ -300,17 +300,7 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con response = new_json_result(cmd); json_object_start(response, NULL); - json_array_start(response, "route"); - for (i = 0; i < tal_count(hops); i++) { - json_object_start(response, NULL); - json_add_pubkey(response, "id", &hops[i].nodeid); - json_add_short_channel_id(response, "channel", - &hops[i].channel_id); - json_add_u64(response, "msatoshi", hops[i].amount); - json_add_num(response, "delay", hops[i].delay); - json_object_end(response); - } - json_array_end(response); + json_add_route(response, "route", hops, tal_count(hops)); json_object_end(response); command_success(cmd, response); } diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 45b8e5499..eb2770c18 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -1,4 +1,5 @@ #include "invoice.h" +#include "json.h" #include "jsonrpc.h" #include "lightningd.h" #include diff --git a/lightningd/json.c b/lightningd/json.c new file mode 100644 index 000000000..eea79ee57 --- /dev/null +++ b/lightningd/json.c @@ -0,0 +1,134 @@ +#include "json.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Output a route hop */ +static void +json_add_route_hop(struct json_result *r, char const *n, + const struct route_hop *h) +{ + /* Imitate what getroute/sendpay use */ + json_object_start(r, n); + json_add_pubkey(r, "id", &h->nodeid); + json_add_short_channel_id(r, "channel", + &h->channel_id); + json_add_u64(r, "msatoshi", h->amount); + json_add_num(r, "delay", h->delay); + json_object_end(r); +} + +/* Output a route */ +void +json_add_route(struct json_result *r, char const *n, + const struct route_hop *hops, size_t hops_len) +{ + size_t i; + json_array_start(r, n); + for (i = 0; i < hops_len; ++i) { + json_add_route_hop(r, NULL, &hops[i]); + } + json_array_end(r); +} + +/* Outputs fields, not a separate object*/ +void +json_add_payment_fields(struct json_result *response, + const struct wallet_payment *t) +{ + json_add_u64(response, "id", t->id); + json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash)); + json_add_pubkey(response, "destination", &t->destination); + json_add_u64(response, "msatoshi", t->msatoshi); + if (deprecated_apis) + json_add_u64(response, "timestamp", t->timestamp); + json_add_u64(response, "created_at", t->timestamp); + + switch (t->status) { + case PAYMENT_PENDING: + json_add_string(response, "status", "pending"); + break; + case PAYMENT_COMPLETE: + json_add_string(response, "status", "complete"); + break; + case PAYMENT_FAILED: + json_add_string(response, "status", "failed"); + break; + } + if (t->payment_preimage) + json_add_hex(response, "payment_preimage", + t->payment_preimage, + sizeof(*t->payment_preimage)); +} + +void json_add_pubkey(struct json_result *response, + const char *fieldname, + const struct pubkey *key) +{ + u8 der[PUBKEY_DER_LEN]; + + pubkey_to_der(der, key); + json_add_hex(response, fieldname, der, sizeof(der)); +} + +void json_add_txid(struct json_result *result, const char *fieldname, + const struct bitcoin_txid *txid) +{ + char hex[hex_str_size(sizeof(*txid))]; + + bitcoin_txid_to_hex(txid, hex, sizeof(hex)); + json_add_string(result, fieldname, hex); +} + +bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, + struct pubkey *pubkey) +{ + return pubkey_from_hexstr(buffer + tok->start, + tok->end - tok->start, pubkey); +} + +void json_add_short_channel_id(struct json_result *response, + const char *fieldname, + const struct short_channel_id *id) +{ + json_add_string(response, fieldname, + type_to_string(response, struct short_channel_id, id)); +} + +bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, + struct short_channel_id *scid) +{ + return short_channel_id_from_str(buffer + tok->start, + tok->end - tok->start, + scid); +} + +void json_add_address(struct json_result *response, const char *fieldname, + const struct wireaddr *addr) +{ + /* No need to print padding */ + if (addr->type == ADDR_TYPE_PADDING) + return; + + json_object_start(response, fieldname); + char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN); + if (addr->type == ADDR_TYPE_IPV4) { + inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN); + json_add_string(response, "type", "ipv4"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } else if (addr->type == ADDR_TYPE_IPV6) { + inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN); + json_add_string(response, "type", "ipv6"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } + json_object_end(response); +} + diff --git a/lightningd/json.h b/lightningd/json.h new file mode 100644 index 000000000..c2bfa8590 --- /dev/null +++ b/lightningd/json.h @@ -0,0 +1,58 @@ +/* lightningd/json.h + * Helpers for outputting JSON results that are specific only for + * lightningd. + */ +#ifndef LIGHTNING_LIGHTNINGD_JSON_H +#define LIGHTNING_LIGHTNINGD_JSON_H +#include "config.h" +#include +#include + +#define JSMN_STRICT 1 +# include + +struct bitcoin_txid; +struct json_result; +struct pubkey; +struct route_hop; +struct short_channel_id; +struct wallet_payment; +struct wireaddr; + +/* Output a route array. */ +void json_add_route(struct json_result *r, char const *n, + const struct route_hop *hops, size_t hops_len); + +/* Output the fields of a wallet payment. + * Should be used within an object context. */ +void json_add_payment_fields(struct json_result *response, + const struct wallet_payment *t); + +/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ +void json_add_pubkey(struct json_result *response, + const char *fieldname, + const struct pubkey *key); + +/* '"fieldname" : ' or "" if fieldname is NULL */ +void json_add_txid(struct json_result *result, const char *fieldname, + const struct bitcoin_txid *txid); + +/* Extract a pubkey from this */ +bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, + struct pubkey *pubkey); + +/* Extract a short_channel_id from this */ +bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, + struct short_channel_id *scid); + +/* '"fieldname" : "1234:5:6"' */ +void json_add_short_channel_id(struct json_result *response, + const char *fieldname, + const struct short_channel_id *id); + +/* JSON serialize a network address for a node */ +void json_add_address(struct json_result *response, const char *fieldname, + const struct wireaddr *addr); + + +#endif /* !defined (LIGHTNING_LIGHTNINGD_JSON_H) */ diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 107cb0dab..d13ab708c 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -316,71 +317,6 @@ struct json_result *null_response(const tal_t *ctx) return response; } -void json_add_pubkey(struct json_result *response, - const char *fieldname, - const struct pubkey *key) -{ - u8 der[PUBKEY_DER_LEN]; - - pubkey_to_der(der, key); - json_add_hex(response, fieldname, der, sizeof(der)); -} - -void json_add_txid(struct json_result *result, const char *fieldname, - const struct bitcoin_txid *txid) -{ - char hex[hex_str_size(sizeof(*txid))]; - - bitcoin_txid_to_hex(txid, hex, sizeof(hex)); - json_add_string(result, fieldname, hex); -} - -bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, - struct pubkey *pubkey) -{ - return pubkey_from_hexstr(buffer + tok->start, - tok->end - tok->start, pubkey); -} - -void json_add_short_channel_id(struct json_result *response, - const char *fieldname, - const struct short_channel_id *id) -{ - json_add_string(response, fieldname, - type_to_string(response, struct short_channel_id, id)); -} - -bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, - struct short_channel_id *scid) -{ - return short_channel_id_from_str(buffer + tok->start, - tok->end - tok->start, - scid); -} - -void json_add_address(struct json_result *response, const char *fieldname, - const struct wireaddr *addr) -{ - /* No need to print padding */ - if (addr->type == ADDR_TYPE_PADDING) - return; - - json_object_start(response, fieldname); - char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN); - if (addr->type == ADDR_TYPE_IPV4) { - inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN); - json_add_string(response, "type", "ipv4"); - json_add_string(response, "address", addrstr); - json_add_num(response, "port", addr->port); - } else if (addr->type == ADDR_TYPE_IPV6) { - inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN); - json_add_string(response, "type", "ipv6"); - json_add_string(response, "address", addrstr); - json_add_num(response, "port", addr->port); - } - json_object_end(response); -} - static bool cmd_in_jcon(const struct json_connection *jcon, const struct command *cmd) { diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index ebdf02b68..33f7344d7 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -80,32 +80,6 @@ void PRINTF_FMT(4, 5) command_fail_detailed(struct command *cmd, /* Mainly for documentation, that we plan to close this later. */ void command_still_pending(struct command *cmd); -/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */ -void json_add_pubkey(struct json_result *response, - const char *fieldname, - const struct pubkey *key); - -/* '"fieldname" : ' or "" if fieldname is NULL */ -void json_add_txid(struct json_result *result, const char *fieldname, - const struct bitcoin_txid *txid); - -/* Extract a pubkey from this */ -bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, - struct pubkey *pubkey); - -/* Extract a short_channel_id from this */ -bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok, - struct short_channel_id *scid); - -/* '"fieldname" : "1234:5:6"' */ -void json_add_short_channel_id(struct json_result *response, - const char *fieldname, - const struct short_channel_id *id); - -/* JSON serialize a network address for a node */ -void json_add_address(struct json_result *response, const char *fieldname, - const struct wireaddr *addr); - /* For initialization */ void setup_jsonrpc(struct lightningd *ld, const char *rpc_filename); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index eb87ebcd6..088c5ba0f 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/lightningd/pay.c b/lightningd/pay.c index c7d1c2dbe..a8680c2ff 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -798,41 +799,6 @@ send_payment(const tal_t *ctx, return true; } -/*----------------------------------------------------------------------------- -Utility ------------------------------------------------------------------------------*/ - -/* Outputs fields, not a separate object*/ -void -json_add_payment_fields(struct json_result *response, - const struct wallet_payment *t) -{ - json_add_u64(response, "id", t->id); - json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash)); - json_add_pubkey(response, "destination", &t->destination); - json_add_u64(response, "msatoshi", t->msatoshi); - if (deprecated_apis) - json_add_u64(response, "timestamp", t->timestamp); - json_add_u64(response, "created_at", t->timestamp); - - switch (t->status) { - case PAYMENT_PENDING: - json_add_string(response, "status", "pending"); - break; - case PAYMENT_COMPLETE: - json_add_string(response, "status", "complete"); - break; - case PAYMENT_FAILED: - json_add_string(response, "status", "failed"); - break; - } - if (t->payment_preimage) - json_add_hex(response, "payment_preimage", - t->payment_preimage, - sizeof(*t->payment_preimage)); - -} - /*----------------------------------------------------------------------------- JSON-RPC sendpay interface -----------------------------------------------------------------------------*/ diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index f2ec9fb72..b157b0a98 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -39,36 +40,6 @@ struct pay_failure { struct routing_failure *routing_failure; }; -/* FIXME: move json_add_route_hop and json_add_route to - * common/json.c, share code with getroute */ -/* Output a route hop */ -static void -json_add_route_hop(struct json_result *r, char const *n, - const struct route_hop *h) -{ - /* Imitate what getroute/sendpay use */ - json_object_start(r, n); - json_add_pubkey(r, "id", &h->nodeid); - json_add_short_channel_id(r, "channel", - &h->channel_id); - json_add_u64(r, "msatoshi", h->amount); - json_add_num(r, "delay", h->delay); - json_object_end(r); -} - -/* Output a route */ -static void -json_add_route(struct json_result *r, char const *n, - const struct route_hop *hops, size_t hops_len) -{ - size_t i; - json_array_start(r, n); - for (i = 0; i < hops_len; ++i) { - json_add_route_hop(r, NULL, &hops[i]); - } - json_array_end(r); -} - /* Output a pay failure */ static void json_add_failure(struct json_result *r, char const *n, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 7b6e14f11..cb5850a5e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 53a8369ca..437b3b01b 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include