Skip to content

Commit

Permalink
forwards: hook into the wait subsystem.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
Changelog-Added: JSON-RPC: `wait` now works for `forwards` infrastructure.
  • Loading branch information
rustyrussell committed Oct 24, 2023
1 parent 8568038 commit b73f4b9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lightningd/forwards.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
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);
}

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,
Expand All @@ -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);
}

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

Expand Down
2 changes: 1 addition & 1 deletion lightningd/forwards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
30 changes: 21 additions & 9 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 26 additions & 3 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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"
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit b73f4b9

Please sign in to comment.