Skip to content

Commit 5826bf5

Browse files
committed
test: Add test for getblockfrompeer on syncing pruned nodes
1 parent 7fa851f commit 5826bf5

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

test/functional/rpc_getblockfrompeer.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
"""Test the getblockfrompeer RPC."""
66

77
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+
)
914
from test_framework.p2p import (
1015
P2P_SERVICES,
1116
P2PInterface,
@@ -16,6 +21,7 @@
1621
assert_raises_rpc_error,
1722
)
1823

24+
1925
class GetBlockFromPeerTest(BitcoinTestFramework):
2026
def set_test_params(self):
2127
self.num_nodes = 2
@@ -78,6 +84,30 @@ def run_test(self):
7884
self.log.info("Don't fetch blocks we already have")
7985
assert_raises_rpc_error(-1, "Block already downloaded", self.nodes[0].getblockfrompeer, short_tip, peer_0_peer_1_id)
8086

87+
self.log.info("Don't fetch blocks while the node has not synced past it yet")
88+
# For this test we need node 1 in prune mode and as a side effect this also disconnects
89+
# the nodes which is also necessary for the rest of the test.
90+
self.restart_node(1, ["-prune=550"])
91+
92+
# Generate a block on the disconnected node that the pruning node is not connected to
93+
blockhash = self.generate(self.nodes[0], 1, sync_fun=self.no_op)[0]
94+
block_hex = self.nodes[0].getblock(blockhash=blockhash, verbosity=0)
95+
block = from_hex(CBlock(), block_hex)
96+
97+
# Connect a P2PInterface to the pruning node and have it submit only the header of the
98+
# block that the pruning node has not seen
99+
node1_interface = self.nodes[1].add_p2p_connection(P2PInterface())
100+
node1_interface.send_message(msg_headers([block]))
101+
102+
# Get the peer id of the P2PInterface from the pruning node
103+
node1_peers = self.nodes[1].getpeerinfo()
104+
assert_equal(len(node1_peers), 1)
105+
node1_interface_id = node1_peers[0]["id"]
106+
107+
# Trying to fetch this block from the P2PInterface should not be possible
108+
error_msg = "In prune mode, only blocks that the node has already synced previously can be fetched from a peer"
109+
assert_raises_rpc_error(-1, error_msg, self.nodes[1].getblockfrompeer, blockhash, node1_interface_id)
110+
81111

82112
if __name__ == '__main__':
83113
GetBlockFromPeerTest().main()

0 commit comments

Comments
 (0)