Skip to content

Commit 0b4565f

Browse files
achow101vijaydasmp
authored andcommitted
Merge bitcoin#28996: test: maxuploadtarget: check for mempool msg disconnect if limit is reached, improve existing test coverage
b58f009 test: check that mempool msgs lead to disconnect if uploadtarget is reached (Sebastian Falbesoner) dd5cf38 test: check for specific disconnect reasons in feature_maxuploadtarget.py (Sebastian Falbesoner) 73d7372 test: verify `-maxuploadtarget` limit state via `getnettotals` RPC result (Sebastian Falbesoner) Pull request description: This PR improves existing and adds new test coverage for the `-maxuploadtarget` mechanism (feature_maxuploadtarget.py) in the following ways, one commit each: * verify the uploadtarget state via the `getnettotals` RPC (`uploadtarget` result field): https://github.com/bitcoin/bitcoin/blob/160d23677ad799cf9b493eaa923b2ac080c3fb8e/src/rpc/net.cpp#L581-L582 Note that reaching the total limit (`target_reached` == True) always implies that the historical blocks serving limits is also reached (`serve_historical_blocks` == False), i.e. it's impossible that both flags are set to True. * check for peer's specific disconnect reason (in this case, `"historical block serving limit reached, disconnect peer"`): https://github.com/bitcoin/bitcoin/blob/160d23677ad799cf9b493eaa923b2ac080c3fb8e/src/net_processing.cpp#L2272-L2280 * add a test for a peer disconnect if the uploadtarget is reached and a `mempool` message is received (if bloom filters are enabled): https://github.com/bitcoin/bitcoin/blob/160d23677ad799cf9b493eaa923b2ac080c3fb8e/src/net_processing.cpp#L4755-L4763 Note that another reason for disconnect after receiving a MEMPOOL msg of a peer is if bloom filters are disabled on the node. This case is already covered in the functional test `p2p_nobloomfilter_messages.py`. ACKs for top commit: maflcko: lgtm ACK b58f009 achow101: ACK b58f009 sr-gi: tACK [b58f009](bitcoin@b58f009) Tree-SHA512: 7439134129695c9c3a7ddc5e39f2ed700f91e7c91f0b7a9e0a783f275c6aa2f9918529cbfd38bb37f9139184e05e0f0354ef3c3df56da310177ec1d6b48b43d0
1 parent 40d15b6 commit 0b4565f

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

test/functional/feature_maxuploadtarget.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
if uploadtarget has been reached.
99
* Verify that getdata requests for recent blocks are respected even
1010
if uploadtarget has been reached.
11+
* Verify that mempool requests lead to a disconnect if uploadtarget has been reached.
1112
* Verify that the upload counters are reset after 24 hours.
1213
"""
1314
from collections import defaultdict
@@ -17,6 +18,7 @@
1718
MAX_BLOCK_SIZE,
1819
MSG_BLOCK,
1920
msg_getdata,
21+
msg_mempool,
2022
)
2123
from test_framework.p2p import P2PInterface
2224
from test_framework.test_framework import BitcoinTestFramework
@@ -28,6 +30,9 @@
2830
from test_framework.wallet import MiniWallet
2931

3032

33+
UPLOAD_TARGET_MB = 400
34+
35+
3136
class TestP2PConn(P2PInterface):
3237
def __init__(self):
3338
super().__init__()
@@ -46,17 +51,25 @@ def set_test_params(self):
4651
self.setup_clean_chain = True
4752
self.num_nodes = 1
4853
self.extra_args = [[
49-
"-maxuploadtarget=400M",
54+
f"-maxuploadtarget={UPLOAD_TARGET_MB}M",
5055
"-datacarriersize=100000",
5156
]]
5257
self.supports_cli = False
5358

59+
def assert_uploadtarget_state(self, *, target_reached, serve_historical_blocks):
60+
"""Verify the node's current upload target state via the `getnettotals` RPC call."""
61+
uploadtarget = self.nodes[0].getnettotals()["uploadtarget"]
62+
assert_equal(uploadtarget["target_reached"], target_reached)
63+
assert_equal(uploadtarget["serve_historical_blocks"], serve_historical_blocks)
64+
5465
def run_test(self):
5566
# Advance all nodes 2 weeks in the future
5667
old_mocktime = self.mocktime
5768
current_mocktime = old_mocktime + 2*60*60*24*7
5869
self.mocktime = current_mocktime
5970
set_node_times(self.nodes, current_mocktime)
71+
# Initially, neither historical blocks serving limit nor total limit are reached
72+
self.assert_uploadtarget_state(target_reached=False, serve_historical_blocks=True)
6073

6174
# Before we connect anything, we first set the time on the node
6275
# to be in the past, otherwise things break because the CNode
@@ -100,7 +113,7 @@ def run_test(self):
100113
getdata_request = msg_getdata()
101114
getdata_request.inv.append(CInv(MSG_BLOCK, big_old_block))
102115

