Browse Source

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)
0.13
Wladimir J. van der Laan 9 years ago
parent
commit
36b74002f8
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 18
      qa/rpc-tests/fundrawtransaction.py
  2. 9
      qa/rpc-tests/test_framework/util.py
  3. 7
      qa/rpc-tests/wallet.py

18
qa/rpc-tests/fundrawtransaction.py

@ -58,7 +58,6 @@ class RawTransactionsTest(BitcoinTestFramework):
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0)
self.sync_all()
self.nodes[0].generate(1) self.nodes[0].generate(1)
self.sync_all() self.sync_all()
@ -552,7 +551,6 @@ class RawTransactionsTest(BitcoinTestFramework):
self.nodes[1].walletpassphrase("test", 100) self.nodes[1].walletpassphrase("test", 100)
signedTx = self.nodes[1].signrawtransaction(fundedTx['hex']) signedTx = self.nodes[1].signrawtransaction(fundedTx['hex'])
txId = self.nodes[1].sendrawtransaction(signedTx['hex']) txId = self.nodes[1].sendrawtransaction(signedTx['hex'])
self.sync_all()
self.nodes[1].generate(1) self.nodes[1].generate(1)
self.sync_all() self.sync_all()
@ -572,7 +570,6 @@ class RawTransactionsTest(BitcoinTestFramework):
for i in range(0,20): for i in range(0,20):
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
self.sync_all()
self.nodes[0].generate(1) self.nodes[0].generate(1)
self.sync_all() self.sync_all()
@ -603,7 +600,6 @@ class RawTransactionsTest(BitcoinTestFramework):
for i in range(0,20): for i in range(0,20):
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
self.sync_all()
self.nodes[0].generate(1) self.nodes[0].generate(1)
self.sync_all() self.sync_all()
@ -677,6 +673,15 @@ class RawTransactionsTest(BitcoinTestFramework):
signedtx = self.nodes[0].signrawtransaction(signedtx["hex"]) signedtx = self.nodes[0].signrawtransaction(signedtx["hex"])
assert(signedtx["complete"]) assert(signedtx["complete"])
self.nodes[0].sendrawtransaction(signedtx["hex"]) self.nodes[0].sendrawtransaction(signedtx["hex"])
self.nodes[0].generate(1)
self.sync_all()
#######################
# Test feeRate option #
#######################
# Make sure there is exactly one input so coin selection can't skew the result
assert_equal(len(self.nodes[3].listunspent(1)), 1)
inputs = [] inputs = []
outputs = {self.nodes[2].getnewaddress() : 1} outputs = {self.nodes[2].getnewaddress() : 1}
@ -684,8 +689,9 @@ class RawTransactionsTest(BitcoinTestFramework):
result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee) result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee)
result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee}) result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee})
result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee}) result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee})
assert_equal(result['fee']*2, result2['fee']) result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
assert_equal(result['fee']*10, result3['fee']) assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
if __name__ == '__main__': if __name__ == '__main__':
RawTransactionsTest().main() RawTransactionsTest().main()

9
qa/rpc-tests/test_framework/util.py

@ -477,6 +477,15 @@ def random_transaction(nodes, amount, min_fee, fee_increment, fee_variants):
return (txid, signresult["hex"], fee) return (txid, signresult["hex"], fee)
def assert_fee_amount(fee, tx_size, fee_per_kB):
"""Assert the fee was in range"""
target_fee = tx_size * fee_per_kB / 1000
if fee < target_fee:
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)"%(str(fee), str(target_fee)))
# allow the wallet's estimation to be at most 2 bytes off
if fee > (tx_size + 2) * fee_per_kB / 1000:
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)"%(str(fee), str(target_fee)))
def assert_equal(thing1, thing2): def assert_equal(thing1, thing2):
if thing1 != thing2: if thing1 != thing2:
raise AssertionError("%s != %s"%(str(thing1),str(thing2))) raise AssertionError("%s != %s"%(str(thing1),str(thing2)))

7
qa/rpc-tests/wallet.py

@ -11,12 +11,7 @@ class WalletTest (BitcoinTestFramework):
def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size): def check_fee_amount(self, curr_balance, balance_with_fee, fee_per_byte, tx_size):
"""Return curr_balance after asserting the fee was in range""" """Return curr_balance after asserting the fee was in range"""
fee = balance_with_fee - curr_balance fee = balance_with_fee - curr_balance
target_fee = fee_per_byte * tx_size assert_fee_amount(fee, tx_size, fee_per_byte * 1000)
if fee < target_fee:
raise AssertionError("Fee of %s BTC too low! (Should be %s BTC)"%(str(fee), str(target_fee)))
# allow the node's estimation to be at most 2 bytes off
if fee > fee_per_byte * (tx_size + 2):
raise AssertionError("Fee of %s BTC too high! (Should be %s BTC)"%(str(fee), str(target_fee)))
return curr_balance return curr_balance
def __init__(self): def __init__(self):

Loading…
Cancel
Save