Skip to content

Commit

Permalink
lightningd: put min/max feerates into db, struct channel.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Apr 4, 2018
1 parent 4234321 commit 6bb4727
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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... */
Expand Down
3 changes: 2 additions & 1 deletion lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
16 changes: 12 additions & 4 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6bb4727

Please sign in to comment.