From e7778a04948532b0cee1aebcb2a208ff61d31c57 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 31 May 2019 17:00:32 +0930 Subject: [PATCH] channeld: extract HTLC trim logic into common. Signed-off-by: Rusty Russell --- channeld/Makefile | 1 + channeld/commit_tx.c | 33 +++------------------------------ channeld/test/Makefile | 1 + common/Makefile | 1 + common/htlc_trim.c | 41 +++++++++++++++++++++++++++++++++++++++++ common/htlc_trim.h | 14 ++++++++++++++ common/htlc_tx.h | 3 +++ 7 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 common/htlc_trim.c create mode 100644 common/htlc_trim.h diff --git a/channeld/Makefile b/channeld/Makefile index 18224212f..6e3583c90 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -49,6 +49,7 @@ CHANNELD_COMMON_OBJS := \ common/gen_peer_status_wire.o \ common/gossip_store.o \ common/htlc_state.o \ + common/htlc_trim.o \ common/htlc_tx.o \ common/htlc_wire.o \ common/initial_channel.o \ diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index c0b917074..1ee72406f 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -16,36 +17,8 @@ static bool trim(const struct htlc *htlc, 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(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); + return htlc_is_trimmed(htlc_owner(htlc), htlc->amount, + feerate_per_kw, dust_limit, side); } size_t commit_tx_num_untrimmed(const struct htlc **htlcs, diff --git a/channeld/test/Makefile b/channeld/test/Makefile index 55403a1fc..f197c070d 100644 --- a/channeld/test/Makefile +++ b/channeld/test/Makefile @@ -11,6 +11,7 @@ CHANNELD_TEST_COMMON_OBJS := \ common/amount.o \ common/daemon_conn.o \ common/htlc_state.o \ + common/htlc_trim.o \ common/htlc_tx.o \ common/initial_commit_tx.o \ common/key_derive.o \ diff --git a/common/Makefile b/common/Makefile index 951611bea..f53a7710c 100644 --- a/common/Makefile +++ b/common/Makefile @@ -21,6 +21,7 @@ COMMON_SRC_NOGEN := \ common/gossip_store.c \ common/hash_u5.c \ common/htlc_state.c \ + common/htlc_trim.c \ common/htlc_tx.c \ common/htlc_wire.c \ common/initial_channel.c \ diff --git a/common/htlc_trim.c b/common/htlc_trim.c new file mode 100644 index 000000000..e2d01f8eb --- /dev/null +++ b/common/htlc_trim.c @@ -0,0 +1,41 @@ +#include +#include + +/* 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); +} diff --git a/common/htlc_trim.h b/common/htlc_trim.h new file mode 100644 index 000000000..3a8a23003 --- /dev/null +++ b/common/htlc_trim.h @@ -0,0 +1,14 @@ +#ifndef LIGHTNING_COMMON_HTLC_TRIM_H +#define LIGHTNING_COMMON_HTLC_TRIM_H +#include "config.h" +#include +#include + +/* 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 */ diff --git a/common/htlc_tx.h b/common/htlc_tx.h index b0cf87101..93e7b771c 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -4,9 +4,12 @@ #include #include +struct bitcoin_signature; +struct bitcoin_txid; struct keyset; struct preimage; struct pubkey; +struct ripemd160; static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw) {