mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +01:00
channeld: extract HTLC trim logic into common.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -49,6 +49,7 @@ CHANNELD_COMMON_OBJS := \
|
|||||||
common/gen_peer_status_wire.o \
|
common/gen_peer_status_wire.o \
|
||||||
common/gossip_store.o \
|
common/gossip_store.o \
|
||||||
common/htlc_state.o \
|
common/htlc_state.o \
|
||||||
|
common/htlc_trim.o \
|
||||||
common/htlc_tx.o \
|
common/htlc_tx.o \
|
||||||
common/htlc_wire.o \
|
common/htlc_wire.o \
|
||||||
common/initial_channel.o \
|
common/initial_channel.o \
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <bitcoin/tx.h>
|
#include <bitcoin/tx.h>
|
||||||
#include <ccan/endian/endian.h>
|
#include <ccan/endian/endian.h>
|
||||||
#include <channeld/commit_tx.h>
|
#include <channeld/commit_tx.h>
|
||||||
|
#include <common/htlc_trim.h>
|
||||||
#include <common/htlc_tx.h>
|
#include <common/htlc_tx.h>
|
||||||
#include <common/keyset.h>
|
#include <common/keyset.h>
|
||||||
#include <common/permute_tx.h>
|
#include <common/permute_tx.h>
|
||||||
@@ -16,36 +17,8 @@ static bool trim(const struct htlc *htlc,
|
|||||||
struct amount_sat dust_limit,
|
struct amount_sat dust_limit,
|
||||||
enum side side)
|
enum side side)
|
||||||
{
|
{
|
||||||
struct amount_sat htlc_fee, htlc_min;
|
return htlc_is_trimmed(htlc_owner(htlc), htlc->amount,
|
||||||
|
feerate_per_kw, dust_limit, side);
|
||||||
/* 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(htlc) == side)
|
|
||||||
htlc_fee = htlc_timeout_fee(feerate_per_kw);
|
|
||||||
/* 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
|
|
||||||
htlc_fee = htlc_success_fee(feerate_per_kw);
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ CHANNELD_TEST_COMMON_OBJS := \
|
|||||||
common/amount.o \
|
common/amount.o \
|
||||||
common/daemon_conn.o \
|
common/daemon_conn.o \
|
||||||
common/htlc_state.o \
|
common/htlc_state.o \
|
||||||
|
common/htlc_trim.o \
|
||||||
common/htlc_tx.o \
|
common/htlc_tx.o \
|
||||||
common/initial_commit_tx.o \
|
common/initial_commit_tx.o \
|
||||||
common/key_derive.o \
|
common/key_derive.o \
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ COMMON_SRC_NOGEN := \
|
|||||||
common/gossip_store.c \
|
common/gossip_store.c \
|
||||||
common/hash_u5.c \
|
common/hash_u5.c \
|
||||||
common/htlc_state.c \
|
common/htlc_state.c \
|
||||||
|
common/htlc_trim.c \
|
||||||
common/htlc_tx.c \
|
common/htlc_tx.c \
|
||||||
common/htlc_wire.c \
|
common/htlc_wire.c \
|
||||||
common/initial_channel.c \
|
common/initial_channel.c \
|
||||||
|
|||||||
41
common/htlc_trim.c
Normal file
41
common/htlc_trim.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#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,
|
||||||
|
enum side side)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
htlc_fee = htlc_timeout_fee(feerate_per_kw);
|
||||||
|
/* 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
|
||||||
|
htlc_fee = htlc_success_fee(feerate_per_kw);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
14
common/htlc_trim.h
Normal file
14
common/htlc_trim.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef LIGHTNING_COMMON_HTLC_TRIM_H
|
||||||
|
#define LIGHTNING_COMMON_HTLC_TRIM_H
|
||||||
|
#include "config.h"
|
||||||
|
#include <common/amount.h>
|
||||||
|
#include <common/htlc.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,
|
||||||
|
enum side side);
|
||||||
|
|
||||||
|
#endif /* LIGHTNING_COMMON_HTLC_TRIM_H */
|
||||||
@@ -4,9 +4,12 @@
|
|||||||
#include <common/amount.h>
|
#include <common/amount.h>
|
||||||
#include <common/htlc.h>
|
#include <common/htlc.h>
|
||||||
|
|
||||||
|
struct bitcoin_signature;
|
||||||
|
struct bitcoin_txid;
|
||||||
struct keyset;
|
struct keyset;
|
||||||
struct preimage;
|
struct preimage;
|
||||||
struct pubkey;
|
struct pubkey;
|
||||||
|
struct ripemd160;
|
||||||
|
|
||||||
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw)
|
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user