test: Make local symbols in run_test members

This commit is contained in:
MarcoFalke 2019-06-26 12:42:11 -04:00
parent 2f717fb5cd
commit fa6fba3bc8
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -41,11 +41,11 @@ class RawTransactionsTest(BitcoinTestFramework):
connect_nodes_bi(self.nodes, 0, 3) connect_nodes_bi(self.nodes, 0, 3)
def run_test(self): def run_test(self):
min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee'] self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
# This test is not meant to test fee estimation and we'd like # This test is not meant to test fee estimation and we'd like
# to be sure all txs are sent at a consistent desired feerate # to be sure all txs are sent at a consistent desired feerate
for node in self.nodes: for node in self.nodes:
node.settxfee(min_relay_tx_fee) node.settxfee(self.min_relay_tx_fee)
# if the fee's positive delta is higher than this value tests will fail, # if the fee's positive delta is higher than this value tests will fail,
# neg. delta always fail the tests. # neg. delta always fail the tests.
@ -53,7 +53,7 @@ class RawTransactionsTest(BitcoinTestFramework):
# than a minimum sized signature. # than a minimum sized signature.
# = 2 bytes * minRelayTxFeePerByte # = 2 bytes * minRelayTxFeePerByte
feeTolerance = 2 * min_relay_tx_fee/1000 self.fee_tolerance = 2 * self.min_relay_tx_fee / 1000
self.nodes[2].generate(1) self.nodes[2].generate(1)
self.sync_all() self.sync_all()
@ -67,15 +67,15 @@ class RawTransactionsTest(BitcoinTestFramework):
watchonly_address = self.nodes[0].getnewaddress() watchonly_address = self.nodes[0].getnewaddress()
watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"] watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"]
watchonly_amount = Decimal(200) self.watchonly_amount = Decimal(200)
self.nodes[3].importpubkey(watchonly_pubkey, "", True) self.nodes[3].importpubkey(watchonly_pubkey, "", True)
watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, watchonly_amount) self.watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, self.watchonly_amount)
# Lock UTXO so nodes[0] doesn't accidentally spend it # Lock UTXO so nodes[0] doesn't accidentally spend it
watchonly_vout = find_vout_for_address(self.nodes[0], watchonly_txid, watchonly_address) self.watchonly_vout = find_vout_for_address(self.nodes[0], self.watchonly_txid, watchonly_address)
self.nodes[0].lockunspent(False, [{"txid": watchonly_txid, "vout": watchonly_vout}]) self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}])
self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), watchonly_amount / 10) self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), self.watchonly_amount / 10)
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5)
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
@ -156,6 +156,7 @@ class RawTransactionsTest(BitcoinTestFramework):
rawtxfund = self.nodes[2].fundrawtransaction(rawtx) rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
fee = rawtxfund['fee'] fee = rawtxfund['fee']
self.test_no_change_fee = fee # Use the same fee for the next tx
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
totalOut = 0 totalOut = 0
for out in dec_tx['vout']: for out in dec_tx['vout']:
@ -170,7 +171,7 @@ class RawTransactionsTest(BitcoinTestFramework):
utx = get_unspent(self.nodes[2].listunspent(), 5) utx = get_unspent(self.nodes[2].listunspent(), 5)
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}] inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
outputs = { self.nodes[0].getnewaddress() : Decimal(5.0) - fee - feeTolerance } outputs = {self.nodes[0].getnewaddress(): Decimal(5.0) - self.test_no_change_fee - self.fee_tolerance}
rawtx = self.nodes[2].createrawtransaction(inputs, outputs) rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
dec_tx = self.nodes[2].decoderawtransaction(rawtx) dec_tx = self.nodes[2].decoderawtransaction(rawtx)
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
@ -363,7 +364,7 @@ class RawTransactionsTest(BitcoinTestFramework):
#compare fee #compare fee
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
assert feeDelta >= 0 and feeDelta <= feeTolerance assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
############################################################ ############################################################
############################################################ ############################################################
@ -378,7 +379,7 @@ class RawTransactionsTest(BitcoinTestFramework):
#compare fee #compare fee
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
assert feeDelta >= 0 and feeDelta <= feeTolerance assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
############################################################ ############################################################
@ -405,7 +406,7 @@ class RawTransactionsTest(BitcoinTestFramework):
#compare fee #compare fee
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
assert feeDelta >= 0 and feeDelta <= feeTolerance assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
############################################################ ############################################################
@ -438,7 +439,7 @@ class RawTransactionsTest(BitcoinTestFramework):
#compare fee #compare fee
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
assert feeDelta >= 0 and feeDelta <= feeTolerance assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
############################################################ ############################################################
@ -485,7 +486,7 @@ class RawTransactionsTest(BitcoinTestFramework):
# This test is not meant to test fee estimation and we'd like # This test is not meant to test fee estimation and we'd like
# to be sure all txs are sent at a consistent desired feerate # to be sure all txs are sent at a consistent desired feerate
for node in self.nodes: for node in self.nodes:
node.settxfee(min_relay_tx_fee) node.settxfee(self.min_relay_tx_fee)
connect_nodes_bi(self.nodes,0,1) connect_nodes_bi(self.nodes,0,1)
connect_nodes_bi(self.nodes,1,2) connect_nodes_bi(self.nodes,1,2)
@ -493,7 +494,7 @@ class RawTransactionsTest(BitcoinTestFramework):
connect_nodes_bi(self.nodes,0,3) connect_nodes_bi(self.nodes,0,3)
# Again lock the watchonly UTXO or nodes[0] may spend it, because # Again lock the watchonly UTXO or nodes[0] may spend it, because
# lockunspent is memory-only and thus lost on restart # lockunspent is memory-only and thus lost on restart
self.nodes[0].lockunspent(False, [{"txid": watchonly_txid, "vout": watchonly_vout}]) self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}])
self.sync_all() self.sync_all()
# drain the keypool # drain the keypool
@ -558,7 +559,7 @@ class RawTransactionsTest(BitcoinTestFramework):
#compare fee #compare fee
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
assert feeDelta >= 0 and feeDelta <= feeTolerance*19 #~19 inputs assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 #~19 inputs
############################################# #############################################
@ -612,13 +613,13 @@ class RawTransactionsTest(BitcoinTestFramework):
################################################## ##################################################
inputs = [] inputs = []
outputs = {self.nodes[2].getnewaddress() : watchonly_amount / 2} outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2}
rawtx = self.nodes[3].createrawtransaction(inputs, outputs) rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True }) result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True })
res_dec = self.nodes[0].decoderawtransaction(result["hex"]) res_dec = self.nodes[0].decoderawtransaction(result["hex"])
assert_equal(len(res_dec["vin"]), 1) assert_equal(len(res_dec["vin"]), 1)
assert_equal(res_dec["vin"][0]["txid"], watchonly_txid) assert_equal(res_dec["vin"][0]["txid"], self.watchonly_txid)
assert "fee" in result.keys() assert "fee" in result.keys()
assert_greater_than(result["changepos"], -1) assert_greater_than(result["changepos"], -1)
@ -628,18 +629,18 @@ class RawTransactionsTest(BitcoinTestFramework):
############################################################### ###############################################################
inputs = [] inputs = []
outputs = {self.nodes[2].getnewaddress() : watchonly_amount} outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount}
rawtx = self.nodes[3].createrawtransaction(inputs, outputs) rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
# Backward compatibility test (2nd param is includeWatching) # Backward compatibility test (2nd param is includeWatching)
result = self.nodes[3].fundrawtransaction(rawtx, True) result = self.nodes[3].fundrawtransaction(rawtx, True)
res_dec = self.nodes[0].decoderawtransaction(result["hex"]) res_dec = self.nodes[0].decoderawtransaction(result["hex"])
assert_equal(len(res_dec["vin"]), 2) assert_equal(len(res_dec["vin"]), 2)
assert res_dec["vin"][0]["txid"] == watchonly_txid or res_dec["vin"][1]["txid"] == watchonly_txid assert res_dec["vin"][0]["txid"] == self.watchonly_txid or res_dec["vin"][1]["txid"] == self.watchonly_txid
assert_greater_than(result["fee"], 0) assert_greater_than(result["fee"], 0)
assert_greater_than(result["changepos"], -1) assert_greater_than(result["changepos"], -1)
assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], watchonly_amount / 10) assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], self.watchonly_amount / 10)
signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"]) signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"])
assert not signedtx["complete"] assert not signedtx["complete"]
@ -659,9 +660,9 @@ class RawTransactionsTest(BitcoinTestFramework):
inputs = [] inputs = []
outputs = {self.nodes[3].getnewaddress() : 1} outputs = {self.nodes[3].getnewaddress() : 1}
rawtx = self.nodes[3].createrawtransaction(inputs, outputs) rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
result = self.nodes[3].fundrawtransaction(rawtx) # uses min_relay_tx_fee (set by settxfee) result = self.nodes[3].fundrawtransaction(rawtx) # uses self.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 * self.min_relay_tx_fee})
result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10*min_relay_tx_fee}) result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10 * self.min_relay_tx_fee})
assert_raises_rpc_error(-4, "Fee exceeds maximum configured by -maxtxfee", self.nodes[3].fundrawtransaction, rawtx, {"feeRate": 1}) assert_raises_rpc_error(-4, "Fee exceeds maximum configured by -maxtxfee", self.nodes[3].fundrawtransaction, rawtx, {"feeRate": 1})
result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex']) result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate) assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
@ -671,6 +672,7 @@ class RawTransactionsTest(BitcoinTestFramework):
# Test no address reuse occurs # # Test no address reuse occurs #
################################ ################################
rawtx = self.nodes[3].createrawtransaction(inputs=[], outputs={self.nodes[3].getnewaddress(): 1})
result3 = self.nodes[3].fundrawtransaction(rawtx) result3 = self.nodes[3].fundrawtransaction(rawtx)
res_dec = self.nodes[0].decoderawtransaction(result3["hex"]) res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
changeaddress = "" changeaddress = ""
@ -693,11 +695,11 @@ class RawTransactionsTest(BitcoinTestFramework):
outputs = {self.nodes[2].getnewaddress(): 1} outputs = {self.nodes[2].getnewaddress(): 1}
rawtx = self.nodes[3].createrawtransaction(inputs, outputs) rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
result = [self.nodes[3].fundrawtransaction(rawtx), # uses min_relay_tx_fee (set by settxfee) result = [self.nodes[3].fundrawtransaction(rawtx), # uses self.min_relay_tx_fee (set by settxfee)
self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list
self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee) self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0]}), # uses self.min_relay_tx_fee (set by settxfee)
self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee}), self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * self.min_relay_tx_fee}),
self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2*min_relay_tx_fee, "subtractFeeFromOutputs": [0]})] self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * self.min_relay_tx_fee, "subtractFeeFromOutputs": [0]}),]
dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) for tx_ in result] dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) for tx_ in result]
output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)] output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]