From 702a88de0c0d3603158cd2c183389ba6bf79535e Mon Sep 17 00:00:00 2001
From: Rusty Russell <rusty@rustcorp.com.au>
Date: Tue, 30 Sep 2025 11:33:19 +0930
Subject: [PATCH] pytest: fix flake in test_coinmoves_unilateral_htlc_fulfill /
 test_coinmoves_unilateral_htlc_timeout

DER sigs!  Normally, the commitment weight is:

```
Anchorspend for local commit tx fee 9751sat (w=722), commit_tx fee 4866sat (w=1284): package feerate 7286 perkw
Creating anchor spend for local commit tx 6a0816ca60d499edc70bfb786ebd164fb7a55d234c84d926102f5bd35087fd45: we're paying fee 9751sat
```

But if we're "lucky" the commitment tx is shorter:

```
Anchorspend for local commit tx fee 9744sat (w=722), commit_tx fee 4866sat (w=1283): package feerate 7286 perkw
Creating anchor spend for local commit tx acf78532a9448dd62a4e6319a3d2712189a88b6e59abc637260067d60df70782: we're paying fee 9744sat
```

The resulting failure:

```
2025-08-21T02:30:34.1906751Z >       assert moves == expected
...
...
2025-08-21T02:30:34.1965346Z E               {
2025-08-21T02:30:34.1965529Z E                   'account_id': 'wallet',
2025-08-21T02:30:34.1965767Z E                   'blockheight': 104,
2025-08-21T02:30:34.1965997Z E                   'created_index': 6,
2025-08-21T02:30:34.1966229Z E         -         'credit_msat': 15579000,
2025-08-21T02:30:34.1966467Z E         ?                           ^^
2025-08-21T02:30:34.1966698Z E         +         'credit_msat': 15586000,
2025-08-21T02:30:34.1966927Z E         ?                           ^^
2025-08-21T02:30:34.1967150Z E                   'debit_msat': 0,
2025-08-21T02:30:34.1967376Z E                   'extra_tags': [],
2025-08-21T02:30:34.1967599Z E         -         'output_msat': 15579000,
2025-08-21T02:30:34.1967832Z E         ?                           ^^
2025-08-21T02:30:34.1968061Z E         +         'output_msat': 15586000,
2025-08-21T02:30:34.1968294Z E         ?                           ^^
2025-08-21T02:30:34.1968540Z E                   'primary_tag': 'deposit',
2025-08-21T02:30:34.1968908Z E                   'utxo': 'acf78532a9448dd62a4e6319a3d2712189a88b6e59abc637260067d60df70782:0',
2025-08-21T02:30:34.1969366Z E               },
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
 tests/test_coinmoves.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tests/test_coinmoves.py b/tests/test_coinmoves.py
index 0cdefe83fc7347fcf065af7e8fc8f11071c7b581..21f6dea8f9377a8ccf7688e48f68f046dfb3d80e 100644
--- a/tests/test_coinmoves.py
+++ b/tests/test_coinmoves.py
@@ -727,6 +727,11 @@ def test_coinmoves_unilateral_htlc_timeout(node_factory, bitcoind):
     line = l1.daemon.is_in_log('Tracking output.*/OUR_HTLC')
     htlc = int(re.search(r'output [0-9a-f]{64}:([0-9]):', line).group(1))
 
+    # commitment tx weight can vary (DER sigs, FML) and so even though the feerate target
+    # is fixed, the amount of the child tx we create will vary, hence the change varies.
+    # So it's usually 15579000, but one in 128 it will be 15586000...
+    anchor_change_msats = bitcoind.rpc.gettxout(anchor_spend_txid, 0)['value'] * 100_000_000_000
+
     expected_chain1 += [{'account_id': 'wallet',  # Anchor spend from fundchannel change
                          'blockheight': 104,
                          'credit_msat': 0,
@@ -738,10 +743,10 @@ def test_coinmoves_unilateral_htlc_timeout(node_factory, bitcoind):
                          'utxo': f"{fundchannel['txid']}:{fundchannel['outnum'] ^ 1}"},
                         {'account_id': 'wallet',  # change from anchor spend
                          'blockheight': 104,
-                         'credit_msat': 15579000,
+                         'credit_msat': anchor_change_msats,
                          'debit_msat': 0,
                          'extra_tags': [],
-                         'output_msat': 15579000,
+                         'output_msat': anchor_change_msats,
                          'primary_tag': 'deposit',
                          'utxo': f"{anchor_spend_txid}:0"},
                         {'account_id': fundchannel['channel_id'],
@@ -1221,6 +1226,11 @@ def test_coinmoves_unilateral_htlc_fulfill(node_factory, bitcoind):
     line = l1.daemon.is_in_log('Tracking output.*/OUR_HTLC')
     htlc = int(re.search(r'output [0-9a-f]{64}:([0-9]):', line).group(1))
 
+    # commitment tx weight can vary (DER sigs, FML) and so even though the feerate target
+    # is fixed, the amount of the child tx we create will vary, hence the change varies.
+    # So it's usually 15579000, but one in 128 it will be 15586000...
+    anchor_change_msats = bitcoind.rpc.gettxout(anchor_spend_txid, 0)['value'] * 100_000_000_000
+
     expected_chain1 += [{'account_id': 'wallet',  # Anchor spend from fundchannel change
                          'blockheight': 104,
                          'credit_msat': 0,
@@ -1232,10 +1242,10 @@ def test_coinmoves_unilateral_htlc_fulfill(node_factory, bitcoind):
                          'utxo': f"{fundchannel['txid']}:{fundchannel['outnum'] ^ 1}"},
                         {'account_id': 'wallet',  # Change from anchor spend
                          'blockheight': 104,
-                         'credit_msat': 15579000,
+                         'credit_msat': anchor_change_msats,
                          'debit_msat': 0,
                          'extra_tags': [],
-                         'output_msat': 15579000,
+                         'output_msat': anchor_change_msats,
                          'primary_tag': 'deposit',
                          'utxo': f"{anchor_spend_txid}:0"},
                         {'account_id': fundchannel['channel_id'],
