mirror of https://github.com/bitcoin/bitcoin
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.7 KiB
65 lines
2.7 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test the invalidateblock RPC."""
|
||
10 years ago
|
|
||
10 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
10 years ago
|
|
||
|
class InvalidateTest(BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
9 years ago
|
self.setup_clean_chain = True
|
||
|
self.num_nodes = 3
|
||
|
|
||
10 years ago
|
def setup_network(self):
|
||
8 years ago
|
self.setup_nodes()
|
||
|
|
||
10 years ago
|
def run_test(self):
|
||
8 years ago
|
self.log.info("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:")
|
||
|
self.log.info("Mine 4 blocks on Node 0")
|
||
10 years ago
|
self.nodes[0].generate(4)
|
||
10 years ago
|
assert(self.nodes[0].getblockcount() == 4)
|
||
|
besthash = self.nodes[0].getbestblockhash()
|
||
|
|
||
8 years ago
|
self.log.info("Mine competing 6 blocks on Node 1")
|
||
10 years ago
|
self.nodes[1].generate(6)
|
||
10 years ago
|
assert(self.nodes[1].getblockcount() == 6)
|
||
|
|
||
8 years ago
|
self.log.info("Connect nodes to force a reorg")
|
||
10 years ago
|
connect_nodes_bi(self.nodes,0,1)
|
||
10 years ago
|
sync_blocks(self.nodes[0:2])
|
||
10 years ago
|
assert(self.nodes[0].getblockcount() == 6)
|
||
|
badhash = self.nodes[1].getblockhash(2)
|
||
|
|
||
8 years ago
|
self.log.info("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain")
|
||
10 years ago
|
self.nodes[0].invalidateblock(badhash)
|
||
|
newheight = self.nodes[0].getblockcount()
|
||
|
newhash = self.nodes[0].getbestblockhash()
|
||
|
if (newheight != 4 or newhash != besthash):
|
||
|
raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
|
||
|
|
||
8 years ago
|
self.log.info("Make sure we won't reorg to a lower work chain:")
|
||
10 years ago
|
connect_nodes_bi(self.nodes,1,2)
|
||
8 years ago
|
self.log.info("Sync node 2 to node 1 so both have 6 blocks")
|
||
10 years ago
|
sync_blocks(self.nodes[1:3])
|
||
|
assert(self.nodes[2].getblockcount() == 6)
|
||
8 years ago
|
self.log.info("Invalidate block 5 on node 1 so its tip is now at 4")
|
||
10 years ago
|
self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
|
||
|
assert(self.nodes[1].getblockcount() == 4)
|
||
8 years ago
|
self.log.info("Invalidate block 3 on node 2, so its tip is now 2")
|
||
10 years ago
|
self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))
|
||
|
assert(self.nodes[2].getblockcount() == 2)
|
||
8 years ago
|
self.log.info("..and then mine a block")
|
||
10 years ago
|
self.nodes[2].generate(1)
|
||
8 years ago
|
self.log.info("Verify all nodes are at the right height")
|
||
10 years ago
|
time.sleep(5)
|
||
8 years ago
|
assert_equal(self.nodes[2].getblockcount(), 3)
|
||
|
assert_equal(self.nodes[0].getblockcount(), 4)
|
||
10 years ago
|
node1height = self.nodes[1].getblockcount()
|
||
|
if node1height < 4:
|
||
|
raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
|
||
|
|
||
10 years ago
|
if __name__ == '__main__':
|
||
|
InvalidateTest().main()
|