I am trying to write a test with the bitcoin/test/functional/example_test.py to get node 1 to mine another block, send it to node 2, and check that node 2 received it. Any idea on how to go about/get started on this? I am familiar with python but this is fairly new to me.



The default setup_network() implemented by the test framework connects all nodes, as explained by the comment in example_test.py's setup_network(). Since you want node 2 to be connected to nodes 1 and 0, you can simply delete the setup_network() implemented in example_test.py to use the default setup_network(). Then inside of run_test(), you can just do .generate() with any of the nodes and the block should be synced to all of the other nodes.

To guarantee that all nodes are synced to the same block, you can use self.sync_blocks() which will ensure that every node has the same tip before it returns.

Your run test would have something like:

def run_test(self):
  • This is very illuminating, thank you, just to confirm: 1. Getting node 1 to mine another block can be done with self.nodes[1].generate(1) ? 2. Send the mined block from node 1 to node 2 can be done with self.sync_blocks() [after deleting the setup_network example_test.py to use the default setup_network()] 3. How do I check that the node 2 has received it? Jul 29 '21 at 20:18
  • 1. Yes. 2. Once a block is mined, it is automatically sent to all of the nodes the mining node is connected to. sync_blocks() just ensures that all nodes are synced before continuing with the test. 3. You can use getbestblockhash() on nodes 1 and 2 and check that they match. But it is unnecessary to do that since sync_blocks() will do that for you.
    – Andrew Chow
    Jul 29 '21 at 20:26
  • Sorry for being such a noob, You can use getbestblockhash() on nodes 1 and 2 and check that they match. How would that look in code? thanks again. Jul 29 '21 at 21:07
  • The check would be something like assert_equal(self.nodes[1].getbestblockhash(), self.nodes[2].getbestblockhash())
    – Andrew Chow
    Jul 29 '21 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.