Skip to content

Commit

Permalink
commit_tx: make fee msat vs sat explicit.
Browse files Browse the repository at this point in the history
Suggested-by: @niftynei
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Dec 15, 2018
1 parent a8e0e17 commit 72b6884
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* 2. Calculate the base [commitment transaction
* fee](#fee-calculation).
*/
base_fee_msat = commit_tx_base_fee(feerate_per_kw, untrimmed) * 1000;
base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed);

SUPERVERBOSE("# base commitment transaction fee = %"PRIu64"\n",
base_fee_msat / 1000);
Expand Down
4 changes: 2 additions & 2 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
- commit_tx_num_untrimmed(removing, feerate, dust,
recipient);

fee_msat = commit_tx_base_fee(feerate, untrimmed) * 1000;
fee_msat = commit_tx_base_fee_msat(feerate, untrimmed);
} else
fee_msat = 0;

Expand Down Expand Up @@ -718,7 +718,7 @@ bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust,
!channel->funder);

fee_msat = commit_tx_base_fee(feerate_per_kw, untrimmed) * 1000;
fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed);

/* BOLT #2:
*
Expand Down
3 changes: 1 addition & 2 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,7 @@ int main(void)
/* Now make sure we cover case where funder can't afford the fee;
* its output cannot go negative! */
for (;;) {
u64 base_fee_msat = commit_tx_base_fee(feerate_per_kw, 0)
* 1000;
u64 base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, 0);

if (base_fee_msat <= to_local_msat) {
feerate_per_kw++;
Expand Down
2 changes: 1 addition & 1 deletion common/initial_commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
* 2. Calculate the base [commitment transaction
* fee](#fee-calculation).
*/
base_fee_msat = commit_tx_base_fee(feerate_per_kw, untrimmed) * 1000;
base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed);

/* BOLT #3:
*
Expand Down
13 changes: 10 additions & 3 deletions common/initial_commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
const struct pubkey *accepter_payment_basepoint);

/* Helper to calculate the base fee if we have this many htlc outputs */
static inline u64 commit_tx_base_fee(u32 feerate_per_kw,
size_t num_untrimmed_htlcs)
static inline u64 commit_tx_base_fee_sat(u32 feerate_per_kw,
size_t num_untrimmed_htlcs)
{
u64 weight;

Expand All @@ -44,7 +44,14 @@ static inline u64 commit_tx_base_fee(u32 feerate_per_kw,
* 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 (rounding
* down).
*/
return feerate_per_kw * weight / 1000;
return (feerate_per_kw * weight / 1000);
}

static inline u64 commit_tx_base_fee_msat(u32 feerate_per_kw,
size_t num_untrimmed_htlcs)
{
return commit_tx_base_fee_sat(feerate_per_kw, num_untrimmed_htlcs)
* 1000;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ void peer_start_closingd(struct channel *channel,
* fee of the final commitment transaction, as calculated in
* [BOLT #3](03-transactions.md#fee-calculation).
*/
feelimit = commit_tx_base_fee(channel->channel_info.feerate_per_kw[LOCAL],
0);
feelimit = commit_tx_base_fee_sat(channel->channel_info.feerate_per_kw[LOCAL],
0);

/* Pick some value above slow feerate (or min possible if unknown) */
minfee = commit_tx_base_fee(feerate_min(ld, NULL), 0);
minfee = commit_tx_base_fee_sat(feerate_min(ld, NULL), 0);

/* If we can't determine feerate, start at half unilateral feerate. */
feerate = mutual_close_feerate(ld->topology);
Expand All @@ -197,7 +197,7 @@ void peer_start_closingd(struct channel *channel,
if (feerate < feerate_floor())
feerate = feerate_floor();
}
startfee = commit_tx_base_fee(feerate, 0);
startfee = commit_tx_base_fee_sat(feerate, 0);

if (startfee > feelimit)
startfee = feelimit;
Expand Down

0 comments on commit 72b6884

Please sign in to comment.