Skip to content

Commit

Permalink
Auto merge of #4074 - zancas:3802_py2_to_py3, r=<try>
Browse files Browse the repository at this point in the history
WIP: Migrate tests from py2 to py3

When executed with the `zcash/qa/pull-tester/rpc-tests.sh` script the Python code in the `zcash/qa/rpc-tests` directory, and subdirectories, runs under Python 3.5.3.

I tried to _only_ make changes that were necessary for this goal, and nothing else (like causing `BitcoinTestFramework` to inherit from `unittest.TestCase`, deduplicating repeated code, replacing bespoke components with standard solutions, etc.)

I've begun collecting ideas related to such potential improvements here:

#4076  <- (Note: This "improvement list" is, itself, much in need of improvement.)
  • Loading branch information
zkbot committed Feb 7, 2020
2 parents e93586a + 74974eb commit 2bcd96c
Show file tree
Hide file tree
Showing 104 changed files with 971 additions and 1,054 deletions.
19 changes: 10 additions & 9 deletions qa/rpc-tests/addressindex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright (c) 2019 The Zcash developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

#
# Test addressindex generation and fetching for insightexplorer
#
Expand All @@ -12,11 +13,11 @@
# getaddressdeltas
# getaddressutxos
# getaddressmempool

import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
#

from test_framework.test_framework import BitcoinTestFramework


from test_framework.util import (
assert_equal,
initialize_chain_clean,
Expand All @@ -40,7 +41,7 @@
CTxIn, CTxOut, COutPoint,
)

from binascii import hexlify
from binascii import hexlify, unhexlify


class AddressIndexTest(BitcoinTestFramework):
Expand Down Expand Up @@ -252,7 +253,7 @@ def check_balance(node_index, address, expected_balance, expected_received=None)

