Skip to content

Commit 9883f24

Browse files
committed
bitcoin: make input witness weight calculation explicit.
This is inspired by a patch from @whitslack, which overlapped with this series. Most importantly, there was only one call to bitcoin_tx_simple_input_weight(), and it is better to be explicit with that one. This also changes our funder calculation to assume our own input is taproot, which it is likely to be given we've defaulted to taproot for outputs for change addresses since 23.08. Signed-off-by: Rusty Russell <[email protected]>
1 parent ae2f23d commit 9883f24

File tree

5 files changed

+43
-65
lines changed

5 files changed

+43
-65
lines changed

bitcoin/tx.c

+26-11
Original file line numberDiff line numberDiff line change
@@ -913,17 +913,32 @@ size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight)
913913
return weight;
914914
}
915915

916-
size_t bitcoin_tx_simple_input_witness_weight(void)
917-
{
918-
/* Account for witness (sig + key) */
919-
return bitcoin_tx_input_sig_weight() + 1 + 33;
920-
}
921-
922-
/* We only do segwit inputs, and we assume witness is sig + key */
923-
size_t bitcoin_tx_simple_input_weight(bool p2sh)
924-
{
925-
return bitcoin_tx_input_weight(p2sh,
926-
bitcoin_tx_simple_input_witness_weight());
916+
size_t bitcoin_tx_input_witness_weight(enum utxotype utxotype)
917+
{
918+
switch (utxotype) {
919+
case UTXO_P2SH_P2WPKH:
920+
case UTXO_P2WPKH:
921+
/* Account for witness (sig + key) */
922+
return bitcoin_tx_input_sig_weight() + 1 + 33;
923+
case UTXO_P2WSH_FROM_CLOSE:
924+
/* BOLT #3:
925+
* #### `to_remote` Output
926+
*
927+
* If `option_anchors` applies to the commitment
928+
* transaction, the `to_remote` output is encumbered by a one
929+
* block csv lock.
930+
* <remotepubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
931+
*
932+
* The output is spent by an input with `nSequence` field set
933+
* to `1` and witness: <remote_sig>
934+
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
935+
*/
936+
/* In practice, these predate anchors, so: */
937+
return 1 + 1 + bitcoin_tx_input_sig_weight();
938+
case UTXO_P2TR:
939+
return 1 + 64;
940+
}
941+
abort();
927942
}
928943

929944
size_t bitcoin_tx_2of2_input_witness_weight(void)

bitcoin/tx.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ struct bitcoin_tx_output {
4848
u8 *script;
4949
};
5050

51+
enum utxotype {
52+
/* Obsolete: we used to have P2SH-wrapped outputs (removed in 24.02, though can still have old UTXOs) */
53+
UTXO_P2SH_P2WPKH = 1,
54+
/* "bech32" addresses */
55+
UTXO_P2WPKH = 2,
56+
/* Used for closing addresses: implies ->close_info is non-NULL */
57+
UTXO_P2WSH_FROM_CLOSE = 3,
58+
/* "p2tr" addresses. */
59+
UTXO_P2TR = 4,
60+
};
61+
5162
struct bitcoin_tx_output *new_tx_output(const tal_t *ctx,
5263
struct amount_sat amount,
5364
const u8 *script);
@@ -320,11 +331,8 @@ size_t bitcoin_tx_input_sig_weight(void);
320331
* but not the varint_size() for the number of elements. */
321332
size_t bitcoin_tx_input_weight(bool p2sh, size_t witness_weight);
322333

323-
/* The witness weight for a simple (sig + key) input */
324-
size_t bitcoin_tx_simple_input_witness_weight(void);
325-
326-
/* We only do segwit inputs, and we assume witness is sig + key */
327-
size_t bitcoin_tx_simple_input_weight(bool p2sh);
334+
/* The witness weight */
335+
size_t bitcoin_tx_input_witness_weight(enum utxotype utxotype);
328336

329337
/* The witness for our 2of2 input (closing or commitment tx). */
330338
size_t bitcoin_tx_2of2_input_witness_weight(void);

common/utxo.c

+2-36
Original file line numberDiff line numberDiff line change
@@ -65,44 +65,10 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
6565
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight)
6666
{
6767
size_t witness_weight;
68-
bool p2sh;
68+
bool p2sh = (utxo->utxotype == UTXO_P2SH_P2WPKH);
6969

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();
70+
witness_weight = bitcoin_tx_input_witness_weight(utxo->utxotype);
10471

105-
have_weight:
10672
/* If the min is less than what we'd use for a 'normal' tx,
10773
* we return the value with the greater added/calculated */
10874
if (witness_weight < min_witness_weight)

common/utxo.h

-11
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ enum output_status {
3131
OUTPUT_STATE_ANY = 255
3232
};
3333

34-
enum utxotype {
35-
/* Obsolete: we used to have P2SH-wrapped outputs (removed in 24.02, though can still have old UTXOs) */
36-
UTXO_P2SH_P2WPKH = 1,
37-
/* "bech32" addresses */
38-
UTXO_P2WPKH = 2,
39-
/* Used for closing addresses: implies ->close_info is non-NULL */
40-
UTXO_P2WSH_FROM_CLOSE = 3,
41-
/* "p2tr" addresses. */
42-
UTXO_P2TR = 4,
43-
};
44-
4534
const char *utxotype_to_str(enum utxotype utxotype);
4635

4736
struct utxo {

plugins/funder_policy.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ default_lease_rates(const tal_t *ctx)
127127
rates->channel_fee_max_base_msat = 5000000;
128128

129129
/* Let's set our default max weight to two inputs + an output
130-
* (use helpers b/c elements) */
130+
* (use helpers b/c elements). We're mainly taproot now. */
131131
rates->funding_weight
132-
= 2 * bitcoin_tx_simple_input_weight(false)
132+
= 2 * bitcoin_tx_input_weight(false, bitcoin_tx_input_witness_weight(UTXO_P2TR))
133133
+ bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN);
134134

135135
return rates;

0 commit comments

Comments
 (0)