diff --git a/lightningd/channel.c b/lightningd/channel.c index 605e1aa50..c67a21b13 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -151,7 +151,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool last_was_revoke, /* NULL or stolen */ struct changed_htlc *last_sent_commit, - u32 first_blocknum) + u32 first_blocknum, + u32 min_possible_feerate, + u32 max_possible_feerate) { struct channel *channel = tal(peer->ld, struct channel); @@ -204,6 +206,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->last_was_revoke = last_was_revoke; channel->last_sent_commit = tal_steal(channel, last_sent_commit); channel->first_blocknum = first_blocknum; + channel->min_possible_feerate = min_possible_feerate; + channel->max_possible_feerate = max_possible_feerate; derive_channel_seed(peer->ld, &channel->seed, &peer->id, channel->dbid); list_add_tail(&peer->channels, &channel->list); diff --git a/lightningd/channel.h b/lightningd/channel.h index d4adaa516..a4a3911ad 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -90,6 +90,9 @@ struct channel { /* Blockheight at creation, scans for funding confirmations * will start here */ u64 first_blocknum; + + /* Feerate range */ + u32 min_possible_feerate, max_possible_feerate; }; struct channel *new_channel(struct peer *peer, u64 dbid, @@ -126,7 +129,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool last_was_revoke, /* NULL or stolen */ struct changed_htlc *last_sent_commit, - u32 first_blocknum); + u32 first_blocknum, + u32 min_possible_feerate, + u32 max_possible_feerate); void delete_channel(struct channel *channel); diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 071f25e4d..10a97a9d0 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -418,7 +418,8 @@ enum watch_result funding_spent(struct channel *channel, 3, channel->last_htlc_sigs, tal_count(stubs), - 0, 250000); + channel->min_possible_feerate, + channel->max_possible_feerate); subd_send_msg(channel->owner, take(msg)); /* FIXME: Don't queue all at once, use an empty cb... */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index cfa050cfa..cde7f0dc7 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -211,7 +211,8 @@ wallet_commit_channel(struct lightningd *ld, NULL, /* No remote_shutdown_scriptpubkey yet */ final_key_idx, false, NULL, /* No commit sent yet */ - uc->first_blocknum); + uc->first_blocknum, + feerate, feerate); /* Now we finally put it in the database. */ wallet_channel_insert(ld->wallet, channel); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index ba226aacb..502af9932 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1022,6 +1022,10 @@ void peer_sending_commitsig(struct channel *channel, const u8 *msg) /* Update their feerate. */ channel->channel_info.feerate_per_kw[REMOTE] = feerate; + if (feerate > channel->max_possible_feerate) + channel->max_possible_feerate = feerate; + if (feerate < channel->min_possible_feerate) + channel->min_possible_feerate = feerate; if (!peer_save_commitsig_sent(channel, commitnum)) return; @@ -1185,6 +1189,11 @@ void peer_got_commitsig(struct channel *channel, const u8 *msg) = channel->channel_info.feerate_per_kw[REMOTE] = feerate; + if (feerate > channel->max_possible_feerate) + channel->max_possible_feerate = feerate; + if (feerate < channel->min_possible_feerate) + channel->min_possible_feerate = feerate; + /* Since we're about to send revoke, bump state again. */ if (!peer_sending_revocation(channel, added, fulfilled, failed, changed)) return; diff --git a/wallet/db.c b/wallet/db.c index 058863f30..825185de1 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -276,6 +276,12 @@ char *dbmigrations[] = { " FROM utxoset LEFT OUTER JOIN blocks on (blockheight == blocks.height) " " WHERE blocks.hash IS NULL" ");", + /* Record feerate range, to optimize onchaind grinding actual fees. */ + "ALTER TABLE channels ADD min_possible_feerate INTEGER;", + "ALTER TABLE channels ADD max_possible_feerate INTEGER;", + /* 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;", NULL, }; diff --git a/wallet/wallet.c b/wallet/wallet.c index 68d6e5217..a3320d608 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -645,7 +645,9 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s final_key_idx, sqlite3_column_int(stmt, 34) != 0, last_sent_commit, - sqlite3_column_int64(stmt, 35)); + sqlite3_column_int64(stmt, 35), + sqlite3_column_int(stmt, 36), + sqlite3_column_int(stmt, 37)); return chan; } @@ -665,7 +667,9 @@ static const char *channel_fields = "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"; + "last_tx, last_sig, last_was_revoke, first_blocknum, " + " min_possible_feerate, max_possible_feerate"; + bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w) { @@ -918,7 +922,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " shutdown_keyidx_local=?," " channel_config_local=?," " last_tx=?, last_sig=?," - " last_was_revoke=?" + " last_was_revoke=?," + " min_possible_feerate=?," + " max_possible_feerate=?" " WHERE id=?"); sqlite3_bind_int64(stmt, 1, chan->their_shachain.id); if (chan->scid) @@ -950,7 +956,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) sqlite3_bind_tx(stmt, 19, chan->last_tx); sqlite3_bind_signature(stmt, 20, &chan->last_sig); sqlite3_bind_int(stmt, 21, chan->last_was_revoke); - sqlite3_bind_int64(stmt, 22, chan->dbid); + 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); db_exec_prepared(w->db, stmt); wallet_channel_config_save(w, &chan->channel_info.their_config);