elements: Fix fee estimation for the htlc success and timeout txs

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-07-02 17:21:26 +02:00 committed by Rusty Russell
parent e6d5b70e72
commit 83140892fb
2 changed files with 35 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#include <bitcoin/chainparams.h>
#include <common/amount.h>
#include <common/htlc.h>
#include <common/utils.h>
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

View File

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