From b1a1f9e9693114bfea4118828fb08ff19ea9f006 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sun, 26 Dec 2021 13:21:12 +0300 Subject: [PATCH] Remove BerkeleyDB support for block index --- CMakeLists.txt | 23 +-- doc/build-ubuntu.txt | 6 +- src/CMakeLists.txt | 23 +-- src/checkpoints.cpp | 18 +-- src/db.cpp | 25 --- src/init.cpp | 2 +- src/kernel.cpp | 6 +- src/main.cpp | 6 +- src/miner.cpp | 2 +- src/qt/multisigdialog.cpp | 5 - src/qt/transactiondesc.cpp | 2 +- src/rpcmining.cpp | 2 +- src/rpcrawtransaction.cpp | 2 +- src/txdb-bdb.cpp | 436 -------------------------------------------- src/txdb-bdb.h | 45 ----- src/txdb-leveldb.cpp | 2 +- src/txdb.h | 20 -- src/version.cpp | 4 - src/wallet.cpp | 2 +- 19 files changed, 31 insertions(+), 600 deletions(-) delete mode 100644 src/txdb-bdb.cpp delete mode 100644 src/txdb-bdb.h delete mode 100644 src/txdb.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a45b97..9b23274 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.4.1) ## ## mkdir build && cd build ## -## cmake -DBerkeleyDB_ROOT:STRING=/opt/homebrew/Cellar/berkeley-db@4/4.8.30 -DQt5_DIR:STRING=/opt/homebrew/Cellar/qt@5/5.15.2_1/lib/cmake/Qt5 -DUSE_LEVELDB=1 .. -## cmake -DUSE_LEVELDB=1 .. +## cmake -DBerkeleyDB_ROOT:STRING=/opt/homebrew/Cellar/berkeley-db@4/4.8.30 -DQt5_DIR:STRING=/opt/homebrew/Cellar/qt@5/5.15.2_1/lib/cmake/Qt5 .. +## cmake .. ## project(novacoin-qt VERSION 0.5.9 LANGUAGES C CXX) @@ -233,19 +233,14 @@ endif() list(APPEND ALL_DEFINITIONS QT_GUI USE_QRCODE USE_IPV6 BOOST_SPIRIT_THREADSAFE) -if(USE_LEVELDB) - # Disable useless targets - option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" OFF) - option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" OFF) - option(LEVELDB_INSTALL "Install LevelDB's header and library" OFF) +# Disable useless targets +option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" OFF) +option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" OFF) +option(LEVELDB_INSTALL "Install LevelDB's header and library" OFF) - add_subdirectory(src/additional/leveldb) - list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/txdb-leveldb.cpp) - list(APPEND ALL_LIBRARIES leveldb) - list(APPEND ALL_DEFINITIONS USE_LEVELDB) -else() - list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/txdb-bdb.cpp) -endif() +add_subdirectory(src/additional/leveldb) +list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/txdb-leveldb.cpp) +list(APPEND ALL_LIBRARIES leveldb) if (NOT USE_GENERIC_SCRYPT) list( APPEND ALL_SOURCES ${generic_sources} ${CMAKE_CURRENT_SOURCE_DIR}/src/crypto/scrypt/intrin/scrypt-intrin.cpp ) diff --git a/doc/build-ubuntu.txt b/doc/build-ubuntu.txt index 9b4c7f8..b8b36f0 100644 --- a/doc/build-ubuntu.txt +++ b/doc/build-ubuntu.txt @@ -18,11 +18,11 @@ Create build directory: Configure build files: - ```cmake -DUSE_LEVELDB=1 ..``` + ```cmake ..``` You may specify BDB library path, if necessary: - ```cmake -DBerkeleyDB_LIBS=/usr/lib/aarch64-linux-gnu/ -DUSE_LEVELDB=1 ..``` + ```cmake -DBerkeleyDB_LIBS=/usr/lib/aarch64-linux-gnu/ ..``` Compile: @@ -40,7 +40,7 @@ Create build directory: Configure build files: - ```cmake -DUSE_LEVELDB=1 ../src``` + ```cmake ../src``` Compile: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b16cff4..ad82480 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.4.1) ## ## mkdir build && cd build ## -## cmake -DBerkeleyDB_ROOT:STRING=/opt/homebrew/Cellar/berkeley-db@4/4.8.30 -DUSE_LEVELDB=1 .. -## cmake -DUSE_LEVELDB=1 .. +## cmake -DBerkeleyDB_ROOT:STRING=/opt/homebrew/Cellar/berkeley-db@4/4.8.30 .. +## cmake .. ## project(novacoind VERSION 0.5.9 LANGUAGES C CXX) @@ -144,19 +144,14 @@ if (NOT BDB_FOUND) message(FATAL_ERROR "Unable to find ${BerkeleyDB_LIB_name} library in ${BerkeleyDB_LIBS} directory. Try to specify either library path via BerkeleyDB_LIBS or berkeley db root path via BerkeleyDB_ROOT variable. You can also use BerkeleyDB_LIB_name variable to provide alternative file name search for.") endif() -if(USE_LEVELDB) - # Disable useless targets - option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" OFF) - option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" OFF) - option(LEVELDB_INSTALL "Install LevelDB's header and library" OFF) +# Disable useless targets +option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" OFF) +option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" OFF) +option(LEVELDB_INSTALL "Install LevelDB's header and library" OFF) - add_subdirectory(additional/leveldb) - list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/txdb-leveldb.cpp) - list(APPEND ALL_LIBRARIES leveldb) - list(APPEND ALL_DEFINITIONS USE_LEVELDB) -else() - list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/txdb-bdb.cpp) -endif() +add_subdirectory(additional/leveldb) +list(APPEND ALL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/txdb-leveldb.cpp) +list(APPEND ALL_LIBRARIES leveldb) if (NOT USE_GENERIC_SCRYPT) list( APPEND ALL_SOURCES ${generic_sources} ${CMAKE_CURRENT_SOURCE_DIR}/crypto/scrypt/intrin/scrypt-intrin.cpp ) diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp index 5de09b3..7d6ffcb 100644 --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -8,7 +8,7 @@ #include "checkpoints.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "main.h" #include "uint256.h" @@ -169,10 +169,6 @@ namespace Checkpoints if (!txdb.TxnCommit()) return error("WriteSyncCheckpoint(): failed to commit to db sync checkpoint %s", hashCheckpoint.ToString().c_str()); -#ifndef USE_LEVELDB - txdb.Close(); -#endif - Checkpoints::hashSyncCheckpoint = hashCheckpoint; return true; } @@ -203,9 +199,6 @@ namespace Checkpoints } } -#ifndef USE_LEVELDB - txdb.Close(); -#endif if (!WriteSyncCheckpoint(hashPendingCheckpoint)) return error("AcceptPendingSyncCheckpoint(): failed to write sync checkpoint %s", hashPendingCheckpoint.ToString().c_str()); hashPendingCheckpoint = 0; @@ -291,11 +284,6 @@ namespace Checkpoints { return error("ResetSyncCheckpoint: SetBestChain failed for hardened checkpoint %s", hash.ToString().c_str()); } - -#ifndef USE_LEVELDB - txdb.Close(); -#endif - } else if(!mapBlockIndex.count(hash)) { @@ -450,10 +438,6 @@ bool CSyncCheckpoint::ProcessSyncCheckpoint(CNode* pfrom) } } -#ifndef USE_LEVELDB - txdb.Close(); -#endif - if (!Checkpoints::WriteSyncCheckpoint(hashCheckpoint)) return error("ProcessSyncCheckpoint(): failed to write sync checkpoint %s", hashCheckpoint.ToString().c_str()); Checkpoints::checkpointMessage = *this; diff --git a/src/db.cpp b/src/db.cpp index 7ee93cf..5b6d66b 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -107,31 +107,6 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_) fDbEnvInit = true; fMockDb = false; -#ifndef USE_LEVELDB - // Check that the number of locks is sufficient (to prevent chain fork possibility, read http://bitcoin.org/may15 for more info) - u_int32_t nMaxLocks; - if (!dbenv.get_lk_max_locks(&nMaxLocks)) - { - int nBlocks, nDeepReorg; - std::string strMessage; - - nBlocks = nMaxLocks / 48768; - nDeepReorg = (nBlocks - 1) / 2; - - printf("Final lk_max_locks is %u, sufficient for (worst case) %d block%s in a single transaction (up to a %d-deep reorganization)\n", nMaxLocks, nBlocks, (nBlocks == 1) ? "" : "s", nDeepReorg); - if (nDeepReorg < 3) - { - if (nBlocks < 1) - strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %u, which may be too low for a single block. If this limit is reached, NovaCoin may stop working."), nMaxLocks); - else - strMessage = strprintf(_("Warning: DB_CONFIG has set_lk_max_locks %u, which may be too low for a common blockchain reorganization. If this limit is reached, NovaCoin may stop working."), nMaxLocks); - - strMiscWarning = strMessage; - printf("*** %s\n", strMessage.c_str()); - } - } -#endif - return true; } diff --git a/src/init.cpp b/src/init.cpp index a16f7fb..001b128 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "txdb.h" +#include "txdb-leveldb.h" #include "walletdb.h" #include "bitcoinrpc.h" #include "net.h" diff --git a/src/kernel.cpp b/src/kernel.cpp index bd8c357..97007c9 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -9,7 +9,7 @@ #include "kernel.h" #include "kernel_worker.h" -#include "txdb.h" +#include "txdb-leveldb.h" extern unsigned int nStakeMaxAge; extern unsigned int nStakeTargetSpacing; @@ -492,10 +492,6 @@ bool CheckProofOfStake(const CTransaction& tx, unsigned int nBits, uint256& hash if (!txPrev.ReadFromDisk(txdb, txin.prevout, txindex)) return tx.DoS(1, error("CheckProofOfStake() : INFO: read txPrev failed")); // previous transaction not in main chain, may occur during initial download -#ifndef USE_LEVELDB - txdb.Close(); -#endif - // Verify signature if (!VerifySignature(txPrev, tx, 0, MANDATORY_SCRIPT_VERIFY_FLAGS, 0)) return tx.DoS(100, error("CheckProofOfStake() : VerifySignature failed on coinstake %s", tx.GetHash().ToString().c_str())); diff --git a/src/main.cpp b/src/main.cpp index 328d61b..d09d737 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "alert.h" #include "checkpoints.h" #include "db.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "init.h" #include "interface.h" #include "checkqueue.h" @@ -2860,10 +2860,6 @@ bool LoadBlockIndex(bool fAllowNew) if (!txdb.WriteModifierUpgradeTime(nModifierUpgradeTime)) return error("LoadBlockIndex() : failed to write upgrade info"); } - -#ifndef USE_LEVELDB - txdb.Close(); -#endif } return true; diff --git a/src/miner.cpp b/src/miner.cpp index 2712207..1c4d283 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -4,7 +4,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "txdb.h" +#include "txdb-leveldb.h" #include "miner.h" #include "kernel.h" #include "kernel_worker.h" diff --git a/src/qt/multisigdialog.cpp b/src/qt/multisigdialog.cpp index c804c99..b1a2f64 100644 --- a/src/qt/multisigdialog.cpp +++ b/src/qt/multisigdialog.cpp @@ -18,12 +18,7 @@ #include "util.h" #include "wallet.h" #include "walletmodel.h" - -#ifdef USE_LEVELDB #include "txdb-leveldb.h" -#else -#include "txdb-bdb.h" -#endif MultisigDialog::MultisigDialog(QWidget *parent) : QWidget(parent), ui(new Ui::MultisigDialog), model(0) { diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index 742de00..b809125 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -5,7 +5,7 @@ #include "main.h" #include "wallet.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "interface.h" #include "base58.h" diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 8f3358f..ce6545b 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -5,7 +5,7 @@ #include "main.h" #include "db.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "init.h" #include "miner.h" #include "kernel.h" diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index b8365c6..66d9999 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -7,7 +7,7 @@ #include "base58.h" #include "bitcoinrpc.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "init.h" #include "main.h" #include "net.h" diff --git a/src/txdb-bdb.cpp b/src/txdb-bdb.cpp deleted file mode 100644 index 1235125..0000000 --- a/src/txdb-bdb.cpp +++ /dev/null @@ -1,436 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include "db.h" -#include "kernel.h" -#include "checkpoints.h" -#include "txdb-bdb.h" -#include "util.h" -#include "main.h" -#include -#include -#include - -#ifndef WIN32 -#include "sys/stat.h" -#endif - -using namespace std; -using namespace boost; - -void MakeMockTXDB() { - // In practice this won't do anything because the test framework always initializes - // an in-memory BDB via bitdb.MakeMock() first, as we use BDB for addresses and wallets. - if (!bitdb.IsMock()) - bitdb.MakeMock(); -} - -// -// CTxDB -// - -bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex) -{ - assert(!fClient); - txindex.SetNull(); - return Read(make_pair(string("tx"), hash), txindex); -} - -bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex) -{ - assert(!fClient); - return Write(make_pair(string("tx"), hash), txindex); -} - -bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight) -{ - assert(!fClient); - - // Add to tx index - uint256 hash = tx.GetHash(); - CTxIndex txindex(pos, tx.vout.size()); - return Write(make_pair(string("tx"), hash), txindex); -} - -bool CTxDB::EraseTxIndex(const CTransaction& tx) -{ - assert(!fClient); - uint256 hash = tx.GetHash(); - - return Erase(make_pair(string("tx"), hash)); -} - -bool CTxDB::ContainsTx(uint256 hash) -{ - assert(!fClient); - return Exists(make_pair(string("tx"), hash)); -} - -bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex) -{ - assert(!fClient); - tx.SetNull(); - if (!ReadTxIndex(hash, txindex)) - return false; - return (tx.ReadFromDisk(txindex.pos)); -} - -bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx) -{ - CTxIndex txindex; - return ReadDiskTx(hash, tx, txindex); -} - -bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex) -{ - return ReadDiskTx(outpoint.hash, tx, txindex); -} - -bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx) -{ - CTxIndex txindex; - return ReadDiskTx(outpoint.hash, tx, txindex); -} - -bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex) -{ - return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex); -} - -bool CTxDB::ReadHashBestChain(uint256& hashBestChain) -{ - return Read(string("hashBestChain"), hashBestChain); -} - -bool CTxDB::WriteHashBestChain(uint256 hashBestChain) -{ - return Write(string("hashBestChain"), hashBestChain); -} - -bool CTxDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust) -{ - return Read(string("bnBestInvalidTrust"), bnBestInvalidTrust); -} - -bool CTxDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust) -{ - return Write(string("bnBestInvalidTrust"), bnBestInvalidTrust); -} - -bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint) -{ - return Read(string("hashSyncCheckpoint"), hashCheckpoint); -} - -bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint) -{ - return Write(string("hashSyncCheckpoint"), hashCheckpoint); -} - -bool CTxDB::ReadCheckpointPubKey(string& strPubKey) -{ - return Read(string("strCheckpointPubKey"), strPubKey); -} - -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); -} - -CBlockIndex static * InsertBlockIndex(uint256 hash) -{ - if (hash == 0) - return NULL; - - // Return existing - map::iterator mi = mapBlockIndex.find(hash); - if (mi != mapBlockIndex.end()) - return (*mi).second; - - // Create new - CBlockIndex* pindexNew = new(nothrow) CBlockIndex(); - if (!pindexNew) - throw runtime_error("LoadBlockIndex() : new CBlockIndex failed"); - mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first; - pindexNew->phashBlock = &((*mi).first); - - return pindexNew; -} - -bool CTxDB::LoadBlockIndex() -{ - if (!LoadBlockIndexGuts()) - return false; - - if (fRequestShutdown) - return true; - - // Calculate nChainTrust - vector > vSortedByHeight; - vSortedByHeight.reserve(mapBlockIndex.size()); - BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& 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) - { - CBlockIndex* pindex = item.second; - pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust(); - // ppcoin: 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" PRIx64, pindex->nHeight, pindex->nStakeModifier); - } - - // Load hashBestChain pointer to end of best chain - if (!ReadHashBestChain(hashBestChain)) - { - if (pindexGenesisBlock == NULL) - return true; - return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded"); - } - if (!mapBlockIndex.count(hashBestChain)) - return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index"); - pindexBest = mapBlockIndex[hashBestChain]; - nBestHeight = pindexBest->nHeight; - nBestChainTrust = pindexBest->nChainTrust; - printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n", - hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(), - DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str()); - - // ppcoin: load hashSyncCheckpoint - if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint)) - return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded"); - printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str()); - - // Load bnBestInvalidTrust, OK if it doesn't exist - CBigNum bnBestInvalidTrust; - ReadBestInvalidTrust(bnBestInvalidTrust); - nBestInvalidTrust = bnBestInvalidTrust.getuint256(); - - // Verify blocks in the best chain - int nCheckLevel = GetArgInt("-checklevel", 1); - int nCheckDepth = GetArgInt( "-checkblocks", 2500); - if (nCheckDepth == 0) - nCheckDepth = 1000000000; // suffices until the year 19000 - if (nCheckDepth > nBestHeight) - nCheckDepth = nBestHeight; - printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel); - CBlockIndex* pindexFork = NULL; - map, CBlockIndex*> mapBlockPos; - for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev) - { - if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth) - break; - CBlock block; - if (!block.ReadFromDisk(pindex)) - return error("LoadBlockIndex() : block.ReadFromDisk failed"); - // check level 1: verify block validity - // check level 7: verify block signature too - if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6))) - { - printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str()); - pindexFork = pindex->pprev; - } - // check level 2: verify transaction index validity - if (nCheckLevel>1) - { - pair pos = make_pair(pindex->nFile, pindex->nBlockPos); - mapBlockPos[pos] = pindex; - BOOST_FOREACH(const CTransaction &tx, block.vtx) - { - uint256 hashTx = tx.GetHash(); - CTxIndex txindex; - if (ReadTxIndex(hashTx, txindex)) - { - // check level 3: checker transaction hashes - if (nCheckLevel>2 || pindex->nFile != txindex.pos.nFile || pindex->nBlockPos != txindex.pos.nBlockPos) - { - // either an error or a duplicate transaction - CTransaction txFound; - if (!txFound.ReadFromDisk(txindex.pos)) - { - printf("LoadBlockIndex() : *** cannot read mislocated transaction %s\n", hashTx.ToString().c_str()); - pindexFork = pindex->pprev; - } - else - if (txFound.GetHash() != hashTx) // not a duplicate tx - { - printf("LoadBlockIndex(): *** invalid tx position for %s\n", hashTx.ToString().c_str()); - pindexFork = pindex->pprev; - } - } - // check level 4: check whether spent txouts were spent within the main chain - unsigned int nOutput = 0; - if (nCheckLevel>3) - { - BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent) - { - if (!txpos.IsNull()) - { - pair posFind = make_pair(txpos.nFile, txpos.nBlockPos); - if (!mapBlockPos.count(posFind)) - { - printf("LoadBlockIndex(): *** found bad spend at %d, hashBlock=%s, hashTx=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str(), hashTx.ToString().c_str()); - pindexFork = pindex->pprev; - } - // check level 6: check whether spent txouts were spent by a valid transaction that consume them - if (nCheckLevel>5) - { - CTransaction txSpend; - if (!txSpend.ReadFromDisk(txpos)) - { - printf("LoadBlockIndex(): *** cannot read spending transaction of %s:%i from disk\n", hashTx.ToString().c_str(), nOutput); - pindexFork = pindex->pprev; - } - else if (!txSpend.CheckTransaction()) - { - printf("LoadBlockIndex(): *** spending transaction of %s:%i is invalid\n", hashTx.ToString().c_str(), nOutput); - pindexFork = pindex->pprev; - } - else - { - bool fFound = false; - BOOST_FOREACH(const CTxIn &txin, txSpend.vin) - if (txin.prevout.hash == hashTx && txin.prevout.n == nOutput) - fFound = true; - if (!fFound) - { - printf("LoadBlockIndex(): *** spending transaction of %s:%i does not spend it\n", hashTx.ToString().c_str(), nOutput); - pindexFork = pindex->pprev; - } - } - } - } - nOutput++; - } - } - } - // check level 5: check whether all prevouts are marked spent - if (nCheckLevel>4) - { - BOOST_FOREACH(const CTxIn &txin, tx.vin) - { - CTxIndex txindex; - if (ReadTxIndex(txin.prevout.hash, txindex)) - if (txindex.vSpent.size()-1 < txin.prevout.n || txindex.vSpent[txin.prevout.n].IsNull()) - { - printf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString().c_str(), txin.prevout.n, hashTx.ToString().c_str()); - pindexFork = pindex->pprev; - } - } - } - } - } - } - if (pindexFork && !fRequestShutdown) - { - // Reorg back to the fork - printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight); - CBlock block; - if (!block.ReadFromDisk(pindexFork)) - return error("LoadBlockIndex() : block.ReadFromDisk failed"); - CTxDB txdb; - block.SetBestChain(txdb, pindexFork); - } - - return true; -} - - - -bool CTxDB::LoadBlockIndexGuts() -{ - // Get database cursor - Dbc* pcursor = GetCursor(); - if (!pcursor) - return false; - - // Load mapBlockIndex - unsigned int fFlags = DB_SET_RANGE; - for ( ; ; ) - { - // Read next record - CDataStream ssKey(SER_DISK, CLIENT_VERSION); - if (fFlags == DB_SET_RANGE) - ssKey << make_pair(string("blockindex"), uint256(0)); - CDataStream ssValue(SER_DISK, CLIENT_VERSION); - int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags); - fFlags = DB_NEXT; - if (ret == DB_NOTFOUND) - break; - else if (ret != 0) - return false; - - // Unserialize - - try { - string strType; - ssKey >> strType; - if (strType == "blockindex" && !fRequestShutdown) - { - CDiskBlockIndex diskindex; - ssValue >> diskindex; - - uint256 blockHash = diskindex.GetBlockHash(); - - // Construct block index object - CBlockIndex* pindexNew = InsertBlockIndex(blockHash); - pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); - pindexNew->pnext = InsertBlockIndex(diskindex.hashNext); - pindexNew->nFile = diskindex.nFile; - pindexNew->nBlockPos = diskindex.nBlockPos; - pindexNew->nHeight = diskindex.nHeight; - pindexNew->nMint = diskindex.nMint; - pindexNew->nMoneySupply = diskindex.nMoneySupply; - pindexNew->nFlags = diskindex.nFlags; - pindexNew->nStakeModifier = diskindex.nStakeModifier; - pindexNew->prevoutStake = diskindex.prevoutStake; - pindexNew->nStakeTime = diskindex.nStakeTime; - pindexNew->hashProofOfStake = diskindex.hashProofOfStake; - pindexNew->nVersion = diskindex.nVersion; - pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot; - pindexNew->nTime = diskindex.nTime; - pindexNew->nBits = diskindex.nBits; - pindexNew->nNonce = diskindex.nNonce; - - // Watch for genesis block - if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet)) - pindexGenesisBlock = pindexNew; - - if (!pindexNew->CheckIndex()) - return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight); - - // ppcoin: build setStakeSeen - if (pindexNew->IsProofOfStake()) - setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime)); - } - else - { - break; // if shutdown requested or finished loading block index - } - } // try - catch (const std::exception&) { - return error("%s() : deserialize error", BOOST_CURRENT_FUNCTION); - } - } - pcursor->close(); - - return true; -} - - diff --git a/src/txdb-bdb.h b/src/txdb-bdb.h deleted file mode 100644 index 4537275..0000000 --- a/src/txdb-bdb.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_TXDB_BDB_H -#define BITCOIN_TXDB_BDB_H - - -/** Access to the transaction database (blkindex.dat) */ -class CTxDB : public CDB -{ -public: - CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { } -private: - CTxDB(const CTxDB&); - void operator=(const CTxDB&); -public: - - bool ReadTxIndex(uint256 hash, CTxIndex& txindex); - bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex); - bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight); - bool EraseTxIndex(const CTransaction& tx); - bool ContainsTx(uint256 hash); - bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex); - bool ReadDiskTx(uint256 hash, CTransaction& tx); - bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex); - bool ReadDiskTx(COutPoint outpoint, CTransaction& tx); - bool WriteBlockIndex(const CDiskBlockIndex& blockindex); - bool ReadHashBestChain(uint256& hashBestChain); - bool WriteHashBestChain(uint256 hashBestChain); - bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust); - bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust); - bool ReadSyncCheckpoint(uint256& hashCheckpoint); - bool WriteSyncCheckpoint(uint256 hashCheckpoint); - bool ReadCheckpointPubKey(std::string& strPubKey); - bool WriteCheckpointPubKey(const std::string& strPubKey); - bool ReadModifierUpgradeTime(unsigned int& nUpgradeTime); - bool WriteModifierUpgradeTime(const unsigned int& nUpgradeTime); - bool LoadBlockIndex(); -private: - bool LoadBlockIndexGuts(); -}; - -#endif diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index 1cf3c68..1d1ca5d 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -16,7 +16,7 @@ #include "kernel.h" #include "checkpoints.h" -#include "txdb.h" +#include "txdb-leveldb.h" #include "util.h" #include "main.h" diff --git a/src/txdb.h b/src/txdb.h deleted file mode 100644 index 939e59d..0000000 --- a/src/txdb.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_TXDB_H -#define BITCOIN_TXDB_H - -// Allow switching between LevelDB and BerkelyDB here in case we need to temporarily -// go back to BDB for any reason. Once we're confident enough with LevelDB to stick -// with it, this can be deleted. - -#ifdef USE_LEVELDB -#include "txdb-leveldb.h" -#else -#include "db.h" -#include "txdb-bdb.h" -#endif - -#endif // BITCOIN_TXDB_H diff --git a/src/version.cpp b/src/version.cpp index 54e3202..b4a4dbc 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -11,11 +11,7 @@ const std::string CLIENT_NAME("Satoshi"); // Client version number -#ifdef USE_LEVELDB #define CLIENT_VERSION_SUFFIX "-leveldb" -#else -#define CLIENT_VERSION_SUFFIX "-bdb" -#endif // Compiler name #ifdef __INTEL_COMPILER diff --git a/src/wallet.cpp b/src/wallet.cpp index 4abe450..b2b395b 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -3,7 +3,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "txdb.h" +#include "txdb-leveldb.h" #include "wallet.h" #include "walletdb.h" #include "crypter.h" -- 1.7.1