From 24298a4278ee5b944065aefe7e6e1563f42177e8 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 23 Aug 2016 10:12:00 +0930 Subject: [PATCH] channel: approx_max_feerate and can_afford_feerate Routines for getting maximum feerate we should offer, and checking if their offer is valid. Signed-off-by: Rusty Russell --- daemon/channel.c | 46 +++++++++++++++++++++++++++++++++------------- daemon/channel.h | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/daemon/channel.c b/daemon/channel.c index 9d75e004c..c56adaf34 100644 --- a/daemon/channel.c +++ b/daemon/channel.c @@ -18,23 +18,23 @@ uint64_t fee_by_feerate(size_t txsize, uint64_t fee_rate) return txsize * fee_rate / 2000 * 2; } +/* BOLT #2: + * + * A node MUST use the formula 338 + 32 bytes for every non-dust HTLC + * as the bytecount for calculating commitment transaction fees. Note + * that the fee requirement is unchanged, even if the elimination of + * dust HTLC outputs has caused a non-zero fee already. + */ +static size_t tx_bytes(size_t num_nondust_htlcs) +{ + return 338 + 32 * num_nondust_htlcs; +} + static uint64_t calculate_fee_msat(size_t num_nondust_htlcs, uint64_t fee_rate) { - uint64_t bytes; - - /* BOLT #2: - * - * A node MUST use the formula 338 + 32 bytes for every - * non-dust HTLC as the bytecount for calculating commitment - * transaction fees. Note that the fee requirement is - * unchanged, even if the elimination of dust HTLC outputs has - * caused a non-zero fee already. - */ - bytes = 338 + 32 * num_nondust_htlcs; - /* milli-satoshis */ - return fee_by_feerate(bytes, fee_rate) * 1000; + return fee_by_feerate(tx_bytes(num_nondust_htlcs), fee_rate) * 1000; } /* Pay this much fee, if possible. Return amount unpaid. */ @@ -155,6 +155,26 @@ struct channel_state *initial_cstate(const tal_t *ctx, return cstate; } +/* FIXME: Write exact variant! */ +uint64_t approx_max_feerate(const struct channel_state *cstate, + enum channel_side side) +{ + uint64_t max_funds; + + max_funds = cstate->side[side].pay_msat + cstate->side[side].fee_msat; + + return max_funds * 1000 / tx_bytes(cstate->num_nondust); +} + +bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate, + enum channel_side side) +{ + u64 fee_msat = calculate_fee_msat(cstate->num_nondust, fee_rate); + + return cstate->side[side].pay_msat + cstate->side[side].fee_msat + >= fee_msat; +} + void adjust_fee(struct channel_state *cstate, uint64_t fee_rate) { uint64_t fee_msat; diff --git a/daemon/channel.h b/daemon/channel.h index 4c45d4598..14c1794f4 100644 --- a/daemon/channel.h +++ b/daemon/channel.h @@ -84,6 +84,25 @@ void cstate_fail_htlc(struct channel_state *cstate, const struct htlc *htlc); */ void cstate_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc); +/** + * approx_max_feerate: what's the most side could raise fee rate to? + * @cstate: The channel state + * @side: OURS or THEIRS + * + * This is not exact! To check if their offer is valid, use can_afford_feerate. + */ +uint64_t approx_max_feerate(const struct channel_state *cstate, + enum channel_side side); + +/** + * can_afford_feerate: could this side pay for the fee if changed to fee_rate? + * @cstate: The channel state + * @fee_rate: the new fee rate proposed + * @side: OURS or THEIRS + */ +bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate, + enum channel_side side); + /** * adjust_fee: Change fee rate. * @cstate: The channel state