wallet: Add msatoshi_to_us_min and msatoshi_to_us_max statistics for channels.

So we know how much counterparty could theoretically steal from us
 (msatoshi_to_us - msatoshi_to_us_min) and how much we could
 theoretically steal from counterparty (msatoshi_to_us_max -
 msatoshi_to_us).
For more piloting goodness.
This commit is contained in:
ZmnSCPxj 2018-03-31 00:21:13 +00:00 committed by Christian Decker
parent d3d195dd85
commit f83c4ff903
7 changed files with 56 additions and 17 deletions

View file

@ -139,6 +139,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
/* NULL or stolen */ /* NULL or stolen */
struct short_channel_id *scid, struct short_channel_id *scid,
u64 our_msatoshi, u64 our_msatoshi,
u64 msatoshi_to_us_min,
u64 msatoshi_to_us_max,
/* Stolen */ /* Stolen */
struct bitcoin_tx *last_tx, struct bitcoin_tx *last_tx,
const secp256k1_ecdsa_signature *last_sig, const secp256k1_ecdsa_signature *last_sig,
@ -196,6 +198,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->remote_funding_locked = remote_funding_locked; channel->remote_funding_locked = remote_funding_locked;
channel->scid = tal_steal(channel, scid); channel->scid = tal_steal(channel, scid);
channel->our_msatoshi = our_msatoshi; channel->our_msatoshi = our_msatoshi;
channel->msatoshi_to_us_min = msatoshi_to_us_min;
channel->msatoshi_to_us_max = msatoshi_to_us_max;
channel->last_tx = tal_steal(channel, last_tx); channel->last_tx = tal_steal(channel, last_tx);
channel->last_sig = *last_sig; channel->last_sig = *last_sig;
channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs);

View file

@ -66,6 +66,9 @@ struct channel {
/* Amount going to us, not counting unfinished HTLCs; if we have one. */ /* Amount going to us, not counting unfinished HTLCs; if we have one. */
u64 our_msatoshi; u64 our_msatoshi;
/* Statistics for min and max our_msatoshi. */
u64 msatoshi_to_us_min;
u64 msatoshi_to_us_max;
/* Last tx they gave us. */ /* Last tx they gave us. */
struct bitcoin_tx *last_tx; struct bitcoin_tx *last_tx;
@ -117,6 +120,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
/* NULL or stolen */ /* NULL or stolen */
struct short_channel_id *scid, struct short_channel_id *scid,
u64 our_msatoshi, u64 our_msatoshi,
u64 msatoshi_to_us_min,
u64 msatoshi_to_us_max,
/* Stolen */ /* Stolen */
struct bitcoin_tx *last_tx, struct bitcoin_tx *last_tx,
const secp256k1_ecdsa_signature *last_sig, const secp256k1_ecdsa_signature *last_sig,

View file

@ -203,7 +203,13 @@ wallet_commit_channel(struct lightningd *ld,
push_msat, push_msat,
false, /* !remote_funding_locked */ false, /* !remote_funding_locked */
NULL, /* no scid yet */ NULL, /* no scid yet */
/* The three arguments below are msatoshi_to_us,
* msatoshi_to_us_min, and msatoshi_to_us_max.
* Because, this is a newly-funded channel,
* all three are same value. */
our_msatoshi, our_msatoshi,
our_msatoshi, /* msatoshi_to_us_min */
our_msatoshi, /* msatoshi_to_us_max */
remote_commit, remote_commit,
remote_commit_sig, remote_commit_sig,
NULL, /* No HTLC sigs yet */ NULL, /* No HTLC sigs yet */

View file

@ -678,6 +678,10 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg,
&channel->funding_txid); &channel->funding_txid);
json_add_u64(response, "msatoshi_to_us", json_add_u64(response, "msatoshi_to_us",
channel->our_msatoshi); channel->our_msatoshi);
json_add_u64(response, "msatoshi_to_us_min",
channel->msatoshi_to_us_min);
json_add_u64(response, "msatoshi_to_us_max",
channel->msatoshi_to_us_max);
json_add_u64(response, "msatoshi_total", json_add_u64(response, "msatoshi_total",
channel->funding_satoshi * 1000); channel->funding_satoshi * 1000);

View file

@ -843,6 +843,8 @@ static void remove_htlc_in(struct channel *channel, struct htlc_in *hin)
channel->our_msatoshi, channel->our_msatoshi,
channel->our_msatoshi + hin->msatoshi); channel->our_msatoshi + hin->msatoshi);
channel->our_msatoshi += hin->msatoshi; channel->our_msatoshi += hin->msatoshi;
if (channel->our_msatoshi > channel->msatoshi_to_us_max)
channel->msatoshi_to_us_max = channel->our_msatoshi;
} }
tal_free(hin); tal_free(hin);
@ -867,6 +869,8 @@ static void remove_htlc_out(struct channel *channel, struct htlc_out *hout)
channel->our_msatoshi, channel->our_msatoshi,
channel->our_msatoshi - hout->msatoshi); channel->our_msatoshi - hout->msatoshi);
channel->our_msatoshi -= hout->msatoshi; channel->our_msatoshi -= hout->msatoshi;
if (channel->our_msatoshi < channel->msatoshi_to_us_min)
channel->msatoshi_to_us_min = channel->our_msatoshi;
} }
tal_free(hout); tal_free(hout);

View file

