From 1459ab0457eb5cd25cfbb48c20ea2aa47a7fe9e6 Mon Sep 17 00:00:00 2001 From: Nalin Bhardwaj Date: Thu, 27 May 2021 03:26:58 +0530 Subject: [PATCH] plugins/pay: sort output payments in listpays Changelog-Changed: `listpays` output is now ordered by the `created_at` timestamp. --- plugins/pay.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/plugins/pay.c b/plugins/pay.c index c3007dc64715..b08c55c64213 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -1701,6 +1702,17 @@ static bool pay_mpp_eq(const struct pay_mpp *pm, const struct sha256 *payment_ha return memcmp(pm->payment_hash, payment_hash, sizeof(struct sha256)) == 0; } +static int cmp_pay_mpp(const struct pay_mpp *a, + const struct pay_mpp *b, + void *unused UNUSED) +{ + if (a->timestamp < b->timestamp) + return -1; + if (a->timestamp == b->timestamp) + return 0; + return 1; +} + HTABLE_DEFINE_TYPE(struct pay_mpp, pay_mpp_key, pay_mpp_hash, pay_mpp_eq, pay_map); @@ -1796,6 +1808,7 @@ static struct command_result *listsendpays_done(struct command *cmd, struct pay_map pay_map; struct pay_map_iter it; struct pay_mpp *pm; + struct pay_mpp *pays; pay_map_init(&pay_map); @@ -1864,17 +1877,26 @@ static struct command_result *listsendpays_done(struct command *cmd, } } - /* Now we've collapsed them, provide summary. */ - ret = jsonrpc_stream_success(cmd); - json_array_start(ret, "pays"); + pays = tal_arr(NULL, struct pay_mpp, pay_map_count(&pay_map)); + i = 0; for (pm = pay_map_first(&pay_map, &it); pm; pm = pay_map_next(&pay_map, &it)) { - add_new_entry(ret, buf, pm); + pays[i++] = *pm; } pay_map_clear(&pay_map); + asort(pays, tal_count(pays), cmp_pay_mpp, NULL); + + /* Now we've collapsed and sorted them, provide summary. */ + ret = jsonrpc_stream_success(cmd); + json_array_start(ret, "pays"); + + for (i = 0; i < tal_count(pays); i++) + add_new_entry(ret, buf, &pays[i]); + tal_free(pays); + json_array_end(ret); return command_finished(cmd, ret); }