aboutsummaryrefslogtreecommitdiff
path: root/src/init.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/init.cpp')
-rw-r--r--src/init.cpp127
1 files changed, 69 insertions, 58 deletions
diff --git a/src/init.cpp b/src/init.cpp
index acf6deab8d..fce5992255 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -3,6 +3,10 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#if defined(HAVE_CONFIG_H)
+#include "bitcoin-config.h"
+#endif
+
#include "init.h"
#include "main.h"
#include "core.h"
@@ -96,6 +100,7 @@ static CCoinsViewDB *pcoinsdbview;
void Shutdown()
{
+ LogPrintf("Shutdown : In progress...\n");
static CCriticalSection cs_Shutdown;
TRY_LOCK(cs_Shutdown, lockShutdown);
if (!lockShutdown) return;
@@ -104,13 +109,14 @@ void Shutdown()
nTransactionsUpdated++;
StopRPCThreads();
ShutdownRPCMining();
- bitdb.Flush(false);
+ if (pwalletMain)
+ bitdb.Flush(false);
GenerateBitcoins(false, NULL);
StopNode();
{
LOCK(cs_main);
if (pwalletMain)
- pwalletMain->SetBestChain(CBlockLocator(pindexBest));
+ pwalletMain->SetBestChain(chainActive.GetLocator());
if (pblocktree)
pblocktree->Flush();
if (pcoinsTip)
@@ -119,10 +125,13 @@ void Shutdown()
delete pcoinsdbview; pcoinsdbview = NULL;
delete pblocktree; pblocktree = NULL;
}
- bitdb.Flush(true);
+ if (pwalletMain)
+ bitdb.Flush(true);
boost::filesystem::remove(GetPidFile());
UnregisterAllWallets();
- delete pwalletMain;
+ if (pwalletMain)
+ delete pwalletMain;
+ LogPrintf("Shutdown : done\n");
}
//
@@ -280,13 +289,13 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
FILE *file = OpenBlockFile(pos, true);
if (!file)
break;
- printf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
+ LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
LoadExternalBlockFile(file, &pos);
nFile++;
}
pblocktree->WriteReindexing(false);
fReindex = false;
- printf("Reindexing finished\n");
+ LogPrintf("Reindexing finished\n");
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
InitBlockIndex();
}
@@ -298,7 +307,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
if (file) {
CImportingNow imp;
filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
- printf("Importing bootstrap.dat...\n");
+ LogPrintf("Importing bootstrap.dat...\n");
LoadExternalBlockFile(file);
RenameOver(pathBootstrap, pathBootstrapOld);
}
@@ -309,7 +318,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
FILE *file = fopen(path.string().c_str(), "rb");
if (file) {
CImportingNow imp;
- printf("Importing %s...\n", path.string().c_str());
+ LogPrintf("Importing %s...\n", path.string().c_str());
LoadExternalBlockFile(file);
}
}
@@ -335,8 +344,8 @@ bool AppInit2(boost::thread_group& threadGroup)
// Minimum supported OS versions: WinXP SP3, WinVista >= SP1, Win Server 2008
// A failure is non-critical and needs no further attention!
#ifndef PROCESS_DEP_ENABLE
-// We define this here, because GCCs winbase.h limits this to _WIN32_WINNT >= 0x0601 (Windows 7),
-// which is not correct. Can be removed, when GCCs winbase.h is fixed!
+ // We define this here, because GCCs winbase.h limits this to _WIN32_WINNT >= 0x0601 (Windows 7),
+ // which is not correct. Can be removed, when GCCs winbase.h is fixed!
#define PROCESS_DEP_ENABLE 0x00000001
#endif
typedef BOOL (WINAPI *PSETPROCDEPPOL)(DWORD);
@@ -377,8 +386,6 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 2: parameter interactions
- Checkpoints::fEnabled = GetBoolArg("-checkpoints", true);
-
if (mapArgs.count("-bind")) {
// when specifying an explicit binding address, you want to listen on it
// even when -connect or -proxy is specified
@@ -424,9 +431,10 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 3: parameter-to-internal-flags
- fDebug = GetBoolArg("-debug", false);
+ if (mapMultiArgs.count("-debug")) fDebug = true;
fBenchmark = GetBoolArg("-benchmark", false);
mempool.fChecks = GetBoolArg("-checkmempool", RegTest());
+ Checkpoints::fEnabled = GetBoolArg("-checkpoints", true);
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
nScriptCheckThreads = GetArg("-par", 0);
@@ -519,21 +527,21 @@ bool AppInit2(boost::thread_group& threadGroup)
if (GetBoolArg("-shrinkdebugfile", !fDebug))
ShrinkDebugFile();
- printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
- printf("Bitcoin version %s (%s)\n", FormatFullVersion().c_str(), CLIENT_DATE.c_str());
- printf("Using OpenSSL version %s\n", SSLeay_version(SSLEAY_VERSION));
+ LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
+ LogPrintf("Bitcoin version %s (%s)\n", FormatFullVersion().c_str(), CLIENT_DATE.c_str());
+ LogPrintf("Using OpenSSL version %s\n", SSLeay_version(SSLEAY_VERSION));
if (!fLogTimestamps)
- printf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
- printf("Default data directory %s\n", GetDefaultDataDir().string().c_str());
- printf("Using data directory %s\n", strDataDir.c_str());
- printf("Using at most %i connections (%i file descriptors available)\n", nMaxConnections, nFD);
+ LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
+ LogPrintf("Default data directory %s\n", GetDefaultDataDir().string().c_str());
+ LogPrintf("Using data directory %s\n", strDataDir.c_str());
+ LogPrintf("Using at most %i connections (%i file descriptors available)\n", nMaxConnections, nFD);
std::ostringstream strErrors;
if (fDaemon)
fprintf(stdout, "Bitcoin server starting\n");
if (nScriptCheckThreads) {
- printf("Using %u threads for script verification\n", nScriptCheckThreads);
+ LogPrintf("Using %u threads for script verification\n", nScriptCheckThreads);
for (int i=0; i<nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck);
}
@@ -551,7 +559,7 @@ bool AppInit2(boost::thread_group& threadGroup)
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRI64d".bak", GetTime());
try {
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
- printf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str());
+ LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str());
} catch(boost::filesystem::filesystem_error &error) {
// failure is ok (well, not really, but it's not worse than what we started with)
}
@@ -702,12 +710,12 @@ bool AppInit2(boost::thread_group& threadGroup)
filesystem::path dest = blocksDir / strprintf("blk%05u.dat", i-1);
try {
filesystem::create_hard_link(source, dest);
- printf("Hardlinked %s -> %s\n", source.string().c_str(), dest.string().c_str());
+ LogPrintf("Hardlinked %s -> %s\n", source.string().c_str(), dest.string().c_str());
linked = true;
} catch (filesystem::filesystem_error & e) {
// Note: hardlink creation failing is not a disaster, it just means
// blocks will get re-downloaded from peers.
- printf("Error hardlinking blk%04u.dat : %s\n", i, e.what());
+ LogPrintf("Error hardlinking blk%04u.dat : %s\n", i, e.what());
break;
}
}
@@ -758,7 +766,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
- if (!mapBlockIndex.empty() && pindexGenesisBlock == NULL)
+ if (!mapBlockIndex.empty() && chainActive.Genesis() == NULL)
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
// Initialize the block index (no-op if non-empty database was already loaded)
@@ -775,12 +783,12 @@ bool AppInit2(boost::thread_group& threadGroup)
uiInterface.InitMessage(_("Verifying blocks..."));
if (!VerifyDB(GetArg("-checklevel", 3),
- GetArg( "-checkblocks", 288))) {
+ GetArg("-checkblocks", 288))) {
strLoadError = _("Corrupted block database detected");
break;
}
} catch(std::exception &e) {
- if (fDebug) printf("%s\n", e.what());
+ if (fDebug) LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database");
break;
}
@@ -798,7 +806,7 @@ bool AppInit2(boost::thread_group& threadGroup)
fReindex = true;
fRequestShutdown = false;
} else {
- printf("Aborted block database rebuild. Exiting.\n");
+ LogPrintf("Aborted block database rebuild. Exiting.\n");
return false;
}
} else {
@@ -812,10 +820,10 @@ bool AppInit2(boost::thread_group& threadGroup)
// As the program has not fully started yet, Shutdown() is possibly overkill.
if (fRequestShutdown)
{
- printf("Shutdown requested. Exiting.\n");
+ LogPrintf("Shutdown requested. Exiting.\n");
return false;
}
- printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
+ LogPrintf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
if (GetBoolArg("-printblockindex", false) || GetBoolArg("-printblocktree", false))
{
@@ -837,12 +845,12 @@ bool AppInit2(boost::thread_group& threadGroup)
ReadBlockFromDisk(block, pindex);
block.BuildMerkleTree();
block.print();
- printf("\n");
+ LogPrintf("\n");
nFound++;
}
}
if (nFound == 0)
- printf("No blocks matching %s were found\n", strMatch.c_str());
+ LogPrintf("No blocks matching %s were found\n", strMatch.c_str());
return false;
}
@@ -869,7 +877,7 @@ bool AppInit2(boost::thread_group& threadGroup)
else if (nLoadWalletRet == DB_NEED_REWRITE)
{
strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n";
- printf("%s", strErrors.str().c_str());
+ LogPrintf("%s", strErrors.str().c_str());
return InitError(strErrors.str());
}
else
@@ -881,12 +889,12 @@ bool AppInit2(boost::thread_group& threadGroup)
int nMaxVersion = GetArg("-upgradewallet", 0);
if (nMaxVersion == 0) // the -upgradewallet without argument case
{
- printf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
+ LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
nMaxVersion = CLIENT_VERSION;
pwalletMain->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
}
else
- printf("Allowing wallet upgrade up to %i\n", nMaxVersion);
+ LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
if (nMaxVersion < pwalletMain->GetVersion())
strErrors << _("Cannot downgrade wallet") << "\n";
pwalletMain->SetMaxVersion(nMaxVersion);
@@ -904,34 +912,34 @@ bool AppInit2(boost::thread_group& threadGroup)
strErrors << _("Cannot write default address") << "\n";
}
- pwalletMain->SetBestChain(CBlockLocator(pindexBest));
+ pwalletMain->SetBestChain(chainActive.GetLocator());
}
- printf("%s", strErrors.str().c_str());
- printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);
+ LogPrintf("%s", strErrors.str().c_str());
+ LogPrintf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);
RegisterWallet(pwalletMain);
- CBlockIndex *pindexRescan = pindexBest;
+ CBlockIndex *pindexRescan = chainActive.Tip();
if (GetBoolArg("-rescan", false))
- pindexRescan = pindexGenesisBlock;
+ pindexRescan = chainActive.Genesis();
else
{
CWalletDB walletdb(strWalletFile);
CBlockLocator locator;
if (walletdb.ReadBestBlock(locator))
- pindexRescan = locator.GetBlockIndex();
+ pindexRescan = chainActive.FindFork(locator);
else
- pindexRescan = pindexGenesisBlock;
+ pindexRescan = chainActive.Genesis();
}
- if (pindexBest && pindexBest != pindexRescan)
+ if (chainActive.Tip() && chainActive.Tip() != pindexRescan)
{
uiInterface.InitMessage(_("Rescanning..."));
- printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
+ LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
nStart = GetTimeMillis();
pwalletMain->ScanForWalletTransactions(pindexRescan, true);
- printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
- pwalletMain->SetBestChain(CBlockLocator(pindexBest));
+ LogPrintf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
+ pwalletMain->SetBestChain(chainActive.GetLocator());
nWalletDBUpdated++;
}
@@ -959,10 +967,10 @@ bool AppInit2(boost::thread_group& threadGroup)
{
CAddrDB adb;
if (!adb.Read(addrman))
- printf("Invalid or missing peers.dat; recreating\n");
+ LogPrintf("Invalid or missing peers.dat; recreating\n");
}
- printf("Loaded %i addresses from peers.dat %"PRI64d"ms\n",
+ LogPrintf("Loaded %i addresses from peers.dat %"PRI64d"ms\n",
addrman.size(), GetTimeMillis() - nStart);
// ********************************************************* Step 11: start node
@@ -976,11 +984,11 @@ bool AppInit2(boost::thread_group& threadGroup)
RandAddSeedPerfmon();
//// debug print
- printf("mapBlockIndex.size() = %"PRIszu"\n", mapBlockIndex.size());
- printf("nBestHeight = %d\n", nBestHeight);
- printf("setKeyPool.size() = %"PRIszu"\n", pwalletMain->setKeyPool.size());
- printf("mapWallet.size() = %"PRIszu"\n", pwalletMain->mapWallet.size());
- printf("mapAddressBook.size() = %"PRIszu"\n", pwalletMain->mapAddressBook.size());
+ LogPrintf("mapBlockIndex.size() = %"PRIszu"\n", mapBlockIndex.size());
+ LogPrintf("nBestHeight = %d\n", chainActive.Height());
+ LogPrintf("setKeyPool.size() = %"PRIszu"\n", pwalletMain ? pwalletMain->setKeyPool.size() : 0);
+ LogPrintf("mapWallet.size() = %"PRIszu"\n", pwalletMain ? pwalletMain->mapWallet.size() : 0);
+ LogPrintf("mapAddressBook.size() = %"PRIszu"\n", pwalletMain ? pwalletMain->mapAddressBook.size() : 0);
StartNode(threadGroup);
@@ -990,17 +998,20 @@ bool AppInit2(boost::thread_group& threadGroup)
StartRPCThreads();
// Generate coins in the background
- GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain);
+ if (pwalletMain)
+ GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain);
// ********************************************************* Step 12: finished
uiInterface.InitMessage(_("Done loading"));
- // Add wallet transactions that aren't already in a block to mapTransactions
- pwalletMain->ReacceptWalletTransactions();
+ if (pwalletMain) {
+ // Add wallet transactions that aren't already in a block to mapTransactions
+ pwalletMain->ReacceptWalletTransactions();
- // Run a thread to flush wallet periodically
- threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(pwalletMain->strWalletFile)));
+ // Run a thread to flush wallet periodically
+ threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(pwalletMain->strWalletFile)));
+ }
return !fRequestShutdown;
}