feerate: keep feerates separately for each side.

When we support changing them, they can be different during the transition.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-11-21 14:14:35 +10:30 committed by Christian Decker
parent 24b4326b30
commit b836b452dc
11 changed files with 50 additions and 32 deletions

View File

@ -2017,7 +2017,7 @@ static void init_channel(struct peer *peer)
bool reconnected; bool reconnected;
u8 *funding_signed; u8 *funding_signed;
u8 *msg; u8 *msg;
u32 feerate_per_kw; u32 feerate_per_kw[NUM_SIDES];
assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK)); assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK));
@ -2029,7 +2029,7 @@ static void init_channel(struct peer *peer)
&funding_txid, &funding_txout, &funding_txid, &funding_txout,
&funding_satoshi, &funding_satoshi,
&peer->conf[LOCAL], &peer->conf[REMOTE], &peer->conf[LOCAL], &peer->conf[REMOTE],
&feerate_per_kw, feerate_per_kw,
&peer->their_commit_sig, &peer->their_commit_sig,
&peer->pcs.cs, &peer->pcs.cs,
&funding_pubkey[REMOTE], &funding_pubkey[REMOTE],

View File

@ -12,7 +12,8 @@ channel_init,,funding_txout,u16
channel_init,,funding_satoshi,u64 channel_init,,funding_satoshi,u64
channel_init,,our_config,struct channel_config channel_init,,our_config,struct channel_config
channel_init,,their_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,,first_commit_sig,secp256k1_ecdsa_signature
channel_init,,crypto_state,struct crypto_state channel_init,,crypto_state,struct crypto_state
channel_init,,remote_fundingkey,struct pubkey channel_init,,remote_fundingkey,struct pubkey

1 # Received and sent funding_locked
12 channel_init,,their_config,struct channel_config
13 channel_init,,feerate_per_kw,u32 # FIXME: Fix generate-wire.py to allow NUM_SIDES*u32 here.
14 channel_init,,first_commit_sig,secp256k1_ecdsa_signature channel_init,,feerate_per_kw,2*u32
15 channel_init,,crypto_state,struct crypto_state channel_init,,first_commit_sig,secp256k1_ecdsa_signature
16 channel_init,,crypto_state,struct crypto_state
17 channel_init,,remote_fundingkey,struct pubkey
18 channel_init,,remote_revocation_basepoint,struct pubkey
19 channel_init,,remote_payment_basepoint,struct pubkey

View File

