Update CMakeLists.txt - play with openssl
[novacoin.git] / src / txdb-leveldb.cpp
index 591f309..69a29b0 100644 (file)
@@ -3,7 +3,10 @@
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
-#include <map>
+#include "txdb-leveldb.h"
+#include "kernel.h"
+#include "checkpoints.h"
+#include "main.h"
 
 #include <boost/version.hpp>
 #include <boost/filesystem.hpp>
 #include <leveldb/filter_policy.h>
 #include <memenv/memenv.h>
 
-#include "kernel.h"
-#include "checkpoints.h"
-#include "txdb.h"
-#include "util.h"
-#include "main.h"
-
 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.
+    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()));
+    }
 }
 
-// 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 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 activeBatch;
+            activeBatch = NULL;
+            delete txdb;
+            txdb = pdb = 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 +300,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 +321,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 +361,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 +384,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()) {
@@ -362,20 +406,20 @@ bool CTxDB::LoadBlockIndex()
     // Calculate nChainTrust
     vector<pair<int, CBlockIndex*> > vSortedByHeight;
     vSortedByHeight.reserve(mapBlockIndex.size());
-    BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
+    for (const auto& item : mapBlockIndex)
     {
         CBlockIndex* pindex = item.second;
         vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
     }
     sort(vSortedByHeight.begin(), vSortedByHeight.end());
-    BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
+    for (const auto& item : vSortedByHeight)
     {
         CBlockIndex* pindex = item.second;
         pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
         // 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 +450,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)
@@ -434,7 +478,7 @@ bool CTxDB::LoadBlockIndex()
         {
             pair<unsigned int, unsigned int> pos = make_pair(pindex->nFile, pindex->nBlockPos);
             mapBlockPos[pos] = pindex;
-            BOOST_FOREACH(const CTransaction &tx, block.vtx)
+            for (const CTransaction &tx : block.vtx)
             {
                 uint256 hashTx = tx.GetHash();
                 CTxIndex txindex;
@@ -461,7 +505,7 @@ bool CTxDB::LoadBlockIndex()
                     unsigned int nOutput = 0;
                     if (nCheckLevel>3)
                     {
-                        BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent)
+                        for (const CDiskTxPos &txpos : txindex.vSpent)
                         {
                             if (!txpos.IsNull())
                             {
@@ -488,7 +532,7 @@ bool CTxDB::LoadBlockIndex()
                                     else
                                     {
                                         bool fFound = false;
-                                        BOOST_FOREACH(const CTxIn &txin, txSpend.vin)
+                                        for (const CTxIn &txin : txSpend.vin)
                                             if (txin.prevout.hash == hashTx && txin.prevout.n == nOutput)
                                                 fFound = true;
                                         if (!fFound)
@@ -506,7 +550,7 @@ bool CTxDB::LoadBlockIndex()
                 // check level 5: check whether all prevouts are marked spent
                 if (nCheckLevel>4)
                 {
-                     BOOST_FOREACH(const CTxIn &txin, tx.vin)
+                     for (const CTxIn &txin : tx.vin)
                      {
                           CTxIndex txindex;
                           if (ReadTxIndex(txin.prevout.hash, txindex))
@@ -533,120 +577,3 @@ bool CTxDB::LoadBlockIndex()
 
     return true;
 }
-
-extern bool fDisableSignatureChecking;
-
-static uint64 nTotalBytes;
-static uint64 nTotalBytesCompleted;
-static double nProgressPercent;
-static LevelDBMigrationProgress *callbackTotalOperationProgress;
-
-void MigrationProgress(unsigned int bytesRead) {
-    // Called from inside LoadExternalBlockFile with how many bytes were
-    // processed so far.
-    nTotalBytesCompleted += bytesRead;
-    double newProgressPercent = 100.0 * ((double)nTotalBytesCompleted / (double)nTotalBytes);
-    // Throttle UI notifications.
-    if (newProgressPercent - nProgressPercent < 0.01)
-        return;
-    nProgressPercent = newProgressPercent;
-    printf("LevelDB migration %0.2f%% complete.\n", nProgressPercent);
-    (*callbackTotalOperationProgress)(nProgressPercent);
-}
-
-LevelDBMigrationResult MaybeMigrateToLevelDB(LevelDBMigrationProgress &progress) {
-    // Check if we have a blkindex.dat: if so, delete it. Because leveldb is
-    // more efficient (space-wise) than bdb, this should ensure we have enough
-    // disk space to perform the migration. We delete before migrate because if
-    // we got here, the code to handle the BDB based block index is not compiled
-    // in anymore, so there's no point in keeping the old file around - it's
-    // onwards and upwards.
-    //
-    // The act of replaying would normally append data to the blk data files,
-    // but we're reading from them so we don't want that. We disable it here,
-    // along with the signature checking as it doesn't help us right now. Note
-    // that replaying the chain could b0rk the wallet, but this process takes
-    // place before any wallets are registered.
-    //
-    // TODO(hearn): Assert on lack of a wallet here.
-
-    int64 nStart = GetTimeMillis();
-
-    boost::filesystem::path oldIndex = GetDataDir() / "blkindex.dat";
-    if (!boost::filesystem::exists(oldIndex)) {
-        return NONE_NEEDED;
-    }
-
-    // Check we have enough disk space for migration. We need at least 2GB free
-    // to hold the blk file we are migrating, and leveldb may have transient
-    // storage spikes, so we ask for at least 3GB.
-    uint64 nFreeBytesAvailable = filesystem::space(GetDataDir()).available;
-    if (nFreeBytesAvailable < 3UL * 1024UL * 1024UL * 1024UL) {
-        return INSUFFICIENT_DISK_SPACE;
-    }
-
-    printf("Deleting old blkindex.dat to make space for leveldb import.\n");
-    boost::filesystem::remove(oldIndex);
-    FILE *file;
-    int nFile = 1;
-    // Firstly, figure out the total number of bytes we need to migrate, for
-    // the progress indicator.
-    nTotalBytes = 0;
-    while (true)
-    {
-        std::string filename = strprintf("blk%04d.dat", nFile);
-        boost::filesystem::path blkpath = GetDataDir() / filename;
-        if (!boost::filesystem::exists(blkpath))
-            break;
-        uintmax_t nFileSize = boost::filesystem::file_size(blkpath);
-        if (nFileSize == static_cast<uintmax_t>(-1))   // Some other error.
-            break;
-        nTotalBytes += nFileSize;
-        nFile++;
-    }
-    nFile = 1;
-
-    // Set up progress calculations and callbacks.
-    callbackTotalOperationProgress = &progress;
-    ExternalBlockFileProgress callbackProgress;
-    callbackProgress.connect(MigrationProgress);
-    (*callbackTotalOperationProgress)(0.0);
-
-    // We don't need to re-run scripts during migration as they were run already
-    // and this saves a lot of time.
-    fDisableSignatureChecking = true;
-    // There may be multiple blk0000?.dat files, iterate over each one, rename
-    // it and then reimport it. For the first one, we need to initialize the
-    // fresh file with the genesis block.
-    while (true)
-    {
-        std::string filename = strprintf("blk%04d.dat", nFile);
-        std::string tmpname = strprintf("tmp-blk%04d.dat", nFile);
-        boost::filesystem::path blkpath = GetDataDir() / filename;
-        if (!boost::filesystem::exists(blkpath)) {
-            // No more work to do.
-            break;
-        }
-        boost::filesystem::path tmppath = GetDataDir() / tmpname;
-        boost::filesystem::rename(blkpath, tmppath);
-        printf("Migrating blk%04d.dat to leveldb\n", nFile);
-        file = fopen(tmppath.string().c_str(), "rb");
-        if (nFile == 1) {
-            // This will create a fresh blk0001.dat ready for usage.
-            LoadBlockIndex();
-        }
-        // LoadExternalBlockFile will close the given input file itself.
-        // It reads each block from the storage files and calls ProcessBlock
-        // on each one, which will go back and add to the database.
-        if (!LoadExternalBlockFile(file, &callbackProgress)) {
-            // We can't really clean up elegantly here.
-            fDisableSignatureChecking = false;
-            return OTHER_ERROR;
-        }
-        boost::filesystem::remove(tmppath);
-        nFile++;
-    }
-    fDisableSignatureChecking = false;
-    printf("LevelDB migration took %fs\n", (GetTimeMillis() - nStart) / 1000.0);
-    return COMPLETED;
-}