2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-05-31 09:30:32 +02:00
|
|
|
#include <common/htlc_trim.h>
|
|
|
|
#include <common/htlc_tx.h>
|
|
|
|
|
|
|
|
/* If this htlc too small to create an output on @side's commitment tx? */
|
|
|
|
bool htlc_is_trimmed(enum side htlc_owner,
|
|
|
|
struct amount_msat htlc_amount,
|
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
2020-08-13 19:44:02 +02:00
|
|
|
enum side side,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx)
|
2019-05-31 09:30:32 +02:00
|
|
|
{
|
|
|
|
struct amount_sat htlc_fee, htlc_min;
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* - for every offered HTLC:
|
|
|
|
* - if the HTLC amount minus the HTLC-timeout fee would be less than
|
|
|
|
* `dust_limit_satoshis` set by the transaction owner:
|
|
|
|
* - MUST NOT contain that output.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST be generated as specified in
|
|
|
|
* [Offered HTLC Outputs](#offered-htlc-outputs).
|
|
|
|
*/
|
|
|
|
if (htlc_owner == side)
|
2020-08-13 19:44:02 +02:00
|
|
|
htlc_fee = htlc_timeout_fee(feerate_per_kw,
|
2023-06-26 01:18:21 +02:00
|
|
|
option_anchor_outputs,
|
|
|
|
option_anchors_zero_fee_htlc_tx);
|
2019-05-31 09:30:32 +02:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* - for every received HTLC:
|
|
|
|
* - if the HTLC amount minus the HTLC-success fee would be less than
|
|
|
|
* `dust_limit_satoshis` set by the transaction owner:
|
|
|
|
* - MUST NOT contain that output.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST be generated as specified in
|
|
|
|
*/
|
|
|
|
else
|
2020-08-13 19:44:02 +02:00
|
|
|
htlc_fee = htlc_success_fee(feerate_per_kw,
|
2023-06-26 01:18:21 +02:00
|
|
|
option_anchor_outputs,
|
|
|
|
option_anchors_zero_fee_htlc_tx);
|
2019-05-31 09:30:32 +02:00
|
|
|
|
|
|
|
/* If these overflow, it implies htlc must be less. */
|
|
|
|
if (!amount_sat_add(&htlc_min, dust_limit, htlc_fee))
|
|
|
|
return true;
|
|
|
|
return amount_msat_less_sat(htlc_amount, htlc_min);
|
|
|
|
}
|
2021-09-28 21:12:01 +02:00
|
|
|
|
|
|
|
/* Minimum amount of headroom we should use for
|
|
|
|
* anticipated feerate adjustments */
|
|
|
|
#define HTLC_FEE_MIN_RANGE 2530
|
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
|
|
|
u32 htlc_trim_feerate_ceiling(u32 feerate_per_kw)
|
|
|
|
{
|
2021-10-04 21:06:53 +02:00
|
|
|
/* BOLT-919 #2:
|
|
|
|
*
|
|
|
|
* `dust_buffer_feerate` is defined as the maximum
|
|
|
|
* of either 2530 sats per kWU or 125% of the
|
|
|
|
* current `feerate_per_kw`. */
|
2021-09-28 21:12:01 +02:00
|
|
|
return max(feerate_per_kw + feerate_per_kw / 4,
|
|
|
|
feerate_per_kw + HTLC_FEE_MIN_RANGE);
|
|
|
|
}
|