Skip to content

Commit

Permalink
onchaind: Adjust witness weight estimate to be more conservative
Browse files Browse the repository at this point in the history
We were missing the OP_PUSH for the pubkeys, and the spec mentions we
should be using 73 bytes to estimate the witness weight. Effectively
this adds 4 bytes which really just matters in case fees hit the
floor, and computing the weight becomes important.

Changelog-Fixed: onchaind: Witness weight estimations could be slightly lower than the VLS signer
  • Loading branch information
cdecker committed Oct 20, 2022
1 parent 249730a commit 689de41
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ int main(int argc, const char *argv[])

/* 1 byte for num witnesses, one per witness element */
weight = 1;

/* Two signatures, slightly overestimated to be 73 bytes each,
* while the actual witness will often be smaller.*/
/* BOLT #03:
* Signatures are 73 bytes long (the maximum length).
*/
weight += 2 + 2;

for (size_t i = 0; i < tal_count(wit); i++)
weight += 1 + tal_bytelen(wit[i]);
assert(bitcoin_tx_2of2_input_witness_weight() == weight);
Expand Down
11 changes: 7 additions & 4 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,13 +886,16 @@ size_t bitcoin_tx_simple_input_weight(bool p2sh)

size_t bitcoin_tx_2of2_input_witness_weight(void)
{
/* BOLT #03:
* Signatures are 73 bytes long (the maximum length).
*/
return 1 + /* Prefix: 4 elements to push on stack */
(1 + 0) + /* [0]: witness-marker-and-flag */
(1 + 72) + /* [1] Party A signature and length prefix */
(1 + 72) + /* [2] Party B signature and length prefix */
(1 + 73) + /* [1] Party A signature and length prefix */
(1 + 73) + /* [2] Party B signature and length prefix */
(1 + 1 + /* [3] length prefix and numpushes (2) */
33 + /* pubkey A (missing prefix) */
33 + /* pubkey B (missing prefix) */
1 + 33 + /* pubkey A (with prefix) */
1 + 33 + /* pubkey B (with prefix) */
1 + 1 /* num sigs required and checkmultisig */
);
}
Expand Down

0 comments on commit 689de41

Please sign in to comment.