Skip to content

Commit

Permalink
bkpr: properly account for fees and channel closures if splice
Browse files Browse the repository at this point in the history
We do some fancy accounting for channel closures; since we're tagging
splice txs as closes we need to mark them as splices so we can treat them
as any other 'normal' on chain event.
  • Loading branch information
niftynei committed Aug 7, 2024
1 parent 271bcb9 commit 61996dd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ static bool new_missed_channel_account(struct command *cmd,
chain_ev->payment_id = NULL;
chain_ev->ignored = false;
chain_ev->stealable = false;
chain_ev->splice_close = false;
chain_ev->desc = NULL;

/* Update the account info too */
Expand Down Expand Up @@ -1458,9 +1459,11 @@ parse_and_log_chain_move(struct command *cmd,

e->ignored = false;
e->stealable = false;
e->splice_close = false;
for (size_t i = 0; i < tal_count(tags); i++) {
e->ignored |= tags[i] == IGNORED;
e->stealable |= tags[i] == STEALABLE;
e->splice_close |= tags[i] == SPLICE;
}

db_begin_transaction(db);
Expand Down
4 changes: 4 additions & 0 deletions plugins/bkpr/chain_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct chain_event {
* we'll need to watch it for longer */
bool stealable;

/* Is this chain event because of a splice
* confirmation? */
bool splice_close;

/* Is this a rebalance event? */
bool rebalance;

Expand Down
1 change: 1 addition & 0 deletions plugins/bkpr/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static struct migration db_migrations[] = {
{SQL("ALTER TABLE chain_events ADD ev_desc TEXT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channel_events ADD ev_desc TEXT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE channel_events ADD rebalance_id BIGINT DEFAULT NULL;"), NULL},
{SQL("ALTER TABLE chain_events ADD spliced INTEGER DEFAULT 0;"), NULL},
{NULL, migration_remove_dupe_lease_fees}
};

Expand Down
22 changes: 19 additions & 3 deletions plugins/bkpr/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ static struct chain_event *stmt2chain_event(const tal_t *ctx, struct db_stmt *st
else
e->desc = NULL;

e->splice_close = db_col_int(stmt, "e.spliced") == 1;

return e;
}

Expand Down Expand Up @@ -162,6 +164,7 @@ struct chain_event **list_chain_events_timebox(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -204,6 +207,7 @@ struct chain_event **account_get_chain_events(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -239,6 +243,7 @@ static struct chain_event **find_txos_for_tx(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -548,7 +553,9 @@ struct account *find_close_account(const tal_t *ctx,
" ON e.account_id = a.id"
" WHERE "
" e.tag = ?"
" AND e.spending_txid = ?"));
" AND e.spending_txid = ?"
/* ignore splicing 'close' events */
" AND e.spliced = 0 "));

db_bind_text(stmt, mvt_tag_str(CHANNEL_CLOSE));
db_bind_txid(stmt, txid);
Expand Down Expand Up @@ -678,6 +685,7 @@ struct chain_event *find_chain_event_by_id(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -726,6 +734,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -755,6 +764,7 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON e.account_id = a.id"
Expand Down Expand Up @@ -1263,6 +1273,9 @@ void maybe_update_account(struct db *db,
*acct->open_event_db_id = e->db_id;
break;
case CHANNEL_CLOSE:
/* Splices dont count as closes */
if (e->splice_close)
break;
updated = true;
acct->closed_event_db_id = tal(acct, u64);
*acct->closed_event_db_id = e->db_id;
Expand Down Expand Up @@ -1305,7 +1318,7 @@ void maybe_update_account(struct db *db,
acct->peer_id = tal_dup(acct, struct node_id, peer_id);
}

if (closed_count > 0) {
if (!e->splice_close && closed_count > 0) {
updated = true;
acct->closed_count = closed_count;
}
Expand Down Expand Up @@ -1424,6 +1437,7 @@ static struct chain_event **find_chain_events_bytxid(const tal_t *ctx, struct db
", e.ignored"
", e.stealable"
", e.ev_desc"
", e.spliced"
" FROM chain_events e"
" LEFT OUTER JOIN accounts a"
" ON a.id = e.account_id"
Expand Down Expand Up @@ -2003,9 +2017,10 @@ bool log_chain_event(struct db *db,
", ignored"
", stealable"
", ev_desc"
", spliced"
")"
" VALUES "
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"));

db_bind_u64(stmt, acct->db_id);
if (e->origin_acct)
Expand Down Expand Up @@ -2038,6 +2053,7 @@ bool log_chain_event(struct db *db,
db_bind_text(stmt, e->desc);
else
db_bind_null(stmt);
db_bind_int(stmt, e->splice_close ? 1 : 0);
db_exec_prepared_v2(stmt);
e->db_id = db_last_insert_id_v2(stmt);
e->acct_db_id = acct->db_id;
Expand Down
7 changes: 7 additions & 0 deletions plugins/bkpr/test/run-recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ static bool chain_events_eq(struct chain_event *e1, struct chain_event *e2)
if (e1->desc)
CHECK(streq(e1->desc, e2->desc));

CHECK(e1->splice_close == e2->splice_close);

return true;
}

Expand Down Expand Up @@ -406,6 +408,7 @@ static struct chain_event *make_chain_event(const tal_t *ctx,
ev->blockheight = blockheight;
ev->ignored = false;
ev->stealable = false;
ev->splice_close = false;
ev->desc = tal_fmt(ev, "hello hello");
memset(&ev->outpoint.txid, outpoint_char, sizeof(struct bitcoin_txid));
ev->outpoint.n = outnum;
Expand Down Expand Up @@ -1084,6 +1087,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p)
ev1->blockheight = 1919191;
ev1->ignored = false;
ev1->stealable = false;
ev1->splice_close = false;
memset(&ev1->outpoint.txid, 'D', sizeof(struct bitcoin_txid));
ev1->outpoint.n = 1;
ev1->spending_txid = tal(ctx, struct bitcoin_txid);
Expand All @@ -1105,6 +1109,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p)
ev2->blockheight = 1919191;
ev2->ignored = false;
ev2->stealable = false;
ev2->splice_close = false;
memset(&ev2->outpoint.txid, 'D', sizeof(struct bitcoin_txid));
ev2->outpoint.n = 1;
ev2->spending_txid = NULL;
Expand All @@ -1124,6 +1129,7 @@ static bool test_chain_event_crud(const tal_t *ctx, struct plugin *p)
ev3->blockheight = 3939393;
ev3->ignored = false;
ev3->stealable = false;
ev3->splice_close = false;
memset(&ev3->outpoint.txid, 'E', sizeof(struct bitcoin_txid));
ev3->outpoint.n = 1;
ev3->spending_txid = tal(ctx, struct bitcoin_txid);
Expand Down Expand Up @@ -1351,6 +1357,7 @@ static bool test_account_crud(const tal_t *ctx, struct plugin *p)
ev1->blockheight = 1919191;
ev1->ignored = false;
ev1->stealable = false;
ev1->splice_close = false;
memset(&ev1->outpoint.txid, 'D', sizeof(struct bitcoin_txid));
ev1->outpoint.n = 1;
ev1->spending_txid = tal(ctx, struct bitcoin_txid);
Expand Down

0 comments on commit 61996dd

Please sign in to comment.