Skip to content

Commit

Permalink
initial_commit_tx.h: extract base-weight calculation.
Browse files Browse the repository at this point in the history
This avoids reproducing it inside full_channel.c.

Not sure it does elements correctly though.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 14, 2020
1 parent 3ff8311 commit f0afd06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
23 changes: 1 addition & 22 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,28 +1015,7 @@ u32 approx_max_feerate(const struct channel *channel)
/* Assume none are trimmed; this gives lower bound on feerate. */
num = tal_count(committed) + tal_count(adding) - tal_count(removing);

/* BOLT #3:
*
* commitment_transaction: 125 + 43 * num-htlc-outputs bytes
* - version: 4 bytes
* - witness_header <---- part of the witness data
* - count_tx_in: 1 byte
* - tx_in: 41 bytes
* funding_input
* - count_tx_out: 1 byte
* - tx_out: 74 + 43 * num-htlc-outputs bytes
* output_paying_to_remote,
* output_paying_to_local,
* ....htlc_output's...
* - lock_time: 4 bytes
*/
/* Those 74 bytes static output are effectively 2 outputs, one
* `output_paying_to_local` and one `output_paying_to_remote`. So when
* adding the elements overhead we need to add 2 + num htlcs
* outputs. */

weight = 724 + 172 * num;
weight = elements_add_overhead(weight, 1, num + 2);
weight = commit_tx_base_weight(num, false /* FIXME-anchor */);

/* Available is their view */
avail = amount_msat_to_sat_round_down(channel->view[!channel->opener].owed[channel->opener]);
Expand Down
2 changes: 2 additions & 0 deletions common/htlc_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct ripemd160;
* proofs per input and 35 bytes per output. In addition the explicit fee
* output will add 9 bytes and the per output overhead as well.
*/
/* FIXME: This seems to be a generalization of the logic in initial_commit_tx.h
* and should be in bitcoin/tx.h */
static inline size_t elements_add_overhead(size_t weight, size_t incount,
size_t outcount)
{
Expand Down
27 changes: 16 additions & 11 deletions common/initial_commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ struct keyset;
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 struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
size_t num_untrimmed_htlcs,
bool option_anchor_outputs)

/* The base weight of a commitment tx */
static inline size_t commit_tx_base_weight(size_t num_untrimmed_htlcs,
bool option_anchor_outputs)
{
u64 weight;
size_t weight;

/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
Expand Down Expand Up @@ -68,12 +68,17 @@ static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
weight += (32 + 1 + 1 + 1) * 4; /* Elements added fields */
}

/* BOLT #3:
*
* 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 (rounding
* down).
*/
return amount_tx_fee(feerate_per_kw, weight);
return weight;
}

/* Helper to calculate the base fee if we have this many htlc outputs */
static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
size_t num_untrimmed_htlcs,
bool option_anchor_outputs)
{
return amount_tx_fee(feerate_per_kw,
commit_tx_base_weight(num_untrimmed_htlcs,
option_anchor_outputs));
}

/**
Expand Down

0 comments on commit f0afd06

Please sign in to comment.