Commit cfca26b
committed
common: calculate correct weight of Taproot spends
utxo_spend_weight(…) was assuming that witnesses always include a pubkey, but
that is not true of Taproot (key-path) spends. The effect was that wallet-
funded commands (e.g., withdraw, fundchannel, multifundchannel, etc.) were
overpaying on fees due to calculating Taproot input weights as 34 sipa greater
than they actually are.
Also as a consequence of the miscalculation, users who carefully select a set of
UTxOs to spend so as to avoid producing a change output would experience an
error when trying to spend them. As an example, I tried to spend a tiny 1390-sat
P2TR output to a new 1275-sat output at minimum feerate. The actual weight of
the transaction is 451 sipa, so the 115-sat fee yields a feerate of 255 sat/kw.
However, CLN refuses the transaction, claiming:
{
"code": 301,
"message": "Could not afford 1275sat using UTXOs totalling 1390sat with weight 485 at feerate 253"
}
The reported weight of 485 sipa is wrong, as it assumes the input will include a
34-sipa public key when in fact it will not.
This commit amends the bitcoin_tx_simple_input_witness_weight() and
bitcoin_tx_simple_input_weight(…) functions to take a parameter specifying the
scriptPubKey type of the spend. The implementation of the former is corrected so
that P2TR spends are not charged for a public key that they actually lack.
An enum type is introduced to enumerate the known scriptPubKey types, and a new
scriptpubkey_type(…) utility function is implemented to discern the type of a
given scriptPubKey. The existing is_known_scripttype(…) function is
reimplemented as an inline wrapper around the new function.
The default_lease_rates(…) function in plugins/funder_policy.c is amended so as
to state explicitly an assumption that it has been making: that the two inputs
it assumes for its default max weight calculation will not be Taproot inputs.
Fixes: #8164
Changelog-Fixed: P2TR inputs are assessed the correct weight in fee calculations.1 parent 427c4c5 commit cfca26b
6 files changed
Lines changed: 54 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
550 | 550 | | |
551 | 551 | | |
552 | 552 | | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
560 | 566 | | |
561 | 567 | | |
562 | 568 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
16 | 26 | | |
17 | 27 | | |
18 | 28 | | |
| |||
173 | 183 | | |
174 | 184 | | |
175 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
176 | 189 | | |
177 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
178 | 194 | | |
179 | 195 | | |
180 | 196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
912 | 912 | | |
913 | 913 | | |
914 | 914 | | |
915 | | - | |
| 915 | + | |
916 | 916 | | |
917 | | - | |
918 | | - | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
919 | 927 | | |
920 | 928 | | |
921 | | - | |
922 | | - | |
| 929 | + | |
| 930 | + | |
923 | 931 | | |
924 | | - | |
925 | | - | |
| 932 | + | |
| 933 | + | |
926 | 934 | | |
927 | 935 | | |
928 | 936 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
319 | 320 | | |
320 | 321 | | |
321 | 322 | | |
322 | | - | |
| 323 | + | |
323 | 324 | | |
324 | 325 | | |
325 | | - | |
| 326 | + | |
326 | 327 | | |
327 | 328 | | |
328 | 329 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| 131 | + | |
131 | 132 | | |
132 | | - | |
| 133 | + | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| |||
0 commit comments