Skip to content

Commit 22cfe23

Browse files
committed
Merge pull request #5890
cd3d67c Fix InvalidateBlock to add chainActive.Tip to setBlockIndexCandidates (Alex Morcos)
2 parents 2afd919 + cd3d67c commit 22cfe23

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

qa/rpc-tests/invalidateblock.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ class InvalidateTest(BitcoinTestFramework):
1616

1717
def setup_chain(self):
1818
print("Initializing test directory "+self.options.tmpdir)
19-
initialize_chain_clean(self.options.tmpdir, 2)
19+
initialize_chain_clean(self.options.tmpdir, 3)
2020

2121
def setup_network(self):
2222
self.nodes = []
2323
self.is_network_split = False
2424
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
2525
self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"]))
26+
self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"]))
2627

2728
def run_test(self):
29+
print "Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:"
2830
print "Mine 4 blocks on Node 0"
2931
self.nodes[0].setgenerate(True, 4)
3032
assert(self.nodes[0].getblockcount() == 4)
@@ -36,7 +38,7 @@ def run_test(self):
3638

3739
print "Connect nodes to force a reorg"
3840
connect_nodes_bi(self.nodes,0,1)
39-
sync_blocks(self.nodes)
41+
sync_blocks(self.nodes[0:2])
4042
assert(self.nodes[0].getblockcount() == 6)
4143
badhash = self.nodes[1].getblockhash(2)
4244

@@ -47,5 +49,28 @@ def run_test(self):
4749
if (newheight != 4 or newhash != besthash):
4850
raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight))
4951

52+
print "\nMake sure we won't reorg to a lower work chain:"
53+
connect_nodes_bi(self.nodes,1,2)
54+
print "Sync node 2 to node 1 so both have 6 blocks"
55+
sync_blocks(self.nodes[1:3])
56+
assert(self.nodes[2].getblockcount() == 6)
57+
print "Invalidate block 5 on node 1 so its tip is now at 4"
58+
self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5))
59+
assert(self.nodes[1].getblockcount() == 4)
60+
print "Invalidate block 3 on node 2, so its tip is now 2"
61+
self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3))
62+
assert(self.nodes[2].getblockcount() == 2)
63+
print "..and then mine a block"
64+
self.nodes[2].setgenerate(True, 1)
65+
print "Verify all nodes are at the right height"
66+
time.sleep(5)
67+
for i in xrange(3):
68+
print i,self.nodes[i].getblockcount()
69+
assert(self.nodes[2].getblockcount() == 3)
70+
assert(self.nodes[0].getblockcount() == 4)
71+
node1height = self.nodes[1].getblockcount()
72+
if node1height < 4:
73+
raise AssertionError("Node 1 reorged to a lower height: %d"%node1height)
74+
5075
if __name__ == '__main__':
5176
InvalidateTest().main()

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex) {
23202320
// add them again.
23212321
BlockMap::iterator it = mapBlockIndex.begin();
23222322
while (it != mapBlockIndex.end()) {
2323-
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2323+
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
23242324
setBlockIndexCandidates.insert(it->second);
23252325
}
23262326
it++;

0 commit comments

Comments
 (0)