2 # Copyright (c) 2014-2016 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.
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.
13 from test_framework
.test_framework
import BitcoinTestFramework
14 from test_framework
.util
import assert_equal
16 class GetChainTipsTest (BitcoinTestFramework
):
18 tips
= self
.nodes
[0].getchaintips ()
19 assert_equal (len (tips
), 1)
20 assert_equal (tips
[0]['branchlen'], 0)
21 assert_equal (tips
[0]['height'], 200)
22 assert_equal (tips
[0]['status'], 'active')
24 # Split the network and build two chains of different lengths.
26 self
.nodes
[0].generate(10)
27 self
.nodes
[2].generate(20)
28 self
.sync_all([self
.nodes
[:2], self
.nodes
[2:]])
30 tips
= self
.nodes
[1].getchaintips ()
31 assert_equal (len (tips
), 1)
33 assert_equal (shortTip
['branchlen'], 0)
34 assert_equal (shortTip
['height'], 210)
35 assert_equal (tips
[0]['status'], 'active')
37 tips
= self
.nodes
[3].getchaintips ()
38 assert_equal (len (tips
), 1)
40 assert_equal (longTip
['branchlen'], 0)
41 assert_equal (longTip
['height'], 220)
42 assert_equal (tips
[0]['status'], 'active')
44 # Join the network halves and check that we now have two tips
45 # (at least at the nodes that previously had the short chain).
48 tips
= self
.nodes
[0].getchaintips ()
49 assert_equal (len (tips
), 2)
50 assert_equal (tips
[0], longTip
)
52 assert_equal (tips
[1]['branchlen'], 10)
53 assert_equal (tips
[1]['status'], 'valid-fork')
54 tips
[1]['branchlen'] = 0
55 tips
[1]['status'] = 'active'
56 assert_equal (tips
[1], shortTip
)
58 if __name__
== '__main__':
59 GetChainTipsTest ().main ()