From b73f4b9f4ac4bb7da4a53ad781544fb716d8fbf9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 24 Oct 2023 12:00:20 +1030 Subject: [PATCH] forwards: hook into the wait subsystem. Signed-off-by: Rusty Russell Changelog-Added: JSON-RPC: `wait` now works for `forwards` infrastructure. --- lightningd/forwards.c | 10 +++++----- lightningd/forwards.h | 2 +- wallet/test/run-db.c | 21 +++++++++++++++++++++ wallet/test/run-wallet.c | 30 +++++++++++++++++++++--------- wallet/wallet.c | 29 ++++++++++++++++++++++++++--- 5 files changed, 74 insertions(+), 18 deletions(-) diff --git a/lightningd/forwards.c b/lightningd/forwards.c index 4648adea738b..8924a9aee372 100644 --- a/lightningd/forwards.c +++ b/lightningd/forwards.c @@ -14,14 +14,14 @@ static u64 forward_index_inc(struct lightningd *ld, enum forward_status status, struct short_channel_id in_channel, - struct amount_msat in_amount, + const struct amount_msat *in_amount, const struct short_channel_id *out_channel, enum wait_index idx) { return wait_index_increment(ld, WAIT_SUBSYSTEM_FORWARD, idx, "status", forward_status_name(status), "in_channel", short_channel_id_to_str(tmpctx, &in_channel), - "=in_msat", tal_fmt(tmpctx, "%"PRIu64, in_amount.millisatoshis), /* Raw: JSON output */ + "=in_msat", in_amount ? tal_fmt(tmpctx, "%"PRIu64, in_amount->millisatoshis) : NULL, /* Raw: JSON output */ "out_channel", out_channel ? short_channel_id_to_str(tmpctx, out_channel): NULL, NULL); } @@ -29,7 +29,7 @@ static u64 forward_index_inc(struct lightningd *ld, void forward_index_deleted(struct lightningd *ld, enum forward_status status, struct short_channel_id in_channel, - struct amount_msat in_amount, + const struct amount_msat *in_amount, const struct short_channel_id *out_channel) { forward_index_inc(ld, status, in_channel, in_amount, out_channel, @@ -43,7 +43,7 @@ u64 forward_index_created(struct lightningd *ld, struct amount_msat in_amount, const struct short_channel_id *out_channel) { - return forward_index_inc(ld, status, in_channel, in_amount, out_channel, + return forward_index_inc(ld, status, in_channel, &in_amount, out_channel, WAIT_INDEX_CREATED); } @@ -53,7 +53,7 @@ u64 forward_index_update_status(struct lightningd *ld, struct amount_msat in_amount, const struct short_channel_id *out_channel) { - return forward_index_inc(ld, status, in_channel, in_amount, out_channel, + return forward_index_inc(ld, status, in_channel, &in_amount, out_channel, WAIT_INDEX_UPDATED); } diff --git a/lightningd/forwards.h b/lightningd/forwards.h index 01ab3b90a4d9..ee9507c78172 100644 --- a/lightningd/forwards.h +++ b/lightningd/forwards.h @@ -92,7 +92,7 @@ static inline const char *forward_style_name(enum forward_style style) void forward_index_deleted(struct lightningd *ld, enum forward_status status, struct short_channel_id in_channel, - struct amount_msat in_amount, + const struct amount_msat *in_amount, const struct short_channel_id *out_channel); u64 forward_index_created(struct lightningd *ld, enum forward_status status, diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 6f40c4036dbd..fbb9469a0179 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -52,6 +52,27 @@ void fatal_vfmt(const char *fmt UNNEEDED, va_list ap UNNEEDED) /* Generated stub for find_peer_by_dbid */ struct peer *find_peer_by_dbid(struct lightningd *ld UNNEEDED, u64 dbid UNNEEDED) { fprintf(stderr, "find_peer_by_dbid called!\n"); abort(); } +/* Generated stub for forward_index_created */ +u64 forward_index_created(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + struct amount_msat in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_created called!\n"); abort(); } +/* Generated stub for forward_index_deleted */ +void forward_index_deleted(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + const struct amount_msat *in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_deleted called!\n"); abort(); } +/* Generated stub for forward_index_update_status */ +u64 forward_index_update_status(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + struct amount_msat in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_update_status called!\n"); abort(); } /* Generated stub for fromwire_hsmd_get_channel_basepoints_reply */ bool fromwire_hsmd_get_channel_basepoints_reply(const void *p UNNEEDED, struct basepoints *basepoints UNNEEDED, struct pubkey *funding_pubkey UNNEEDED) { fprintf(stderr, "fromwire_hsmd_get_channel_basepoints_reply called!\n"); abort(); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 0028c3029531..3dbf8bad373b 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -170,6 +170,27 @@ void force_peer_disconnect(struct lightningd *ld UNNEEDED, const struct peer *peer UNNEEDED, const char *why UNNEEDED) { fprintf(stderr, "force_peer_disconnect called!\n"); abort(); } +/* Generated stub for forward_index_created */ +u64 forward_index_created(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + struct amount_msat in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_created called!\n"); abort(); } +/* Generated stub for forward_index_deleted */ +void forward_index_deleted(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + const struct amount_msat *in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_deleted called!\n"); abort(); } +/* Generated stub for forward_index_update_status */ +u64 forward_index_update_status(struct lightningd *ld UNNEEDED, + enum forward_status status UNNEEDED, + struct short_channel_id in_channel UNNEEDED, + struct amount_msat in_amount UNNEEDED, + const struct short_channel_id *out_channel UNNEEDED) +{ fprintf(stderr, "forward_index_update_status called!\n"); abort(); } /* Generated stub for fromwire_channeld_dev_memleak_reply */ bool fromwire_channeld_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED) { fprintf(stderr, "fromwire_channeld_dev_memleak_reply called!\n"); abort(); } @@ -344,10 +365,6 @@ void json_add_string(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED, const char *str TAKES UNNEEDED) { fprintf(stderr, "json_add_string called!\n"); abort(); } -/* Generated stub for json_add_timeabs */ -void json_add_timeabs(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, - struct timeabs t UNNEEDED) -{ fprintf(stderr, "json_add_timeabs called!\n"); abort(); } /* Generated stub for json_add_timeiso */ void json_add_timeiso(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, @@ -626,11 +643,6 @@ struct command_result *param_string(struct command *cmd UNNEEDED, const char *na const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char **str UNNEEDED) { fprintf(stderr, "param_string called!\n"); abort(); } -/* Generated stub for param_u64 */ -struct command_result *param_u64(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - uint64_t **num UNNEEDED) -{ fprintf(stderr, "param_u64 called!\n"); abort(); } /* Generated stub for parse_onionpacket */ struct onionpacket *parse_onionpacket(const tal_t *ctx UNNEEDED, const u8 *src UNNEEDED, diff --git a/wallet/wallet.c b/wallet/wallet.c index 4373e416321b..7b63f22254f5 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -4639,13 +4639,20 @@ static bool wallet_forwarded_payment_update(struct wallet *w, * one without the htlc_out restriction).*/ stmt = db_prepare_v2(w->db, SQL("UPDATE forwards SET" - " in_msatoshi=?" + " updated_index=?" + ", in_msatoshi=?" ", out_msatoshi=?" ", state=?" ", resolved_time=?" ", failcode=?" ", forward_style=?" " WHERE in_htlc_id=? AND in_channel_scid=?")); + db_bind_u64(stmt, + forward_index_update_status(w->ld, + state, + *channel_scid_or_local_alias(in->key.channel), + in->msat, + out ? channel_scid_or_local_alias(in->key.channel) : NULL)); db_bind_amount_msat(stmt, &in->msat); if (out) { @@ -4692,6 +4699,7 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in, { struct db_stmt *stmt; struct timeabs *resolved_time; + u64 id; if (state == FORWARD_SETTLED || state == FORWARD_FAILED) { resolved_time = tal(tmpctx, struct timeabs); @@ -4705,7 +4713,8 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in, stmt = db_prepare_v2(w->db, SQL("INSERT INTO forwards (" - " in_htlc_id" + " id" + ", in_htlc_id" ", out_htlc_id" ", in_channel_scid" ", out_channel_scid" @@ -4716,7 +4725,14 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in, ", resolved_time" ", failcode" ", forward_style" - ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); + id = forward_index_created(w->ld, + state, + *channel_scid_or_local_alias(in->key.channel), + in->msat, + out ? channel_scid_or_local_alias(in->key.channel) : NULL); + + db_bind_u64(stmt, id); db_bind_u64(stmt, in->key.id); /* FORWARD_LOCAL_FAILED may occur before we get htlc_out */ @@ -5010,6 +5026,13 @@ bool wallet_forward_delete(struct wallet *w, changed = db_count_changes(stmt) != 0; tal_free(stmt); + /* FIXME: We don't set in->msat or out here, since that would + * need an extra lookup */ + forward_index_deleted(w->ld, + state, + *chan_in, + NULL, NULL); + return changed; }