mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-05 23:24:21 +01:00
bkpr: create accounts for zero sat channels
we weren't making records for 'missed' accounts that had a zero balance at snapshot time (if peer opens channel and is unused) Fixes: #5502 Reported-By: https://github.com/niftynei/cln-logmaid
This commit is contained in:
@@ -474,7 +474,8 @@ static struct command_result *json_list_balances(struct command *cmd,
|
||||
accts[i]->name,
|
||||
true,
|
||||
false, /* don't skip ignored */
|
||||
&balances);
|
||||
&balances,
|
||||
NULL);
|
||||
|
||||
if (err)
|
||||
plugin_err(cmd->plugin,
|
||||
@@ -888,7 +889,7 @@ listpeers_multi_done(struct command *cmd,
|
||||
|
||||
db_begin_transaction(db);
|
||||
err = account_get_balance(tmpctx, db, info->acct->name,
|
||||
false, false, &balances);
|
||||
false, false, &balances, NULL);
|
||||
db_commit_transaction(db);
|
||||
|
||||
if (err)
|
||||
@@ -1001,6 +1002,7 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
|
||||
struct acct_balance **balances, *bal;
|
||||
struct amount_msat snap_balance, credit_diff, debit_diff;
|
||||
char *acct_name, *currency;
|
||||
bool exists;
|
||||
|
||||
err = json_scan(cmd, buf, acct_tok,
|
||||
"{account_id:%"
|
||||
@@ -1029,7 +1031,8 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
|
||||
/* Ignore non-clightning
|
||||
* balances items */
|
||||
true,
|
||||
&balances);
|
||||
&balances,
|
||||
&exists);
|
||||
|
||||
if (err)
|
||||
plugin_err(cmd->plugin,
|
||||
@@ -1056,7 +1059,8 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
|
||||
"Unable to find_diff for amounts: %s",
|
||||
err);
|
||||
|
||||
if (!amount_msat_zero(credit_diff)
|
||||
if (!exists
|
||||
|| !amount_msat_zero(credit_diff)
|
||||
|| !amount_msat_zero(debit_diff)) {
|
||||
struct account *acct;
|
||||
struct channel_event *ev;
|
||||
@@ -1328,7 +1332,7 @@ listpeers_done(struct command *cmd, const char *buf,
|
||||
info->ev->timestamp)) {
|
||||
db_begin_transaction(db);
|
||||
err = account_get_balance(tmpctx, db, info->acct->name,
|
||||
false, false, &balances);
|
||||
false, false, &balances, NULL);
|
||||
db_commit_transaction(db);
|
||||
|
||||
if (err)
|
||||
|
||||
@@ -235,6 +235,10 @@ static struct income_event *rebalance_fee(const tal_t *ctx,
|
||||
static struct income_event *maybe_channel_income(const tal_t *ctx,
|
||||
struct channel_event *ev)
|
||||
{
|
||||
if (amount_msat_zero(ev->credit)
|
||||
&& amount_msat_zero(ev->debit))
|
||||
return NULL;
|
||||
|
||||
/* We record a +/- penalty adj, but we only count the credit */
|
||||
if (streq(ev->tag, "penalty_adj")) {
|
||||
if (!amount_msat_zero(ev->credit))
|
||||
|
||||
@@ -786,7 +786,8 @@ char *account_get_balance(const tal_t *ctx,
|
||||
const char *acct_name,
|
||||
bool calc_sum,
|
||||
bool skip_ignored,
|
||||
struct acct_balance ***balances)
|
||||
struct acct_balance ***balances,
|
||||
bool *account_exists)
|
||||
{
|
||||
struct db_stmt *stmt;
|
||||
|
||||
@@ -807,6 +808,8 @@ char *account_get_balance(const tal_t *ctx,
|
||||
db_bind_int(stmt, 1, skip_ignored ? 1 : 2);
|
||||
db_query_prepared(stmt);
|
||||
*balances = tal_arr(ctx, struct acct_balance *, 0);
|
||||
if (account_exists)
|
||||
*account_exists = false;
|
||||
|
||||
while (db_step(stmt)) {
|
||||
struct acct_balance *bal;
|
||||
@@ -817,6 +820,9 @@ char *account_get_balance(const tal_t *ctx,
|
||||
db_col_amount_msat(stmt, "credit", &bal->credit);
|
||||
db_col_amount_msat(stmt, "debit", &bal->debit);
|
||||
tal_arr_expand(balances, bal);
|
||||
|
||||
if (account_exists)
|
||||
*account_exists = true;
|
||||
}
|
||||
tal_free(stmt);
|
||||
|
||||
|
||||
@@ -108,7 +108,8 @@ char *account_get_balance(const tal_t *ctx,
|
||||
const char *acct_name,
|
||||
bool calc_sum,
|
||||
bool skip_ignored,
|
||||
struct acct_balance ***balances);
|
||||
struct acct_balance ***balances,
|
||||
bool *account_exists);
|
||||
|
||||
/* Get chain fees for account */
|
||||
struct onchain_fee **account_get_chain_fees(const tal_t *ctx, struct db *db,
|
||||
|
||||
@@ -1195,6 +1195,7 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p)
|
||||
struct account *acct, *acct2;
|
||||
struct chain_event *ev1;
|
||||
struct acct_balance **balances;
|
||||
bool exists;
|
||||
char *err;
|
||||
|
||||
memset(&peer_id, 3, sizeof(struct node_id));
|
||||
@@ -1203,6 +1204,13 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p)
|
||||
acct2 = new_account(ctx, tal_fmt(ctx, "wallet"), &peer_id);
|
||||
|
||||
db_begin_transaction(db);
|
||||
/* Check that account does not exist yet */
|
||||
err = account_get_balance(ctx, db, acct->name, true, false,
|
||||
&balances, &exists);
|
||||
|
||||
CHECK(!err);
|
||||
CHECK_MSG(!exists, "expected account not to exist");
|
||||
|
||||
account_add(db, acct);
|
||||
account_add(db, acct2);
|
||||
|
||||
@@ -1251,7 +1259,7 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p)
|
||||
log_chain_event(db, acct2, ev1);
|
||||
|
||||
err = account_get_balance(ctx, db, acct->name, true, false,
|
||||
&balances);
|
||||
&balances, NULL);
|
||||
CHECK_MSG(!err, err);
|
||||
db_commit_transaction(db);
|
||||
CHECK_MSG(!db_err, db_err);
|
||||
@@ -1274,17 +1282,18 @@ static bool test_account_balances(const tal_t *ctx, struct plugin *p)
|
||||
log_chain_event(db, acct, ev1);
|
||||
|
||||
err = account_get_balance(ctx, db, acct->name, true, false,
|
||||
&balances);
|
||||
&balances, &exists);
|
||||
CHECK_MSG(err != NULL, "Expected err message");
|
||||
CHECK(streq(err, "chf channel balance is negative? 5000msat - 5001msat"));
|
||||
CHECK_MSG(exists, "expected account to exist");
|
||||
|
||||
err = account_get_balance(ctx, db, acct->name, false, false,
|
||||
&balances);
|
||||
&balances, NULL);
|
||||
CHECK_MSG(!err, err);
|
||||
|
||||
/* Now with ignored events */
|
||||
err = account_get_balance(ctx, db, acct->name, true, true,
|
||||
&balances);
|
||||
&balances, NULL);
|
||||
CHECK(streq(balances[0]->currency, "btc"));
|
||||
CHECK(amount_msat_eq(balances[0]->balance,
|
||||
AMOUNT_MSAT(500 - 440 + 1000)));
|
||||
|
||||
Reference in New Issue
Block a user