Browse Source

channeld: extract HTLC trim logic into common.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pull/2938/head
Rusty Russell 6 years ago
parent
commit
e7778a0494
  1. 1
      channeld/Makefile
  2. 33
      channeld/commit_tx.c
  3. 1
      channeld/test/Makefile
  4. 1
      common/Makefile
  5. 41
      common/htlc_trim.c
  6. 14
      common/htlc_trim.h
  7. 3
      common/htlc_tx.h

1
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 \

33
channeld/commit_tx.c

@ -2,6 +2,7 @@
#include <bitcoin/tx.h>
#include <ccan/endian/endian.h>
#include <channeld/commit_tx.h>
#include <common/htlc_trim.h>
#include <common/htlc_tx.h>
#include <common/keyset.h>
#include <common/permute_tx.h>
@ -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,

1
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 \

1
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 \

41
common/htlc_trim.c

@ -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

@ -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 */

3
common/htlc_tx.h

@ -4,9 +4,12 @@
#include <common/amount.h>
#include <common/htlc.h>
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)
{

Loading…
Cancel
Save