11
11
#include < boost/filesystem.hpp>
12
12
#include < boost/filesystem/fstream.hpp>
13
13
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>
19
19
20
20
#include " kernel.h"
21
21
#include " checkpoints.h"
@@ -38,29 +38,28 @@ static leveldb::Options GetOptions() {
38
38
return options;
39
39
}
40
40
41
-
41
+ void init_blockindex (leveldb::Options& options, bool fRemoveOld = false ) {
42
42
// First time init.
43
43
boost::filesystem::path directory = GetDataDir () / " txleveldb" ;
44
- bool fCreate = strchr(pszMode, ' c' );
45
44
46
45
if (fRemoveOld ) {
47
46
boost::filesystem::remove_all (directory); // remove directory
48
47
unsigned int nFile = 1 ;
49
- filesystem::path bootstrap = GetDataDir () / " bootstrap.dat" ;
50
- boost::filesystem::path strBlockFile = GetDataDir () / strprintf (" blk%04u.dat" , nFile);
51
48
52
- if (txdb) {
53
- pdb = txdb;
54
- return ;
55
- }
49
+ while (true )
50
+ {
51
+ boost::filesystem::path strBlockFile = GetDataDir () / strprintf (" blk%04u.dat" , nFile);
56
52
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
+ }
60
62
61
- options = GetOptions ();
62
- options.create_if_missing = fCreate ;
63
- options.filter_policy = leveldb::NewBloomFilterPolicy (10 );
64
63
boost::filesystem::create_directory (directory);
65
64
LogPrintf (" Opening LevelDB in %s\n " , directory.string ());
66
65
leveldb::Status status = leveldb::DB::Open (options, directory.string (), &txdb);
@@ -82,19 +81,13 @@ CTxDB::CTxDB(const char* pszMode)
82
81
return ;
83
82
}
84
83
85
- // First time init.
86
- filesystem::path directory = GetDataDir () / " txleveldb" ;
87
84
bool fCreate = strchr (pszMode, ' c' );
88
85
89
86
options = GetOptions ();
90
87
options.create_if_missing = fCreate ;
91
88
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
98
91
pdb = txdb;
99
92
100
93
if (Exists (string (" version" )))
@@ -215,12 +208,12 @@ bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
215
208
}
216
209
217
210
bool CTxDB::UpdateTxIndex (uint256 hash, const CTxIndex& txindex)
218
- {
211
+ {
219
212
return Write (make_pair (string (" tx" ), hash), txindex);
220
213
}
221
214
222
215
bool CTxDB::AddTxIndex (const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
223
- {
216
+ {
224
217
// Add to tx index
225
218
uint256 hash = tx.GetHash ();
226
219
CTxIndex txindex (pos, tx.vout .size ());
@@ -230,7 +223,7 @@ bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeigh
230
223
bool CTxDB::EraseTxIndex (const CTransaction& tx)
231
224
{
232
225
uint256 hash = tx.GetHash ();
233
-
226
+
234
227
return Erase (make_pair (string (" tx" ), hash));
235
228
}
236
229
@@ -240,7 +233,7 @@ bool CTxDB::ContainsTx(uint256 hash)
240
233
}
241
234
242
235
bool CTxDB::ReadDiskTx (uint256 hash, CTransaction& tx, CTxIndex& txindex)
243
- {
236
+ {
244
237
tx.SetNull ();
245
238
if (!ReadTxIndex (hash, txindex))
246
239
return false ;
@@ -309,16 +302,6 @@ bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
309
302
return Write (string (" strCheckpointPubKey" ), strPubKey);
310
303
}
311
304
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
-
322
305
static CBlockIndex *InsertBlockIndex (uint256 hash)
323
306
{
324
307
if (hash == 0 )
@@ -402,18 +385,16 @@ bool CTxDB::LoadBlockIndex()
402
385
return error (" LoadBlockIndex() : CheckIndex failed at %d" , pindexNew->nHeight );
403
386
}
404
387
405
- // NovaCoin : build setStakeSeen
388
+ // LiteDogecoin : build setStakeSeen
406
389
if (pindexNew->IsProofOfStake ())
407
390
setStakeSeen.insert (make_pair (pindexNew->prevoutStake , pindexNew->nStakeTime ));
408
391
409
392
iterator->Next ();
410
393
}
411
394
delete iterator;
412
395
413
- // if (fRequestShutdown)
414
- // return true;
415
396
boost::this_thread::interruption_point ();
416
-
397
+
417
398
// Calculate nChainTrust
418
399
vector<pair<int , CBlockIndex*> > vSortedByHeight;
419
400
vSortedByHeight.reserve (mapBlockIndex.size ());
@@ -428,7 +409,7 @@ bool CTxDB::LoadBlockIndex()
428
409
CBlockIndex* pindex = item.second ;
429
410
pindex->nChainTrust = (pindex->pprev ? pindex->pprev ->nChainTrust : 0 ) + pindex->GetBlockTrust ();
430
411
}
431
-
412
+
432
413
// Load hashBestChain pointer to end of best chain
433
414
if (!ReadHashBestChain (hashBestChain))
434
415
{
@@ -443,14 +424,13 @@ bool CTxDB::LoadBlockIndex()
443
424
nBestChainTrust = pindexBest->nChainTrust ;
444
425
445
426
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 ()));
448
429
449
-
450
- // Novacoin: load hashSyncCheckpoint
430
+ // LiteDogecoin: load hashSyncCheckpoint
451
431
if (!ReadSyncCheckpoint (Checkpoints::hashSyncCheckpoint))
452
432
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 ());
454
434
455
435
// Load bnBestInvalidTrust, OK if it doesn't exist
456
436
CBigNum bnBestInvalidTrust;
@@ -468,7 +448,7 @@ bool CTxDB::LoadBlockIndex()
468
448
CBlockIndex* pindexFork = NULL ;
469
449
map<pair<unsigned int , unsigned int >, CBlockIndex*> mapBlockPos;
470
450
for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev ; pindex = pindex->pprev )
471
- {
451
+ {
472
452
boost::this_thread::interruption_point ();
473
453
if (pindex->nHeight < nBestHeight-nCheckDepth)
474
454
break ;
@@ -479,7 +459,7 @@ bool CTxDB::LoadBlockIndex()
479
459
// check level 7: verify block signature too
480
460
if (nCheckLevel>0 && !block.CheckBlock (true , true , (nCheckLevel>6 )))
481
461
{
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 ());
483
463
pindexFork = pindex->pprev ;
484
464
}
485
465
// check level 2: verify transaction index validity
@@ -565,7 +545,7 @@ bool CTxDB::LoadBlockIndex()
565
545
if (ReadTxIndex (txin.prevout .hash , txindex))
566
546
if (txindex.vSpent .size ()-1 < txin.prevout .n || txindex.vSpent [txin.prevout .n ].IsNull ())
567
547
{
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 ());
569
549
pindexFork = pindex->pprev ;
570
550
}
571
551
}
0 commit comments