diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 70201405b..e7c681870 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -260,14 +260,17 @@ static void push_measure(const void *data, size_t len, void *lenp) *(size_t *)lenp += len; } -size_t measure_tx_cost(const struct bitcoin_tx *tx) +size_t measure_tx_weight(const struct bitcoin_tx *tx) { size_t non_witness_len = 0, witness_len = 0; push_tx(tx, push_measure, &non_witness_len, false); - if (uses_witness(tx)) + if (uses_witness(tx)) { push_witnesses(tx, push_measure, &witness_len); + /* Include BIP 144 marker and flag bytes in witness length */ + witness_len += 2; + } - /* Witness bytes only push 1/4 of normal bytes, for cost. */ + /* Normal bytes weigh 4 times more than Witness bytes */ return non_witness_len * 4 + witness_len; } diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 1272a118d..f42e7079e 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -47,8 +47,8 @@ void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx, /* Linear bytes of tx. */ u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx); -/* Get cost of tx in (x4 of non-witness bytecount). */ -size_t measure_tx_cost(const struct bitcoin_tx *tx); +/* Get weight of tx in Sipa. */ +size_t measure_tx_weight(const struct bitcoin_tx *tx); /* Allocate a tx: you just need to fill in inputs and outputs (they're * zeroed with inputs' sequence_number set to FFFFFFFF) */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 6f613fdfd..d177053be 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1786,7 +1786,7 @@ static bool better_closing_fee(struct peer *peer, const struct bitcoin_tx *tx) " vs previous %"PRIu64, fee, last_fee); /* Weight once we add in sigs. */ - weight = measure_tx_cost(tx) + 74 * 2; + weight = measure_tx_weight(tx) + 74 * 2; min_fee = get_feerate(peer->ld->topology, FEERATE_SLOW) * weight / 1000; if (fee < min_fee) { diff --git a/onchaind/onchain.c b/onchaind/onchain.c index b3b039a46..ca74a4276 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -236,7 +236,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, &our_wallet_pubkey); /* Worst-case sig is 73 bytes */ - fee = feerate_per_kw * (measure_tx_cost(tx) + fee = feerate_per_kw * (measure_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_len(wscript)) / 1000;