|
5 | 5 | """Test the getblockfrompeer RPC."""
|
6 | 6 |
|
7 | 7 | from test_framework.authproxy import JSONRPCException
|
8 |
| -from test_framework.messages import NODE_WITNESS |
| 8 | +from test_framework.messages import ( |
| 9 | + CBlock, |
| 10 | + from_hex, |
| 11 | + msg_headers, |
| 12 | + NODE_WITNESS, |
| 13 | +) |
9 | 14 | from test_framework.p2p import (
|
10 | 15 | P2P_SERVICES,
|
11 | 16 | P2PInterface,
|
|
16 | 21 | assert_raises_rpc_error,
|
17 | 22 | )
|
18 | 23 |
|
| 24 | + |
19 | 25 | class GetBlockFromPeerTest(BitcoinTestFramework):
|
20 | 26 | def set_test_params(self):
|
21 | 27 | self.num_nodes = 2
|
@@ -81,6 +87,30 @@ def run_test(self):
|
81 | 87 | self.log.info("Don't fetch blocks we already have")
|
82 | 88 | assert_raises_rpc_error(-1, "Block already downloaded", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id)
|
83 | 89 |
|
| 90 | + self.log.info("Don't fetch blocks while the node has not synced past it yet") |
| 91 | + # For this test we need node 1 in prune mode and as a side effect this also disconnects |
| 92 | + # the nodes which is also necessary for the rest of the test. |
| 93 | + self.restart_node(1, ["-prune=550"]) |
| 94 | + |
| 95 | + # Generate a block on the disconnected node that the pruning node is not connected to |
| 96 | + blockhash = self.generate(self.nodes[0], 1, sync_fun=self.no_op)[0] |
| 97 | + block_hex = self.nodes[0].getblock(blockhash=blockhash, verbosity=0) |
| 98 | + block = from_hex(CBlock(), block_hex) |
| 99 | + |
| 100 | + # Connect a P2PInterface to the pruning node and have it submit only the header of the |
| 101 | + # block that the pruning node has not seen |
| 102 | + node1_interface = self.nodes[1].add_p2p_connection(P2PInterface()) |
| 103 | + node1_interface.send_message(msg_headers([block])) |
| 104 | + |
| 105 | + # Get the peer id of the P2PInterface from the pruning node |
| 106 | + node1_peers = self.nodes[1].getpeerinfo() |
| 107 | + assert_equal(len(node1_peers), 1) |
| 108 | + node1_interface_id = node1_peers[0]["id"] |
| 109 | + |
| 110 | + # Trying to fetch this block from the P2PInterface should not be possible |
| 111 | + error_msg = "In prune mode, only blocks that the node has already synced previously can be fetched from a peer" |
| 112 | + assert_raises_rpc_error(-1, error_msg, self.nodes[1].getblockfrompeer, blockhash, node1_interface_id) |
| 113 | + |
84 | 114 |
|
85 | 115 | if __name__ == '__main__':
|
86 | 116 | GetBlockFromPeerTest().main()
|
0 commit comments