103-
max_bytes_per_day = 400*1024*1024
116+
max_bytes_per_day = UPLOAD_TARGET_MB * 1024 *1024
104117
daily_buffer = 144 * MAX_BLOCK_SIZE
105118
max_bytes_available = max_bytes_per_day - daily_buffer
106119
success_count = max_bytes_available // old_block_size
@@ -114,12 +127,16 @@ def run_test(self):
114127
assert_equal(len(self.nodes[0].getpeerinfo()), 3)
115128
# At most a couple more tries should succeed (depending on how long
116129
# the test has been running so far).
117-
for _ in range(3):
118-
p2p_conns[0].send_message(getdata_request)
119-
p2p_conns[0].wait_for_disconnect()
130+
with self.nodes[0].assert_debug_log(expected_msgs=["historical block serving limit reached, disconnect peer"]):
131+
for _ in range(3):
132+
p2p_conns[0].send_message(getdata_request)
133+
p2p_conns[0].wait_for_disconnect()
120134
assert_equal(len(self.nodes[0].getpeerinfo()), 2)
121135
self.log.info("Peer 0 disconnected after downloading old block too many times")
122136

137+
# Historical blocks serving limit is reached by now, but total limit still isn't
138+
self.assert_uploadtarget_state(target_reached=False, serve_historical_blocks=False)
139+
123140
# Requesting the current block on p2p_conns[1] should succeed indefinitely,
124141
# even when over the max upload target.
125142
# We'll try 200 times
@@ -128,12 +145,16 @@ def run_test(self):
128145
p2p_conns[1].send_and_ping(getdata_request)
129146
assert_equal(p2p_conns[1].block_receive_map[big_new_block], i+1)
130147

148+
# Both historical blocks serving limit and total limit are reached
149+
self.assert_uploadtarget_state(target_reached=True, serve_historical_blocks=False)
150+
131151
self.log.info("Peer 1 able to repeatedly download new block")
132152

133153
# But if p2p_conns[1] tries for an old block, it gets disconnected too.
134154
getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
135-
p2p_conns[1].send_message(getdata_request)
136-
p2p_conns[1].wait_for_disconnect()
155+
with self.nodes[0].assert_debug_log(expected_msgs=["historical block serving limit reached, disconnect peer"]):
156+
p2p_conns[1].send_message(getdata_request)
157+
p2p_conns[1].wait_for_disconnect()
137158
assert_equal(len(self.nodes[0].getpeerinfo()), 1)
138159

139160
self.log.info("Peer 1 disconnected after trying to download old block")
@@ -146,23 +167,32 @@ def run_test(self):
146167
p2p_conns[2].sync_with_ping()
147168
p2p_conns[2].send_and_ping(getdata_request)
148169
assert_equal(p2p_conns[2].block_receive_map[big_old_block], 1)
170+
self.assert_uploadtarget_state(target_reached=False, serve_historical_blocks=True)
149171

150172
self.log.info("Peer 2 able to download old block")
151173

152174
self.nodes[0].disconnect_p2ps()
153175

154-
self.log.info("Restarting node 0 with download permission and 1MB maxuploadtarget")
155-
self.restart_node(0, ["-whitelist=download@127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000", "-mocktime="+str(current_mocktime)])
176+
self.log.info("Restarting node 0 with download permission, bloom filter support and 1MB maxuploadtarget")
177+
self.restart_node(0, ["-whitelist=download@127.0.0.1","-peerbloomfilters", "-maxuploadtarget=1", "-blockmaxsize=999000", "-mocktime="+str(current_mocktime)])
178+
# Total limit isn't reached after restart, but 1 MB is too small to serve historical blocks
179+
self.assert_uploadtarget_state(target_reached=False, serve_historical_blocks=False)
156180

157181
# Reconnect to self.nodes[0]
158182
peer = self.nodes[0].add_p2p_connection(TestP2PConn(), supports_v2_p2p=False)
159183

184+
# Sending mempool message shouldn't disconnect peer, as total limit isn't reached yet
185+
peer.send_and_ping(msg_mempool())
186+
160187
#retrieve 20 blocks which should be enough to break the 1MB limit
161188
getdata_request.inv = [CInv(MSG_BLOCK, big_new_block)]
162189
for i in range(20):
163190
peer.send_and_ping(getdata_request)
164191
assert_equal(peer.block_receive_map[big_new_block], i+1)
165192

193+
# Total limit is exceeded
194+
self.assert_uploadtarget_state(target_reached=True, serve_historical_blocks=False)
195+
166196
getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
167197
peer.send_and_ping(getdata_request)
168198

@@ -171,6 +201,11 @@ def run_test(self):
171201
assert_equal(len(peer_info), 1) # node is still connected
172202
assert_equal(peer_info[0]['permissions'], ['download'])
173203

204+
self.log.info("Peer gets disconnected for a mempool request after limit is reached")
205+
with self.nodes[0].assert_debug_log(expected_msgs=["mempool request with bandwidth limit reached, disconnect peer"]):
206+
peer.send_message(msg_mempool())
207+
peer.wait_for_disconnect()
208+
174209
self.log.info("Test passing an unparsable value to -maxuploadtarget throws an error")
175210
self.stop_node(0)
176211
self.nodes[0].assert_start_raises_init_error(extra_args=["-maxuploadtarget=abc"], expected_msg="Error: Unable to parse -maxuploadtarget: 'abc'")

0 commit comments

Comments
 (0)