Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listpays: sort output payments #4518

Merged
merged 4 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/lightning-listpays.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions doc/lightning-listpays.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ If **status** is "failed":
- **erroronion** (hex, optional): the error onion returned on failure, if any.
[comment]: # (GENERATE-FROM-SCHEMA-END)

The returned array is ordered by increasing **created_at** fields.

AUTHOR
------

Expand Down
4 changes: 2 additions & 2 deletions doc/lightning-listsendpays.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion doc/lightning-listsendpays.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ command per *pay*, so this command should be used with caution.
RETURN VALUE
------------

On success, an array of objects is returned. Each object contains:
On success, an array of objects is returned, ordered by increasing *id*. Each object contains:

*id*
unique internal value assigned at creation
Expand Down
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
14 changes: 14 additions & 0 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,20 @@ def test_listpays_ongoing_attempt(node_factory, bitcoind, executor):
l1.rpc.listpays()


def test_listsendpays_and_listpays_order(node_factory):
"""listsendpays should be in increasing id order, listpays in created_at"""
l1, l2 = node_factory.line_graph(2)
for i in range(5):
inv = l2.rpc.invoice(1000 - i, "test {}".format(i), "test")['bolt11']
l1.rpc.pay(inv)

ids = [p['id'] for p in l1.rpc.listsendpays()['payments']]
assert ids == sorted(ids)

created_at = [p['created_at'] for p in l1.rpc.listpays()['pays']]
assert created_at == sorted(created_at)


@pytest.mark.developer("needs use_shadow")
def test_mpp_waitblockheight_routehint_conflict(node_factory, bitcoind, executor):
'''
Expand Down
6 changes: 3 additions & 3 deletions wallet/db_postgres_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions wallet/db_sqlite3_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading