static leveldb::Options GetOptions() {
leveldb::Options options;
- int nCacheSizeMB = GetArg("-dbcache", 25);
+ int nCacheSizeMB = GetArgInt("-dbcache", 25);
options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
return options;
}
+void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
+ // First time init.
+ boost::filesystem::path directory = GetDataDir() / "txleveldb";
+
+ if (fRemoveOld) {
+ boost::filesystem::remove_all(directory); // remove directory
+ unsigned int nFile = 1;
+
+ for ( ; ; )
+ {
+ boost::filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
+
+ // Break if no such file
+ if( !boost::filesystem::exists( strBlockFile ) )
+ break;
+
+ boost::filesystem::remove(strBlockFile);
+
+ nFile++;
+ }
+ }
+
+ boost::filesystem::create_directory(directory);
+ printf("Opening LevelDB in %s\n", directory.string().c_str());
+ leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
+ if (!status.ok()) {
+ throw runtime_error(strprintf("init_blockindex(): error opening database environment %s", status.ToString().c_str()));
+ }
+}
+
// CDB subclasses are created and destroyed VERY OFTEN. That's why
-// we shouldn't treat this it a free operations.
+// we shouldn't treat this as a free operations.
CTxDB::CTxDB(const char* pszMode)
{
assert(pszMode);
return;
}
- // First time init.
- filesystem::path directory = GetDataDir() / "txleveldb";
bool fCreate = strchr(pszMode, 'c');
options = GetOptions();
options.create_if_missing = fCreate;
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
- filesystem::create_directory(directory);
- printf("Opening LevelDB in %s\n", directory.string().c_str());
- leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
- if (!status.ok()) {
- throw runtime_error(strprintf("CDB(): error opening database environment %s", status.ToString().c_str()));
- }
+
+ init_blockindex(options); // Init directory
pdb = txdb;
if (Exists(string("version")))
{
ReadVersion(nVersion);
printf("Transaction index version is %d\n", nVersion);
+
+ if (nVersion < DATABASE_VERSION)
+ {
+ printf("Required index version is %d, removing old database\n", DATABASE_VERSION);
+
+ // Leveldb instance destruction
+ delete txdb;
+ txdb = pdb = NULL;
+ delete activeBatch;
+ activeBatch = NULL;
+
+ init_blockindex(options, true); // Remove directory and create new database
+ pdb = txdb;
+
+ bool fTmp = fReadOnly;
+ fReadOnly = false;
+ WriteVersion(DATABASE_VERSION); // Save transaction index version
+ fReadOnly = fTmp;
+ }
}
- else if(fCreate)
+ else if (fCreate)
{
bool fTmp = fReadOnly;
fReadOnly = false;
return Write(string("strCheckpointPubKey"), strPubKey);
}
+bool CTxDB::ReadModifierUpgradeTime(unsigned int& nUpgradeTime)
+{
+ return Read(string("nUpgradeTime"), nUpgradeTime);
+}
+
+bool CTxDB::WriteModifierUpgradeTime(const unsigned int& nUpgradeTime)
+{
+ return Write(string("nUpgradeTime"), nUpgradeTime);
+}
+
static CBlockIndex *InsertBlockIndex(uint256 hash)
{
if (hash == 0)
return (*mi).second;
// Create new
- CBlockIndex* pindexNew = new CBlockIndex();
+ CBlockIndex* pindexNew = new(nothrow) CBlockIndex();
if (!pindexNew)
throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
// NovaCoin: calculate stake modifier checksum
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
- return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016"PRI64x, pindex->nHeight, pindex->nStakeModifier);
+ return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016" PRIx64, pindex->nHeight, pindex->nStakeModifier);
}
// Load hashBestChain pointer to end of best chain
nBestInvalidTrust = bnBestInvalidTrust.getuint256();
// Verify blocks in the best chain
- int nCheckLevel = GetArg("-checklevel", 1);
- int nCheckDepth = GetArg( "-checkblocks", 2500);
+ int nCheckLevel = GetArgInt("-checklevel", 1);
+ int nCheckDepth = GetArgInt( "-checkblocks", 2500);
if (nCheckDepth == 0)
nCheckDepth = 1000000000; // suffices until the year 19000
if (nCheckDepth > nBestHeight)