BIP141: Other consensus critical limits, and BIP145
[bitcoinplatinum.git] / qa / rpc-tests / maxuploadtarget.py
blob125d4eb275190238baa2355e93b0a05f7bc6e4ea
1 #!/usr/bin/env python3
2 # Copyright (c) 2015-2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 from test_framework.mininode import *
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
9 import time
11 '''
12 Test behavior of -maxuploadtarget.
14 * Verify that getdata requests for old blocks (>1week) are dropped
15 if uploadtarget has been reached.
16 * Verify that getdata requests for recent blocks are respecteved even
17 if uploadtarget has been reached.
18 * Verify that the upload counters are reset after 24 hours.
19 '''
21 # TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
22 # p2p messages to a node, generating the messages in the main testing logic.
23 class TestNode(NodeConnCB):
24 def __init__(self):
25 NodeConnCB.__init__(self)
26 self.connection = None
27 self.ping_counter = 1
28 self.last_pong = msg_pong()
29 self.block_receive_map = {}
31 def add_connection(self, conn):
32 self.connection = conn
33 self.peer_disconnected = False
35 def on_inv(self, conn, message):
36 pass
38 # Track the last getdata message we receive (used in the test)
39 def on_getdata(self, conn, message):
40 self.last_getdata = message
42 def on_block(self, conn, message):
43 message.block.calc_sha256()
44 try:
45 self.block_receive_map[message.block.sha256] += 1
46 except KeyError as e:
47 self.block_receive_map[message.block.sha256] = 1
49 # Spin until verack message is received from the node.
50 # We use this to signal that our test can begin. This
51 # is called from the testing thread, so it needs to acquire
52 # the global lock.
53 def wait_for_verack(self):
54 def veracked():
55 return self.verack_received
56 return wait_until(veracked, timeout=10)
58 def wait_for_disconnect(self):
59 def disconnected():
60 return self.peer_disconnected
61 return wait_until(disconnected, timeout=10)
63 # Wrapper for the NodeConn's send_message function
64 def send_message(self, message):
65 self.connection.send_message(message)
67 def on_pong(self, conn, message):
68 self.last_pong = message
70 def on_close(self, conn):
71 self.peer_disconnected = True
73 # Sync up with the node after delivery of a block
74 def sync_with_ping(self, timeout=30):
75 def received_pong():
76 return (self.last_pong.nonce == self.ping_counter)
77 self.connection.send_message(msg_ping(nonce=self.ping_counter))
78 success = wait_until(received_pong, timeout)
79 self.ping_counter += 1
80 return success
82 class MaxUploadTest(BitcoinTestFramework):
84 def add_options(self, parser):
85 parser.add_option("--testbinary", dest="testbinary",
86 default=os.getenv("BITCOIND", "bitcoind"),
87 help="bitcoind binary to test")
89 def __init__(self):
90 super().__init__()
91 self.setup_clean_chain = True
92 self.num_nodes = 1
94 self.utxo = []
95 self.txouts = gen_return_txouts()
97 def setup_network(self):
98 # Start a node with maxuploadtarget of 200 MB (/24h)
99 self.nodes = []
100 self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=800", "-blockmaxsize=999000"]))
102 def mine_full_block(self, node, address):
103 # Want to create a full block
104 # We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit
105 for j in range(14):
106 if len(self.utxo) < 14:
107 self.utxo = node.listunspent()
108 inputs=[]
109 outputs = {}
110 t = self.utxo.pop()
111 inputs.append({ "txid" : t["txid"], "vout" : t["vout"]})
112 remchange = t["amount"] - Decimal("0.001000")
113 outputs[address]=remchange
114 # Create a basic transaction that will send change back to ourself after account for a fee
115 # And then insert the 128 generated transaction outs in the middle rawtx[92] is where the #
116 # of txouts is stored and is the only thing we overwrite from the original transaction
117 rawtx = node.createrawtransaction(inputs, outputs)
118 newtx = rawtx[0:92]
119 newtx = newtx + self.txouts
120 newtx = newtx + rawtx[94:]
121 # Appears to be ever so slightly faster to sign with SIGHASH_NONE
122 signresult = node.signrawtransaction(newtx,None,None,"NONE")
123 txid = node.sendrawtransaction(signresult["hex"], True)
124 # Mine a full sized block which will be these transactions we just created
125 node.generate(1)
127 def run_test(self):
128 # Before we connect anything, we first set the time on the node
129 # to be in the past, otherwise things break because the CNode
130 # time counters can't be reset backward after initialization
131 old_time = int(time.time() - 2*60*60*24*7)
132 self.nodes[0].setmocktime(old_time)
134 # Generate some old blocks
135 self.nodes[0].generate(130)
137 # test_nodes[0] will only request old blocks
138 # test_nodes[1] will only request new blocks
139 # test_nodes[2] will test resetting the counters
140 test_nodes = []
141 connections = []
143 for i in range(3):
144 test_nodes.append(TestNode())
145 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
146 test_nodes[i].add_connection(connections[i])
148 NetworkThread().start() # Start up network handling in another thread
149 [x.wait_for_verack() for x in test_nodes]
151 # Test logic begins here
153 # Now mine a big block
154 self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress())
156 # Store the hash; we'll request this later
157 big_old_block = self.nodes[0].getbestblockhash()
158 old_block_size = self.nodes[0].getblock(big_old_block, True)['size']
159 big_old_block = int(big_old_block, 16)
161 # Advance to two days ago
162 self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
164 # Mine one more block, so that the prior block looks old
165 self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress())
167 # We'll be requesting this new block too
168 big_new_block = self.nodes[0].getbestblockhash()
169 new_block_size = self.nodes[0].getblock(big_new_block)['size']
170 big_new_block = int(big_new_block, 16)
172 # test_nodes[0] will test what happens if we just keep requesting the
173 # the same big old block too many times (expect: disconnect)
175 getdata_request = msg_getdata()
176 getdata_request.inv.append(CInv(2, big_old_block))
178 max_bytes_per_day = 800*1024*1024
179 daily_buffer = 144 * 4000000
180 max_bytes_available = max_bytes_per_day - daily_buffer
181 success_count = max_bytes_available // old_block_size
183 # 576MB will be reserved for relaying new blocks, so expect this to
184 # succeed for ~235 tries.
185 for i in range(success_count):
186 test_nodes[0].send_message(getdata_request)
187 test_nodes[0].sync_with_ping()
188 assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1)
190 assert_equal(len(self.nodes[0].getpeerinfo()), 3)
191 # At most a couple more tries should succeed (depending on how long
192 # the test has been running so far).
193 for i in range(3):
194 test_nodes[0].send_message(getdata_request)
195 test_nodes[0].wait_for_disconnect()
196 assert_equal(len(self.nodes[0].getpeerinfo()), 2)
197 print("Peer 0 disconnected after downloading old block too many times")
199 # Requesting the current block on test_nodes[1] should succeed indefinitely,
200 # even when over the max upload target.
201 # We'll try 800 times
202 getdata_request.inv = [CInv(2, big_new_block)]
203 for i in range(800):
204 test_nodes[1].send_message(getdata_request)
205 test_nodes[1].sync_with_ping()
206 assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
208 print("Peer 1 able to repeatedly download new block")
210 # But if test_nodes[1] tries for an old block, it gets disconnected too.
211 getdata_request.inv = [CInv(2, big_old_block)]
212 test_nodes[1].send_message(getdata_request)
213 test_nodes[1].wait_for_disconnect()
214 assert_equal(len(self.nodes[0].getpeerinfo()), 1)
216 print("Peer 1 disconnected after trying to download old block")
218 print("Advancing system time on node to clear counters...")
220 # If we advance the time by 24 hours, then the counters should reset,
221 # and test_nodes[2] should be able to retrieve the old block.
222 self.nodes[0].setmocktime(int(time.time()))
223 test_nodes[2].sync_with_ping()
224 test_nodes[2].send_message(getdata_request)
225 test_nodes[2].sync_with_ping()
226 assert_equal(test_nodes[2].block_receive_map[big_old_block], 1)
228 print("Peer 2 able to download old block")
230 [c.disconnect_node() for c in connections]
232 #stop and start node 0 with 1MB maxuploadtarget, whitelist 127.0.0.1
233 print("Restarting nodes with -whitelist=127.0.0.1")
234 stop_node(self.nodes[0], 0)
235 self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-maxuploadtarget=1", "-blockmaxsize=999000"])
237 #recreate/reconnect 3 test nodes
238 test_nodes = []
239 connections = []
241 for i in range(3):
242 test_nodes.append(TestNode())
243 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i]))
244 test_nodes[i].add_connection(connections[i])
246 NetworkThread().start() # Start up network handling in another thread
247 [x.wait_for_verack() for x in test_nodes]
249 #retrieve 20 blocks which should be enough to break the 1MB limit
250 getdata_request.inv = [CInv(2, big_new_block)]
251 for i in range(20):
252 test_nodes[1].send_message(getdata_request)
253 test_nodes[1].sync_with_ping()
254 assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1)
256 getdata_request.inv = [CInv(2, big_old_block)]
257 test_nodes[1].send_message(getdata_request)
258 test_nodes[1].wait_for_disconnect()
259 assert_equal(len(self.nodes[0].getpeerinfo()), 3) #node is still connected because of the whitelist
261 print("Peer 1 still connected after trying to download old block (whitelisted)")
263 [c.disconnect_node() for c in connections]
265 if __name__ == '__main__':
266 MaxUploadTest().main()