Skip to content

Commit a89a2d4

Browse files
committed
common: fix utxo_spend_weight to understand how cheap P2TR is.
We previously treated it as a P2WPKH, which is wrong. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 3cc095c commit a89a2d4

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

common/utxo.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,52 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
6464

6565
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight)
6666
{
67-
size_t wit_weight = bitcoin_tx_simple_input_witness_weight();
67+
size_t witness_weight;
68+
bool p2sh;
69+
70+
switch (utxo->utxotype) {
71+
case UTXO_P2SH_P2WPKH:
72+
witness_weight = bitcoin_tx_simple_input_witness_weight();
73+
p2sh = true;
74+
goto have_weight;
75+
case UTXO_P2WPKH:
76+
witness_weight = bitcoin_tx_simple_input_witness_weight();
77+
p2sh = false;
78+
goto have_weight;
79+
case UTXO_P2WSH_FROM_CLOSE:
80+
/* BOLT #3:
81+
* #### `to_remote` Output
82+
*
83+
* If `option_anchors` applies to the commitment
84+
* transaction, the `to_remote` output is encumbered by a one
85+
* block csv lock.
86+
* <remotepubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
87+
*
88+
* The output is spent by an input with `nSequence` field set
89+
* to `1` and witness: <remote_sig>
90+
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
91+
*/
92+
if (utxo->close_info->option_anchors)
93+
witness_weight = 1 + 33 + 3 + 1 + 64;
94+
else
95+
witness_weight = 1 + 64;
96+
p2sh = false;
97+
goto have_weight;
98+
case UTXO_P2TR:
99+
witness_weight = 1 + 64;
100+
p2sh = false;
101+
goto have_weight;
102+
}
103+
abort();
104+
105+
have_weight:
68106
/* If the min is less than what we'd use for a 'normal' tx,
69107
* we return the value with the greater added/calculated */
70-
if (wit_weight < min_witness_weight)
71-
return bitcoin_tx_input_weight(utxo->utxotype == UTXO_P2SH_P2WPKH,
108+
if (witness_weight < min_witness_weight)
109+
return bitcoin_tx_input_weight(p2sh,
72110
min_witness_weight);
73111

74-
return bitcoin_tx_input_weight(utxo->utxotype == UTXO_P2SH_P2WPKH, wit_weight);
112+
return bitcoin_tx_input_weight(p2sh, witness_weight);
75113
}
76114

77115
u32 utxo_is_immature(const struct utxo *utxo, u32 blockheight)

0 commit comments

Comments
 (0)