Skip to content

Conversation

@whitslack
Copy link
Collaborator

@whitslack whitslack commented Apr 13, 2025

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.

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:

  • The changelog has been updated in the relevant commit(s) according to the guidelines.
  • Tests have been added or modified to reflect the changes. (All tests pass with no changes.)
  • Documentation has been reviewed and updated as needed. (I don't believe there will be any documentation inconsistencies introduced by this fix.)
  • Related issues have been listed and linked, including any that this PR closes.

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: ElementsProject#8164
Changelog-Fixed: P2TR inputs are assessed the correct weight in fee calculations.
@whitslack whitslack force-pushed the correct-p2tr-input-weight branch from c93bb8b to cfca26b Compare April 14, 2025 07:59
@rustyrussell
Copy link
Contributor

This was fixed with a more broad rewrite of how we calculate fees in #8236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

txprepare does not use provided feerate argument correctly

2 participants