From ccb8d4b6877d65eeb23feda41c87f3a973e13735 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 27 Oct 2023 16:06:28 +1030 Subject: [PATCH] sendpays: add to wait subsystem. Adding an index means: 1. Add the new subsystem, and new updated_index field to the db, and create xxx_index_deleted/created/updated APIs. 2. Hook up these functions to the points they need to be called. 3. Add index, start and limit fields to the list command. 4. Add created_index and updated_index into the list command. This does #1. Signed-off-by: Rusty Russell --- lightningd/pay.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lightningd/pay.h | 17 +++++++++++++++++ lightningd/wait.c | 3 +++ lightningd/wait.h | 3 ++- wallet/db.c | 2 ++ 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lightningd/pay.c b/lightningd/pay.c index 3bba874d4d5c..0d97ecadc612 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -1690,6 +1690,52 @@ static const struct json_command waitsendpay_command = { }; AUTODATA(json_command, &waitsendpay_command); +static u64 sendpay_index_inc(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status, + enum wait_index idx) +{ + return wait_index_increment(ld, WAIT_SUBSYSTEM_SENDPAY, idx, + "status", payment_status_to_string(status), + "=partid", tal_fmt(tmpctx, "%"PRIu64, partid), + "=groupid", tal_fmt(tmpctx, "%"PRIu64, groupid), + "payment_hash", + type_to_string(tmpctx, struct sha256, payment_hash), + NULL); +} + +void sendpay_index_deleted(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status) +{ + sendpay_index_inc(ld, payment_hash, partid, groupid, status, WAIT_INDEX_DELETED); +} + +/* Fortuntely, dbids start at 1, not 0! */ +u64 sendpay_index_created(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status) +{ + return sendpay_index_inc(ld, payment_hash, partid, groupid, status, + WAIT_INDEX_CREATED); +} + +u64 sendpay_index_update_status(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status) +{ + return sendpay_index_inc(ld, payment_hash, partid, groupid, status, + WAIT_INDEX_UPDATED); +} + static struct command_result *param_payment_status(struct command *cmd, const char *name, const char *buffer, diff --git a/lightningd/pay.h b/lightningd/pay.h index d8444f4f97cc..20bef9ada2e3 100644 --- a/lightningd/pay.h +++ b/lightningd/pay.h @@ -2,6 +2,7 @@ #define LIGHTNING_LIGHTNINGD_PAY_H #include "config.h" #include +#include struct htlc_out; struct lightningd; @@ -35,4 +36,20 @@ void json_sendpay_fail_fields(struct json_stream *js, const struct onionreply *onionreply, const struct routing_failure *fail); +/* wait() hooks in here */ +void sendpay_index_deleted(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status); +u64 sendpay_index_created(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status); +u64 sendpay_index_update_status(struct lightningd *ld, + const struct sha256 *payment_hash, + u64 partid, + u64 groupid, + enum payment_status status); #endif /* LIGHTNING_LIGHTNINGD_PAY_H */ diff --git a/lightningd/wait.c b/lightningd/wait.c index 70de05a9ad4f..6409912c403f 100644 --- a/lightningd/wait.c +++ b/lightningd/wait.c @@ -20,6 +20,7 @@ struct waiter { static const char *subsystem_names[] = { + "sendpays", "invoices", }; @@ -44,6 +45,8 @@ const char *wait_index_name(enum wait_index index) const char *wait_subsystem_name(enum wait_subsystem subsystem) { switch (subsystem) { + case WAIT_SUBSYSTEM_SENDPAY: + return subsystem_names[subsystem]; case WAIT_SUBSYSTEM_INVOICE: return subsystem_names[subsystem]; } diff --git a/lightningd/wait.h b/lightningd/wait.h index 35f50d45c469..c9c40206d4ff 100644 --- a/lightningd/wait.h +++ b/lightningd/wait.h @@ -7,7 +7,8 @@ struct lightningd; /* This WAIT_SUBSYSTEM_X corresponds to listX */ enum wait_subsystem { - WAIT_SUBSYSTEM_INVOICE + WAIT_SUBSYSTEM_SENDPAY, + WAIT_SUBSYSTEM_INVOICE, }; #define NUM_WAIT_SUBSYSTEM (WAIT_SUBSYSTEM_INVOICE+1) diff --git a/wallet/db.c b/wallet/db.c index c9d0dcee6b2d..bdb7787ab8df 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -994,6 +994,8 @@ static struct migration dbmigrations[] = { " commitment_fee BIGINT," " commitment_weight INTEGER)"), NULL}, {SQL("CREATE INDEX local_anchors_idx ON local_anchors (channel_id)"), NULL}, + {SQL("ALTER TABLE payments ADD updated_index BIGINT DEFAULT 0"), NULL}, + {SQL("CREATE INDEX payments_update_idx ON payments (updated_index)"), NULL}, }; /**