27
27
from test_framework .wallet import MiniWallet
28
28
29
29
30
+ UPLOAD_TARGET_MB = 800
31
+
32
+
30
33
class TestP2PConn (P2PInterface ):
31
34
def __init__ (self ):
32
35
super ().__init__ ()
@@ -45,12 +48,21 @@ def set_test_params(self):
45
48
self .setup_clean_chain = True
46
49
self .num_nodes = 1
47
50
self .extra_args = [[
48
- "-maxuploadtarget=800M " ,
51
+ f "-maxuploadtarget={ UPLOAD_TARGET_MB } M " ,
49
52
"-datacarriersize=100000" ,
50
53
]]
51
54
self .supports_cli = False
52
55
56
+ def assert_uploadtarget_state (self , * , target_reached , serve_historical_blocks ):
57
+ """Verify the node's current upload target state via the `getnettotals` RPC call."""
58
+ uploadtarget = self .nodes [0 ].getnettotals ()["uploadtarget" ]
59
+ assert_equal (uploadtarget ["target_reached" ], target_reached )
60
+ assert_equal (uploadtarget ["serve_historical_blocks" ], serve_historical_blocks )
61
+
53
62
def run_test (self ):
63
+ # Initially, neither historical blocks serving limit nor total limit are reached
64
+ self .assert_uploadtarget_state (target_reached = False , serve_historical_blocks = True )
65
+
54
66
# Before we connect anything, we first set the time on the node
55
67
# to be in the past, otherwise things break because the CNode
56
68
# time counters can't be reset backward after initialization
@@ -93,7 +105,7 @@ def run_test(self):
93
105
getdata_request = msg_getdata ()
94
106
getdata_request .inv .append (CInv (MSG_BLOCK , big_old_block ))
95
107
96
- max_bytes_per_day = 800 * 1024 * 1024
108
+ max_bytes_per_day = UPLOAD_TARGET_MB * 1024 * 1024
97
109
daily_buffer = 144 * 4000000
98
110
max_bytes_available = max_bytes_per_day - daily_buffer
99
111
success_count = max_bytes_available // old_block_size
@@ -113,6 +125,9 @@ def run_test(self):
113
125
assert_equal (len (self .nodes [0 ].getpeerinfo ()), 2 )
114
126
self .log .info ("Peer 0 disconnected after downloading old block too many times" )
115
127
128
+ # Historical blocks serving limit is reached by now, but total limit still isn't
129
+ self .assert_uploadtarget_state (target_reached = False , serve_historical_blocks = False )
130
+
116
131
# Requesting the current block on p2p_conns[1] should succeed indefinitely,
117
132
# even when over the max upload target.
118
133
# We'll try 800 times
@@ -121,6 +136,9 @@ def run_test(self):
121
136
p2p_conns [1 ].send_and_ping (getdata_request )
122
137
assert_equal (p2p_conns [1 ].block_receive_map [big_new_block ], i + 1 )
123
138
139
+ # Both historical blocks serving limit and total limit are reached
140
+ self .assert_uploadtarget_state (target_reached = True , serve_historical_blocks = False )
141
+
124
142
self .log .info ("Peer 1 able to repeatedly download new block" )
125
143
126
144
# But if p2p_conns[1] tries for an old block, it gets disconnected too.
@@ -139,13 +157,16 @@ def run_test(self):
139
157
p2p_conns [2 ].sync_with_ping ()
140
158
p2p_conns [2 ].send_and_ping (getdata_request )
141
159
assert_equal (p2p_conns [2 ].block_receive_map [big_old_block ], 1 )
160
+ self .assert_uploadtarget_state (target_reached = False , serve_historical_blocks = True )
142
161
143
162
self .log .info ("Peer 2 able to download old block" )
144
163
145
164
self .nodes [0 ].disconnect_p2ps ()
146
165
147
166
self .log .info ("Restarting node 0 with download permission and 1MB maxuploadtarget" )
148
167
self .restart_node (0 , ["-whitelist=download@127.0.0.1" , "-maxuploadtarget=1" ])
168
+ # Total limit isn't reached after restart, but 1 MB is too small to serve historical blocks
169
+ self .assert_uploadtarget_state (target_reached = False , serve_historical_blocks = False )
149
170
150
171
# Reconnect to self.nodes[0]
151
172
peer = self .nodes [0 ].add_p2p_connection (TestP2PConn ())
@@ -156,6 +177,9 @@ def run_test(self):
156
177
peer .send_and_ping (getdata_request )
157
178
assert_equal (peer .block_receive_map [big_new_block ], i + 1 )
158
179
180
+ # Total limit is exceeded
181
+ self .assert_uploadtarget_state (target_reached = True , serve_historical_blocks = False )
182
+
159
183
getdata_request .inv = [CInv (MSG_BLOCK , big_old_block )]
160
184
peer .send_and_ping (getdata_request )
161
185
0 commit comments