test: Migrate maxuploadtarget.py to Python 3

This commit is contained in:
Jack Grigg 2020-03-07 10:51:11 +13:00 committed by Kris Nuttycombe
parent 69ca2dde56
commit 2f24abe457
1 changed files with 17 additions and 17 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Distributed under the MIT/X11 software license, see the accompanying # Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -90,11 +90,11 @@ class MaxUploadTest(BitcoinTestFramework):
# So we have big transactions and full blocks to fill up our block files # So we have big transactions and full blocks to fill up our block files
# create one script_pubkey # create one script_pubkey
script_pubkey = "6a4d0200" #OP_RETURN OP_PUSH2 512 bytes script_pubkey = "6a4d0200" #OP_RETURN OP_PUSH2 512 bytes
for i in xrange (512): for _ in range(512):
script_pubkey = script_pubkey + "01" script_pubkey = script_pubkey + "01"
# concatenate 128 txouts of above script_pubkey which we'll insert before the txout for change # concatenate 128 txouts of above script_pubkey which we'll insert before the txout for change
self.txouts = "81" self.txouts = "81"
for k in xrange(128): for _ in range(128):
# add txout value # add txout value
self.txouts = self.txouts + "0000000000000000" self.txouts = self.txouts + "0000000000000000"
# add length of script_pubkey # add length of script_pubkey
@ -118,7 +118,7 @@ class MaxUploadTest(BitcoinTestFramework):
def mine_full_block(self, node, address): def mine_full_block(self, node, address):
# Want to create a full block # Want to create a full block
# We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit # We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit
for j in xrange(14): for _ in range(14):
if len(self.utxo) < 14: if len(self.utxo) < 14:
self.utxo = node.listunspent() self.utxo = node.listunspent()
inputs=[] inputs=[]
@ -156,7 +156,7 @@ class MaxUploadTest(BitcoinTestFramework):
test_nodes = [] test_nodes = []
connections = [] connections = []
for i in xrange(3): for i in range(3):
test_nodes.append(TestNode()) test_nodes.append(TestNode())
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i])) connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
test_nodes[i].add_connection(connections[i]) test_nodes[i].add_connection(connections[i])
@ -198,7 +198,7 @@ class MaxUploadTest(BitcoinTestFramework):
# 144MB will be reserved for relaying new blocks, so expect this to # 144MB will be reserved for relaying new blocks, so expect this to
# succeed for ~70 tries. # succeed for ~70 tries.
for i in xrange(success_count): for i in range(int(success_count)):
test_nodes[0].send_message(getdata_request) test_nodes[0].send_message(getdata_request)
test_nodes[0].sync_with_ping() test_nodes[0].sync_with_ping()
assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1) assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1)
@ -206,22 +206,22 @@ class MaxUploadTest(BitcoinTestFramework):
assert_equal(len(self.nodes[0].getpeerinfo()), 3) assert_equal(len(self.nodes[0].getpeerinfo()), 3)
# At most a couple more tries should succeed (depending on how long # At most a couple more tries should succeed (depending on how long
# the test has been running so far). # the test has been running so far).
for i in xrange(3): for i in range(3):
test_nodes[0].send_message(getdata_request) test_nodes[0].send_message(getdata_request)
test_nodes[0].wait_for_disconnect() test_nodes[0].wait_for_disconnect()
assert_equal(len(self.nodes[0].getpeerinfo()), 2) assert_equal(len(self.nodes[0].getpeerinfo()), 2)
print "Peer 0 disconnected after downloading old block too many times" print("Peer 0 disconnected after downloading old block too many times")
# Requesting the current block on test_nodes[1] should succeed indefinitely, # Requesting the current block on test_nodes[1] should succeed indefinitely,
# even when over the max upload target. # even when over the max upload target.
# We'll try 200 times # We'll try 200 times
getdata_request.inv = [CInv(2, big_new_block)] getdata_request.inv = [CInv(2, big_new_block)]
for i in xrange(200): for i in range(200):
test_nodes[1].send_message(getdata_request) test_nodes[1].send_message(getdata_request)
test_nodes[1].sync_with_ping() test_nodes[1].sync_with_ping()
assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1) assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
print "Peer 1 able to repeatedly download new block" print("Peer 1 able to repeatedly download new block")
# But if test_nodes[1] tries for an old block, it gets disconnected too. # But if test_nodes[1] tries for an old block, it gets disconnected too.
getdata_request.inv = [CInv(2, big_old_block)] getdata_request.inv = [CInv(2, big_old_block)]
@ -229,9 +229,9 @@ class MaxUploadTest(BitcoinTestFramework):
test_nodes[1].wait_for_disconnect() test_nodes[1].wait_for_disconnect()
assert_equal(len(self.nodes[0].getpeerinfo()), 1) assert_equal(len(self.nodes[0].getpeerinfo()), 1)
print "Peer 1 disconnected after trying to download old block" print("Peer 1 disconnected after trying to download old block")
print "Advancing system time on node to clear counters..." print("Advancing system time on node to clear counters...")
# If we advance the time by 24 hours, then the counters should reset, # If we advance the time by 24 hours, then the counters should reset,
# and test_nodes[2] should be able to retrieve the old block. # and test_nodes[2] should be able to retrieve the old block.
@ -241,12 +241,12 @@ class MaxUploadTest(BitcoinTestFramework):
test_nodes[2].sync_with_ping() test_nodes[2].sync_with_ping()
assert_equal(test_nodes[2].block_receive_map[big_old_block], 1) assert_equal(test_nodes[2].block_receive_map[big_old_block], 1)
print "Peer 2 able to download old block" print("Peer 2 able to download old block")
[c.disconnect_node() for c in connections] [c.disconnect_node() for c in connections]
#stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1 #stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
print "Restarting nodes with -whitelist=127.0.0.1" print("Restarting nodes with -whitelist=127.0.0.1")
stop_node(self.nodes[0], 0) stop_node(self.nodes[0], 0)
self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"]) self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
@ -254,7 +254,7 @@ class MaxUploadTest(BitcoinTestFramework):
test_nodes = [] test_nodes = []
connections = [] connections = []
for i in xrange(3): for i in range(3):
test_nodes.append(TestNode()) test_nodes.append(TestNode())
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i])) connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
test_nodes[i].add_connection(connections[i]) test_nodes[i].add_connection(connections[i])
@ -264,7 +264,7 @@ class MaxUploadTest(BitcoinTestFramework):
#retrieve 20 blocks which should be enough to break the 1MB limit #retrieve 20 blocks which should be enough to break the 1MB limit
getdata_request.inv = [CInv(2, big_new_block)] getdata_request.inv = [CInv(2, big_new_block)]
for i in xrange(20): for i in range(20):
test_nodes[1].send_message(getdata_request) test_nodes[1].send_message(getdata_request)
test_nodes[1].sync_with_ping() test_nodes[1].sync_with_ping()
assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1) assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
@ -274,7 +274,7 @@ class MaxUploadTest(BitcoinTestFramework):
test_nodes[1].wait_for_disconnect() test_nodes[1].wait_for_disconnect()
assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist
print "Peer 1 still connected after trying to download old block (whitelisted)" print("Peer 1 still connected after trying to download old block (whitelisted)")
[c.disconnect_node() for c in connections] [c.disconnect_node() for c in connections]