Skip to content

Commit 0421c18

Browse files
committed
Fix CheckBlockIndex for reindex.
Some tests in CheckBlockIndex require chainActive.Tip(), but when reindexing, chainActive has not been set on the first call to CheckBlockIndex. reindex.py starts a node, mines 3 blocks, stops, and reindexes with CheckBlockIndex enabled.
1 parent d3eb5ae commit 0421c18

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

qa/rpc-tests/reindex.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 -reindex with CheckBlockIndex
8+
#
9+
from test_framework import BitcoinTestFramework
10+
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
11+
from util import *
12+
import os.path
13+
14+
class ReindexTest(BitcoinTestFramework):
15+
16+
def setup_chain(self):
17+
print("Initializing test directory "+self.options.tmpdir)
18+
initialize_chain_clean(self.options.tmpdir, 1)
19+
20+
def setup_network(self):
21+
self.nodes = []
22+
self.is_network_split = False
23+
self.nodes.append(start_node(0, self.options.tmpdir))
24+
25+
def run_test(self):
26+
self.nodes[0].generate(3)
27+
stop_node(self.nodes[0], 0)
28+
wait_bitcoinds()
29+
self.nodes[0]=start_node(0, self.options.tmpdir, ["-debug", "-reindex", "-checkblockindex=1"])
30+
assert_equal(self.nodes[0].getblockcount(), 3)
31+
print "Success"
32+
33+
if __name__ == '__main__':
34+
ReindexTest().main()

src/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,6 +3227,14 @@ void static CheckBlockIndex()
32273227

32283228
LOCK(cs_main);
32293229

3230+
// During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
3231+
// so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
3232+
// iterating the block tree require that chainActive has been initialized.)
3233+
if (chainActive.Height() < 0) {
3234+
assert(mapBlockIndex.size() <= 1);
3235+
return;
3236+
}
3237+
32303238
// Build forward-pointing map of the entire block tree.
32313239
std::multimap<CBlockIndex*,CBlockIndex*> forward;
32323240
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {

0 commit comments

Comments
 (0)