<p>The default <code>setup_network()</code> implemented by the test framework connects all nodes, as explained by the comment in <code>example_test.py</code>'s <code>setup_network()</code>. Since you want node 2 to be connected to nodes 1 and 0, you can simply delete the <code>setup_network()</code> implemented in <code>example_test.py</code> to use the default <code>setup_network()</code>. Then inside of <code>run_test()</code>, you can just do <code>.generate()</code> with any of the nodes and the block should be synced to all of the other nodes.</p>
<p>To guarantee that all nodes are synced to the same block, you can use <code>self.sync_blocks()</code> which will ensure that every node has the same tip before it returns.</p>
<p>Your run test would have something like:</p>
<pre><code>def run_test(self):
    self.nodes[0].generate(1)
    self.sync_blocks()
</code></pre>

CC BY-SA 4.0
2021-07-29T20:18:58.327Z
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?
spraveenitpro
CC BY-SA 4.0
2021-07-29T20:26:38.817Z
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.
Ava Chow
CC BY-SA 4.0
2021-07-29T21:07:58.103Z
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.
spraveenitpro
CC BY-SA 4.0
2021-07-29T21:09:15.150Z
The check would be something like `assert_equal(self.nodes[1].getbestblockhash(), self.nodes[2].getbestblockhash())`
Ava Chow
CC BY-SA 4.0
2021-07-29T16:42:04.530Z
2021-07-29T16:42:04.530Z
Ava Chow
<p>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.</p>
<p>Thanks.</p>

<bitcoin-core-development><python><functional-test>
Get node 1 to mine another block, send it to node 2, and check that node 2 received it
spraveenitpro
# Get node 1 to mine another block, send it to node 2, and check that node 2 received it

(Asked by: username_0 on 2021-07-29)

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.

Thanks.

**Tags:** bitcoin-core-development, python, functional-test


---

## 1 answers

### Answer by username_1 on 2021-07-29. Score: 3

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):
    self.nodes[0].generate(1)
    self.sync_blocks()

```

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  
> **username_0**: 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.  
> **username_1**: The check would be something like `assert_equal(self.nodes[1].getbestblockhash(), self.nodes[2].getbestblockhash())`  

---