@ -24,7 +24,7 @@ struct channel *new_channel(const tal_t *ctx,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,
u32 feerate_per_kw, const u32 feerate_per_kw[NUM_SIDES],
const struct channel_config *local, const struct channel_config *local,
const struct channel_config *remote, const struct channel_config *remote,
const struct basepoints *local_basepoints, const struct basepoints *local_basepoints,
@ -37,13 +37,17 @@ struct channel *new_channel(const tal_t *ctx,
funding_txout, funding_txout,
funding_satoshis, funding_satoshis,
local_msatoshi, local_msatoshi,
feerate_per_kw, feerate_per_kw[LOCAL],
local, remote, local, remote,
local_basepoints, local_basepoints,
remote_basepoints, remote_basepoints,
local_funding_pubkey, local_funding_pubkey,
remote_funding_pubkey, remote_funding_pubkey,
funder); funder);
/* Feerates can be different. */
channel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE];
if (channel) { if (channel) {
channel->htlcs = tal(channel, struct htlc_map); channel->htlcs = tal(channel, struct htlc_map);
htlc_map_init(channel->htlcs); htlc_map_init(channel->htlcs);

View File

@ -14,7 +14,7 @@
* @funding_satoshis: The commitment transaction amount. * @funding_satoshis: The commitment transaction amount.
* @local_msatoshi: The amount for the local side (remainder goes to remote) * @local_msatoshi: The amount for the local side (remainder goes to remote)
* @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment * @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment
* transaction and HTLCS * transaction and HTLCS for each side.
* @local: local channel configuration * @local: local channel configuration
* @remote: remote channel configuration * @remote: remote channel configuration
* @local_basepoints: local basepoints. * @local_basepoints: local basepoints.
@ -30,7 +30,7 @@ struct channel *new_channel(const tal_t *ctx,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,
u32 feerate_per_kw, const u32 feerate_per_kw[NUM_SIDES],
const struct channel_config *local, const struct channel_config *local,
const struct channel_config *remote, const struct channel_config *remote,
const struct basepoints *local_basepoints, const struct basepoints *local_basepoints,

View File

@ -305,7 +305,7 @@ int main(void)
/* We test from both sides. */ /* We test from both sides. */
struct channel *lchannel, *rchannel; struct channel *lchannel, *rchannel;
u64 funding_amount_satoshi; u64 funding_amount_satoshi;
u32 feerate_per_kw; u32 *feerate_per_kw = tal_arr(tmpctx, u32, NUM_SIDES);
unsigned int funding_output_index; unsigned int funding_output_index;
struct keyset keyset; struct keyset keyset;
struct pubkey local_funding_pubkey, remote_funding_pubkey; struct pubkey local_funding_pubkey, remote_funding_pubkey;
@ -423,7 +423,7 @@ int main(void)
to_local_msat = 7000000000; to_local_msat = 7000000000;
to_remote_msat = 3000000000; 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, lchannel = new_channel(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi, to_local_msat, funding_amount_satoshi, to_local_msat,
feerate_per_kw, feerate_per_kw,
@ -465,7 +465,7 @@ int main(void)
funding_amount_satoshi, funding_amount_satoshi,
LOCAL, remote_config->to_self_delay, LOCAL, remote_config->to_self_delay,
&keyset, &keyset,
feerate_per_kw, feerate_per_kw[LOCAL],
local_config->dust_limit_satoshis, local_config->dust_limit_satoshis,
to_local_msat, to_local_msat,
to_remote_msat, to_remote_msat,
@ -492,7 +492,7 @@ int main(void)
*/ */
to_local_msat = 6988000000; to_local_msat = 6988000000;
to_remote_msat = 3000000000; 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 /* 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 * 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); txs_must_be_eq(txs, txs2);
/* FIXME: Adjust properly! */ /* FIXME: Adjust properly! */
lchannel->view[LOCAL].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; rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE];
htlcs = include_htlcs(lchannel, LOCAL); htlcs = include_htlcs(lchannel, LOCAL);
include_htlcs(rchannel, REMOTE); include_htlcs(rchannel, REMOTE);
@ -574,16 +574,16 @@ int main(void)
/* FIXME: Compare HTLCs for these too! */ /* FIXME: Compare HTLCs for these too! */
for (i = 0; i < ARRAY_SIZE(feerates); i++) { 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; lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw[LOCAL];
rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw; rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE];
raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index,
funding_amount_satoshi, funding_amount_satoshi,
LOCAL, remote_config->to_self_delay, LOCAL, remote_config->to_self_delay,
&keyset, &keyset,
feerate_per_kw, feerate_per_kw[LOCAL],
local_config->dust_limit_satoshis, local_config->dust_limit_satoshis,
to_local_msat, to_local_msat,
to_remote_msat, to_remote_msat,

View File

@ -133,7 +133,7 @@ static inline u16 to_self_delay(const struct channel *channel, enum side side)
* @funding_satoshis: The commitment transaction amount. * @funding_satoshis: The commitment transaction amount.
* @local_msatoshi: The amount for the local side (remainder goes to remote) * @local_msatoshi: The amount for the local side (remainder goes to remote)
* @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment * @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 * @local: local channel configuration
* @remote: remote channel configuration * @remote: remote channel configuration
* @local_basepoints: local basepoints. * @local_basepoints: local basepoints.

View File

@ -1893,7 +1893,8 @@ static void peer_start_closingd(struct peer *peer,
* calculated in [BOLT * calculated in [BOLT
* #3](03-transactions.md#fee-calculation). * #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, maxfee = commit_tx_base_fee(get_feerate(peer->ld->topology,
FEERATE_IMMEDIATE), 0); FEERATE_IMMEDIATE), 0);
@ -2193,11 +2194,14 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
&fc->peer->minimum_depth, &fc->peer->minimum_depth,
&channel_info->remote_fundingkey, &channel_info->remote_fundingkey,
&funding_txid, &funding_txid,
&channel_info->feerate_per_kw)) { &channel_info->feerate_per_kw[REMOTE])) {
peer_internal_error(fc->peer, "bad funder_reply: %s", peer_internal_error(fc->peer, "bad funder_reply: %s",
tal_hex(resp, resp)); tal_hex(resp, resp));
return; 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. */ /* old_remote_per_commit not valid yet, copy valid one. */
channel_info->old_remote_per_commit = channel_info->remote_per_commit; 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->funding_satoshi,
&peer->push_msat, &peer->push_msat,
&peer->channel_flags, &peer->channel_flags,
&channel_info->feerate_per_kw, &channel_info->feerate_per_kw[REMOTE],
&funding_signed)) { &funding_signed)) {
peer_internal_error(peer, "bad OPENING_FUNDEE_REPLY %s", peer_internal_error(peer, "bad OPENING_FUNDEE_REPLY %s",
tal_hex(reply, reply)); tal_hex(reply, reply));
return; 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. */ /* old_remote_per_commit not valid yet, copy valid one. */
channel_info->old_remote_per_commit = channel_info->remote_per_commit; channel_info->old_remote_per_commit = channel_info->remote_per_commit;

View File

@ -14,7 +14,8 @@ struct channel_info {
/* The old_remote_per_commit is for the locked-in remote commit_tx, /* 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. */ * and the remote_per_commit is for the commit_tx we're modifying now. */
struct pubkey remote_per_commit, old_remote_per_commit; 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. */ /* Get all HTLCs for a peer, to send in init message. */

View File

@ -62,7 +62,8 @@ char *dbmigrations[] = {
" delayed_payment_basepoint_remote BLOB," " delayed_payment_basepoint_remote BLOB,"
" per_commit_remote BLOB," " per_commit_remote BLOB,"
" old_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 */ /* END channel_info */
" shachain_remote_id INTEGER," " shachain_remote_id INTEGER,"
" shutdown_scriptpubkey_remote BLOB," " shutdown_scriptpubkey_remote BLOB,"

View File

@ -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->theirbase.delayed_payment);
ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->remote_per_commit); ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->remote_per_commit);
ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->old_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); wallet_channel_config_load(w, remote_config_id, &chan->peer->channel_info->their_config);
} else { } else {
/* No channel_info, skip positions in the result */ /* No channel_info, skip positions in the result */
col += 8; col += 9;
} }
/* Load shachain */ /* Load shachain */
@ -483,7 +484,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt,
col += 2; col += 2;
} }
assert(col == 33); assert(col == 34);
chan->peer->channel = chan; chan->peer->channel = chan;
@ -502,7 +503,7 @@ const char *channel_fields =
"fundingkey_remote, revocation_basepoint_remote, " "fundingkey_remote, revocation_basepoint_remote, "
"payment_basepoint_remote, htlc_basepoint_remote, " "payment_basepoint_remote, htlc_basepoint_remote, "
"delayed_payment_basepoint_remote, per_commit_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, " "shutdown_scriptpubkey_remote, shutdown_keyidx_local, "
"last_sent_commit_state, last_sent_commit_id, " "last_sent_commit_state, last_sent_commit_id, "
"last_tx, last_sig"; "last_tx, last_sig";
@ -701,7 +702,8 @@ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan){
" delayed_payment_basepoint_remote=?," " delayed_payment_basepoint_remote=?,"
" per_commit_remote=?," " per_commit_remote=?,"
" old_per_commit_remote=?," " old_per_commit_remote=?,"
" feerate_per_kw=?," " local_feerate_per_kw=?,"
" remote_feerate_per_kw=?,"
" channel_config_remote=?" " channel_config_remote=?"
" WHERE id=?"); " WHERE id=?");
sqlite3_bind_pubkey(stmt, 1, &p->channel_info->remote_fundingkey); 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, 5, &p->channel_info->theirbase.delayed_payment);
sqlite3_bind_pubkey(stmt, 6, &p->channel_info->remote_per_commit); 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_pubkey(stmt, 7, &p->channel_info->old_remote_per_commit);
sqlite3_bind_int(stmt, 8, p->channel_info->feerate_per_kw); sqlite3_bind_int(stmt, 8, p->channel_info->feerate_per_kw[LOCAL]);
sqlite3_bind_int64(stmt, 9, p->channel_info->their_config.id); sqlite3_bind_int(stmt, 9, p->channel_info->feerate_per_kw[REMOTE]);
sqlite3_bind_int64(stmt, 10, chan->id); sqlite3_bind_int64(stmt, 10, p->channel_info->their_config.id);
sqlite3_bind_int64(stmt, 11, chan->id);
db_exec_prepared(w->db, stmt); db_exec_prepared(w->db, stmt);
} }

View File

@ -259,7 +259,7 @@ static bool test_channel_crud(const tal_t *ctx)
mempat(sig, sizeof(*sig)); mempat(sig, sizeof(*sig));
mempat(&last_commit, sizeof(last_commit)); mempat(&last_commit, sizeof(last_commit));
pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk); 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)); mempat(&p.id, sizeof(p.id));
c1.peer = &p; c1.peer = &p;
p.id = pk; p.id = pk;