common: calculate correct weight of Taproot spends #8234
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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()
andbitcoin_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 existingis_known_scripttype(…)
function is reimplemented as an inline wrapper around the new function.The
default_lease_rates(…)
function inplugins/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.
Checklist
Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked: