Skip to content

Commit a8f75cc

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> Changelog-Fixed: wallet: fees are much closer to target feerate when doing txprepare/fundchannel.
1 parent 27b9889 commit a8f75cc

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

common/utxo.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,52 @@
44

55
size_t utxo_spend_weight(const struct utxo *utxo, size_t min_witness_weight)
66
{
7-
size_t wit_weight = bitcoin_tx_simple_input_witness_weight();
7+
size_t witness_weight;
8+
bool p2sh;
9+
10+
switch (utxo->utxotype) {
11+
case UTXO_P2SH_P2WPKH:
12+
witness_weight = bitcoin_tx_simple_input_witness_weight();
13+
p2sh = true;
14+
goto have_weight;
15+
case UTXO_P2WPKH:
16+
witness_weight = bitcoin_tx_simple_input_witness_weight();
17+
p2sh = false;
18+
goto have_weight;
19+
case UTXO_P2WSH_FROM_CLOSE:
20+
/* BOLT #3:
21+
* #### `to_remote` Output
22+
*
23+
* If `option_anchors` applies to the commitment
24+
* transaction, the `to_remote` output is encumbered by a one
25+
* block csv lock.
26+
* <remotepubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
27+
*
28+
* The output is spent by an input with `nSequence` field set
29+
* to `1` and witness: <remote_sig>
30+
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
31+
*/
32+
if (utxo->close_info->option_anchors)
33+
witness_weight = 1 + 33 + 3 + 1 + 64;
34+
else
35+
witness_weight = 1 + 64;
36+
p2sh = false;
37+
goto have_weight;
38+
case UTXO_P2TR:
39+
witness_weight = 1 + 64;
40+
p2sh = false;
41+
goto have_weight;
42+
}
43+
abort();
44+
45+
have_weight:
846
/* If the min is less than what we'd use for a 'normal' tx,
947
* we return the value with the greater added/calculated */
10-
if (wit_weight < min_witness_weight)
11-
return bitcoin_tx_input_weight(utxo->utxotype == UTXO_P2SH_P2WPKH,
48+
if (witness_weight < min_witness_weight)
49+
return bitcoin_tx_input_weight(p2sh,
1250
min_witness_weight);
1351

14-
return bitcoin_tx_input_weight(utxo->utxotype == UTXO_P2SH_P2WPKH, wit_weight);
52+
return bitcoin_tx_input_weight(p2sh, witness_weight);
1553
}
1654

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

tests/test_closing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4274,7 +4274,7 @@ def censoring_sendrawtx(r):
42744274
height = bitcoind.rpc.getblockchaininfo()['blocks']
42754275
l1.daemon.wait_for_log(r"Low-priority anchorspend aiming for block {} \(feerate 7458\)".format(height + 13))
42764276
# Can be out-by-one (short sig)!
4277-
l1.daemon.wait_for_log(r"Anchorspend for local commit tx fee 12335sat \(w=714\), commit_tx fee 1735sat \(w=768\): package feerate 9493 perkw")
4277+
l1.daemon.wait_for_log(r"Anchorspend for local commit tx fee 12022sat \(w=672\), commit_tx fee 1735sat \(w=768\): package feerate 9553 perkw")
42784278
assert not l1.daemon.is_in_log("Low-priority anchorspend aiming for block {}".format(height + 12))
42794279

42804280
bitcoind.generate_block(1)

tests/test_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def dont_spend_outputs(n, txid):
746746
{'type': 'chain_mvt', 'credit_msat': 2000000000, 'debit_msat': 0, 'tags': ['deposit']},
747747
{'type': 'chain_mvt', 'credit_msat': 2000000000, 'debit_msat': 0, 'tags': ['deposit']},
748748
{'type': 'chain_mvt', 'credit_msat': 2000000000, 'debit_msat': 0, 'tags': ['deposit']},
749-
{'type': 'chain_mvt', 'credit_msat': 11956163000, 'debit_msat': 0, 'tags': ['deposit']},
749+
{'type': 'chain_mvt', 'credit_msat': 11957423000, 'debit_msat': 0, 'tags': ['deposit']},
750750
]
751751

752752
check_coin_moves(l1, 'external', external_moves, chainparams)

0 commit comments

Comments
 (0)