From 9fa7f5e30e86a7c12953fe75ebc81e4a00b22121 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 27 Sep 2018 15:03:25 +0930 Subject: [PATCH] listpeers: include current htlc information. This enables the next patch, which allows us to wait until all HTLCs are completely resolved. Signed-off-by: Rusty Russell --- CHANGELOG.md | 1 + lightningd/peer_control.c | 51 +++++++++++++++++++++++++++++++++++++++ wallet/test/run-wallet.c | 4 +++ 3 files changed, 56 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e97fad78..ecc9b73af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - JSON API: `listpeers` has new field `scratch_txid`: the latest tx in channel. +- JSON API: `listpeers` has new array `htlcs`: the current live payments. - JSON API: `listchannels` has two new fields: `message_flags` and `channel_flags`. This replaces `flags`. - Bitcoind: more parallelism in requests, for very slow nodes. - Testing: fixed logging, cleaner interception of bitcoind, minor fixes. diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 033c746d4..9da3b8462 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -593,6 +593,56 @@ void channel_watch_funding(struct lightningd *ld, struct channel *channel) funding_spent); } +static void json_add_htlcs(struct lightningd *ld, + struct json_result *response, + const struct channel *channel) +{ + /* FIXME: make per-channel htlc maps! */ + const struct htlc_in *hin; + struct htlc_in_map_iter ini; + const struct htlc_out *hout; + struct htlc_out_map_iter outi; + + /* FIXME: Add more fields. */ + json_array_start(response, "htlcs"); + for (hin = htlc_in_map_first(&ld->htlcs_in, &ini); + hin; + hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { + if (hin->key.channel != channel) + continue; + + json_object_start(response, NULL); + json_add_string(response, "direction", "in"); + json_add_u64(response, "id", hin->key.id); + json_add_u64(response, "msatoshi", hin->msatoshi); + json_add_u64(response, "expiry", hin->cltv_expiry); + json_add_hex(response, "payment_hash", + &hin->payment_hash, sizeof(hin->payment_hash)); + json_add_string(response, "state", + htlc_state_name(hin->hstate)); + json_object_end(response); + } + + for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); + hout; + hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { + if (hout->key.channel != channel) + continue; + + json_object_start(response, NULL); + json_add_string(response, "direction", "out"); + json_add_u64(response, "id", hout->key.id); + json_add_u64(response, "msatoshi", hout->msatoshi); + json_add_u64(response, "expiry", hout->cltv_expiry); + json_add_hex(response, "payment_hash", + &hout->payment_hash, sizeof(hout->payment_hash)); + json_add_string(response, "state", + htlc_state_name(hout->hstate)); + json_object_end(response); + } + json_array_end(response); +} + static void json_add_peer(struct lightningd *ld, struct json_result *response, struct peer *p, @@ -744,6 +794,7 @@ static void json_add_peer(struct lightningd *ld, json_add_u64(response, "out_msatoshi_fulfilled", channel_stats.out_msatoshi_fulfilled); + json_add_htlcs(ld, response, channel); json_object_end(response); } json_array_end(response); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 8d460e28d..22f5bd1ce 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -176,6 +176,10 @@ 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,