From b836b452dc7a0b0b70d8173321e33474ee710a91 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 21 Nov 2017 14:14:35 +1030 Subject: [PATCH] feerate: keep feerates separately for each side. When we support changing them, they can be different during the transition. Signed-off-by: Rusty Russell --- channeld/channel.c | 4 ++-- channeld/channel_wire.csv | 3 ++- channeld/full_channel.c | 8 ++++++-- channeld/full_channel.h | 4 ++-- channeld/test/run-full_channel.c | 20 ++++++++++---------- common/initial_channel.h | 2 +- lightningd/peer_control.c | 14 +++++++++++--- lightningd/peer_htlcs.h | 3 ++- wallet/db.c | 3 ++- wallet/wallet.c | 19 +++++++++++-------- wallet/wallet_tests.c | 2 +- 11 files changed, 50 insertions(+), 32 deletions(-) diff --git a/channeld/channel.c b/channeld/channel.c index 4f95b8906..63a05cff1 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -2017,7 +2017,7 @@ static void init_channel(struct peer *peer) bool reconnected; u8 *funding_signed; u8 *msg; - u32 feerate_per_kw; + u32 feerate_per_kw[NUM_SIDES]; assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK)); @@ -2029,7 +2029,7 @@ static void init_channel(struct peer *peer) &funding_txid, &funding_txout, &funding_satoshi, &peer->conf[LOCAL], &peer->conf[REMOTE], - &feerate_per_kw, + feerate_per_kw, &peer->their_commit_sig, &peer->pcs.cs, &funding_pubkey[REMOTE], diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index 4efa901bb..68f994d79 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -12,7 +12,8 @@ channel_init,,funding_txout,u16 channel_init,,funding_satoshi,u64 channel_init,,our_config,struct channel_config channel_init,,their_config,struct channel_config -channel_init,,feerate_per_kw,u32 +# FIXME: Fix generate-wire.py to allow NUM_SIDES*u32 here. +channel_init,,feerate_per_kw,2*u32 channel_init,,first_commit_sig,secp256k1_ecdsa_signature channel_init,,crypto_state,struct crypto_state channel_init,,remote_fundingkey,struct pubkey diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 7ba502175..cf92ded49 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -24,7 +24,7 @@ struct channel *new_channel(const tal_t *ctx, unsigned int funding_txout, u64 funding_satoshis, u64 local_msatoshi, - u32 feerate_per_kw, + const u32 feerate_per_kw[NUM_SIDES], const struct channel_config *local, const struct channel_config *remote, const struct basepoints *local_basepoints, @@ -37,13 +37,17 @@ struct channel *new_channel(const tal_t *ctx, funding_txout, funding_satoshis, local_msatoshi, - feerate_per_kw, + feerate_per_kw[LOCAL], local, remote, local_basepoints, remote_basepoints, local_funding_pubkey, remote_funding_pubkey, funder); + + /* Feerates can be different. */ + channel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE]; + if (channel) { channel->htlcs = tal(channel, struct htlc_map); htlc_map_init(channel->htlcs); diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 9317fa28a..0f5b8b4f4 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -14,7 +14,7 @@ * @funding_satoshis: The commitment transaction amount. * @local_msatoshi: The amount for the local side (remainder goes to remote) * @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment - * transaction and HTLCS + * transaction and HTLCS for each side. * @local: local channel configuration * @remote: remote channel configuration * @local_basepoints: local basepoints. @@ -30,7 +30,7 @@ struct channel *new_channel(const tal_t *ctx, unsigned int funding_txout, u64 funding_satoshis, u64 local_msatoshi, - u32 feerate_per_kw, + const u32 feerate_per_kw[NUM_SIDES], const struct channel_config *local, const struct channel_config *remote, const struct basepoints *local_basepoints, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 47f661a3d..c767be737 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -305,7 +305,7 @@ int main(void) /* We test from both sides. */ struct channel *lchannel, *rchannel; u64 funding_amount_satoshi; - u32 feerate_per_kw; + u32 *feerate_per_kw = tal_arr(tmpctx, u32, NUM_SIDES); unsigned int funding_output_index; struct keyset keyset; struct pubkey local_funding_pubkey, remote_funding_pubkey; @@ -423,7 +423,7 @@ int main(void) to_local_msat = 7000000000; to_remote_msat = 3000000000; - feerate_per_kw = 15000; + feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000; lchannel = new_channel(tmpctx, &funding_txid, funding_output_index, funding_amount_satoshi, to_local_msat, feerate_per_kw, @@ -465,7 +465,7 @@ int main(void) funding_amount_satoshi, LOCAL, remote_config->to_self_delay, &keyset, - feerate_per_kw, + feerate_per_kw[LOCAL], local_config->dust_limit_satoshis, to_local_msat, to_remote_msat, @@ -492,7 +492,7 @@ int main(void) */ to_local_msat = 6988000000; to_remote_msat = 3000000000; - feerate_per_kw = 0; + feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 0; /* Now, BOLT doesn't adjust owed amounts the same way we do * here: it's as if local side paid for all the HTLCs. We can @@ -514,8 +514,8 @@ int main(void) txs_must_be_eq(txs, txs2); /* FIXME: Adjust properly! */ - lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw; - rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw; + lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw[LOCAL]; + rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE]; htlcs = include_htlcs(lchannel, LOCAL); include_htlcs(rchannel, REMOTE); @@ -574,16 +574,16 @@ int main(void) /* FIXME: Compare HTLCs for these too! */ for (i = 0; i < ARRAY_SIZE(feerates); i++) { - feerate_per_kw = feerates[i]; + feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = feerates[i]; - lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw; - rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw; + lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw[LOCAL]; + rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE]; raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, funding_amount_satoshi, LOCAL, remote_config->to_self_delay, &keyset, - feerate_per_kw, + feerate_per_kw[LOCAL], local_config->dust_limit_satoshis, to_local_msat, to_remote_msat, diff --git a/common/initial_channel.h b/common/initial_channel.h index 3e0adff0f..84f20260f 100644 --- a/common/initial_channel.h +++ b/common/initial_channel.h @@ -133,7 +133,7 @@ static inline u16 to_self_delay(const struct channel *channel, enum side side) * @funding_satoshis: The commitment transaction amount. * @local_msatoshi: The amount for the local side (remainder goes to remote) * @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment - * transaction and HTLCS + * transaction and HTLCS (at this stage, same for both sides) * @local: local channel configuration * @remote: remote channel configuration * @local_basepoints: local basepoints. diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 4c962a30f..3bb475d1c 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1893,7 +1893,8 @@ static void peer_start_closingd(struct peer *peer, * calculated in [BOLT * #3](03-transactions.md#fee-calculation). */ - feelimit = commit_tx_base_fee(peer->channel_info->feerate_per_kw, 0); + feelimit = commit_tx_base_fee(peer->channel_info->feerate_per_kw[LOCAL], + 0); maxfee = commit_tx_base_fee(get_feerate(peer->ld->topology, FEERATE_IMMEDIATE), 0); @@ -2193,11 +2194,14 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, &fc->peer->minimum_depth, &channel_info->remote_fundingkey, &funding_txid, - &channel_info->feerate_per_kw)) { + &channel_info->feerate_per_kw[REMOTE])) { peer_internal_error(fc->peer, "bad funder_reply: %s", tal_hex(resp, resp)); return; } + /* Feerates begin identical. */ + channel_info->feerate_per_kw[LOCAL] + = channel_info->feerate_per_kw[REMOTE]; /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; @@ -2312,13 +2316,17 @@ static void opening_fundee_finished(struct subd *opening, &peer->funding_satoshi, &peer->push_msat, &peer->channel_flags, - &channel_info->feerate_per_kw, + &channel_info->feerate_per_kw[REMOTE], &funding_signed)) { peer_internal_error(peer, "bad OPENING_FUNDEE_REPLY %s", tal_hex(reply, reply)); return; } + /* Feerates begin identical. */ + channel_info->feerate_per_kw[LOCAL] + = channel_info->feerate_per_kw[REMOTE]; + /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index 9d3e6f0ab..c5ea41a44 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -14,7 +14,8 @@ struct channel_info { /* The old_remote_per_commit is for the locked-in remote commit_tx, * and the remote_per_commit is for the commit_tx we're modifying now. */ struct pubkey remote_per_commit, old_remote_per_commit; - u32 feerate_per_kw; + /* In transition, these can be different! */ + u32 feerate_per_kw[NUM_SIDES]; }; /* Get all HTLCs for a peer, to send in init message. */ diff --git a/wallet/db.c b/wallet/db.c index 2904c779c..15d473d72 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -62,7 +62,8 @@ char *dbmigrations[] = { " delayed_payment_basepoint_remote BLOB," " per_commit_remote BLOB," " old_per_commit_remote BLOB," - " feerate_per_kw INTEGER," + " local_feerate_per_kw INTEGER," + " remote_feerate_per_kw INTEGER," /* END channel_info */ " shachain_remote_id INTEGER," " shutdown_scriptpubkey_remote BLOB," diff --git a/wallet/wallet.c b/wallet/wallet.c index 4d38d61af..c1ac442b4 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -438,11 +438,12 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->theirbase.delayed_payment); ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->remote_per_commit); ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->old_remote_per_commit); - channel_info->feerate_per_kw = sqlite3_column_int(stmt, col++); + channel_info->feerate_per_kw[LOCAL] = sqlite3_column_int(stmt, col++); + channel_info->feerate_per_kw[REMOTE] = sqlite3_column_int(stmt, col++); wallet_channel_config_load(w, remote_config_id, &chan->peer->channel_info->their_config); } else { /* No channel_info, skip positions in the result */ - col += 8; + col += 9; } /* Load shachain */ @@ -483,7 +484,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, col += 2; } - assert(col == 33); + assert(col == 34); chan->peer->channel = chan; @@ -502,7 +503,7 @@ const char *channel_fields = "fundingkey_remote, revocation_basepoint_remote, " "payment_basepoint_remote, htlc_basepoint_remote, " "delayed_payment_basepoint_remote, per_commit_remote, " - "old_per_commit_remote, feerate_per_kw, shachain_remote_id, " + "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"; @@ -701,7 +702,8 @@ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ " delayed_payment_basepoint_remote=?," " per_commit_remote=?," " old_per_commit_remote=?," - " feerate_per_kw=?," + " local_feerate_per_kw=?," + " remote_feerate_per_kw=?," " channel_config_remote=?" " WHERE id=?"); sqlite3_bind_pubkey(stmt, 1, &p->channel_info->remote_fundingkey); @@ -711,9 +713,10 @@ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ sqlite3_bind_pubkey(stmt, 5, &p->channel_info->theirbase.delayed_payment); sqlite3_bind_pubkey(stmt, 6, &p->channel_info->remote_per_commit); sqlite3_bind_pubkey(stmt, 7, &p->channel_info->old_remote_per_commit); - sqlite3_bind_int(stmt, 8, p->channel_info->feerate_per_kw); - sqlite3_bind_int64(stmt, 9, p->channel_info->their_config.id); - sqlite3_bind_int64(stmt, 10, chan->id); + sqlite3_bind_int(stmt, 8, p->channel_info->feerate_per_kw[LOCAL]); + sqlite3_bind_int(stmt, 9, p->channel_info->feerate_per_kw[REMOTE]); + sqlite3_bind_int64(stmt, 10, p->channel_info->their_config.id); + sqlite3_bind_int64(stmt, 11, chan->id); db_exec_prepared(w->db, stmt); } diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index 43a693b08..d7fa45b8e 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -259,7 +259,7 @@ static bool test_channel_crud(const tal_t *ctx) mempat(sig, sizeof(*sig)); mempat(&last_commit, sizeof(last_commit)); pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk); - ci.feerate_per_kw = 31337; + ci.feerate_per_kw[LOCAL] = ci.feerate_per_kw[REMOTE] = 31337; mempat(&p.id, sizeof(p.id)); c1.peer = &p; p.id = pk;