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.

71 lines
3.0 KiB

#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or
"""Test the invalidateblock RPC."""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
class InvalidateTest(BitcoinTestFramework):
def __init__(self):
self.setup_clean_chain = True
self.num_nodes = 3
def setup_network(self):
self.nodes = []
self.is_network_split = False
self.nodes.append(start_node(0, self.options.tmpdir))
self.nodes.append(start_node(1, self.options.tmpdir))
self.nodes.append(start_node(2, self.options.tmpdir))
def run_test(self):"Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:")"Mine 4 blocks on Node 0")
assert(self.nodes[0].getblockcount() == 4)
besthash = self.nodes[0].getbestblockhash()"Mine competing 6 blocks on Node 1")
assert(self.nodes[1].getblockcount() == 6)"Connect nodes to force a reorg")
assert(self.nodes[0].getblockcount() == 6)
badhash = self.nodes[1].getblockhash(2)"Invalidate block 2 on node 0 and verify we reorg to node 0's original chain")
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))"Make sure we won't reorg to a lower work chain:")
connect_nodes_bi(self.nodes,1,2)"Sync node 2 to node 1 so both have 6 blocks")
assert(self.nodes[2].getblockcount() == 6)"Invalidate block 5 on node 1 so its tip is now at 4")
assert(self.nodes[1].getblockcount() == 4)"Invalidate block 3 on node 2, so its tip is now 2")
assert(self.nodes[2].getblockcount() == 2)"..and then mine a block")
self.nodes[2].generate(1)"Verify all nodes are at the right height")
assert_equal(self.nodes[2].getblockcount(), 3)
assert_equal(self.nodes[0].getblockcount(), 4)
node1height = self.nodes[1].getblockcount()
if node1height < 4:
raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
if __name__ == '__main__':