Skip to content

Commit 8fe0ac8

Browse files
rustyrussellcdecker
authored andcommitted
lightningd: refactor forward feecheck.
Make it do the feerate calc internally. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 09e0794 commit 8fe0ac8

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lightningd/peer_htlcs.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,19 @@ static void fail_out_htlc(struct htlc_out *hout,
334334
static bool check_fwd_amount(struct htlc_in *hin,
335335
struct amount_msat amt_to_forward,
336336
struct amount_msat amt_in_htlc,
337-
struct amount_msat fee)
337+
u32 feerate_base, u32 feerate_ppm)
338338
{
339+
struct amount_msat fee;
339340
struct amount_msat fwd;
340341

342+
if (!amount_msat_fee(&fee, amt_to_forward,
343+
feerate_base, feerate_ppm)) {
344+
log_broken(hin->key.channel->log, "Fee overflow forwarding %s!",
345+
type_to_string(tmpctx, struct amount_msat,
346+
&amt_to_forward));
347+
return false;
348+
}
349+
341350
if (amount_msat_sub(&fwd, amt_in_htlc, fee)
342351
&& amount_msat_greater_eq(fwd, amt_to_forward))
343352
return true;
@@ -673,7 +682,6 @@ static void forward_htlc(struct htlc_in *hin,
673682
const struct pubkey *next_blinding)
674683
{
675684
const u8 *failmsg;
676-
struct amount_msat fee;
677685
struct lightningd *ld = hin->key.channel->peer->ld;
678686
struct channel *next = active_channel_by_scid(ld, scid);
679687
struct htlc_out *hout = NULL;
@@ -695,17 +703,9 @@ static void forward_htlc(struct htlc_in *hin,
695703
* - SHOULD accept HTLCs that pay a fee equal to or greater than:
696704
* - fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
697705
*/
698-
if (!amount_msat_fee(&fee, amt_to_forward,
699-
next->feerate_base,
700-
next->feerate_ppm)) {
701-
log_broken(ld->log, "Fee overflow forwarding %s!",
702-
type_to_string(tmpctx, struct amount_msat,
703-
&amt_to_forward));
704-
needs_update_appended = true;
705-
failmsg = towire_fee_insufficient(tmpctx, hin->msat, NULL);
706-
goto fail;
707-
}
708-
if (!check_fwd_amount(hin, amt_to_forward, hin->msat, fee)) {
706+
if (!check_fwd_amount(hin, amt_to_forward, hin->msat,
707+
next->feerate_base,
708+
next->feerate_ppm)) {
709709
needs_update_appended = true;
710710
failmsg = towire_fee_insufficient(tmpctx, hin->msat, NULL);
711711
goto fail;

0 commit comments

Comments
 (0)