rpc_getchaintips.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-2021 The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test the getchaintips RPC. 6 7 - introduce a network split 8 - work on chains of different lengths 9 - join the network together again 10 - verify that getchaintips now returns two chain tips. 11 """ 12 13 from test_framework.test_framework import BitcoinTestFramework 14 from test_framework.util import assert_equal 15 16 class GetChainTipsTest (BitcoinTestFramework): 17 def set_test_params(self): 18 self.num_nodes = 4 19 20 def run_test(self): 21 tips = self.nodes[0].getchaintips() 22 assert_equal(len(tips), 1) 23 assert_equal(tips[0]['branchlen'], 0) 24 assert_equal(tips[0]['height'], 200) 25 assert_equal(tips[0]['status'], 'active') 26 27 # Split the network and build two chains of different lengths. 28 self.split_network() 29 self.generate(self.nodes[0], 10, sync_fun=lambda: self.sync_all(self.nodes[:2])) 30 self.generate(self.nodes[2], 20, sync_fun=lambda: self.sync_all(self.nodes[2:])) 31 32 tips = self.nodes[1].getchaintips () 33 assert_equal (len (tips), 1) 34 shortTip = tips[0] 35 assert_equal (shortTip['branchlen'], 0) 36 assert_equal (shortTip['height'], 210) 37 assert_equal (tips[0]['status'], 'active') 38 39 tips = self.nodes[3].getchaintips () 40 assert_equal (len (tips), 1) 41 longTip = tips[0] 42 assert_equal (longTip['branchlen'], 0) 43 assert_equal (longTip['height'], 220) 44 assert_equal (tips[0]['status'], 'active') 45 46 # Join the network halves and check that we now have two tips 47 # (at least at the nodes that previously had the short chain). 48 self.join_network () 49 50 tips = self.nodes[0].getchaintips () 51 assert_equal (len (tips), 2) 52 assert_equal (tips[0], longTip) 53 54 assert_equal (tips[1]['branchlen'], 10) 55 assert_equal (tips[1]['status'], 'valid-fork') 56 tips[1]['branchlen'] = 0 57 tips[1]['status'] = 'active' 58 assert_equal (tips[1], shortTip) 59 60 if __name__ == '__main__': 61 GetChainTipsTest ().main ()