Dash Core  0.12.2.1
P2P Digital Currency
maxblocksinflight.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 #
3 # Distributed under the MIT/X11 software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #
6 
7 from test_framework.mininode import *
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import *
10 import logging
11 
12 '''
13 In this test we connect to one node over p2p, send it numerous inv's, and
14 compare the resulting number of getdata requests to a max allowed value. We
15 test for exceeding 128 blocks in flight, which was the limit an 0.9 client will
16 reach. [0.10 clients shouldn't request more than 16 from a single peer.]
17 '''
18 MAX_REQUESTS = 128
19 
21  # set up NodeConnCB callbacks, overriding base class
22  def on_getdata(self, conn, message):
23  self.log.debug("got getdata %s" % repr(message))
24  # Log the requests
25  for inv in message.inv:
26  if inv.hash not in self.blockReqCounts:
27  self.blockReqCounts[inv.hash] = 0
28  self.blockReqCounts[inv.hash] += 1
29 
30  def on_close(self, conn):
31  if not self.disconnectOkay:
32  raise EarlyDisconnectError(0)
33 
34  def __init__(self):
35  NodeConnCB.__init__(self)
36  self.log = logging.getLogger("BlockRelayTest")
37 
38  def add_new_connection(self, connection):
39  self.connection = connection
40  self.blockReqCounts = {}
41  self.disconnectOkay = False
42 
43  def run(self):
44  self.connection.rpc.generate(1) # Leave IBD
45 
46  numBlocksToGenerate = [8, 16, 128, 1024]
47  for count in range(len(numBlocksToGenerate)):
48  current_invs = []
49  for i in range(numBlocksToGenerate[count]):
50  current_invs.append(CInv(2, random.randrange(0, 1 << 256)))
51  if len(current_invs) >= 50000:
52  self.connection.send_message(msg_inv(current_invs))
53  current_invs = []
54  if len(current_invs) > 0:
55  self.connection.send_message(msg_inv(current_invs))
56 
57  # Wait and see how many blocks were requested
58  time.sleep(2)
59 
60  total_requests = 0
61  with mininode_lock:
62  for key in self.blockReqCounts:
63  total_requests += self.blockReqCounts[key]
64  if self.blockReqCounts[key] > 1:
65  raise AssertionError("Error, test failed: block %064x requested more than once" % key)
66  if total_requests > MAX_REQUESTS:
67  raise AssertionError("Error, too many blocks (%d) requested" % total_requests)
68  print "Round %d: success (total requests: %d)" % (count, total_requests)
69 
70  self.disconnectOkay = True
71  self.connection.disconnect_node()
72 
73 
75  def add_options(self, parser):
76  parser.add_option("--testbinary", dest="testbinary",
77  default=os.getenv("DASHD", "dashd"),
78  help="Binary to test max block requests behavior")
79 
80  def setup_chain(self):
81  print "Initializing test directory "+self.options.tmpdir
82  initialize_chain_clean(self.options.tmpdir, 1)
83 
84  def setup_network(self):
85  self.nodes = start_nodes(1, self.options.tmpdir,
86  extra_args=[['-debug', '-whitelist=127.0.0.1']],
87  binary=[self.options.testbinary])
88 
89  def run_test(self):
90  test = TestManager()
91  test.add_new_connection(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test))
92  NetworkThread().start() # Start up network handling in another thread
93  test.run()
94 
95 if __name__ == '__main__':
def on_getdata(self, conn, message)
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
def add_new_connection(self, connection)
def initialize_chain_clean(test_dir, num_nodes)
Definition: util.py:252
def p2p_port(n)
Definition: util.py:93
UniValue debug(const UniValue &params, bool fHelp)
Definition: misc.cpp:119