@@ -300,9 +300,25 @@ def feerate_from_psbt(bitcoind, node, psbt):
300300 return fee / weight * 1000
301301
302302
303+ # I wish we could force libwally to use different entropy and thus force it to
304+ # create 71-byte sigs always!
305+ def did_short_sig (node ):
306+ # This can take a moment to appear in the log!
307+ time .sleep (1 )
308+ return node .daemon .is_in_log ('overgrind: short signature length' )
309+
310+
311+ def check_feerate (node , actual_feerate , expected_feerate ):
312+ # Feerate can't be lower.
313+ assert actual_feerate > expected_feerate - 2
314+ if not did_short_sig (node ):
315+ assert actual_feerate < expected_feerate + 2
316+
317+
303318def test_txprepare (node_factory , bitcoind , chainparams ):
304319 amount = 1000000
305- l1 = node_factory .get_node (random_hsm = True )
320+ l1 = node_factory .get_node (random_hsm = True , options = {'dev-warn-on-overgrind' : None },
321+ broken_log = 'overgrind: short signature length' )
306322 addr = chainparams ['example_addr' ]
307323
308324 # Add some funds to withdraw later
@@ -322,8 +338,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
322338 # 4 inputs, 2 outputs (3 if we have a fee output).
323339 assert len (decode ['vin' ]) == 4
324340 assert len (decode ['vout' ]) == 2 if not chainparams ['feeoutput' ] else 3
325- # Feerate should be ~ as we asked for
326- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep ['psbt' ]) < normal_feerate_perkw + 2
341+ check_feerate (l1 , feerate_from_psbt (bitcoind , l1 , prep ['psbt' ]), normal_feerate_perkw )
327342
328343 # One output will be correct.
329344 outnum = [i for i , o in enumerate (decode ['vout' ]) if o ['value' ] == Decimal (amount * 3 ) / 10 ** 8 ][0 ]
@@ -351,8 +366,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
351366 assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 6 ) / 10 ** 8 - Decimal (0.0002 )
352367 assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
353368 assert scriptpubkey_addr (decode ['vout' ][0 ]['scriptPubKey' ]) == addr
354- # Feerate should be ~ as we asked for
355- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep2 ['psbt' ]) < normal_feerate_perkw + 2
369+ check_feerate (l1 , feerate_from_psbt (bitcoind , l1 , prep2 ['psbt' ]), normal_feerate_perkw )
356370
357371 # If I cancel the first one, I can get those first 4 outputs.
358372 discard = l1 .rpc .txdiscard (prep ['txid' ])
@@ -371,8 +385,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
371385 assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 4 ) / 10 ** 8 - Decimal (0.0002 )
372386 assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
373387 assert scriptpubkey_addr (decode ['vout' ][0 ]['scriptPubKey' ]) == addr
374- # Feerate should be ~ as we asked for
375- assert normal_feerate_perkw - 1 < feerate_from_psbt (bitcoind , l1 , prep3 ['psbt' ]) < normal_feerate_perkw + 1
388+ check_feerate (l1 , feerate_from_psbt (bitcoind , l1 , prep3 ['psbt' ]), normal_feerate_perkw )
376389
377390 # Cannot discard twice.
378391 with pytest .raises (RpcError , match = r'not an unreleased txid' ):
@@ -393,17 +406,15 @@ def test_txprepare(node_factory, bitcoind, chainparams):
393406 assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 10 ) / 10 ** 8 - Decimal (0.0003 )
394407 assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
395408 assert scriptpubkey_addr (decode ['vout' ][0 ]['scriptPubKey' ]) == addr
396- # Feerate should be ~ as we asked for
397- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep4 ['psbt' ]) < normal_feerate_perkw + 2
409+ check_feerate (l1 , feerate_from_psbt (bitcoind , l1 , prep4 ['psbt' ]), normal_feerate_perkw )
398410 l1 .rpc .txdiscard (prep4 ['txid' ])
399411
400412 # Try passing in a utxo set
401413 utxos = [utxo ["txid" ] + ":" + str (utxo ["output" ])
402414 for utxo in l1 .rpc .listfunds ()["outputs" ]][:4 ]
403415 prep5 = l1 .rpc .txprepare ([{addr :
404416 Millisatoshi (amount * 3.5 * 1000 )}], utxos = utxos )
405- # Feerate should be ~ as we asked for
406- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep3 ['psbt' ]) < normal_feerate_perkw + 2
417+ check_feerate (l1 , feerate_from_psbt (bitcoind , l1 , prep3 ['psbt' ]), normal_feerate_perkw )
407418
408419 # Try passing unconfirmed utxos
409420 unconfirmed_utxo = l1 .rpc .withdraw (l1 .rpc .newaddr ()["bech32" ], 10 ** 5 )
@@ -414,7 +425,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
414425 # Feerate should be ~ as we asked for
415426 unconfirmed_tx = bitcoind .rpc .getrawmempool (True )[unconfirmed_utxo ["txid" ]]
416427 feerate_perkw = int (unconfirmed_tx ['fees' ]['base' ] * 100_000_000 ) * 1000 / unconfirmed_tx ['weight' ]
417- assert normal_feerate_perkw - 1 < feerate_perkw < normal_feerate_perkw + 1
428+ check_feerate ( l1 , feerate_perkw , normal_feerate_perkw )
418429
419430 decode = bitcoind .rpc .decoderawtransaction (prep5 ['unsigned_tx' ])
420431 assert decode ['txid' ] == prep5 ['txid' ]
@@ -444,15 +455,15 @@ def test_txprepare(node_factory, bitcoind, chainparams):
444455 prep5 = l1 .rpc .txprepare ([{addr : Millisatoshi (amount * 3 * 1000 )},
445456 {addr : 'all' }])
446457 # Feerate should be ~ as we asked for
447- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep5 ['psbt' ]) < normal_feerate_perkw + 2
458+ check_feerate ( l1 , feerate_from_psbt (bitcoind , l1 , prep5 ['psbt' ]), normal_feerate_perkw )
448459 l1 .rpc .txdiscard (prep5 ['txid' ])
449460 with pytest .raises (RpcError , match = r"'all'" ):
450461 prep5 = l1 .rpc .txprepare ([{addr : 'all' }, {addr : 'all' }])
451462
452463 prep5 = l1 .rpc .txprepare ([{addr : Millisatoshi (amount * 3 * 500 + 100000 )},
453464 {addr : Millisatoshi (amount * 3 * 500 - 100000 )}])
454465 # Feerate should be ~ as we asked for
455- assert normal_feerate_perkw - 2 < feerate_from_psbt (bitcoind , l1 , prep5 ['psbt' ]) < normal_feerate_perkw + 2
466+ check_feerate ( l1 , feerate_from_psbt (bitcoind , l1 , prep5 ['psbt' ]), normal_feerate_perkw )
456467 decode = bitcoind .rpc .decoderawtransaction (prep5 ['unsigned_tx' ])
457468 assert decode ['txid' ] == prep5 ['txid' ]
458469 # 4 inputs, 3 outputs(include change).
@@ -484,7 +495,8 @@ def test_txprepare(node_factory, bitcoind, chainparams):
484495
485496def test_txprepare_feerate (node_factory , bitcoind , chainparams ):
486497 # Make sure it works at different feerates!
487- l1 , l2 = node_factory .get_nodes (2 )
498+ l1 , l2 = node_factory .get_nodes (2 , opts = {'dev-warn-on-overgrind' : None ,
499+ 'broken_log' : 'overgrind: short signature length' })
488500
489501 # Add some funds to withdraw later
490502 for i in range (20 ):
@@ -505,7 +517,7 @@ def test_txprepare_feerate(node_factory, bitcoind, chainparams):
505517 fee_output = 1
506518 else :
507519 fee_output = 0
508- if len (bitcoind .rpc .decoderawtransaction (prep ['unsigned_tx' ])['vout' ]) == 1 + 1 + fee_output :
520+ if len (bitcoind .rpc .decoderawtransaction (prep ['unsigned_tx' ])['vout' ]) == 1 + 1 + fee_output and not did_short_sig ( l1 ) :
509521 assert actual_feerate < feerate + 2
510522 l1 .rpc .txdiscard (prep ['txid' ])
511523
@@ -561,8 +573,7 @@ def test_fundpsbt_feerates(node_factory, bitcoind, chainparams, addrtype):
561573 # We never actually added that `amount` output to PSBT, so that appears as "fee"
562574 fee = int (txinfo ['fees' ]['base' ] * 100_000_000 ) - amount
563575 actual_feerate = fee / (txinfo ['weight' ] / 1000 )
564- # Out by one errors (due to rounding) change feerate by 2.
565- assert feerate - 2 < actual_feerate < feerate + 2
576+ check_feerate (l1 , actual_feerate , feerate )
566577
567578
568579def test_reserveinputs (node_factory , bitcoind , chainparams ):
0 commit comments