Skip to content

Commit bafc8da

Browse files
alexalex
authored andcommitted
Checkpoint object replacement with std::pair & support for incompatible database removal
1 parent 0fce2b7 commit bafc8da

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

src/checkpoints.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,7 @@
1313

1414
namespace Checkpoints
1515
{
16-
struct Checkpoint
17-
{
18-
uint256 hashCheckPoint;
19-
unsigned int nTime;
20-
};
21-
22-
typedef std::map<int, Checkpoint> MapCheckpoints;
23-
24-
Checkpoint initCheckpoint(uint256 hashCheckPoint, unsigned int nTime)
25-
{
26-
Checkpoint item;
27-
item.hashCheckPoint = hashCheckPoint;
28-
item.nTime = nTime;
29-
30-
return item;
31-
}
16+
typedef std::map<int, std::pair<uint256, unsigned int> > MapCheckpoints;
3217

3318
//
3419
// What makes a good checkpoint block?
@@ -39,18 +24,18 @@ namespace Checkpoints
3924
//
4025
static MapCheckpoints mapCheckpoints =
4126
boost::assign::map_list_of
42-
( 0, initCheckpoint(hashGenesisBlock, 1360105017) )
43-
( 9690, initCheckpoint(uint256("0x00000000026561450859c46868099e0df6068a538f038cb18988fd8d47dcdaf5"), 1362791423) )
44-
( 13560, initCheckpoint(uint256("0xa1591a0fcbf11f282d671581edb9f0aadcd06fee69761081e0a3245914c13729"), 1364674052) )
45-
( 37092, initCheckpoint(uint256("0x0000000000a38c2f98556f46793b453e92d8fab2d31c0b93fd08bcf78e56099d"), 1376677203) )
46-
( 44200, initCheckpoint(uint256("0xc9bda7232a18b9c1f5ff974a9e5566b2d1879ceb8fc0e9e61fba9038a25b8447"), 1380145962) )
47-
( 65000, initCheckpoint(uint256("0xfb2b51a2fd65062c98a7a6053cde46aeaefebb95ba2f680e85a29ee25b1dcf05"), 1388526385) )
27+
( 0, std::make_pair(hashGenesisBlock, 1360105017) )
28+
( 9690, std::make_pair(uint256("0x00000000026561450859c46868099e0df6068a538f038cb18988fd8d47dcdaf5"), 1362791423) )
29+
( 13560, std::make_pair(uint256("0xa1591a0fcbf11f282d671581edb9f0aadcd06fee69761081e0a3245914c13729"), 1364674052) )
30+
( 37092, std::make_pair(uint256("0x0000000000a38c2f98556f46793b453e92d8fab2d31c0b93fd08bcf78e56099d"), 1376677203) )
31+
( 44200, std::make_pair(uint256("0xc9bda7232a18b9c1f5ff974a9e5566b2d1879ceb8fc0e9e61fba9038a25b8447"), 1380145962) )
32+
( 65000, std::make_pair(uint256("0xfb2b51a2fd65062c98a7a6053cde46aeaefebb95ba2f680e85a29ee25b1dcf05"), 1388526385) )
4833
;
4934

5035
// TestNet has no checkpoints
5136
static MapCheckpoints mapCheckpointsTestnet =
5237
boost::assign::map_list_of
53-
( 0, initCheckpoint(hashGenesisBlockTestNet, 1360105017) )
38+
( 0, std::make_pair(hashGenesisBlockTestNet, 1360105017) )
5439
;
5540

5641
bool CheckHardened(int nHeight, const uint256& hash)
@@ -59,7 +44,7 @@ namespace Checkpoints
5944

6045
MapCheckpoints::const_iterator i = checkpoints.find(nHeight);
6146
if (i == checkpoints.end()) return true;
62-
return hash == i->second.hashCheckPoint;
47+
return hash == i->second.first;
6348
}
6449

6550
int GetTotalBlocksEstimate()
@@ -73,7 +58,7 @@ namespace Checkpoints
7358
{
7459
MapCheckpoints& checkpoints = (fTestNet ? mapCheckpointsTestnet : mapCheckpoints);
7560

76-
return checkpoints.rbegin()->second.nTime;
61+
return checkpoints.rbegin()->second.second;
7762
}
7863

7964
CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
@@ -82,7 +67,7 @@ namespace Checkpoints
8267

