Skip to content

Commit 36b7400

Browse files
committed
Merge #8201: [qa] fundrawtransaction: Fix race, assert amounts
fae1d06 [qa] fundrawtransaction: Fix race, assert amounts (MarcoFalke) fa26c42 [qa] util: Move check_fee_amount out of wallet.py (MarcoFalke)
2 parents be9711e + fae1d06 commit 36b7400

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

qa/rpc-tests/fundrawtransaction.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def run_test(self):
5858
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
5959
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0)
6060

61-
self.sync_all()
6261
self.nodes[0].generate(1)
6362
self.sync_all()
6463

@@ -552,7 +551,6 @@ def run_test(self):
552551
self.nodes[1].walletpassphrase("test", 100)
553552
signedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
554553
txId = self.nodes[1].sendrawtransaction(signedTx['hex'])
555-
self.sync_all()
556554
self.nodes[1].generate(1)
557555
self.sync_all()
558556

@@ -572,7 +570,6 @@ def run_test(self):
572570

573571
for i in range(0,20):
574572
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
575-
self.sync_all()
576573
self.nodes[0].generate(1)
577574
self.sync_all()
578575

@@ -603,7 +600,6 @@ def run_test(self):
603600

604601
for i in range(0,20):
605602
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
606-
self.sync_all()
607603
self.nodes[0].generate(1)
608604
self.sync_all()
609605

@@ -677,15 +673,25 @@ def run_test(self):
677673
signedtx = self.nodes[0].signrawtransaction(signedtx["hex"])
678674
assert(signedtx["complete"])
679675
self.nodes[0].sendrawtransaction(signedtx["hex"])
676+
self.nodes[0].generate(1)
677+
self.sync_all()
678+
679+
#######################
680+
# Test feeRate option #
681+
#######################
682+
683+
# Make sure there is exactly one input so coin selection can't skew the result
684+
assert_equal(len(self.nodes[3].listunspent(1)), 1)
680685

681686
inputs = []
682687
outputs = {self.nodes[2].getnewaddress() : 1}
683688
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
684689
result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee)
685690
result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee})
686691
result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee})
687-
assert_equal(result['fee']*2, result2['fee'])
688-
assert_equal(result['fee']*10, result3['fee'])
692+
result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
693+
assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
694+
assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
689695

690696
if __name__ == '__main__':
691697
RawTransactionsTest().main()

qa/rpc-tests/test_framework/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,15 @@ def random_transaction(nodes, amount, min_fee, fee_increment, fee_variants):
477477

478478
return (txid, signresult["hex"], fee)
479479

480+
def assert_fee_amount(fee, tx_size, fee_per_kB):
481+
"""Assert the fee was in range"""
482+
target_fee = tx_size * fee_per_kB / 1000
483+
if fee < target_fee:
484+
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)"%(str(fee), str(target_fee)))
485+
# allow the wallet's estimation to be at most 2 bytes off
486+
if fee > (tx_size + 2) * fee_per_kB / 1000:
487+
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)"%(str(fee), str(target_fee)))
488+
480489
def assert_equal(thing1, thing2):
481490
if thing1 != thing2:
482491
raise AssertionError("%s != %s"%(str(thing1),str(thing2)))

qa/rpc-tests/wallet.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ class WalletTest (BitcoinTestFramework):
1111
def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size):
1212
"""Return curr_balance after asserting the fee was in range"""
1313
fee = balance_with_fee - curr_balance
14-
target_fee = fee_per_byte * tx_size
15-
if fee < target_fee:
16-
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)"%(str(fee), str(target_fee)))
17-
# allow the node's estimation to be at most 2 bytes off
18-
if fee > fee_per_byte * (tx_size + 2):
19-
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)"%(str(fee), str(target_fee)))
14+
assert_fee_amount(fee, tx_size, fee_per_byte * 1000)
2015
return curr_balance
2116

2217
def __init__(self):

0 commit comments

Comments
 (0)