From 8999e2293a0cc977fa239648161affff8838c3d4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 21 Nov 2017 15:56:59 +1030 Subject: [PATCH] channeld: implement approx_max_feerate. Signed-off-by: Rusty Russell --- channeld/full_channel.c | 27 +++++++++++++++++++++++++++ channeld/full_channel.h | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/channeld/full_channel.c b/channeld/full_channel.c index a5223db4a..eeee198bf 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -678,6 +678,33 @@ static int change_htlcs(struct channel *channel, return cflags; } +/* FIXME: The sender's requirements are *implied* by this, not stated! */ +/* BOLT #2: + * + * A receiving node SHOULD fail the channel if the sender cannot + * afford the new fee rate on the receiving node's current commitment + * transaction + */ +u32 approx_max_feerate(const struct channel *channel) +{ + size_t num; + u64 weight; + const struct htlc **committed, **adding, **removing; + const tal_t *tmpctx = tal_tmpctx(channel); + + gather_htlcs(tmpctx, channel, !channel->funder, + &committed, &removing, &adding); + + /* Assume none are trimmed; this gives lower bound on feerate. */ + num = tal_count(committed) + tal_count(adding) - tal_count(removing); + tal_free(tmpctx); + + weight = 724 + 172 * num; + + return channel->view[!channel->funder].owed_msat[channel->funder] + / weight * 1000; +} + bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw) { u64 fee_msat, dust = dust_limit_satoshis(channel, !channel->funder); diff --git a/channeld/full_channel.h b/channeld/full_channel.h index f5ee6bec1..c992e22f7 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -170,7 +170,7 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel, const struct preimage *preimage); /** - * approx_max_feerate: what's the we (initiator) could raise fee rate to? + * approx_max_feerate: what's the max funder could raise fee rate to? * @channel: The channel state * * This is not exact! To check if their offer is valid, try