splice script: Improve weight guess calculation
What changed, and why it matters
This commit fixes how the Core Lightning 'splice' feature estimates the size (and therefore the fee) of a Bitcoin transaction that combines channel funds with on-chain wallet funds. Previously the estimate could miss a wallet input/output or use the wrong witness-size constant, which could lead to constructing a transaction with an incorrect fee. A bad fee estimate can cause the transaction to be rejected by the network or leave funds stuck.
Treat as a routine correctness fix with possible financial-availability impact. Review related splice tests for fee-edge cases and consider backporting if the splice feature is in a supported release branch. No emergency response is indicated from the diff alone.
Security signals we found
Incorrect transaction weight estimation can produce invalid or under-paid on-chain transactions
Fixes witness-size constant offsets for simulated inputs
Adds previously omitted simulated wallet input/output weight
No explicit security framing by the vendor
Evidence from the diff
The patch refactors calc_weight() in plugins/spender/splice.c. It now: (1) tracks whether a wallet output must be added via a single boolean add_wallet_output instead of checking each action twice; (2) adds simulated weight for unresolved wallet inputs when present; (3) subtracts 1 from the 2-of-2 and P2TR witness weight constants used for simulated inputs, matching the actual serialized witness size; (4) emits a single simulated wallet output at the end rather than per-action. These are correctness fixes to a transaction-weight estimator used during splice PSBT construction and fee computation.
Changed components
plugins/spender/splice.ccalc_weight()splice transaction fee/weight estimationInspect captured patch +30 / −17
diff --git a/plugins/spender/splice.c b/plugins/spender/splice.c
index 62c56b27..1ee5ca89 100644
--- a/plugins/spender/splice.c
+++ b/plugins/spender/splice.c
@@ -633,6 +633,7 @@ static size_t calc_weight(struct splice_cmd *splice_cmd,
size_t lweight = 0, weight = 0;
size_t extra_inputs = 0;
size_t extra_outputs = 0;
+ bool add_wallet_output = output_wallet(splice_cmd) ? true : false;
plugin_log(plugin, LOG_DBG, "Counting potenetial tx weight;");
@@ -643,19 +644,28 @@ static size_t calc_weight(struct splice_cmd *splice_cmd,
*/
for (size_t i = 0; i < psbt->num_inputs; i++) {
weight += psbt_input_get_weight(psbt, i, PSBT_GUESS_2OF2);
- plugin_log(plugin, LOG_DBG, " Adding input; weight: %lu",
+ plugin_log(plugin, LOG_DBG, " Counting input; weight: %lu",
weight - lweight);
lweight = weight;
}
+ if (unresolved_wallet_inputs(splice_cmd)) {
+ add_wallet_output = true;
+ weight += bitcoin_tx_input_weight(false,
+ bitcoin_tx_input_witness_weight(UTXO_P2TR) - 1);
+ plugin_log(plugin, LOG_DBG, " Simulating input (wallet);"
+ " weight: %lu", weight - lweight);
+ lweight = weight;
+ }
+
/* Count the splice input manually */
for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
action = splice_cmd->actions[i];
- if (splice_cmd->actions[i]->channel_id) {
+ if (action->channel_id) {
weight += bitcoin_tx_input_weight(false,
- bitcoin_tx_2of2_input_witness_weight());
- plugin_log(plugin, LOG_DBG, " Adding input"
- " (simulated channel); weight:"
+ bitcoin_tx_2of2_input_witness_weight() - 1);
+ plugin_log(plugin, LOG_DBG, " Simulating input"
+ " (channel); weight:"
" %lu", weight - lweight);
lweight = weight;
extra_inputs++;
@@ -665,21 +675,15 @@ static size_t calc_weight(struct splice_cmd *splice_cmd,
/* Count the splice outputs manually */
for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
action = splice_cmd->actions[i];
- if (simulate_wallet_outputs && action->onchain_wallet) {
- if (!amount_sat_is_zero(action->in_sat) || action->in_ppm) {
- weight += bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2TR_LEN);
- extra_outputs++;
- plugin_log(plugin, LOG_DBG, " Adding output"
- " (simulated wallet); weight:"
- " %lu", weight - lweight);
- lweight = weight;
- }
+ if (action->onchain_wallet) {
+ if (!amount_sat_is_zero(action->in_sat) || action->in_ppm)
+ add_wallet_output = true;
assert(!splice_cmd->actions[i]->channel_id);
}
if (splice_cmd->actions[i]->channel_id) {
weight += bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2WSH_LEN);
- plugin_log(plugin, LOG_DBG, " Adding output"
- " (simulated channel); weight:"
+ plugin_log(plugin, LOG_DBG, " Simulating output"
+ " (channel); weight:"
" %lu", weight - lweight);
lweight = weight;
@@ -687,6 +691,15 @@ static size_t calc_weight(struct splice_cmd *splice_cmd,
}
}
+ if (simulate_wallet_outputs && add_wallet_output) {
+ weight += bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2TR_LEN);
+ extra_outputs++;
+ plugin_log(plugin, LOG_DBG, " Simulating output"
+ " (wallet); weight:"
+ " %lu", weight - lweight);
+ lweight = weight;
+ }
+
for (size_t i = 0; i < psbt->num_outputs; i++) {
weight += psbt_output_get_weight(psbt, i);
plugin_log(plugin, LOG_DBG, " Adding output; weight: %lu",
@@ -710,7 +723,7 @@ static size_t calc_weight(struct splice_cmd *splice_cmd,
" weight: %lu", weight - lweight);
lweight = weight;
- plugin_log(plugin, LOG_DBG, " Total weight: %lu", weight);
+ plugin_log(plugin, LOG_DBG, "Total weight: %lu", weight);
return weight;
}
Why this scored 32/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.