Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_maxuploadtarget.py
Show All 10 Lines | |||||
* Verify that the upload counters are reset after 24 hours. | * Verify that the upload counters are reset after 24 hours. | ||||
""" | """ | ||||
from collections import defaultdict | from collections import defaultdict | ||||
import time | import time | ||||
from test_framework.cdefs import LEGACY_MAX_BLOCK_SIZE | from test_framework.cdefs import LEGACY_MAX_BLOCK_SIZE | ||||
from test_framework.blocktools import mine_big_block | from test_framework.blocktools import mine_big_block | ||||
from test_framework.messages import CInv, msg_getdata | from test_framework.messages import CInv, MSG_BLOCK, msg_getdata | ||||
from test_framework.mininode import P2PInterface | from test_framework.mininode import P2PInterface | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import assert_equal | from test_framework.util import assert_equal | ||||
class TestP2PConn(P2PInterface): | class TestP2PConn(P2PInterface): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
# We'll be requesting this new block too | # We'll be requesting this new block too | ||||
big_new_block = self.nodes[0].getbestblockhash() | big_new_block = self.nodes[0].getbestblockhash() | ||||
big_new_block = int(big_new_block, 16) | big_new_block = int(big_new_block, 16) | ||||
# p2p_conns[0] will test what happens if we just keep requesting the | # p2p_conns[0] will test what happens if we just keep requesting the | ||||
# the same big old block too many times (expect: disconnect) | # the same big old block too many times (expect: disconnect) | ||||
getdata_request = msg_getdata() | getdata_request = msg_getdata() | ||||
getdata_request.inv.append(CInv(2, big_old_block)) | getdata_request.inv.append(CInv(MSG_BLOCK, big_old_block)) | ||||
max_bytes_per_day = 200 * 1024 * 1024 | max_bytes_per_day = 200 * 1024 * 1024 | ||||
daily_buffer = 144 * LEGACY_MAX_BLOCK_SIZE | daily_buffer = 144 * LEGACY_MAX_BLOCK_SIZE | ||||
max_bytes_available = max_bytes_per_day - daily_buffer | max_bytes_available = max_bytes_per_day - daily_buffer | ||||
success_count = max_bytes_available // old_block_size | success_count = max_bytes_available // old_block_size | ||||
# 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. | ||||
Show All 9 Lines | def run_test(self): | ||||
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(2, 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) | ||||
self.log.info("Peer 1 able to repeatedly download new block") | self.log.info("Peer 1 able to repeatedly download new block") | ||||
# But if p2p_conns[1] tries for an old block, it gets disconnected | # But if p2p_conns[1] tries for an old block, it gets disconnected | ||||
# too. | # too. | ||||
getdata_request.inv = [CInv(2, big_old_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)] | ||||
p2p_conns[1].send_message(getdata_request) | p2p_conns[1].send_message(getdata_request) | ||||
p2p_conns[1].wait_for_disconnect() | p2p_conns[1].wait_for_disconnect() | ||||
assert_equal(len(self.nodes[0].getpeerinfo()), 1) | assert_equal(len(self.nodes[0].getpeerinfo()), 1) | ||||
self.log.info("Peer 1 disconnected after trying to download old block") | self.log.info("Peer 1 disconnected after trying to download old block") | ||||
self.log.info("Advancing system time on node to clear counters...") | self.log.info("Advancing system time on node to clear counters...") | ||||
Show All 13 Lines | def run_test(self): | ||||
self.stop_node(0) | self.stop_node(0) | ||||
self.start_node(0, ["-whitelist=noban@127.0.0.1", | self.start_node(0, ["-whitelist=noban@127.0.0.1", | ||||
"-maxuploadtarget=1", "-blockmaxsize=999000"]) | "-maxuploadtarget=1", "-blockmaxsize=999000"]) | ||||
# Reconnect to self.nodes[0] | # Reconnect to self.nodes[0] | ||||
self.nodes[0].add_p2p_connection(TestP2PConn()) | 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(2, big_new_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)] | ||||
for i in range(20): | for i in range(20): | ||||
self.nodes[0].p2p.send_and_ping(getdata_request) | self.nodes[0].p2p.send_and_ping(getdata_request) | ||||
assert_equal( | assert_equal( | ||||
self.nodes[0].p2p.block_receive_map[big_new_block], i + 1) | self.nodes[0].p2p.block_receive_map[big_new_block], i + 1) | ||||
getdata_request.inv = [CInv(2, big_old_block)] | getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)] | ||||
self.nodes[0].p2p.send_and_ping(getdata_request) | self.nodes[0].p2p.send_and_ping(getdata_request) | ||||
# node is still connected because of the whitelist | # node is still connected because of the whitelist | ||||
assert_equal(len(self.nodes[0].getpeerinfo()), 1) | assert_equal(len(self.nodes[0].getpeerinfo()), 1) | ||||
self.log.info( | self.log.info( | ||||
"Peer still connected after trying to download old block (whitelisted)") | "Peer still connected after trying to download old block (whitelisted)") | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
MaxUploadTest().main() | MaxUploadTest().main() |