diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/bitcoinrpc.h | 2 | ||||
-rw-r--r-- | src/db.cpp | 144 | ||||
-rw-r--r-- | src/db.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 8 | ||||
-rw-r--r-- | src/rpcdump.cpp | 6 | ||||
-rw-r--r-- | src/util.cpp | 8 | ||||
-rw-r--r-- | src/util.h | 1 |
8 files changed, 95 insertions, 77 deletions
diff --git a/.gitignore b/.gitignore index fe93caafbf..2b70e2cca5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ src/*.exe src/bitcoin src/bitcoind +src/test_bitcoin src/build.h .*.swp *.*~* diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 8b2d905179..7a8273756d 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -13,6 +13,8 @@ #include "json/json_spirit_writer_template.h" #include "json/json_spirit_utils.h" +json_spirit::Object JSONRPCError(int code, const std::string& message); + void ThreadRPCServer(void* parg); int CommandLineRPC(int argc, char *argv[]); diff --git a/src/db.cpp b/src/db.cpp index 95220059d0..4f4e1d84b7 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -170,6 +170,14 @@ CDB::CDB(const char *pszFile, const char* pszMode) : } } +static bool IsChainFile(std::string strFile) +{ + if (strFile == "blkindex.dat") + return true; + + return false; +} + void CDB::Close() { if (!pdb) @@ -183,9 +191,9 @@ void CDB::Close() unsigned int nMinutes = 0; if (fReadOnly) nMinutes = 1; - if (strFile == "blkindex.dat") + if (IsChainFile(strFile)) nMinutes = 2; - if (strFile == "blkindex.dat" && IsInitialBlockDownload()) + if (IsChainFile(strFile) && IsInitialBlockDownload()) nMinutes = 5; bitdb.dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100)*1024 : 0, nMinutes, 0); @@ -327,7 +335,7 @@ void CDBEnv::Flush(bool fShutdown) CloseDb(strFile); printf("%s checkpoint\n", strFile.c_str()); dbenv.txn_checkpoint(0, 0, 0); - if (strFile != "blkindex.dat" || fDetachDB) { + if (!IsChainFile(strFile) || fDetachDB) { printf("%s detach\n", strFile.c_str()); dbenv.lsn_reset(strFile.c_str(), 0); } @@ -529,68 +537,9 @@ CBlockIndex static * InsertBlockIndex(uint256 hash) bool CTxDB::LoadBlockIndex() { - // Get database cursor - Dbc* pcursor = GetCursor(); - if (!pcursor) + if (!LoadBlockIndexGuts()) return false; - // Load mapBlockIndex - unsigned int fFlags = DB_SET_RANGE; - loop - { - // 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; - - // Construct block index object - CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); - pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); - pindexNew->pnext = InsertBlockIndex(diskindex.hashNext); - pindexNew->nFile = diskindex.nFile; - pindexNew->nBlockPos = diskindex.nBlockPos; - pindexNew->nHeight = diskindex.nHeight; - 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 && diskindex.GetBlockHash() == hashGenesisBlock) - pindexGenesisBlock = pindexNew; - - if (!pindexNew->CheckIndex()) - return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight); - } - else - { - break; // if shutdown requested or finished loading block index - } - } // try - catch (std::exception &e) { - return error("%s() : deserialize error", __PRETTY_FUNCTION__); - } - } - pcursor->close(); - if (fRequestShutdown) return true; @@ -756,6 +705,75 @@ bool CTxDB::LoadBlockIndex() +bool CTxDB::LoadBlockIndexGuts() +{ + // Get database cursor + Dbc* pcursor = GetCursor(); + if (!pcursor) + return false; + + // Load mapBlockIndex + unsigned int fFlags = DB_SET_RANGE; + loop + { + // 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; + + // Construct block index object + CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash()); + pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev); + pindexNew->pnext = InsertBlockIndex(diskindex.hashNext); + pindexNew->nFile = diskindex.nFile; + pindexNew->nBlockPos = diskindex.nBlockPos; + pindexNew->nHeight = diskindex.nHeight; + 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 && diskindex.GetBlockHash() == hashGenesisBlock) + pindexGenesisBlock = pindexNew; + + if (!pindexNew->CheckIndex()) + return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight); + } + else + { + break; // if shutdown requested or finished loading block index + } + } // try + catch (std::exception &e) { + return error("%s() : deserialize error", __PRETTY_FUNCTION__); + } + } + pcursor->close(); + + return true; +} + + + // @@ -318,6 +318,8 @@ public: bool ReadBestInvalidWork(CBigNum& bnBestInvalidWork); bool WriteBestInvalidWork(CBigNum bnBestInvalidWork); bool LoadBlockIndex(); +private: + bool LoadBlockIndexGuts(); }; diff --git a/src/main.cpp b/src/main.cpp index 557f9dd99a..96718cf181 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1501,14 +1501,6 @@ bool static Reorganize(CTxDB& txdb, CBlockIndex* pindexNew) } -static void -runCommand(std::string strCommand) -{ - int nErr = ::system(strCommand.c_str()); - if (nErr) - printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr); -} - // Called from inside SetBestChain: attaches a block to the new best chain being built bool CBlock::SetBestChainInner(CTxDB& txdb, CBlockIndex *pindexNew) { diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index 5fa24f638d..6fb0e35569 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -8,18 +8,12 @@ #include <boost/lexical_cast.hpp> -#include "json/json_spirit_reader_template.h" -#include "json/json_spirit_writer_template.h" -#include "json/json_spirit_utils.h" - #define printf OutputDebugStringF // using namespace boost::asio; using namespace json_spirit; using namespace std; -extern Object JSONRPCError(int code, const string& message); - class CTxDump { public: diff --git a/src/util.cpp b/src/util.cpp index 08e3625b3d..3ab30baff6 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1079,3 +1079,11 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate) return fs::path(""); } #endif + +void runCommand(std::string strCommand) +{ + int nErr = ::system(strCommand.c_str()); + if (nErr) + printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr); +} + diff --git a/src/util.h b/src/util.h index 5b58147ce6..c1c91bdbc9 100644 --- a/src/util.h +++ b/src/util.h @@ -171,6 +171,7 @@ int64 GetAdjustedTime(); std::string FormatFullVersion(); std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments); void AddTimeData(const CNetAddr& ip, int64 nTime); +void runCommand(std::string strCommand); |