Skip to content

Commit

Permalink
delforward: tally up deleted forwards so that getinfo's `fees_collect…
Browse files Browse the repository at this point in the history
…ed_msat` doesn't change.

Signed-off-by: Rusty Russell <[email protected]>
Fixes: ElementsProject#5627
  • Loading branch information
rustyrussell committed Sep 27, 2022
1 parent cafa1a8 commit 6eac8df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3089,6 +3089,8 @@ def test_autoclean(node_factory):
assert l2.rpc.autoclean_status()['autoclean']['failedforwards']['cleaned'] == 1
assert l2.rpc.autoclean_status()['autoclean']['succeededforwards']['cleaned'] == 0

amt_before = l2.rpc.getinfo()['fees_collected_msat']

# Clean succeeded ones
l2.stop()
l2.daemon.opts['autoclean-succeededforwards-age'] = 2
Expand All @@ -3098,6 +3100,9 @@ def test_autoclean(node_factory):
assert l2.rpc.autoclean_status()['autoclean']['failedforwards']['cleaned'] == 1
assert l2.rpc.autoclean_status()['autoclean']['succeededforwards']['cleaned'] == 1

# We still see correct total in getinfo!
assert l2.rpc.getinfo()['fees_collected_msat'] == amt_before


def test_autoclean_once(node_factory):
l1, l2, l3 = node_factory.line_graph(3, opts={'may_reconnect': True},
Expand Down
34 changes: 33 additions & 1 deletion wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -4506,7 +4506,7 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
struct amount_msat wallet_total_forward_fees(struct wallet *w)
{
struct db_stmt *stmt;
struct amount_msat total;
struct amount_msat total, deleted;
bool res;

stmt = db_prepare_v2(w->db, SQL("SELECT"
Expand All @@ -4522,6 +4522,12 @@ struct amount_msat wallet_total_forward_fees(struct wallet *w)
db_col_amount_msat(stmt, "CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)", &total);
tal_free(stmt);

deleted = amount_msat(db_get_intvar(w->db, "deleted_forward_fees", 0));
if (!amount_msat_add(&total, total, deleted))
db_fatal("Adding forward fees %s + %s overflowed",
type_to_string(tmpctx, struct amount_msat, &total),
type_to_string(tmpctx, struct amount_msat, &deleted));

return total;
}

Expand Down Expand Up @@ -4694,6 +4700,32 @@ bool wallet_forward_delete(struct wallet *w,
struct db_stmt *stmt;
bool changed;

/* When deleting settled ones, we have to add to deleted_forward_fees! */
if (state == FORWARD_SETTLED) {
/* Of course, it might not be settled: don't add if they're wrong! */
stmt = db_prepare_v2(w->db, SQL("SELECT"
" in_msatoshi - out_msatoshi"
" FROM forwards "
" WHERE in_channel_scid = ?"
" AND in_htlc_id = ?"
" AND state = ?;"));
db_bind_scid(stmt, 0, chan_in);
db_bind_u64(stmt, 1, htlc_id);
db_bind_int(stmt, 2, wallet_forward_status_in_db(FORWARD_SETTLED));
db_query_prepared(stmt);

if (db_step(stmt)) {
struct amount_msat deleted;

db_col_amount_msat(stmt, "in_msatoshi - out_msatoshi", &deleted);
deleted.millisatoshis += /* Raw: db access */
db_get_intvar(w->db, "deleted_forward_fees", 0);
db_set_intvar(w->db, "deleted_forward_fees",
deleted.millisatoshis); /* Raw: db access */
}
tal_free(stmt);
}

stmt = db_prepare_v2(w->db,
SQL("DELETE FROM forwards"
" WHERE in_channel_scid = ?"
Expand Down

0 comments on commit 6eac8df

Please sign in to comment.