2727from test_framework .wallet import MiniWallet
2828
2929
30+ UPLOAD_TARGET_MB = 800
31+
32+
3033class TestP2PConn (P2PInterface ):
3134 def __init__ (self ):
3235 super ().__init__ ()
@@ -45,12 +48,21 @@ def set_test_params(self):
4548 self .setup_clean_chain = True
4649 self .num_nodes = 1
4750 self .extra_args = [[
48- "-maxuploadtarget=800M " ,
51+ f "-maxuploadtarget={ UPLOAD_TARGET_MB } M " ,
4952 "-datacarriersize=100000" ,
5053 ]]
5154 self .supports_cli = False
5255
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+
5362 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+
5466 # Before we connect anything, we first set the time on the node
5567 # to be in the past, otherwise things break because the CNode
5668 # time counters can't be reset backward after initialization
@@ -93,7 +105,7 @@ def run_test(self):
93105 getdata_request = msg_getdata ()
94106 getdata_request .inv .append (CInv (MSG_BLOCK , big_old_block ))
95107
96- max_bytes_per_day = 800 * 1024 * 1024
108+ max_bytes_per_day = UPLOAD_TARGET_MB * 1024 * 1024
97109 daily_buffer = 144 * 4000000
98110 max_bytes_available = max_bytes_per_day - daily_buffer
99111 success_count = max_bytes_available // old_block_size
@@ -113,6 +125,9 @@ def run_test(self):
113125 assert_equal (len (self .nodes [0 ].getpeerinfo ()), 2 )
114126 self .log .info ("Peer 0 disconnected after downloading old block too many times" )
115127
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+
116131 # Requesting the current block on p2p_conns[1] should succeed indefinitely,
117132 # even when over the max upload target.
118133 # We'll try 800 times
@@ -121,6 +136,9 @@ def run_test(self):
121136 p2p_conns [1 ].send_and_ping (getdata_request )
122137 assert_equal (p2p_conns [1 ].block_receive_map [big_new_block ], i + 1 )
123138
139+ # Both historical blocks serving limit and total limit are reached
140+ self .assert_uploadtarget_state (target_reached = True , serve_historical_blocks = False )
141+
124142 self .log .info ("Peer 1 able to repeatedly download new block" )
125143
126144 # But if p2p_conns[1] tries for an old block, it gets disconnected too.
@@ -139,13 +157,16 @@ def run_test(self):
139157 p2p_conns [2 ].sync_with_ping ()
140158 p2p_conns [2 ].send_and_ping (getdata_request )
141159 assert_equal (p2p_conns [2 ].block_receive_map [big_old_block ], 1 )
160+ self .assert_uploadtarget_state (target_reached = False , serve_historical_blocks = True )
142161
143162 self .log .info ("Peer 2 able to download old block" )
144163
145164 self .nodes [0 ].disconnect_p2ps ()
146165
147166 self .log .info ("Restarting node 0 with download permission and 1MB maxuploadtarget" )
148167 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 )
149170
150171 # Reconnect to self.nodes[0]
151172 peer = self .nodes [0 ].add_p2p_connection (TestP2PConn ())
@@ -156,6 +177,9 @@ def run_test(self):
156177 peer .send_and_ping (getdata_request )
157178 assert_equal (peer .block_receive_map [big_new_block ], i + 1 )
158179
180+ # Total limit is exceeded
181+ self .assert_uploadtarget_state (target_reached = True , serve_historical_blocks = False )
182+
159183 getdata_request .inv = [CInv (MSG_BLOCK , big_old_block )]
160184 peer .send_and_ping (getdata_request )
161185
0 commit comments