Skip to content

Commit

Permalink
plugins/pay: sort output payments in listpays
Browse files Browse the repository at this point in the history
Changelog-Changed: `listpays` output is now ordered by the `created_at` timestamp.
  • Loading branch information
nalinbhardwaj committed May 26, 2021
1 parent 04a6ce7 commit 1459ab0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions plugins/pay.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <bitcoin/chainparams.h>
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h>
#include <ccan/cast/cast.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/htable/htable_type.h>
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1459ab0

Please sign in to comment.