@ -282,6 +282,14 @@ char *dbmigrations[] = {
/* https://bitcoinfees.github.io/#1d says Dec 17 peak was ~1M sat/kb /* https://bitcoinfees.github.io/#1d says Dec 17 peak was ~1M sat/kb
* which is 250,000 sat/Sipa */ * which is 250,000 sat/Sipa */
"UPDATE channels SET min_possible_feerate=0, max_possible_feerate=250000;", "UPDATE channels SET min_possible_feerate=0, max_possible_feerate=250000;",
/* -- Min and max msatoshi_to_us -- */
"ALTER TABLE channels ADD msatoshi_to_us_min INTEGER;",
"ALTER TABLE channels ADD msatoshi_to_us_max INTEGER;",
"UPDATE channels"
" SET msatoshi_to_us_min = msatoshi_local"
" , msatoshi_to_us_max = msatoshi_local"
" ;",
/* -- Min and max msatoshi_to_us ends -- */
NULL, NULL,
}; };

View file

@ -636,6 +636,8 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s
sqlite3_column_int(stmt, 15) != 0, sqlite3_column_int(stmt, 15) != 0,
scid, scid,
sqlite3_column_int64(stmt, 17), sqlite3_column_int64(stmt, 17),
sqlite3_column_int64(stmt, 38), /* msatoshi_to_us_min */
sqlite3_column_int64(stmt, 39), /* msatoshi_to_us_max */
sqlite3_column_tx(tmpctx, stmt, 32), sqlite3_column_tx(tmpctx, stmt, 32),
&last_sig, &last_sig,
wallet_htlc_sigs_load(tmpctx, w, wallet_htlc_sigs_load(tmpctx, w,
@ -654,22 +656,24 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s
/* List of fields to retrieve from the channels DB table, in the order /* List of fields to retrieve from the channels DB table, in the order
* that wallet_stmt2channel understands and will parse correctly */ * that wallet_stmt2channel understands and will parse correctly */
/* Numbers below are sqlite3_column indices for the first field
* of that line. */
static const char *channel_fields = static const char *channel_fields =
"id, peer_id, short_channel_id, channel_config_local, " /*0*/ "id, peer_id, short_channel_id, channel_config_local, "
"channel_config_remote, state, funder, channel_flags, " /*4*/ "channel_config_remote, state, funder, channel_flags, "
"minimum_depth, " /*8*/ "minimum_depth, "
"next_index_local, next_index_remote, " /*9*/ "next_index_local, next_index_remote, "
"next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " /*11*/ "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, "
"funding_locked_remote, push_msatoshi, msatoshi_local, " /*15*/ "funding_locked_remote, push_msatoshi, msatoshi_local, "
"fundingkey_remote, revocation_basepoint_remote, " /*18*/ "fundingkey_remote, revocation_basepoint_remote, "
"payment_basepoint_remote, htlc_basepoint_remote, " /*20*/ "payment_basepoint_remote, htlc_basepoint_remote, "
"delayed_payment_basepoint_remote, per_commit_remote, " /*22*/ "delayed_payment_basepoint_remote, per_commit_remote, "
"old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, " /*24*/ "old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, "
"shutdown_scriptpubkey_remote, shutdown_keyidx_local, " /*28*/ "shutdown_scriptpubkey_remote, shutdown_keyidx_local, "
"last_sent_commit_state, last_sent_commit_id, " /*30*/ "last_sent_commit_state, last_sent_commit_id, "
"last_tx, last_sig, last_was_revoke, first_blocknum, " /*32*/ "last_tx, last_sig, last_was_revoke, first_blocknum, "
" min_possible_feerate, max_possible_feerate"; /*36*/ "min_possible_feerate, max_possible_feerate, "
/*38*/ "msatoshi_to_us_min, msatoshi_to_us_max ";
bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w) bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w)
{ {
@ -924,7 +928,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
" last_tx=?, last_sig=?," " last_tx=?, last_sig=?,"
" last_was_revoke=?," " last_was_revoke=?,"
" min_possible_feerate=?," " min_possible_feerate=?,"
" max_possible_feerate=?" " max_possible_feerate=?,"
" msatoshi_to_us_min=?,"
" msatoshi_to_us_max=?"
" WHERE id=?"); " WHERE id=?");
sqlite3_bind_int64(stmt, 1, chan->their_shachain.id); sqlite3_bind_int64(stmt, 1, chan->their_shachain.id);
if (chan->scid) if (chan->scid)
@ -958,7 +964,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
sqlite3_bind_int(stmt, 21, chan->last_was_revoke); sqlite3_bind_int(stmt, 21, chan->last_was_revoke);
sqlite3_bind_int(stmt, 22, chan->min_possible_feerate); sqlite3_bind_int(stmt, 22, chan->min_possible_feerate);
sqlite3_bind_int(stmt, 23, chan->max_possible_feerate); sqlite3_bind_int(stmt, 23, chan->max_possible_feerate);
sqlite3_bind_int64(stmt, 24, chan->dbid); sqlite3_bind_int64(stmt, 24, chan->msatoshi_to_us_min);
sqlite3_bind_int64(stmt, 25, chan->msatoshi_to_us_max);
sqlite3_bind_int64(stmt, 26, chan->dbid);
db_exec_prepared(w->db, stmt); db_exec_prepared(w->db, stmt);
wallet_channel_config_save(w, &chan->channel_info.their_config); wallet_channel_config_save(w, &chan->channel_info.their_config);