1313
1414class GetBlockFromPeerTest (BitcoinTestFramework ):
1515 def set_test_params (self ):
16- self .num_nodes = 2
16+ self .num_nodes = 3
17+ self .extra_args = [
18+ [],
19+ [],
20+ ["-fastprune" , "-prune=1" ]
21+ ]
1722
1823 def setup_network (self ):
1924 self .setup_nodes ()
2025
21- def check_for_block (self , hash ):
26+ def check_for_block (self , node , hash ):
2227 try :
23- self . nodes [ 0 ] .getblock (hash )
28+ node .getblock (hash )
2429 return True
2530 except JSONRPCException :
2631 return False
@@ -37,7 +42,7 @@ def run_test(self):
3742
3843 self .log .info ("Connect nodes to sync headers" )
3944 self .connect_nodes (0 , 1 )
40- self .sync_blocks ()
45+ self .sync_blocks (self . nodes [ 0 : 1 ] )
4146
4247 self .log .info ("Node 0 should only have the header for node 1's block 3" )
4348 for x in self .nodes [0 ].getchaintips ():
@@ -64,13 +69,33 @@ def run_test(self):
6469
6570 self .log .info ("Successful fetch" )
6671 result = self .nodes [0 ].getblockfrompeer (short_tip , peer_0_peer_1_id )
67- self .wait_until (lambda : self .check_for_block (short_tip ), timeout = 1 )
72+ self .wait_until (lambda : self .check_for_block (self . nodes [ 0 ], short_tip ), timeout = 1 )
6873 assert (not "warnings" in result )
6974
7075 self .log .info ("Don't fetch blocks we already have" )
7176 result = self .nodes [0 ].getblockfrompeer (short_tip , peer_0_peer_1_id )
7277 assert ("warnings" in result )
7378 assert_equal (result ["warnings" ], "Block already downloaded" )
7479
80+ self .log .info ("Connect pruned node" )
81+ # We need to generate more blocks to be able to prune
82+ self .generate (self .nodes [0 ], 400 )
83+ self .connect_nodes (0 , 2 )
84+ self .sync_blocks ()
85+ pruneheight = self .nodes [2 ].pruneblockchain (300 )
86+ assert_equal (pruneheight , 248 )
87+ # Ensure the block is actually pruned
88+ pruned_block = self .nodes [0 ].getblockhash (2 )
89+ assert_raises_rpc_error (- 1 , "Block not available (pruned data)" , self .nodes [2 ].getblock , pruned_block )
90+
91+ self .log .info ("Fetch pruned block" )
92+ peers = self .nodes [2 ].getpeerinfo ()
93+ assert_equal (len (peers ), 1 )
94+ peer_2_peer_0_id = peers [0 ]["id" ]
95+ result = self .nodes [2 ].getblockfrompeer (pruned_block , peer_2_peer_0_id )
96+ self .wait_until (lambda : self .check_for_block (self .nodes [2 ], pruned_block ), timeout = 1 )
97+ assert (not "warnings" in result )
98+
99+
75100if __name__ == '__main__' :
76101 GetBlockFromPeerTest ().main ()
0 commit comments