Skip to content

Commit

Permalink
db: clean up channel_stats handling.
Browse files Browse the repository at this point in the history
Indirection via a string and an enum is just adding confusion here.

And move the `struct channel_stats` into channel.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Sep 20, 2024
1 parent 3aace10 commit eb97998
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 75 deletions.
8 changes: 8 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ struct open_attempt {
const u8 *open_msg;
};

/* Statistics for a channel */
struct channel_stats {
u64 in_payments_offered, in_payments_fulfilled;
struct amount_msat in_msatoshi_offered, in_msatoshi_fulfilled;
u64 out_payments_offered, out_payments_fulfilled;
struct amount_msat out_msatoshi_offered, out_msatoshi_fulfilled;
};

struct channel {
/* Inside peer->channels. */
struct list_node list;
Expand Down
93 changes: 26 additions & 67 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@
/* 12 hours is usually enough reservation time */
#define RESERVATION_INC (6 * 12)

/* Possible channel state */
enum channel_state_bucket {
IN_OFFERED = 0,
IN_FULLFILLED = 1,
OUT_OFFERED = 2,
OUT_FULLFILLED = 3,
};

/* channel state identifier */
struct channel_state_param {
const char *dir_key;
const char *type_key;
const enum channel_state_bucket state;
};

/* These go in db, so values cannot change (we can't put this into
* lightningd/channel_state.h since it confuses cdump!) */
static enum state_change state_change_in_db(enum state_change s)
Expand Down Expand Up @@ -2029,82 +2014,56 @@ bool wallet_init_channels(struct wallet *w)
return wallet_channels_load_active(w);
}

static enum channel_state_bucket get_state_channel_db(const char *dir, const char *typ)
{
enum channel_state_bucket channel_state = IN_OFFERED;
if (streq(dir, "out"))
channel_state += 2;
if (streq(typ, "fulfilled"))
channel_state += 1;
return channel_state;
}

static
void wallet_channel_stats_incr_x(struct wallet *w,
char const *dir,
char const *typ,
u64 cdbid,
struct amount_msat msat)
static void wallet_channel_stats_incr_x(struct wallet *w,
u64 cdbid,
struct amount_msat msat,
const char *query)
{
struct db_stmt *stmt;
const char *query = NULL;

switch (get_state_channel_db(dir, typ)) {
case IN_OFFERED:
query = SQL("UPDATE channels"
" SET in_payments_offered = COALESCE(in_payments_offered, 0) + 1"
" , in_msatoshi_offered = COALESCE(in_msatoshi_offered, 0) + ?"
" WHERE id = ?;");
break;
case IN_FULLFILLED:
query = SQL("UPDATE channels"
" SET in_payments_fulfilled = COALESCE(in_payments_fulfilled, 0) + 1"
" , in_msatoshi_fulfilled = COALESCE(in_msatoshi_fulfilled, 0) + ?"
" WHERE id = ?;");
break;
case OUT_OFFERED:
query = SQL("UPDATE channels"
" SET out_payments_offered = COALESCE(out_payments_offered, 0) + 1"
" , out_msatoshi_offered = COALESCE(out_msatoshi_offered, 0) + ?"
" WHERE id = ?;");
break;
case OUT_FULLFILLED:
query = SQL("UPDATE channels"
" SET out_payments_fulfilled = COALESCE(out_payments_fulfilled, 0) + 1"
" , out_msatoshi_fulfilled = COALESCE(out_msatoshi_fulfilled, 0) + ?"
" WHERE id = ?;");
break;
}

// Sanity check!
if (!query)
fatal("Unknown channel state key (direction %s, type %s)", dir, typ);

stmt = db_prepare_v2(w->db, query);
db_bind_amount_msat(stmt, &msat);
db_bind_u64(stmt, cdbid);

db_exec_prepared_v2(take(stmt));
}

/* I would use macros for these, but gettext needs string literals :( */
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "in", "offered", id, m);
const char query[] = SQL("UPDATE channels"
" SET in_payments_offered = COALESCE(in_payments_offered, 0) + 1"
" , in_msatoshi_offered = COALESCE(in_msatoshi_offered, 0) + ?"
" WHERE id = ?;");
wallet_channel_stats_incr_x(w, id, m, query);
}
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "in", "fulfilled", id, m);
const char query[] = SQL("UPDATE channels"
" SET in_payments_fulfilled = COALESCE(in_payments_fulfilled, 0) + 1"
" , in_msatoshi_fulfilled = COALESCE(in_msatoshi_fulfilled, 0) + ?"
" WHERE id = ?;");
wallet_channel_stats_incr_x(w, id, m, query);
}
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "out", "offered", id, m);
const char query[] = SQL("UPDATE channels"
" SET out_payments_offered = COALESCE(out_payments_offered, 0) + 1"
" , out_msatoshi_offered = COALESCE(out_msatoshi_offered, 0) + ?"
" WHERE id = ?;");
wallet_channel_stats_incr_x(w, id, m, query);
}
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "out", "fulfilled", id, m);
const char query[] = SQL("UPDATE channels"
" SET out_payments_fulfilled = COALESCE(out_payments_fulfilled, 0) + 1"
" , out_msatoshi_fulfilled = COALESCE(out_msatoshi_fulfilled, 0) + ?"
" WHERE id = ?;");
wallet_channel_stats_incr_x(w, id, m, query);
}

u32 wallet_blocks_maxheight(struct wallet *w)
Expand Down
8 changes: 0 additions & 8 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,6 @@ struct outpoint {
u32 spendheight;
};

/* Statistics for a channel */
struct channel_stats {
u64 in_payments_offered, in_payments_fulfilled;
struct amount_msat in_msatoshi_offered, in_msatoshi_fulfilled;
u64 out_payments_offered, out_payments_fulfilled;
struct amount_msat out_msatoshi_offered, out_msatoshi_fulfilled;
};

struct channeltx {
u32 channel_id;
int type;
Expand Down

0 comments on commit eb97998

Please sign in to comment.