Skip to content

Commit

Permalink
feerate: keep feerates separately for each side.
Browse files Browse the repository at this point in the history
When we support changing them, they can be different during the transition.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Nov 23, 2017
1 parent 24b4326 commit b836b45
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 32 deletions.
4 changes: 2 additions & 2 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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],
Expand Down
3 changes: 2 additions & 1 deletion channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion common/initial_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion lightningd/peer_htlcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
3 changes: 2 additions & 1 deletion wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"
Expand Down
19 changes: 11 additions & 8 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;

Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b836b45

Please sign in to comment.