Skip to content

Commit 84a7789

Browse files
committed
Merge pull request #5879
88f6c8c add RPC test for InvalidateBlock (Alex Morcos) a9af415 fix InvalidateBlock to repopulate setBlockIndexCandidates (Alex Morcos)
2 parents dd4ffce + 88f6c8c commit 84a7789

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

qa/rpc-tests/invalidateblock.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2014 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+
6+
#
7+
# Test InvalidateBlock code
8+
#
9+
10+
from test_framework import BitcoinTestFramework
11+
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
12+
from util import *
13+
14+
class InvalidateTest(BitcoinTestFramework):
15+
16+
17+
def setup_chain(self):
18+
print("Initializing test directory "+self.options.tmpdir)
19+
initialize_chain_clean(self.options.tmpdir, 2)
20+
21+
def setup_network(self):
22+
self.nodes = []
23+
self.is_network_split = False
24+
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
25+
self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"]))
26+
27+
def run_test(self):
28+
print "Mine 4 blocks on Node 0"
29+
self.nodes[0].setgenerate(True, 4)
30+
assert(self.nodes[0].getblockcount() == 4)
31+
besthash = self.nodes[0].getbestblockhash()
32+
33+
print "Mine competing 6 blocks on Node 1"
34+
self.nodes[1].setgenerate(True, 6)
35+
assert(self.nodes[1].getblockcount() == 6)
36+
37+
print "Connect nodes to force a reorg"
38+
connect_nodes_bi(self.nodes,0,1)
39+
sync_blocks(self.nodes)
40+
assert(self.nodes[0].getblockcount() == 6)
41+
badhash = self.nodes[1].getblockhash(2)
42+
43+
print "Invalidate block 2 on node 0 and verify we reorg to node 0's original chain"
44+
self.nodes[0].invalidateblock(badhash)
45+
newheight = self.nodes[0].getblockcount()
46+
newhash = self.nodes[0].getbestblockhash()
47+
if (newheight != 4 or newhash != besthash):
48+
raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
49+
50+
if __name__ == '__main__':
51+
InvalidateTest().main()

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,7 @@ bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex) {
23112311
BlockMap::iterator it = mapBlockIndex.begin();
23122312
while (it != mapBlockIndex.end()) {
23132313
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2314-
setBlockIndexCandidates.insert(pindex);
2314+
setBlockIndexCandidates.insert(it->second);
23152315
}
23162316
it++;
23172317
}

0 commit comments

Comments
 (0)