wallet: Track some channel usage statistics.

Fixes: #1049
This commit is contained in:
ZmnSCPxj
2018-03-24 08:26:08 +00:00
committed by Rusty Russell
parent 217d4f99eb
commit 0bb9bcc0f1
5 changed files with 152 additions and 5 deletions

View File

@@ -251,6 +251,22 @@ char *dbmigrations[] = {
" , route_nodes = NULL"
" , route_channels = NULL"
" WHERE status <> 0;", /* PAYMENT_PENDING */
/* -- Routing statistics -- */
"ALTER TABLE channels ADD in_payments_offered INTEGER;",
"ALTER TABLE channels ADD in_payments_fulfilled INTEGER;",
"ALTER TABLE channels ADD in_msatoshi_offered INTEGER;",
"ALTER TABLE channels ADD in_msatoshi_fulfilled INTEGER;",
"ALTER TABLE channels ADD out_payments_offered INTEGER;",
"ALTER TABLE channels ADD out_payments_fulfilled INTEGER;",
"ALTER TABLE channels ADD out_msatoshi_offered INTEGER;",
"ALTER TABLE channels ADD out_msatoshi_fulfilled INTEGER;",
"UPDATE channels"
" SET in_payments_offered = 0, in_payments_fulfilled = 0"
" , in_msatoshi_offered = 0, in_msatoshi_fulfilled = 0"
" , out_payments_offered = 0, out_payments_fulfilled = 0"
" , out_msatoshi_offered = 0, out_msatoshi_fulfilled = 0"
" ;",
/* -- Routing statistics ends --*/
NULL,
};

View File

@@ -719,6 +719,68 @@ bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w)
return ok;
}
static
void wallet_channel_stats_incr_x(struct wallet *w,
char const *dir,
char const *typ,
u64 cdbid,
u64 msatoshi)
{
char const *payments_stat = tal_fmt(tmpctx, "%s_payments_%s",
dir, typ);
char const *msatoshi_stat = tal_fmt(tmpctx, "%s_msatoshi_%s",
dir, typ);
char const *qry = tal_fmt(tmpctx,
"UPDATE channels"
" SET %s = COALESCE(%s, 0) + 1"
" , %s = COALESCE(%s, 0) + %"PRIu64""
" WHERE id = %"PRIu64";",
payments_stat, payments_stat,
msatoshi_stat, msatoshi_stat, msatoshi,
cdbid);
sqlite3_stmt *stmt = db_prepare(w->db, qry);
db_exec_prepared(w->db, stmt);
}
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 id, u64 m)
{
wallet_channel_stats_incr_x(w, "in", "offered", id, m);
}
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 id, u64 m)
{
wallet_channel_stats_incr_x(w, "in", "fulfilled", id, m);
}
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 id, u64 m)
{
wallet_channel_stats_incr_x(w, "out", "offered", id, m);
}
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 id, u64 m)
{
wallet_channel_stats_incr_x(w, "out", "fulfilled", id, m);
}
void wallet_channel_stats_load(struct wallet *w,
u64 id,
struct channel_stats *stats)
{
sqlite3_stmt *stmt;
stmt = db_prepare(w->db,
"SELECT in_payments_offered, in_payments_fulfilled"
" , in_msatoshi_offered, in_msatoshi_fulfilled"
" , out_payments_offered, out_payments_fulfilled"
" , out_msatoshi_offered, out_msatoshi_fulfilled"
" FROM channels"
" WHERE id = ?");
sqlite3_bind_int64(stmt, 1, id);
stats->in_payments_offered = sqlite3_column_int64(stmt, 0);
stats->in_payments_fulfilled = sqlite3_column_int64(stmt, 1);
stats->in_msatoshi_offered = sqlite3_column_int64(stmt, 2);
stats->in_msatoshi_fulfilled = sqlite3_column_int64(stmt, 3);
stats->out_payments_offered = sqlite3_column_int64(stmt, 4);
stats->out_payments_fulfilled = sqlite3_column_int64(stmt, 5);
stats->out_msatoshi_offered = sqlite3_column_int64(stmt, 6);
stats->out_msatoshi_fulfilled = sqlite3_column_int64(stmt, 7);
}
#ifdef COMPAT_V052
/* Upgrade of db (or initial create): do we have anything to scan for? */
static bool wallet_ever_used(struct wallet *w)

View File

@@ -119,6 +119,14 @@ struct outpoint {
u32 spendheight;
};
/* Statistics for a channel */
struct channel_stats {
u64 in_payments_offered, in_payments_fulfilled;
u64 in_msatoshi_offered, in_msatoshi_fulfilled;
u64 out_payments_offered, out_payments_fulfilled;
u64 out_msatoshi_offered, out_msatoshi_fulfilled;
};
/**
* wallet_new - Constructor for a new sqlite3 based wallet
*
@@ -286,6 +294,27 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid,
*/
bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w);
/**
* wallet_channel_stats_incr_* - Increase channel statistics.
*
* @w: wallet containing the channel
* @cdbid: channel database id
* @msatoshi: amount in msatoshi being transferred
*/
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
/**
* wallet_channel_stats_load - Load channel statistics
*
* @w: wallet containing the channel
* @cdbid: channel database id
* @stats: location to load statistics to
*/
void wallet_channel_stats_load(struct wallet *w, u64 cdbid, struct channel_stats *stats);
/**
* wallet_first_blocknum - get first block we're interested in.
*