diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 13aa1a0c7..4664b5044 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -4,6 +4,7 @@ #include #include #include +#include struct bitcoin_signature; struct bitcoin_txid; @@ -12,6 +13,32 @@ struct preimage; struct pubkey; struct ripemd160; +/** Attempt to compute the elements overhead given a base bitcoin size. + * + * The overhead consists of 2 empty proofs for the transaction, 6 bytes of + * proofs per input and 35 bytes per output. In addition the explicit fee + * output will add 9 bytes and the per output overhead as well. + */ +static inline size_t elements_add_overhead(size_t weight, size_t incount, + size_t outcount) +{ + if (is_elements) { + /* Each transaction has surjection and rangeproof (both empty + * for us as long as we use unblinded L-BTC transactions). */ + weight += 2 * 4; + /* For elements we also need to add the fee output and the + * overhead for rangeproofs into the mix. */ + weight += (8 + 1) * 4; /* Bitcoin style output */ + + /* All outputs have a bit of elements overhead */ + weight += (32 + 1 + 1 + 1) * 4 * (outcount + 1); /* Elements added fields */ + + /* Inputs have 6 bytes of blank proofs attached. */ + weight += 6 * incount; + } + return weight; +} + static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw) { /* BOLT #3: @@ -21,7 +48,7 @@ static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw) * 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding * down). */ - return amount_tx_fee(663, feerate_per_kw); + return amount_tx_fee(elements_add_overhead(663, 1, 1), feerate_per_kw); } static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw) @@ -33,7 +60,7 @@ static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw) * 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding * down). */ - return amount_tx_fee(703, feerate_per_kw); + return amount_tx_fee(elements_add_overhead(703, 1, 1), feerate_per_kw); } /* Create HTLC-success tx to spend a received HTLC commitment tx diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 74aefe45e..ea0031aeb 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -165,6 +165,8 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, { static struct amount_sat fee = AMOUNT_SAT_INIT(UINT64_MAX); struct amount_sat amount = bitcoin_tx_output_get_amount(tx, 0); + size_t weight = elements_add_overhead(663, tx->wtx->num_inputs, + tx->wtx->num_outputs); /* BOLT #3: * @@ -175,7 +177,7 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, */ if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) { struct amount_sat grindfee; - if (grind_htlc_tx_fee(&grindfee, tx, remotesig, wscript, 663)) { + if (grind_htlc_tx_fee(&grindfee, tx, remotesig, wscript, weight)) { /* Cache this for next time */ fee = grindfee; return true; @@ -200,7 +202,8 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx, const u8 *wscript) { static struct amount_sat amt, fee = AMOUNT_SAT_INIT(UINT64_MAX); - + size_t weight = elements_add_overhead(703, tx->wtx->num_inputs, + tx->wtx->num_outputs); /* BOLT #3: * * The fee for an HTLC-success transaction: @@ -209,7 +212,7 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx, * (rounding down). */ if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) { - if (!grind_htlc_tx_fee(&fee, tx, remotesig, wscript, 703)) + if (!grind_htlc_tx_fee(&fee, tx, remotesig, wscript, weight)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "htlc_success_fee can't be found " " for tx %s, signature %s, wscript %s",