X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Ftxdb-leveldb.cpp;h=8f956fe16413c5c6f29a3414706fe2401265bf14;hb=63e4509c569f16145ab861717baf865fc6d05af1;hp=5be8753328842f54f0daf985c104b0775617b208;hpb=050edc2860c508b16f700bf069827310d5feb48c;p=novacoin.git diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index 5be8753..8f956fe 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -23,64 +23,100 @@ using namespace std; using namespace boost; -leveldb::DB *txdb; +leveldb::DB *txdb; // global pointer for LevelDB object instance 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); + options.filter_policy = leveldb::NewBloomFilterPolicy(10); return options; } -void MakeMockTXDB() { - leveldb::Options options = GetOptions(); - options.create_if_missing = true; - // This will leak but don't care here. - options.env = leveldb::NewMemEnv(leveldb::Env::Default()); - leveldb::Status status = leveldb::DB::Open(options, "txdb", &txdb); - if (!status.ok()) - throw runtime_error(strprintf("Could not create mock LevelDB: %s", status.ToString().c_str())); - CTxDB txdb("w"); - txdb.WriteVersion(CLIENT_VERSION); +void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) { + // First time init. + filesystem::path directory = GetDataDir() / "txleveldb"; + + if (fRemoveOld) { + filesystem::remove_all(directory); // remove directory + unsigned int nFile = 1; + + for ( ; ; ) + { + filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile); + + // Break if no such file + if( !filesystem::exists( strBlockFile ) ) + break; + + filesystem::remove(strBlockFile); + + nFile++; + } + } + + 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())); + } } -// NOTE: CDB subclasses are created and destroyed VERY OFTEN. Therefore we have -// to keep databases in global variables to avoid constantly creating and -// destroying them, which sucks. In future the code should be changed to not -// treat the instantiation of a database as a free operation. +// CDB subclasses are created and destroyed VERY OFTEN. That's why +// we shouldn't treat this as a free operations. CTxDB::CTxDB(const char* pszMode) { assert(pszMode); - pdb = txdb; activeBatch = NULL; fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); - if (txdb) + if (txdb) { + pdb = txdb; 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 (fCreate && !Exists(string("version"))) + 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) { bool fTmp = fReadOnly; fReadOnly = false; - WriteVersion(CLIENT_VERSION); + WriteVersion(DATABASE_VERSION); fReadOnly = fTmp; } + printf("Opened LevelDB successfully\n"); } @@ -268,6 +304,16 @@ bool CTxDB::WriteCheckpointPubKey(const string& strPubKey) 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) @@ -279,7 +325,7 @@ static CBlockIndex *InsertBlockIndex(uint256 hash) 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; @@ -319,8 +365,10 @@ bool CTxDB::LoadBlockIndex() CDiskBlockIndex diskindex; ssValue >> diskindex; + uint256 blockHash = diskindex.GetBlockHash(); + // Construct block index object - CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); + CBlockIndex* pindexNew = InsertBlockIndex(blockHash); pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); pindexNew->pnext = InsertBlockIndex(diskindex.hashNext); pindexNew->nFile = diskindex.nFile; @@ -340,7 +388,7 @@ bool CTxDB::LoadBlockIndex() pindexNew->nNonce = diskindex.nNonce; // Watch for genesis block - if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock) + if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet)) pindexGenesisBlock = pindexNew; if (!pindexNew->CheckIndex()) { @@ -375,7 +423,7 @@ bool CTxDB::LoadBlockIndex() // 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 @@ -406,8 +454,8 @@ bool CTxDB::LoadBlockIndex() 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)