From 1043df28be7d7015aaa66bc016fbe1c5762959c5 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Thu, 21 Feb 2019 01:01:33 +0100 Subject: [PATCH] adds: new db fields and struct variables - Intrduce DB update `channel` values: `feerate_base` and `feerate_ppm` - Make fist use of now context realted DB migration - Add `struct channel` members of the same name - Use struct values instead of config when commiting new channels --- lightningd/channel.c | 6 +++++- lightningd/channel.h | 7 ++++++- lightningd/channel_control.c | 4 ++-- lightningd/opening_control.c | 4 +++- wallet/db.c | 15 +++++++++++++++ wallet/wallet.c | 15 +++++++++++---- 6 files changed, 42 insertions(+), 9 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index d429fb168..46bc881f1 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -171,7 +171,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool connected, const struct basepoints *local_basepoints, const struct pubkey *local_funding_pubkey, - const struct pubkey *future_per_commitment_point) + const struct pubkey *future_per_commitment_point, + u32 feerate_base, + u32 feerate_ppm) { struct channel *channel = tal(peer->ld, struct channel); @@ -234,6 +236,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->local_funding_pubkey = *local_funding_pubkey; channel->future_per_commitment_point = tal_steal(channel, future_per_commitment_point); + channel->feerate_base = feerate_base; + channel->feerate_ppm = feerate_ppm; list_add_tail(&peer->channels, &channel->list); tal_add_destructor(channel, destroy_channel); diff --git a/lightningd/channel.h b/lightningd/channel.h index fb5b6176d..807083271 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -110,6 +110,9 @@ struct channel { /* Do we have an "impossible" future per_commitment_point from * peer via option_data_loss_protect? */ const struct pubkey *future_per_commitment_point; + + /* Feerate per channel */ + u32 feerate_base, feerate_ppm; }; struct channel *new_channel(struct peer *peer, u64 dbid, @@ -154,7 +157,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, bool connected, const struct basepoints *local_basepoints, const struct pubkey *local_funding_pubkey, - const struct pubkey *future_per_commitment_point); + const struct pubkey *future_per_commitment_point, + u32 feerate_base, + u32 feerate_ppm); void delete_channel(struct channel *channel); diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 98dcba45b..01e52a18b 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -352,8 +352,8 @@ void peer_start_channeld(struct channel *channel, &channel->channel_info.remote_per_commit, &channel->channel_info.old_remote_per_commit, channel->funder, - cfg->fee_base, - cfg->fee_per_satoshi, + channel->feerate_base, + channel->feerate_ppm, channel->our_msat, &channel->local_basepoints, &channel->local_funding_pubkey, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 9db54b122..976b57359 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -223,7 +223,9 @@ wallet_commit_channel(struct lightningd *ld, true, &uc->local_basepoints, &uc->local_funding_pubkey, - NULL); + NULL, + ld->config.fee_base, + ld->config.fee_per_satoshi); /* Now we finally put it in the database. */ wallet_channel_insert(ld->wallet, channel); diff --git a/wallet/db.c b/wallet/db.c index bf12a44d5..e3fc671f3 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -20,6 +20,8 @@ struct migration { void (*func)(struct lightningd *ld, struct db *db); }; +void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db); + /* Do not reorder or remove elements from this array, it is used to * migrate existing databases from a previous state, based on the * string indices */ @@ -367,6 +369,10 @@ static struct migration dbmigrations[] = { { "ALTER TABLE outputs ADD scriptpubkey BLOB;", NULL }, /* Keep bolt11 string for payments. */ { "ALTER TABLE payments ADD bolt11 TEXT;", NULL }, + /* PR #2342 feerate per channel */ + { "ALTER TABLE channels ADD feerate_base INTEGER;", NULL }, + { "ALTER TABLE channels ADD feerate_ppm INTEGER;", NULL }, + { NULL, migrate_pr2342_feerate_per_channel }, }; /* Leak tracking. */ @@ -972,3 +978,12 @@ void sqlite3_bind_amount_sat(sqlite3_stmt *stmt, int col, { sqlite3_bind_int64(stmt, col, sat.satoshis); /* Raw: low level function */ } + +/* Will apply the current config fee settings to all channels */ +void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db) +{ + db_exec(__func__, db, + "UPDATE channels SET feerate_base = %u, feerate_ppm = %u;", + ld->config.fee_base, + ld->config.fee_per_satoshi); +} diff --git a/wallet/wallet.c b/wallet/wallet.c index cee704dd5..eb0ab5357 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -740,7 +740,9 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s /* Not connected */ false, &local_basepoints, &local_funding_pubkey, - future_per_commitment_point); + future_per_commitment_point, + sqlite3_column_int(stmt, 42), + sqlite3_column_int(stmt, 43)); return chan; } @@ -765,7 +767,8 @@ static const char *channel_fields = /*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, future_per_commitment_point, " - /*41*/ "last_sent_commit"; + /*41*/ "last_sent_commit, " + /*42*/ "feerate_base, feerate_ppm"; bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w) { @@ -983,7 +986,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " min_possible_feerate=?," " max_possible_feerate=?," " msatoshi_to_us_min=?," - " msatoshi_to_us_max=?" + " msatoshi_to_us_max=?," + " feerate_base=?," + " feerate_ppm=?" " WHERE id=?"); sqlite3_bind_int64(stmt, 1, chan->their_shachain.id); if (chan->scid) @@ -1023,7 +1028,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) sqlite3_bind_int(stmt, 23, chan->max_possible_feerate); sqlite3_bind_amount_msat(stmt, 24, chan->msat_to_us_min); sqlite3_bind_amount_msat(stmt, 25, chan->msat_to_us_max); - sqlite3_bind_int64(stmt, 26, chan->dbid); + sqlite3_bind_int(stmt, 26, chan->feerate_base); + sqlite3_bind_int(stmt, 27, chan->feerate_ppm); + sqlite3_bind_int64(stmt, 28, chan->dbid); db_exec_prepared(w->db, stmt); wallet_channel_config_save(w, &chan->channel_info.their_config);