/ test / functional / rpc_getchaintips.py
rpc_getchaintips.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2014-present 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.blocktools import (
14      create_block,
15      create_coinbase,
16  )
17  from test_framework.test_framework import BitcoinTestFramework
18  from test_framework.util import assert_equal
19  
20  class GetChainTipsTest (BitcoinTestFramework):
21      def set_test_params(self):
22          self.num_nodes = 4
23  
24      def run_test(self):
25          self.log.info("Test getchaintips behavior with two chains of different length")
26          tips = self.nodes[0].getchaintips()
27          assert_equal(len(tips), 1)
28          assert_equal(tips[0]['branchlen'], 0)
29          assert_equal(tips[0]['height'], 200)
30          assert_equal(tips[0]['status'], 'active')
31  
32          self.log.info("Split the network and build two chains of different lengths.")
33          self.split_network()
34          self.generate(self.nodes[0], 10, sync_fun=lambda: self.sync_all(self.nodes[:2]))
35          self.generate(self.nodes[2], 20, sync_fun=lambda: self.sync_all(self.nodes[2:]))
36  
37          tips = self.nodes[1].getchaintips()
38          assert_equal(len(tips), 1)
39          shortTip = tips[0]
40          assert_equal(shortTip['branchlen'], 0)
41          assert_equal(shortTip['height'], 210)
42          assert_equal(tips[0]['status'], 'active')
43  
44          tips = self.nodes[3].getchaintips()
45          assert_equal(len(tips), 1)
46          longTip = tips[0]
47          assert_equal(longTip['branchlen'], 0)
48          assert_equal(longTip['height'], 220)
49          assert_equal(tips[0]['status'], 'active')
50  
51          self.log.info("Join the network halves and check that we now have two tips")
52          # (at least at the nodes that previously had the short chain).
53          self.join_network()
54  
55          tips = self.nodes[0].getchaintips()
56          assert_equal(len(tips), 2)
57          assert_equal(tips[0], longTip)
58  
59          assert_equal(tips[1]['branchlen'], 10)
60          assert_equal(tips[1]['status'], 'valid-fork')
61          tips[1]['branchlen'] = 0
62          tips[1]['status'] = 'active'
63          assert_equal(tips[1], shortTip)
64  
65          self.log.info("Test getchaintips behavior with invalid blocks")
66          self.disconnect_nodes(0, 1)
67          n0 = self.nodes[0]
68          tip = int(n0.getbestblockhash(), 16)
69          start_height = self.nodes[0].getblockcount()
70          # Create invalid block (too high coinbase)
71          block_time = n0.getblock(n0.getbestblockhash())['time'] + 1
72          invalid_block = create_block(tip, create_coinbase(start_height + 1, nValue=100), ntime=block_time)
73          invalid_block.solve()
74  
75          block_time += 1
76          block2 = create_block(invalid_block.hash_int, height=2, ntime=block_time, version=4)
77          block2.solve()
78  
79          self.log.info("Submit headers-only chain")
80          n0.submitheader(invalid_block.serialize().hex())
81          n0.submitheader(block2.serialize().hex())
82          tips = n0.getchaintips()
83          assert_equal(len(tips), 3)
84          assert_equal(tips[0]['status'], 'headers-only')
85  
86          self.log.info("Submit invalid block that invalidates the headers-only chain")
87          n0.submitblock(invalid_block.serialize().hex())
88          tips = n0.getchaintips()
89          assert_equal(len(tips), 3)
90          assert_equal(tips[0]['status'], 'invalid')
91  
92  
93  if __name__ == '__main__':
94      GetChainTipsTest(__file__).main()