Changeset View
Changeset View
Standalone View
Standalone View
test/functional/example_test.py
Show All 22 Lines | from test_framework.mininode import ( | ||||
mininode_lock, | mininode_lock, | ||||
msg_block, | msg_block, | ||||
msg_getdata, | msg_getdata, | ||||
) | ) | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
connect_nodes, | connect_nodes, | ||||
p2p_port, | |||||
wait_until, | wait_until, | ||||
) | ) | ||||
# NodeConnCB is a class containing callbacks to be executed when a P2P | # NodeConnCB is a class containing callbacks to be executed when a P2P | ||||
# message is received from the node-under-test. Subclass NodeConnCB and | # message is received from the node-under-test. Subclass NodeConnCB and | ||||
# override the on_*() methods if you need custom behaviour. | # override the on_*() methods if you need custom behaviour. | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | def setup_network(self): | ||||
If you do override this method, remember to start the nodes, assign | If you do override this method, remember to start the nodes, assign | ||||
them to self.nodes, connect them and then sync.""" | them to self.nodes, connect them and then sync.""" | ||||
self.setup_nodes() | self.setup_nodes() | ||||
# In this test, we're not connecting node2 to node0 or node1. Calls to | # In this test, we're not connecting node2 to node0 or node1. Calls to | ||||
# sync_all() should not include node2, since we're not expecting it to | # sync_all() should not include node2, since we're not expecting it to | ||||
# sync. | # sync. | ||||
connect_nodes(self.nodes[0], 1) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
self.sync_all([self.nodes[0:1]]) | self.sync_all([self.nodes[0:1]]) | ||||
# Use setup_nodes() to customize the node start behaviour (for example if | # Use setup_nodes() to customize the node start behaviour (for example if | ||||
# you don't want to start all nodes at the start of the test). | # you don't want to start all nodes at the start of the test). | ||||
# def setup_nodes(): | # def setup_nodes(): | ||||
# pass | # pass | ||||
def custom_method(self): | def custom_method(self): | ||||
"""Do some custom behaviour for this test | """Do some custom behaviour for this test | ||||
Define it in a method here because you're going to use it repeatedly. | Define it in a method here because you're going to use it repeatedly. | ||||
If you think it's useful in general, consider moving it to the base | If you think it's useful in general, consider moving it to the base | ||||
BitcoinTestFramework class so other tests can use it.""" | BitcoinTestFramework class so other tests can use it.""" | ||||
self.log.info("Running custom_method") | self.log.info("Running custom_method") | ||||
def run_test(self): | def run_test(self): | ||||
"""Main test logic""" | """Main test logic""" | ||||
# Create a P2P connection to one of the nodes | # Create a P2P connection to one of the nodes | ||||
node0 = BaseNode() | node0 = BaseNode() | ||||
connections = [] | connections = [] | ||||
connections.append( | connections.append( | ||||
NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0)) | NodeConn('127.0.0.1', self.p2p_port(0), self.nodes[0], node0)) | ||||
node0.add_connection(connections[0]) | node0.add_connection(connections[0]) | ||||
# Start up network handling in another thread. This needs to be called | # Start up network handling in another thread. This needs to be called | ||||
# after the P2P connections have been created. | # after the P2P connections have been created. | ||||
NetworkThread().start() | NetworkThread().start() | ||||
# wait_for_verack ensures that the P2P connection is fully up. | # wait_for_verack ensures that the P2P connection is fully up. | ||||
node0.wait_for_verack() | node0.wait_for_verack() | ||||
Show All 40 Lines | def run_test(self): | ||||
self.block_time += 1 | self.block_time += 1 | ||||
height += 1 | height += 1 | ||||
self.log.info( | self.log.info( | ||||
"Wait for node1 to reach current tip (height 11) using RPC") | "Wait for node1 to reach current tip (height 11) using RPC") | ||||
self.nodes[1].waitforblockheight(11) | self.nodes[1].waitforblockheight(11) | ||||
self.log.info("Connect node2 and node1") | self.log.info("Connect node2 and node1") | ||||
connect_nodes(self.nodes[1], 2) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
self.log.info("Add P2P connection to node2") | self.log.info("Add P2P connection to node2") | ||||
node2 = BaseNode() | node2 = BaseNode() | ||||
connections.append( | connections.append( | ||||
NodeConn('127.0.0.1', p2p_port(2), self.nodes[2], node2)) | NodeConn('127.0.0.1', self.p2p_port(2), self.nodes[2], node2)) | ||||
node2.add_connection(connections[1]) | node2.add_connection(connections[1]) | ||||
node2.wait_for_verack() | node2.wait_for_verack() | ||||
self.log.info( | self.log.info( | ||||
"Wait for node2 reach current tip. Test that it has propagated all the blocks to us") | "Wait for node2 reach current tip. Test that it has propagated all the blocks to us") | ||||
getdata_request = msg_getdata() | getdata_request = msg_getdata() | ||||
for block in blocks: | for block in blocks: | ||||
Show All 19 Lines |