2018-03-16 04:45:08 +01:00
|
|
|
#include <arpa/inet.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <bitcoin/address.h>
|
|
|
|
#include <bitcoin/base58.h>
|
|
|
|
#include <bitcoin/script.h>
|
2019-06-12 02:38:54 +02:00
|
|
|
#include <ccan/json_escape/json_escape.h>
|
2018-08-21 15:58:55 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2018-08-30 15:48:36 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2018-05-07 06:29:21 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <common/bech32.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/json.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
2019-01-15 04:54:27 +01:00
|
|
|
#include <common/json_helpers.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
#include <common/memleak.h>
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
#include <common/node_id.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/type_to_string.h>
|
2019-02-21 03:38:42 +01:00
|
|
|
#include <common/wallet_tx.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/wireaddr.h>
|
|
|
|
#include <gossipd/routing.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <lightningd/json.h>
|
|
|
|
#include <lightningd/json_stream.h>
|
2018-08-10 22:16:22 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/options.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <wallet/wallet.h>
|
2018-04-30 14:54:39 +02:00
|
|
|
#include <wire/wire.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
/* Output a route hop */
|
|
|
|
static void
|
2018-10-19 03:17:49 +02:00
|
|
|
json_add_route_hop(struct json_stream *r, char const *n,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct route_hop *h)
|
|
|
|
{
|
|
|
|
/* Imitate what getroute/sendpay use */
|
|
|
|
json_object_start(r, n);
|
2019-04-08 11:58:32 +02:00
|
|
|
json_add_node_id(r, "id", &h->nodeid);
|
2018-03-16 04:45:08 +01:00
|
|
|
json_add_short_channel_id(r, "channel",
|
|
|
|
&h->channel_id);
|
2019-01-15 11:04:07 +01:00
|
|
|
json_add_num(r, "direction", h->direction);
|
2019-05-20 07:07:40 +02:00
|
|
|
json_add_amount_msat_compat(r, h->amount, "msatoshi", "amount_msat");
|
2018-03-16 04:45:08 +01:00
|
|
|
json_add_num(r, "delay", h->delay);
|
|
|
|
json_object_end(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output a route */
|
|
|
|
void
|
2018-10-19 03:17:49 +02:00
|
|
|
json_add_route(struct json_stream *r, char const *n,
|
2018-03-16 04:45:08 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
void json_add_node_id(struct json_stream *response,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct node_id *id)
|
|
|
|
{
|
|
|
|
json_add_hex(response, fieldname, id->k, sizeof(id->k));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_pubkey(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
|
|
|
const struct pubkey *key)
|
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 der[PUBKEY_CMPR_LEN];
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
pubkey_to_der(der, key);
|
|
|
|
json_add_hex(response, fieldname, der, sizeof(der));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_txid(struct json_stream *result, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_pubkey(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey **pubkey)
|
2018-08-14 23:19:31 +02:00
|
|
|
{
|
|
|
|
*pubkey = tal(cmd, struct pubkey);
|
|
|
|
if (json_to_pubkey(buffer, tok, *pubkey))
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-14 23:19:31 +02:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a pubkey, not '%.*s'",
|
2019-06-05 07:28:53 +02:00
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *param_txid(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct bitcoin_txid **txid)
|
|
|
|
{
|
|
|
|
*txid = tal(cmd, struct bitcoin_txid);
|
|
|
|
if (json_to_txid(buffer, tok, *txid))
|
|
|
|
return NULL;
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be txid, not '%.*s'",
|
2018-12-16 05:50:06 +01:00
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
2018-08-14 23:19:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_short_channel_id(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
2019-04-08 11:58:44 +02:00
|
|
|
const struct short_channel_id *scid)
|
2018-03-16 04:45:08 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(response, fieldname, true, "%dx%dx%d",
|
2019-04-08 11:58:44 +02:00
|
|
|
short_channel_id_blocknum(scid),
|
|
|
|
short_channel_id_txnum(scid),
|
|
|
|
short_channel_id_outnum(scid));
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_short_channel_id(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct short_channel_id **scid)
|
2018-08-15 16:20:40 +02:00
|
|
|
{
|
|
|
|
*scid = tal(cmd, struct short_channel_id);
|
2019-09-06 08:41:41 +02:00
|
|
|
if (json_to_short_channel_id(buffer, tok, *scid))
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-15 16:20:40 +02:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a short channel id, not '%.*s'",
|
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:11:39 +02:00
|
|
|
const char *json_feerate_style_name(enum feerate_style style)
|
|
|
|
{
|
|
|
|
switch (style) {
|
|
|
|
case FEERATE_PER_KBYTE:
|
|
|
|
return "perkb";
|
|
|
|
case FEERATE_PER_KSIPA:
|
|
|
|
return "perkw";
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_feerate_style(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
enum feerate_style **style)
|
2018-08-27 07:11:39 +02:00
|
|
|
{
|
|
|
|
*style = tal(cmd, enum feerate_style);
|
|
|
|
if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
**style = FEERATE_PER_KSIPA;
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-27 07:11:39 +02:00
|
|
|
} else if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
**style = FEERATE_PER_KBYTE;
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-27 07:11:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
|
|
|
json_tok_full_len(tok), json_tok_full(buffer, tok));
|
2018-08-27 07:11:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_feerate(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
u32 **feerate)
|
2018-08-28 22:46:32 +02:00
|
|
|
{
|
|
|
|
jsmntok_t base = *tok, suffix = *tok;
|
|
|
|
enum feerate_style style;
|
|
|
|
unsigned int num;
|
|
|
|
|
2018-08-28 22:46:34 +02:00
|
|
|
for (size_t i = 0; i < NUM_FEERATES; i++) {
|
|
|
|
if (json_tok_streq(buffer, tok, feerate_name(i)))
|
2018-12-16 05:50:06 +01:00
|
|
|
return param_feerate_estimate(cmd, feerate, i);
|
2018-08-28 22:46:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
/* We have to split the number and suffix. */
|
|
|
|
suffix.start = suffix.end;
|
|
|
|
while (suffix.start > base.start && !isdigit(buffer[suffix.start-1])) {
|
|
|
|
suffix.start--;
|
|
|
|
base.end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_to_number(buffer, &base, &num)) {
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' prefix should be an integer, not '%.*s'",
|
|
|
|
name, base.end - base.start,
|
|
|
|
buffer + base.start);
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, &suffix, "")
|
|
|
|
|| json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
style = FEERATE_PER_KBYTE;
|
|
|
|
} else if (json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
style = FEERATE_PER_KSIPA;
|
|
|
|
} else {
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' suffix should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
|
|
|
suffix.end - suffix.start,
|
|
|
|
buffer + suffix.start);
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*feerate = tal(cmd, u32);
|
|
|
|
**feerate = feerate_from_style(num, style);
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 14:54:39 +02:00
|
|
|
bool
|
|
|
|
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct channel_id *cid)
|
|
|
|
{
|
|
|
|
return hex_decode(buffer + tok->start, tok->end - tok->start,
|
|
|
|
cid, sizeof(*cid));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address(struct json_stream *response, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct wireaddr *addr)
|
|
|
|
{
|
|
|
|
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);
|
2018-05-10 01:18:19 +02:00
|
|
|
} else if (addr->type == ADDR_TYPE_TOR_V2) {
|
|
|
|
json_add_string(response, "type", "torv2");
|
|
|
|
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
|
|
|
|
json_add_num(response, "port", addr->port);
|
|
|
|
} else if (addr->type == ADDR_TYPE_TOR_V3) {
|
|
|
|
json_add_string(response, "type", "torv3");
|
|
|
|
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
|
|
|
|
json_add_num(response, "port", addr->port);
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address_internal(struct json_stream *response,
|
2018-05-07 06:29:21 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const struct wireaddr_internal *addr)
|
|
|
|
{
|
|
|
|
switch (addr->itype) {
|
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "local socket");
|
|
|
|
json_add_string(response, "socket", addr->u.sockname);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-07 06:29:22 +02:00
|
|
|
case ADDR_INTERNAL_ALLPROTO:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "any protocol");
|
|
|
|
json_add_num(response, "port", addr->u.port);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-10 01:18:24 +02:00
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "Tor generated address");
|
|
|
|
json_add_address(response, "service", &addr->u.torservice);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-10 05:44:23 +02:00
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "unresolved");
|
|
|
|
json_add_string(response, "name", addr->u.unresolved.name);
|
|
|
|
json_add_num(response, "port", addr->u.unresolved.port);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-07 06:29:21 +02:00
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
json_add_address(response, fieldname, &addr->u.wireaddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
2018-08-10 17:00:34 +02:00
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_num(struct json_stream *result, const char *fieldname, unsigned int value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%u", value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_double(struct json_stream *result, const char *fieldname, double value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%f", value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_u64(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
uint64_t value)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%"PRIu64, value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:07:17 +02:00
|
|
|
void json_add_s64(struct json_stream *result, const char *fieldname,
|
|
|
|
int64_t value)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%"PRIi64, value);
|
2019-05-29 22:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_add_u32(struct json_stream *result, const char *fieldname,
|
|
|
|
uint32_t value)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%u", value);
|
2019-05-29 22:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_add_s32(struct json_stream *result, const char *fieldname,
|
|
|
|
int32_t value)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%d", value);
|
2019-05-29 22:07:17 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_literal(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *literal, int len)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Literal may contain quotes, so bypass normal checks */
|
|
|
|
char *dest = json_member_direct(result, fieldname, strlen(literal));
|
|
|
|
if (dest)
|
|
|
|
memcpy(dest, literal, strlen(literal));
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 03:38:35 +01:00
|
|
|
void json_add_string(struct json_stream *result, const char *fieldname, const char *value TAKES)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, true, "%s", value);
|
|
|
|
if (taken(value))
|
|
|
|
tal_free(value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_bool(struct json_stream *result, const char *fieldname, bool value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, value ? "true" : "false");
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 16:14:01 +01:00
|
|
|
void json_add_null(struct json_stream *stream, const char *fieldname)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(stream, fieldname, false, "null");
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_add_hex(struct json_stream *js, const char *fieldname,
|
|
|
|
const void *data, size_t len)
|
|
|
|
{
|
|
|
|
/* Size without NUL term */
|
|
|
|
size_t hexlen = hex_str_size(len) - 1;
|
|
|
|
char *dest;
|
|
|
|
|
|
|
|
dest = json_member_direct(js, fieldname, 1 + hexlen + 1);
|
|
|
|
if (dest) {
|
|
|
|
dest[0] = '"';
|
|
|
|
if (!hex_encode(data, len, dest + 1, hexlen + 1))
|
|
|
|
abort();
|
|
|
|
dest[1+hexlen] = '"';
|
|
|
|
}
|
2019-02-07 16:14:01 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_hex_talarr(struct json_stream *result,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const tal_t *data)
|
|
|
|
{
|
|
|
|
json_add_hex(result, fieldname, data, tal_bytelen(data));
|
|
|
|
}
|
|
|
|
|
2019-06-05 07:29:01 +02:00
|
|
|
void json_add_tx(struct json_stream *result,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
json_add_hex_talarr(result, fieldname, linearize_tx(tmpctx, tx));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_escaped_string(struct json_stream *result, const char *fieldname,
|
2019-06-12 02:38:54 +02:00
|
|
|
const struct json_escape *esc TAKES)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Already escaped, don't re-escape! */
|
|
|
|
char *dest = json_member_direct(result, fieldname,
|
|
|
|
1 + strlen(esc->s) + 1);
|
|
|
|
|
|
|
|
if (dest) {
|
|
|
|
dest[0] = '"';
|
|
|
|
memcpy(dest + 1, esc->s, strlen(esc->s));
|
|
|
|
dest[1+strlen(esc->s)] = '"';
|
|
|
|
}
|
2018-10-19 03:17:48 +02:00
|
|
|
if (taken(esc))
|
|
|
|
tal_free(esc);
|
|
|
|
}
|
2019-02-21 03:38:30 +01:00
|
|
|
|
2019-05-20 07:07:40 +02:00
|
|
|
void json_add_amount_msat_compat(struct json_stream *result,
|
|
|
|
struct amount_msat msat,
|
|
|
|
const char *rawfieldname,
|
|
|
|
const char *msatfieldname)
|
2019-02-21 03:38:30 +01:00
|
|
|
{
|
2019-02-21 04:45:56 +01:00
|
|
|
json_add_u64(result, rawfieldname, msat.millisatoshis); /* Raw: low-level helper */
|
2019-05-20 07:07:40 +02:00
|
|
|
json_add_amount_msat_only(result, msatfieldname, msat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_add_amount_msat_only(struct json_stream *result,
|
|
|
|
const char *msatfieldname,
|
|
|
|
struct amount_msat msat)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_string(result, msatfieldname,
|
2019-02-21 03:38:30 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, &msat));
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:07:40 +02:00
|
|
|
void json_add_amount_sat_compat(struct json_stream *result,
|
|
|
|
struct amount_sat sat,
|
|
|
|
const char *rawfieldname,
|
|
|
|
const char *msatfieldname)
|
2019-02-21 03:38:30 +01:00
|
|
|
{
|
2019-02-21 04:45:56 +01:00
|
|
|
json_add_u64(result, rawfieldname, sat.satoshis); /* Raw: low-level helper */
|
2019-05-20 07:07:40 +02:00
|
|
|
json_add_amount_sat_only(result, msatfieldname, sat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_add_amount_sat_only(struct json_stream *result,
|
|
|
|
const char *msatfieldname,
|
|
|
|
struct amount_sat sat)
|
|
|
|
{
|
|
|
|
struct amount_msat msat;
|
2019-02-21 03:38:30 +01:00
|
|
|
if (amount_sat_to_msat(&msat, sat))
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_string(result, msatfieldname,
|
2019-02-21 03:38:30 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, &msat));
|
|
|
|
}
|
2019-04-10 17:09:20 +02:00
|
|
|
|
|
|
|
void json_add_timeabs(struct json_stream *result, const char *fieldname,
|
|
|
|
struct timeabs t)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
json_add_member(result, fieldname, false, "%" PRIu64 ".%03" PRIu64,
|
2019-04-10 17:09:20 +02:00
|
|
|
(u64)t.ts.tv_sec, (u64)t.ts.tv_nsec / 1000000);
|
|
|
|
}
|
plugin: Add new notification type: warning
This notification bases on `LOG_BROKEN` and `LOG_UNUSUAL` level log.
--Introduction
A notification for topic `warning` is sent every time a new `BROKEN`/
`UNUSUAL` level(in plugins, we use `error`/`warn`) log generated, which
means an unusual/borken thing happens, such as channel failed,
message resolving failed...
```json
{
"warning": {
"level": "warn",
"time": "1559743608.565342521",
"source": "lightningd(17652): 0821f80652fb840239df8dc99205792bba2e559a05469915804c08420230e23c7c chan #7854:",
"log": "Peer permanent failure in CHANNELD_NORMAL: lightning_channeld: sent ERROR bad reestablish dataloss msg"
}
}
```
1. `level` is `warn` or `error`:
`warn` means something seems bad happened and it's under control, but
we'd better check it;
`error` means something extremely bad is out of control, and it may lead
to crash;
2. `time` is the second since epoch;
3. `source`, in fact, is the `prefix` of the log_entry. It means where
the event happened, it may have the following forms:
`<node_id> chan #<db_id_of_channel>:`, `lightningd(<lightningd_pid>):`,
`plugin-<plugin_name>:`, `<daemon_name>(<daemon_pid>):`, `jsonrpc:`,
`jcon fd <error_fd_to_jsonrpc>:`, `plugin-manager`;
4. `log` is the context of the original log entry.
--Note:
1. The main code uses `UNUSUAL`/`BROKEN`, and plugin module uses `warn`
/`error`, considering the consistency with plugin, warning choose `warn`
/`error`. But users who use c-lightning with plugins may want to
`getlog` with specified level when receive warning. It's the duty for
plugin dev to turn `warn`/`error` into `UNUSUAL`/`BROKEN` and present it
to the users, or pass it directly to `getlog`;
2. About time, `json_log()` in `log` module uses the Relative Time, from
the time when `log_book` inited to the time when this event happend.
But I consider the `UNUSUAL`/`BROKEN` event is rare, and it is very
likely to happen after running for a long time, so for users, they will
pay more attention to Absolute Time.
-- Related Change
1. Remove the definitions of `log`, `log_book`, `log_entry` from `log.c`
to `log.h`, then they can be used in warning declaration and definition.
2. Remove `void json_add_time(struct json_stream *result, const char
*fieldname, struct timespec ts)` from `log.c` to `json.c`, and add
related declaration in `json.h`. Now the notification function in
`notification.c` can call it.
2. Add a pointer to `struct lightningd` in `struct log_book`. This may
affect the independence of the `log` module, but storing a pointer to
`ld` is more direct;
2019-06-03 20:26:58 +02:00
|
|
|
|
|
|
|
void json_add_time(struct json_stream *result, const char *fieldname,
|
|
|
|
struct timespec ts)
|
|
|
|
{
|
|
|
|
char timebuf[100];
|
|
|
|
|
|
|
|
snprintf(timebuf, sizeof(timebuf), "%lu.%09u",
|
|
|
|
(unsigned long)ts.tv_sec,
|
|
|
|
(unsigned)ts.tv_nsec);
|
|
|
|
json_add_string(result, fieldname, timebuf);
|
|
|
|
}
|
2019-06-08 10:58:08 +02:00
|
|
|
|
|
|
|
void json_add_secret(struct json_stream *response, const char *fieldname,
|
|
|
|
const struct secret *secret)
|
|
|
|
{
|
|
|
|
json_add_hex(response, fieldname, secret, sizeof(struct secret));
|
|
|
|
}
|
2019-08-10 10:19:57 +02:00
|
|
|
|
|
|
|
void json_add_sha256(struct json_stream *result, const char *fieldname,
|
|
|
|
const struct sha256 *hash)
|
|
|
|
{
|
|
|
|
json_add_hex(result, fieldname, hash, sizeof(*hash));
|
|
|
|
}
|
2019-09-29 15:16:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* segwit_addr_net_decode - Try to decode a Bech32 address and detect
|
|
|
|
* testnet/mainnet/regtest/signet
|
|
|
|
*
|
|
|
|
* This processes the address and returns a string if it is a Bech32
|
|
|
|
* address specified by BIP173. The string is set whether it is
|
|
|
|
* testnet ("tb"), mainnet ("bc"), regtest ("bcrt"), or signet ("sb")
|
|
|
|
* It does not check, witness version and program size restrictions.
|
|
|
|
*
|
|
|
|
* Out: witness_version: Pointer to an int that will be updated to contain
|
|
|
|
* the witness program version (between 0 and 16 inclusive).
|
|
|
|
* witness_program: Pointer to a buffer of size 40 that will be updated
|
|
|
|
* to contain the witness program bytes.
|
|
|
|
* witness_program_len: Pointer to a size_t that will be updated to
|
|
|
|
* contain the length of bytes in witness_program.
|
|
|
|
* In: addrz: Pointer to the null-terminated address.
|
|
|
|
* Returns string containing the human readable segment of bech32 address
|
|
|
|
*/
|
|
|
|
static const char *segwit_addr_net_decode(int *witness_version,
|
|
|
|
uint8_t *witness_program,
|
|
|
|
size_t *witness_program_len,
|
|
|
|
const char *addrz,
|
|
|
|
const struct chainparams *chainparams)
|
|
|
|
{
|
|
|
|
if (segwit_addr_decode(witness_version, witness_program,
|
|
|
|
witness_program_len, chainparams->bip173_name,
|
|
|
|
addrz))
|
|
|
|
return chainparams->bip173_name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum address_parse_result
|
|
|
|
json_to_address_scriptpubkey(const tal_t *ctx,
|
|
|
|
const struct chainparams *chainparams,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok, const u8 **scriptpubkey)
|
|
|
|
{
|
|
|
|
struct bitcoin_address destination;
|
|
|
|
int witness_version;
|
|
|
|
/* segwit_addr_net_decode requires a buffer of size 40, and will
|
|
|
|
* not write to the buffer if the address is too long, so a buffer
|
|
|
|
* of fixed size 40 will not overflow. */
|
|
|
|
uint8_t witness_program[40];
|
|
|
|
size_t witness_program_len;
|
|
|
|
|
|
|
|
char *addrz;
|
|
|
|
const char *bip173;
|
|
|
|
|
|
|
|
bool parsed;
|
|
|
|
bool right_network;
|
|
|
|
u8 addr_version;
|
|
|
|
|
|
|
|
parsed =
|
|
|
|
ripemd160_from_base58(&addr_version, &destination.addr,
|
|
|
|
buffer + tok->start, tok->end - tok->start);
|
|
|
|
|
|
|
|
if (parsed) {
|
|
|
|
if (addr_version == chainparams->p2pkh_version) {
|
|
|
|
*scriptpubkey = scriptpubkey_p2pkh(ctx, &destination);
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
} else if (addr_version == chainparams->p2sh_version) {
|
|
|
|
*scriptpubkey =
|
|
|
|
scriptpubkey_p2sh_hash(ctx, &destination.addr);
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ADDRESS_PARSE_WRONG_NETWORK;
|
|
|
|
}
|
|
|
|
/* Insert other parsers that accept pointer+len here. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate null-terminated address. */
|
|
|
|
addrz = tal_dup_arr(ctx, char, buffer + tok->start, tok->end - tok->start, 1);
|
|
|
|
addrz[tok->end - tok->start] = '\0';
|
|
|
|
|
|
|
|
bip173 = segwit_addr_net_decode(&witness_version, witness_program,
|
|
|
|
&witness_program_len, addrz, chainparams);
|
|
|
|
|
|
|
|
if (bip173) {
|
|
|
|
bool witness_ok = false;
|
|
|
|
if (witness_version == 0 && (witness_program_len == 20 ||
|
|
|
|
witness_program_len == 32)) {
|
|
|
|
witness_ok = true;
|
|
|
|
}
|
|
|
|
/* Insert other witness versions here. */
|
|
|
|
|
|
|
|
if (witness_ok) {
|
|
|
|
*scriptpubkey = scriptpubkey_witness_raw(ctx, witness_version,
|
|
|
|
witness_program, witness_program_len);
|
|
|
|
parsed = true;
|
|
|
|
right_network = streq(bip173, chainparams->bip173_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Insert other parsers that accept null-terminated string here. */
|
|
|
|
|
|
|
|
tal_free(addrz);
|
|
|
|
|
|
|
|
if (parsed) {
|
|
|
|
if (right_network)
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
else
|
|
|
|
return ADDRESS_PARSE_WRONG_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ADDRESS_PARSE_UNRECOGNIZED;
|
|
|
|
}
|
2019-09-29 15:19:42 +02:00
|
|
|
|
|
|
|
struct command_result *param_bitcoin_address(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const u8 **scriptpubkey)
|
|
|
|
{
|
|
|
|
/* Parse address. */
|
|
|
|
switch (json_to_address_scriptpubkey(cmd,
|
|
|
|
get_chainparams(cmd->ld),
|
|
|
|
buffer, tok,
|
|
|
|
scriptpubkey)) {
|
|
|
|
case ADDRESS_PARSE_UNRECOGNIZED:
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Could not parse destination address, "
|
|
|
|
"%s should be a valid address",
|
|
|
|
name ? name : "address field");
|
|
|
|
case ADDRESS_PARSE_WRONG_NETWORK:
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Destination address is not on network %s",
|
|
|
|
get_chainparams(cmd->ld)->network_name);
|
|
|
|
case ADDRESS_PARSE_SUCCESS:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|