8368
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, checkpoints)
8469
{
85-
const uint256& hash = i.second.hashCheckPoint;
70+
const uint256& hash = i.second.first;
8671
std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
8772
if (t != mapBlockIndex.end())
8873
return t->second;
@@ -266,7 +251,7 @@ namespace Checkpoints
266251
bool ResetSyncCheckpoint()
267252
{
268253
LOCK(cs_hashSyncCheckpoint);
269-
const uint256& hash = mapCheckpoints.rbegin()->second.hashCheckPoint;
254+
const uint256& hash = mapCheckpoints.rbegin()->second.first;
270255
if (mapBlockIndex.count(hash) && !mapBlockIndex[hash]->IsInMainChain())
271256
{
272257
// checkpoint block accepted but not yet in main chain
@@ -290,7 +275,7 @@ namespace Checkpoints
290275

291276
BOOST_REVERSE_FOREACH(const MapCheckpoints::value_type& i, mapCheckpoints)
292277
{
293-
const uint256& hash = i.second.hashCheckPoint;
278+
const uint256& hash = i.second.first;
294279
if (mapBlockIndex.count(hash) && mapBlockIndex[hash]->IsInMainChain())
295280
{
296281
if (!WriteSyncCheckpoint(hash))

src/txdb-leveldb.cpp

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ static leveldb::Options GetOptions() {
3333
return options;
3434
}
3535

36+
void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
37+
// First time init.
38+
filesystem::path directory = GetDataDir() / "txleveldb";
39+
40+
if (fRemoveOld) {
41+
filesystem::remove_all(directory); // remove directory
42+
unsigned int nFile = 1;
43+
44+
while (true)
45+
{
46+
filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
47+
48+
// Break if no such file
49+
if( !filesystem::exists( strBlockFile ) )
50+
break;
51+
52+
filesystem::remove(strBlockFile);
53+
54+
nFile++;
55+
}
56+
}
57+
58+
filesystem::create_directory(directory);
59+
printf("Opening LevelDB in %s\n", directory.string().c_str());
60+
leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
61+
if (!status.ok()) {
62+
throw runtime_error(strprintf("init_blockindex(): error opening database environment %s", status.ToString().c_str()));
63+
}
64+
}
65+
3666
// CDB subclasses are created and destroyed VERY OFTEN. That's why
3767
// we shouldn't treat this as a free operations.
3868
CTxDB::CTxDB(const char* pszMode)
@@ -46,27 +76,40 @@ CTxDB::CTxDB(const char* pszMode)
4676
return;
4777
}
4878

49-
// First time init.
50-
filesystem::path directory = GetDataDir() / "txleveldb";
5179
bool fCreate = strchr(pszMode, 'c');
5280

5381
options = GetOptions();
5482
options.create_if_missing = fCreate;
5583
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
56-
filesystem::create_directory(directory);
57-
printf("Opening LevelDB in %s\n", directory.string().c_str());
58-
leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
59-
if (!status.ok()) {
60-
throw runtime_error(strprintf("CDB(): error opening database environment %s", status.ToString().c_str()));
61-
}
84+
85+
init_blockindex(options); // Init directory
6286
pdb = txdb;
6387

6488
if (Exists(string("version")))
6589
{
6690
ReadVersion(nVersion);
6791
printf("Transaction index version is %d\n", nVersion);
92+
93+
if (nVersion < DATABASE_VERSION)
94+
{
95+
printf("Required index version is %d, removing old database\n", DATABASE_VERSION);
96+
97+
// Leveldb instance destruction
98+
delete txdb;
99+
txdb = pdb = NULL;
100+
delete activeBatch;
101+
activeBatch = NULL;
102+
103+
init_blockindex(options, true); // Remove directory and create new database
104+
pdb = txdb;
105+
106+
bool fTmp = fReadOnly;
107+
fReadOnly = false;
108+
WriteVersion(DATABASE_VERSION); // Save transaction index version
109+
fReadOnly = fTmp;
110+
}
68111
}
69-
else if(fCreate)
112+
else if (fCreate)
70113
{
71114
bool fTmp = fReadOnly;
72115
fReadOnly = false;

src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern const std::string CLIENT_DATE;
2424
//
2525
// database format versioning
2626
//
27-
static const int DATABASE_VERSION = 60011;
27+
static const int DATABASE_VERSION = 70505;
2828

2929
//
3030
// network protocol versioning

0 commit comments

Comments
 (0)