From 8fe0ac8d37b09f0a6bc72a7943aefe788f49f29b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 22 Sep 2021 22:56:14 +0930 Subject: [PATCH] lightningd: refactor forward feecheck. Make it do the feerate calc internally. Signed-off-by: Rusty Russell --- lightningd/peer_htlcs.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 607e99f35..ad827a495 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -334,10 +334,19 @@ static void fail_out_htlc(struct htlc_out *hout, static bool check_fwd_amount(struct htlc_in *hin, struct amount_msat amt_to_forward, struct amount_msat amt_in_htlc, - struct amount_msat fee) + u32 feerate_base, u32 feerate_ppm) { + struct amount_msat fee; struct amount_msat fwd; + if (!amount_msat_fee(&fee, amt_to_forward, + feerate_base, feerate_ppm)) { + log_broken(hin->key.channel->log, "Fee overflow forwarding %s!", + type_to_string(tmpctx, struct amount_msat, + &amt_to_forward)); + return false; + } + if (amount_msat_sub(&fwd, amt_in_htlc, fee) && amount_msat_greater_eq(fwd, amt_to_forward)) return true; @@ -673,7 +682,6 @@ static void forward_htlc(struct htlc_in *hin, const struct pubkey *next_blinding) { const u8 *failmsg; - struct amount_msat fee; struct lightningd *ld = hin->key.channel->peer->ld; struct channel *next = active_channel_by_scid(ld, scid); struct htlc_out *hout = NULL; @@ -695,17 +703,9 @@ static void forward_htlc(struct htlc_in *hin, * - SHOULD accept HTLCs that pay a fee equal to or greater than: * - fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 ) */ - if (!amount_msat_fee(&fee, amt_to_forward, - next->feerate_base, - next->feerate_ppm)) { - log_broken(ld->log, "Fee overflow forwarding %s!", - type_to_string(tmpctx, struct amount_msat, - &amt_to_forward)); - needs_update_appended = true; - failmsg = towire_fee_insufficient(tmpctx, hin->msat, NULL); - goto fail; - } - if (!check_fwd_amount(hin, amt_to_forward, hin->msat, fee)) { + if (!check_fwd_amount(hin, amt_to_forward, hin->msat, + next->feerate_base, + next->feerate_ppm)) { needs_update_appended = true; failmsg = towire_fee_insufficient(tmpctx, hin->msat, NULL); goto fail;