Skip to content

Commit 21e3148

Browse files
authored
Update txdb-leveldb.cpp
1 parent d0e8e4d commit 21e3148

File tree

1 file changed

+34
-54
lines changed

1 file changed

+34
-54
lines changed

src/txdb-leveldb.cpp

+34-54
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
#include <boost/filesystem.hpp>
1212
#include <boost/filesystem/fstream.hpp>
1313

14-
#include <leveldb/env.h>
15-
#include <leveldb/cache.h>
16-
#include <leveldb/slice.h>
17-
#include <leveldb/filter_policy.h>
18-
#include <memenv/memenv.h>
14+
#include <leveldb/include/leveldb/env.h>
15+
#include <leveldb/include/leveldb/cache.h>
16+
#include <leveldb/include/leveldb/slice.h>
17+
#include <leveldb/include/leveldb/filter_policy.h>
18+
#include <leveldb/helpers/memenv/memenv.h>
1919

2020
#include "kernel.h"
2121
#include "checkpoints.h"
@@ -38,29 +38,28 @@ static leveldb::Options GetOptions() {
3838
return options;
3939
}
4040

41-
41+
void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
4242
// First time init.
4343
boost::filesystem::path directory = GetDataDir() / "txleveldb";
44-
bool fCreate = strchr(pszMode, 'c');
4544

4645
if (fRemoveOld) {
4746
boost::filesystem::remove_all(directory); // remove directory
4847
unsigned int nFile = 1;
49-
filesystem::path bootstrap = GetDataDir() / "bootstrap.dat";
50-
boost::filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
5148

52-
if (txdb) {
53-
pdb = txdb;
54-
return;
55-
}
49+
while (true)
50+
{
51+
boost::filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
5652

57-
// First time init.
58-
filesystem::path directory = GetDataDir() / "txleveldb";
59-
bool fCreate = strchr(pszMode, 'c');
53+
// Break if no such file
54+
if( !boost::filesystem::exists( strBlockFile ) )
55+
break;
56+
57+
boost::filesystem::remove(strBlockFile);
58+
59+
nFile++;
60+
}
61+
}
6062

61-
options = GetOptions();
62-
options.create_if_missing = fCreate;
63-
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
6463
boost::filesystem::create_directory(directory);
6564
LogPrintf("Opening LevelDB in %s\n", directory.string());
6665
leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
@@ -82,19 +81,13 @@ CTxDB::CTxDB(const char* pszMode)
8281
return;
8382
}
8483

85-
// First time init.
86-
filesystem::path directory = GetDataDir() / "txleveldb";
8784
bool fCreate = strchr(pszMode, 'c');
8885

8986
options = GetOptions();
9087
options.create_if_missing = fCreate;
9188
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
92-
filesystem::create_directory(directory);
93-
printf("Opening LevelDB in %s\n", directory.string().c_str());
94-
leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
95-
if (!status.ok()) {
96-
throw runtime_error(strprintf("CDB(): error opening database environment %s", status.ToString().c_str()));
97-
}
89+
90+
init_blockindex(options); // Init directory
9891
pdb = txdb;
9992

10093
if (Exists(string("version")))
@@ -215,12 +208,12 @@ bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
215208
}
216209

217210
bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
218-
{
211+
{
219212
return Write(make_pair(string("tx"), hash), txindex);
220213
}
221214

222215
bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
223-
{
216+
{
224217
// Add to tx index
225218
uint256 hash = tx.GetHash();
226219
CTxIndex txindex(pos, tx.vout.size());
@@ -230,7 +223,7 @@ bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeigh
230223
bool CTxDB::EraseTxIndex(const CTransaction& tx)
231224
{
232225
uint256 hash = tx.GetHash();
233-
226+
234227
return Erase(make_pair(string("tx"), hash));
235228
}
236229

@@ -240,7 +233,7 @@ bool CTxDB::ContainsTx(uint256 hash)
240233
}
241234

242235
bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
243-
{
236+
{
244237
tx.SetNull();
245238
if (!ReadTxIndex(hash, txindex))
246239
return false;
@@ -309,16 +302,6 @@ bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
309302
return Write(string("strCheckpointPubKey"), strPubKey);
310303
}
311304

312-
bool CTxDB::ReadModifierUpgradeTime(unsigned int& nUpgradeTime)
313-
{
314-
return Read(string("nUpgradeTime"), nUpgradeTime);
315-
}
316-
317-
bool CTxDB::WriteModifierUpgradeTime(const unsigned int& nUpgradeTime)
318-
{
319-
return Write(string("nUpgradeTime"), nUpgradeTime);
320-
}
321-
322305
static CBlockIndex *InsertBlockIndex(uint256 hash)
323306
{
324307
if (hash == 0)
@@ -402,18 +385,16 @@ bool CTxDB::LoadBlockIndex()
402385
return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
403386
}
404387

405-
// NovaCoin: build setStakeSeen
388+
// LiteDogecoin: build setStakeSeen
406389
if (pindexNew->IsProofOfStake())
407390
setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime));
408391

409392
iterator->Next();
410393
}
411394
delete iterator;
412395

413-
// if (fRequestShutdown)
414-
// return true;
415396
boost::this_thread::interruption_point();
416-
397+
417398
// Calculate nChainTrust
418399
vector<pair<int, CBlockIndex*> > vSortedByHeight;
419400
vSortedByHeight.reserve(mapBlockIndex.size());
@@ -428,7 +409,7 @@ bool CTxDB::LoadBlockIndex()
428409
CBlockIndex* pindex = item.second;
429410
pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
430411
}
431-
412+
432413
// Load hashBestChain pointer to end of best chain
433414
if (!ReadHashBestChain(hashBestChain))
434415
{
@@ -443,14 +424,13 @@ bool CTxDB::LoadBlockIndex()
443424
nBestChainTrust = pindexBest->nChainTrust;
444425

445426
LogPrintf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
446-
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
447-
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
427+
hashBestChain.ToString(), nBestHeight, CBigNum(nBestChainTrust).ToString(),
428+
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()));
448429

449-
450-
// Novacoin: load hashSyncCheckpoint
430+
// LiteDogecoin: load hashSyncCheckpoint
451431
if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
452432
return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
453-
LogPrintf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
433+
LogPrintf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString());
454434

455435
// Load bnBestInvalidTrust, OK if it doesn't exist
456436
CBigNum bnBestInvalidTrust;
@@ -468,7 +448,7 @@ bool CTxDB::LoadBlockIndex()
468448
CBlockIndex* pindexFork = NULL;
469449
map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
470450
for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
471-
{
451+
{
472452
boost::this_thread::interruption_point();
473453
if (pindex->nHeight < nBestHeight-nCheckDepth)
474454
break;
@@ -479,7 +459,7 @@ bool CTxDB::LoadBlockIndex()
479459
// check level 7: verify block signature too
480460
if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6)))
481461
{
482-
LogPrintf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
462+
LogPrintf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
483463
pindexFork = pindex->pprev;
484464
}
485465
// check level 2: verify transaction index validity
@@ -565,7 +545,7 @@ bool CTxDB::LoadBlockIndex()
565545
if (ReadTxIndex(txin.prevout.hash, txindex))
566546
if (txindex.vSpent.size()-1 < txin.prevout.n || txindex.vSpent[txin.prevout.n].IsNull())
567547
{
568-
LogPrintf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString(), txin.prevout.n, hashTx.ToString().c_str());
548+
LogPrintf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString(), txin.prevout.n, hashTx.ToString());
569549
pindexFork = pindex->pprev;
570550
}
571551
}

0 commit comments

Comments
 (0)