diff --git a/lightningd/channel.c b/lightningd/channel.c index c67a21b13..87325a885 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -139,6 +139,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, /* NULL or stolen */ struct short_channel_id *scid, u64 our_msatoshi, + u64 msatoshi_to_us_min, + u64 msatoshi_to_us_max, /* Stolen */ struct bitcoin_tx *last_tx, 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->scid = tal_steal(channel, scid); 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_sig = *last_sig; channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); diff --git a/lightningd/channel.h b/lightningd/channel.h index a4a3911ad..b178afbf8 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -66,6 +66,9 @@ struct channel { /* Amount going to us, not counting unfinished HTLCs; if we have one. */ 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. */ struct bitcoin_tx *last_tx; @@ -117,6 +120,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, /* NULL or stolen */ struct short_channel_id *scid, u64 our_msatoshi, + u64 msatoshi_to_us_min, + u64 msatoshi_to_us_max, /* Stolen */ struct bitcoin_tx *last_tx, const secp256k1_ecdsa_signature *last_sig, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index cde7f0dc7..8f2503dd0 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -203,7 +203,13 @@ wallet_commit_channel(struct lightningd *ld, push_msat, false, /* !remote_funding_locked */ 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, /* msatoshi_to_us_min */ + our_msatoshi, /* msatoshi_to_us_max */ remote_commit, remote_commit_sig, NULL, /* No HTLC sigs yet */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 126e83f7b..32e6c4b67 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -678,6 +678,10 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg, &channel->funding_txid); json_add_u64(response, "msatoshi_to_us", 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", channel->funding_satoshi * 1000); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index c8c1d97a5..242310cea 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -843,6 +843,8 @@ static void remove_htlc_in(struct channel *channel, struct htlc_in *hin) channel->our_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); @@ -867,6 +869,8 @@ static void remove_htlc_out(struct channel *channel, struct htlc_out *hout) channel->our_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); diff --git a/wallet/db.c b/wallet/db.c index 825185de1..57ff7f778 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -282,6 +282,14 @@ char *dbmigrations[] = { /* https://bitcoinfees.github.io/#1d says Dec 17 peak was ~1M sat/kb * which is 250,000 sat/Sipa */ "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, }; diff --git a/wallet/wallet.c b/wallet/wallet.c index a3320d608..329f24d00 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -636,6 +636,8 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s sqlite3_column_int(stmt, 15) != 0, scid, 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), &last_sig, 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 * 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 = - "id, peer_id, short_channel_id, channel_config_local, " - "channel_config_remote, state, funder, channel_flags, " - "minimum_depth, " - "next_index_local, next_index_remote, " - "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " - "funding_locked_remote, push_msatoshi, msatoshi_local, " - "fundingkey_remote, revocation_basepoint_remote, " - "payment_basepoint_remote, htlc_basepoint_remote, " - "delayed_payment_basepoint_remote, per_commit_remote, " - "old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, " - "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " - "last_sent_commit_state, last_sent_commit_id, " - "last_tx, last_sig, last_was_revoke, first_blocknum, " - " min_possible_feerate, max_possible_feerate"; - + /*0*/ "id, peer_id, short_channel_id, channel_config_local, " + /*4*/ "channel_config_remote, state, funder, channel_flags, " + /*8*/ "minimum_depth, " + /*9*/ "next_index_local, next_index_remote, " + /*11*/ "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " + /*15*/ "funding_locked_remote, push_msatoshi, msatoshi_local, " + /*18*/ "fundingkey_remote, revocation_basepoint_remote, " + /*20*/ "payment_basepoint_remote, htlc_basepoint_remote, " + /*22*/ "delayed_payment_basepoint_remote, per_commit_remote, " + /*24*/ "old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, " + /*28*/ "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " + /*30*/ "last_sent_commit_state, last_sent_commit_id, " + /*32*/ "last_tx, last_sig, last_was_revoke, first_blocknum, " + /*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) { @@ -924,7 +928,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " last_tx=?, last_sig=?," " last_was_revoke=?," " min_possible_feerate=?," - " max_possible_feerate=?" + " max_possible_feerate=?," + " msatoshi_to_us_min=?," + " msatoshi_to_us_max=?" " WHERE id=?"); sqlite3_bind_int64(stmt, 1, chan->their_shachain.id); 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, 22, chan->min_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); wallet_channel_config_save(w, &chan->channel_info.their_config);