From f0afd060dbdf32e38ccab0fcb3f0422881bd6839 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 14 Aug 2020 03:19:02 +0930 Subject: [PATCH] initial_commit_tx.h: extract base-weight calculation. This avoids reproducing it inside full_channel.c. Not sure it does elements correctly though. Signed-off-by: Rusty Russell --- channeld/full_channel.c | 23 +---------------------- common/htlc_tx.h | 2 ++ common/initial_commit_tx.h | 27 ++++++++++++++++----------- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index c85fe7503..f6fc1828d 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -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]); diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 511b7b6fc..963e167f0 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -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) { diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index 3a741805b..46da1a334 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -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: * @@ -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)); } /**