Skip to content

Commit 6c1d1ba

Browse files
committed
Python p2p testing framework
mininode.py provides a framework for connecting to a bitcoin node over the p2p network. NodeConn is the main object that manages connectivity to a node and provides callbacks; the interface for those callbacks is defined by NodeConnCB. Defined also are all data structures from bitcoin core that pass on the network (CBlock, CTransaction, etc), along with de-/serialization functions. maxblocksinflight.py is an example test using this framework that tests whether a node is limiting the maximum number of in-flight block requests. This also adds support to util.py for specifying the binary to use when starting nodes (for tests that compare the behavior of different bitcoind versions), and adds maxblocksinflight.py to the pull tester.
1 parent 7bf5d5e commit 6c1d1ba

File tree

4 files changed

+1354
-4
lines changed

4 files changed

+1354
-4
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ testScripts=(
3030
'proxy_test.py'
3131
'merkle_blocks.py'
3232
# 'forknotify.py'
33+
'maxblocksinflight.py'
3334
);
3435
if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then
3536
for (( i = 0; i < ${#testScripts[@]}; i++ ))

qa/rpc-tests/maxblocksinflight.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 mininode import *
8+
from test_framework import BitcoinTestFramework
9+
from 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+
20+
class TestManager(NodeConnCB):
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+
self.create_callback_map()
38+
39+
def add_new_connection(self, connection):
40+
self.connection = connection
41+
self.blockReqCounts = {}
42+
self.disconnectOkay = False
43+
44+
def run(self):
45+
try:
46+
fail = False
47+
self.connection.rpc.generate(1) # Leave IBD
48+
49+
numBlocksToGenerate = [ 8, 16, 128, 1024 ]
50+
for count in range(len(numBlocksToGenerate)):
51+
current_invs = []
52+
for i in range(numBlocksToGenerate[count]):
53+
current_invs.append(CInv(2, random.randrange(0, 1<<256)))
54+
if len(current_invs) >= 50000:
55+
self.connection.send_message(msg_inv(current_invs))
56+
current_invs = []
57+
if len(current_invs) > 0:
58+
self.connection.send_message(msg_inv(current_invs))
59+
60+
# Wait and see how many blocks were requested
61+
time.sleep(2)
62+
63+
total_requests = 0
64+
for key in self.blockReqCounts:
65+
total_requests += self.blockReqCounts[key]
66+
if self.blockReqCounts[key] > 1:
67+
raise AssertionError("Error, test failed: block %064x requested more than once" % key)
68+
if total_requests > MAX_REQUESTS:
69+
raise AssertionError("Error, too many blocks (%d) requested" % total_requests)
70+
print "Round %d: success (total requests: %d)" % (count, total_requests)
71+
except AssertionError as e:
72+
print "TEST FAILED: ", e.args
73+
74+
self.disconnectOkay = True
75+
self.connection.disconnect_node()
76+
77+
78+
class MaxBlocksInFlightTest(BitcoinTestFramework):
79+
def add_options(self, parser):
80+
parser.add_option("--testbinary", dest="testbinary", default="bitcoind",
81+
help="Binary to test max block requests behavior")
82+
83+
def setup_chain(self):
84+
print "Initializing test directory "+self.options.tmpdir
85+
initialize_chain_clean(self.options.tmpdir, 1)
86+
87+
def setup_network(self):
88+
self.nodes = start_nodes(1, self.options.tmpdir,
89+
extra_args=[['-debug', '-whitelist=127.0.0.1']],
90+
binary=[self.options.testbinary])
91+
92+
def run_test(self):
93+
test = TestManager()
94+
test.add_new_connection(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test))
95+
NetworkThread().start() # Start up network handling in another thread
96+
test.run()
97+
98+
if __name__ == '__main__':
99+
MaxBlocksInFlightTest().main()

0 commit comments

Comments
 (0)