@@ -300,9 +300,25 @@ def feerate_from_psbt(bitcoind, node, psbt):
300
300
return fee / weight * 1000
301
301
302
302
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
+
303
318
def test_txprepare (node_factory , bitcoind , chainparams ):
304
319
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' )
306
322
addr = chainparams ['example_addr' ]
307
323
308
324
# Add some funds to withdraw later
@@ -322,8 +338,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
322
338
# 4 inputs, 2 outputs (3 if we have a fee output).
323
339
assert len (decode ['vin' ]) == 4
324
340
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 )
327
342
328
343
# One output will be correct.
329
344
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):
351
366
assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 6 ) / 10 ** 8 - Decimal (0.0002 )
352
367
assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
353
368
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 )
356
370
357
371
# If I cancel the first one, I can get those first 4 outputs.
358
372
discard = l1 .rpc .txdiscard (prep ['txid' ])
@@ -371,8 +385,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
371
385
assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 4 ) / 10 ** 8 - Decimal (0.0002 )
372
386
assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
373
387
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 )
376
389
377
390
# Cannot discard twice.
378
391
with pytest .raises (RpcError , match = r'not an unreleased txid' ):
@@ -393,17 +406,15 @@ def test_txprepare(node_factory, bitcoind, chainparams):
393
406
assert decode ['vout' ][0 ]['value' ] > Decimal (amount * 10 ) / 10 ** 8 - Decimal (0.0003 )
394
407
assert decode ['vout' ][0 ]['scriptPubKey' ]['type' ] == 'witness_v0_keyhash'
395
408
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 )
398
410
l1 .rpc .txdiscard (prep4 ['txid' ])
399
411
400
412
# Try passing in a utxo set
401
413
utxos = [utxo ["txid" ] + ":" + str (utxo ["output" ])
402
414
for utxo in l1 .rpc .listfunds ()["outputs" ]][:4 ]
403
415
prep5 = l1 .rpc .txprepare ([{addr :
404
416
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 )
407
418
408
419
# Try passing unconfirmed utxos
409
420
unconfirmed_utxo = l1 .rpc .withdraw (l1 .rpc .newaddr ()["bech32" ], 10 ** 5 )
@@ -414,7 +425,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
414
425
# Feerate should be ~ as we asked for
415
426
unconfirmed_tx = bitcoind .rpc .getrawmempool (True )[unconfirmed_utxo ["txid" ]]
416
427
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 )
418
429
419
430
decode = bitcoind .rpc .decoderawtransaction (prep5 ['unsigned_tx' ])
420
431
assert decode ['txid' ] == prep5 ['txid' ]
@@ -444,15 +455,15 @@ def test_txprepare(node_factory, bitcoind, chainparams):
444
455
prep5 = l1 .rpc .txprepare ([{addr : Millisatoshi (amount * 3 * 1000 )},
445
456
{addr : 'all' }])
446
457
# 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 )
448
459
l1 .rpc .txdiscard (prep5 ['txid' ])
449
460
with pytest .raises (RpcError , match = r"'all'" ):
450
461
prep5 = l1 .rpc .txprepare ([{addr : 'all' }, {addr : 'all' }])
451
462
452
463
prep5 = l1 .rpc .txprepare ([{addr : Millisatoshi (amount * 3 * 500 + 100000 )},
453
464
{addr : Millisatoshi (amount * 3 * 500 - 100000 )}])
454
465
# 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 )
456
467
decode = bitcoind .rpc .decoderawtransaction (prep5 ['unsigned_tx' ])
457
468
assert decode ['txid' ] == prep5 ['txid' ]
458
469
# 4 inputs, 3 outputs(include change).
@@ -484,7 +495,8 @@ def test_txprepare(node_factory, bitcoind, chainparams):
484
495
485
496
def test_txprepare_feerate (node_factory , bitcoind , chainparams ):
486
497
# 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' })
488
500
489
501
# Add some funds to withdraw later
490
502
for i in range (20 ):
@@ -505,7 +517,7 @@ def test_txprepare_feerate(node_factory, bitcoind, chainparams):
505
517
fee_output = 1
506
518
else :
507
519
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 ) :
509
521
assert actual_feerate < feerate + 2
510
522
l1 .rpc .txdiscard (prep ['txid' ])
511
523
@@ -561,8 +573,7 @@ def test_fundpsbt_feerates(node_factory, bitcoind, chainparams, addrtype):
561
573
# We never actually added that `amount` output to PSBT, so that appears as "fee"
562
574
fee = int (txinfo ['fees' ]['base' ] * 100_000_000 ) - amount
563
575
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 )
566
577
567
578
568
579
def test_reserveinputs (node_factory , bitcoind , chainparams ):
0 commit comments