Skip to content

Commit

Permalink
bitcoin: fix tx weight calculation when there are no witnesses, but w…
Browse files Browse the repository at this point in the history
…ill be.

We had an out-by-two error when calculating weights, because we grab weights
on unsigned txs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 12, 2022
1 parent 52b1f5a commit 4ac8e23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,18 @@ size_t wally_tx_weight(const struct wally_tx *wtx)

size_t bitcoin_tx_weight(const struct bitcoin_tx *tx)
{
return wally_tx_weight(tx->wtx);
size_t extra;
size_t num_witnesses;

/* If we don't have witnesses *yet*, libwally doesn't encode
* in BIP 141 style, omitting the flag and marker bytes */
wally_tx_get_witness_count(tx->wtx, &num_witnesses);
if (num_witnesses == 0)
extra = 2;
else
extra = 0;

return extra + wally_tx_weight(tx->wtx);
}

void wally_txid(const struct wally_tx *wtx, struct bitcoin_txid *txid)
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void wally_txid(const struct wally_tx *wtx, struct bitcoin_txid *txid);
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);
u8 *linearize_wtx(const tal_t *ctx, const struct wally_tx *wtx);

/* Get weight of tx in Sipa. */
/* Get weight of tx in Sipa; assumes it will have witnesses! */
size_t bitcoin_tx_weight(const struct bitcoin_tx *tx);
size_t wally_tx_weight(const struct wally_tx *wtx);

Expand Down

0 comments on commit 4ac8e23

Please sign in to comment.