Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_maxuploadtarget.py
Show All 34 Lines | def on_block(self, message): | ||||
self.block_receive_map[message.block.sha256] += 1 | self.block_receive_map[message.block.sha256] += 1 | ||||
class MaxUploadTest(BitcoinTestFramework): | class MaxUploadTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
# Start a node with maxuploadtarget of 200 MB (/24h) | # Start a node with maxuploadtarget of 200 MB (/24h) | ||||
self.extra_args = [[ | self.extra_args = [ | ||||
[ | |||||
"-maxuploadtarget=200", | "-maxuploadtarget=200", | ||||
"-acceptnonstdtxn=1", | "-acceptnonstdtxn=1", | ||||
]] | ] | ||||
] | |||||
self.supports_cli = False | self.supports_cli = False | ||||
# Cache for utxos, as the listunspent may take a long time later in the | # Cache for utxos, as the listunspent may take a long time later in the | ||||
# test | # test | ||||
self.utxo_cache = [] | self.utxo_cache = [] | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
Show All 16 Lines | def run_test(self): | ||||
for _ in range(3): | for _ in range(3): | ||||
p2p_conns.append(self.nodes[0].add_p2p_connection(TestP2PConn())) | p2p_conns.append(self.nodes[0].add_p2p_connection(TestP2PConn())) | ||||
# Now mine a big block | # Now mine a big block | ||||
mine_big_block(self, self.nodes[0], self.utxo_cache) | mine_big_block(self, self.nodes[0], self.utxo_cache) | ||||
# Store the hash; we'll request this later | # Store the hash; we'll request this later | ||||
big_old_block = self.nodes[0].getbestblockhash() | big_old_block = self.nodes[0].getbestblockhash() | ||||
old_block_size = self.nodes[0].getblock(big_old_block, True)['size'] | old_block_size = self.nodes[0].getblock(big_old_block, True)["size"] | ||||
big_old_block = int(big_old_block, 16) | big_old_block = int(big_old_block, 16) | ||||
# Advance to two days ago | # Advance to two days ago | ||||
self.nodes[0].setmocktime(int(time.time()) - 2 * 60 * 60 * 24) | self.nodes[0].setmocktime(int(time.time()) - 2 * 60 * 60 * 24) | ||||
# Mine one more block, so that the prior block looks old | # Mine one more block, so that the prior block looks old | ||||
mine_big_block(self, self.nodes[0], self.utxo_cache) | mine_big_block(self, self.nodes[0], self.utxo_cache) | ||||
Show All 20 Lines | def run_test(self): | ||||
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 _ in range(3): | for _ in range(3): | ||||
p2p_conns[0].send_message(getdata_request) | p2p_conns[0].send_message(getdata_request) | ||||
p2p_conns[0].wait_for_disconnect() | p2p_conns[0].wait_for_disconnect() | ||||
assert_equal(len(self.nodes[0].getpeerinfo()), 2) | assert_equal(len(self.nodes[0].getpeerinfo()), 2) | ||||
self.log.info( | self.log.info("Peer 0 disconnected after downloading old block too many times") | ||||
"Peer 0 disconnected after downloading old block too many times") | |||||
# Requesting the current block on p2p_conns[1] should succeed indefinitely, | # Requesting the current block on p2p_conns[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(MSG_BLOCK, big_new_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)] | ||||
for i in range(200): | for i in range(200): | ||||
p2p_conns[1].send_and_ping(getdata_request) | p2p_conns[1].send_and_ping(getdata_request) | ||||
assert_equal(p2p_conns[1].block_receive_map[big_new_block], i + 1) | assert_equal(p2p_conns[1].block_receive_map[big_new_block], i + 1) | ||||
Show All 17 Lines | def run_test(self): | ||||
p2p_conns[2].sync_with_ping() | p2p_conns[2].sync_with_ping() | ||||
p2p_conns[2].send_and_ping(getdata_request) | p2p_conns[2].send_and_ping(getdata_request) | ||||
assert_equal(p2p_conns[2].block_receive_map[big_old_block], 1) | assert_equal(p2p_conns[2].block_receive_map[big_old_block], 1) | ||||
self.log.info("Peer 2 able to download old block") | self.log.info("Peer 2 able to download old block") | ||||
self.nodes[0].disconnect_p2ps() | self.nodes[0].disconnect_p2ps() | ||||
self.log.info("Restarting node 0 with download permission" | self.log.info( | ||||
" and 1MB maxuploadtarget") | "Restarting node 0 with download permission and 1MB maxuploadtarget" | ||||
self.restart_node(0, ["-whitelist=download@127.0.0.1", | ) | ||||
"-maxuploadtarget=1", "-blockmaxsize=999000"]) | self.restart_node( | ||||
0, | |||||
[ | |||||
"-whitelist=download@127.0.0.1", | |||||
"-maxuploadtarget=1", | |||||
"-blockmaxsize=999000", | |||||
], | |||||
) | |||||
# Reconnect to self.nodes[0] | # Reconnect to self.nodes[0] | ||||
peer = self.nodes[0].add_p2p_connection(TestP2PConn()) | peer = self.nodes[0].add_p2p_connection(TestP2PConn()) | ||||
# 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(MSG_BLOCK, big_new_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)] | ||||
for i in range(20): | for i in range(20): | ||||
peer.send_and_ping(getdata_request) | peer.send_and_ping(getdata_request) | ||||
assert_equal( | assert_equal(peer.block_receive_map[big_new_block], i + 1) | ||||
peer.block_receive_map[big_new_block], i + 1) | |||||
getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)] | ||||
peer.send_and_ping(getdata_request) | peer.send_and_ping(getdata_request) | ||||
self.log.info( | self.log.info( | ||||
"Peer still connected after trying to download old block (download permission)") | "Peer still connected after trying to download old block (download" | ||||
" permission)" | |||||
) | |||||
peer_info = self.nodes[0].getpeerinfo() | peer_info = self.nodes[0].getpeerinfo() | ||||
# node is still connected | # node is still connected | ||||
assert_equal(len(peer_info), 1) | assert_equal(len(peer_info), 1) | ||||
assert_equal(peer_info[0]['permissions'], ['download']) | assert_equal(peer_info[0]["permissions"], ["download"]) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
MaxUploadTest().main() | MaxUploadTest().main() |