# Ensure the change from that transaction appears
tx = self.nodes[0].getrawtransaction(txid, 1)
change_vout = filter(lambda v: v['valueZat'] != 3 * COIN, tx['vout'])
change_vout = [v for v in tx['vout'] if v['valueZat'] != 3 * COIN]
change = change_vout[0]['scriptPubKey']['addresses'][0]
bal = self.nodes[2].getaddressbalance(change)
assert(bal['received'] > 0)
Expand Down Expand Up @@ -323,7 +324,7 @@ def check_balance(node_index, address, expected_balance, expected_received=None)
# so for comparison, remove the 4 (and -4 for output) from the
# deltas list
deltas = self.nodes[1].getaddressdeltas({'addresses': [addr1]})
deltas = filter(lambda d: abs(d['satoshis']) != 4 * COIN, deltas)
deltas = [d for d in deltas if abs(d['satoshis']) != 4 * COIN]
assert_equal(len(utxos), len(deltas))
for i in range(len(utxos)):
assert_equal(utxos[i]['address'], addr1)
Expand All @@ -334,13 +335,13 @@ def check_balance(node_index, address, expected_balance, expected_received=None)
# Check that outputs with the same address in the same tx return one txid
# (can't use createrawtransaction() as it combines duplicate addresses)
addr = "t2LMJ6Arw9UWBMWvfUr2QLHM4Xd9w53FftS"
addressHash = "97643ce74b188f4fb6bbbb285e067a969041caf2".decode('hex')
addressHash = unhexlify("97643ce74b188f4fb6bbbb285e067a969041caf2")
scriptPubKey = CScript([OP_HASH160, addressHash, OP_EQUAL])
# Add an unrecognized script type to vout[], a legal script that pays,
# but won't modify the addressindex (since the address can't be extracted).
# (This extra output has no effect on the rest of the test.)
scriptUnknown = CScript([OP_HASH160, OP_DUP, OP_DROP, addressHash, OP_EQUAL])
unspent = filter(lambda u: u['amount'] >= 4, self.nodes[0].listunspent())
unspent = [u for u in self.nodes[0].listunspent() if u['amount'] >= 4]
tx = CTransaction()
tx.vin = [CTxIn(COutPoint(int(unspent[0]['txid'], 16), unspent[0]['vout']))]
tx.vout = [
Expand Down
18 changes: 9 additions & 9 deletions qa/rpc-tests/bip65-cltv-p2p.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
#

import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."


from test_framework.test_framework import ComparisonTestFramework
from test_framework.util import start_nodes
Expand All @@ -13,7 +13,7 @@
from test_framework.comptool import TestInstance, TestManager
from test_framework.script import CScript, OP_1NEGATE, OP_NOP2, OP_DROP
from binascii import unhexlify
import cStringIO
import io


'''
Expand All @@ -38,10 +38,10 @@ def setup_network(self):
self.is_network_split = False

def run_test(self):
test = TestManager(self, self.options.tmpdir)
test.add_all_connections(self.nodes)
test_manager = TestManager(self, self.options.tmpdir)
test_manager.add_all_connections(self.nodes)
NetworkThread().start() # Start up network handling in another thread
test.run()
test_manager.run()

def create_transaction(self, node, coinbase, to_address, amount):
from_txid = node.getblock(coinbase)['tx'][0]
Expand All @@ -50,7 +50,7 @@ def create_transaction(self, node, coinbase, to_address, amount):
rawtx = node.createrawtransaction(inputs, outputs)
signresult = node.signrawtransaction(rawtx)
tx = CTransaction()
f = cStringIO.StringIO(unhexlify(signresult['hex']))
f = io.BytesIO(unhexlify(signresult['hex']))
tx.deserialize(f)
return tx

Expand All @@ -67,8 +67,8 @@ def get_tests(self):
self.coinbase_blocks = self.nodes[0].generate(1)
self.nodes[0].generate(100)
hashTip = self.nodes[0].getbestblockhash()
hashFinalSaplingRoot = int("0x" + self.nodes[0].getblock(hashTip)['finalsaplingroot'] + "L", 0)
self.tip = int ("0x" + hashTip + "L", 0)
hashFinalSaplingRoot = int("0x" + self.nodes[0].getblock(hashTip)['finalsaplingroot'], 0)
self.tip = int ("0x" + hashTip, 0)
self.nodeaddress = self.nodes[0].getnewaddress()

'''Check that the rules are enforced.'''
Expand Down
12 changes: 6 additions & 6 deletions qa/rpc-tests/bipdersig-p2p.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
#

import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."


from test_framework.test_framework import ComparisonTestFramework
from test_framework.util import start_nodes
Expand All @@ -13,7 +13,7 @@
from test_framework.comptool import TestInstance, TestManager
from test_framework.script import CScript
from binascii import unhexlify
import cStringIO
import io


'''
Expand Down Expand Up @@ -49,7 +49,7 @@ def create_transaction(self, node, coinbase, to_address, amount):
rawtx = node.createrawtransaction(inputs, outputs)
signresult = node.signrawtransaction(rawtx)
tx = CTransaction()
f = cStringIO.StringIO(unhexlify(signresult['hex']))
f = io.BytesIO(unhexlify(signresult['hex']))
tx.deserialize(f)
return tx

Expand All @@ -74,8 +74,8 @@ def get_tests(self):
self.coinbase_blocks = self.nodes[0].generate(1)
self.nodes[0].generate(100)
hashTip = self.nodes[0].getbestblockhash()
hashFinalSaplingRoot = int("0x" + self.nodes[0].getblock(hashTip)['finalsaplingroot'] + "L", 0)
self.tip = int ("0x" + hashTip + "L", 0)
hashFinalSaplingRoot = int("0x" + self.nodes[0].getblock(hashTip)['finalsaplingroot'], 0)
self.tip = int ("0x" + hashTip, 0)
self.nodeaddress = self.nodes[0].getnewaddress()

'''Check that the rules are enforced.'''
Expand Down
18 changes: 9 additions & 9 deletions qa/rpc-tests/blockchain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Copyright (c) 2014 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
Expand All @@ -8,7 +8,7 @@
# rpc/blockchain.cpp.
#

import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."


import decimal

Expand Down Expand Up @@ -42,13 +42,13 @@ def run_test(self):
node = self.nodes[0]
res = node.gettxoutsetinfo()

assert_equal(res[u'total_amount'], decimal.Decimal('2181.25000000')) # 150*12.5 + 49*6.25
assert_equal(res[u'transactions'], 200)
assert_equal(res[u'height'], 200)
assert_equal(res[u'txouts'], 349) # 150*2 + 49
assert_equal(res[u'bytes_serialized'], 14951), # 32*199 + 48*90 + 49*60 + 27*49
assert_equal(len(res[u'bestblock']), 64)
assert_equal(len(res[u'hash_serialized']), 64)
assert_equal(res['total_amount'], decimal.Decimal('2181.25000000')) # 150*12.5 + 49*6.25
assert_equal(res['transactions'], 200)
assert_equal(res['height'], 200)
assert_equal(res['txouts'], 349) # 150*2 + 49
assert_equal(res['bytes_serialized'], 14951), # 32*199 + 48*90 + 49*60 + 27*49
assert_equal(len(res['bestblock']), 64)
assert_equal(len(res['hash_serialized']), 64)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 2bcd96c

Please sign in to comment.