aboutsummaryrefslogtreecommitdiff
path: root/src/init.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/init.cpp')
-rw-r--r--src/init.cpp124
1 files changed, 84 insertions, 40 deletions
diff --git a/src/init.cpp b/src/init.cpp
index 3845cfad81..78f838f7cb 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -10,6 +10,7 @@
#include "init.h"
#include "util.h"
#include "ui_interface.h"
+#include "checkpoints.h"
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
@@ -96,10 +97,13 @@ void Shutdown()
RenameThread("bitcoin-shutoff");
nTransactionsUpdated++;
StopRPCThreads();
+ ShutdownRPCMining();
bitdb.Flush(false);
StopNode();
{
LOCK(cs_main);
+ if (pwalletMain)
+ pwalletMain->SetBestChain(CBlockLocator(pindexBest));
if (pblocktree)
pblocktree->Flush();
if (pcoinsTip)
@@ -194,7 +198,7 @@ bool AppInit(int argc, char* argv[])
exit(ret);
}
#if !defined(WIN32)
- fDaemon = GetBoolArg("-daemon");
+ fDaemon = GetBoolArg("-daemon", false);
if (fDaemon)
{
// Daemonize
@@ -290,8 +294,7 @@ std::string HelpMessage()
" -? " + _("This help message") + "\n" +
" -conf=<file> " + _("Specify configuration file (default: bitcoin.conf)") + "\n" +
" -pid=<file> " + _("Specify pid file (default: bitcoind.pid)") + "\n" +
- " -gen " + _("Generate coins") + "\n" +
- " -gen=0 " + _("Don't generate coins") + "\n" +
+ " -gen " + _("Generate coins (default: 0)") + "\n" +
" -datadir=<dir> " + _("Specify data directory") + "\n" +
" -dbcache=<n> " + _("Set database cache size in megabytes (default: 25)") + "\n" +
" -timeout=<n> " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" +
@@ -358,7 +361,7 @@ std::string HelpMessage()
" -txindex " + _("Maintain a full transaction index (default: 0)") + "\n" +
" -loadblock=<file> " + _("Imports blocks from external blk000??.dat file") + "\n" +
" -reindex " + _("Rebuild block chain index from current blk000??.dat files") + "\n" +
- " -par=N " + _("Set the number of script verification threads (up to 16, 0=auto, negative=leave N CPUs free, default: 0)") + "\n" +
+ " -par=<n> " + _("Set the number of script verification threads (up to 16, 0 = auto, <0 = leave that many cores free, default: 0)") + "\n" +
"\n" + _("Block creation options:") + "\n" +
" -blockminsize=<n> " + _("Set minimum block size in bytes (default: 0)") + "\n" +
@@ -466,9 +469,9 @@ bool AppInit2(boost::thread_group& threadGroup)
// Initialize Windows Sockets
WSADATA wsadata;
int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
- if (ret != NO_ERROR)
+ if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
{
- return InitError(strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret));
+ return InitError(strprintf("Error: Winsock library failed to start (WSAStartup returned error %d)", ret));
}
#endif
#ifndef WIN32
@@ -492,7 +495,8 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 2: parameter interactions
- fTestNet = GetBoolArg("-testnet");
+ fTestNet = GetBoolArg("-testnet", false);
+ Checkpoints::fEnabled = GetBoolArg("-checkpoints", true);
if (mapArgs.count("-bind")) {
// when specifying an explicit binding address, you want to listen on it
@@ -522,7 +526,7 @@ bool AppInit2(boost::thread_group& threadGroup)
SoftSetBoolArg("-discover", false);
}
- if (GetBoolArg("-salvagewallet")) {
+ if (GetBoolArg("-salvagewallet", false)) {
// Rewrite just private keys: rescan to find transactions
SoftSetBoolArg("-rescan", true);
}
@@ -530,7 +534,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// Make sure enough file descriptors are available
int nBind = std::max((int)mapArgs.count("-bind"), 1);
nMaxConnections = GetArg("-maxconnections", 125);
- nMaxConnections = std::max(std::min(nMaxConnections, FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS), 0);
+ nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS);
if (nFD < MIN_CORE_FILEDESCRIPTORS)
return InitError(_("Not enough file descriptors available."));
@@ -539,8 +543,8 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 3: parameter-to-internal-flags
- fDebug = GetBoolArg("-debug");
- fBenchmark = GetBoolArg("-benchmark");
+ fDebug = GetBoolArg("-debug", false);
+ fBenchmark = GetBoolArg("-benchmark", false);
// -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency
nScriptCheckThreads = GetArg("-par", 0);
@@ -555,20 +559,20 @@ bool AppInit2(boost::thread_group& threadGroup)
if (fDebug)
fDebugNet = true;
else
- fDebugNet = GetBoolArg("-debugnet");
+ fDebugNet = GetBoolArg("-debugnet", false);
if (fDaemon)
fServer = true;
else
- fServer = GetBoolArg("-server");
+ fServer = GetBoolArg("-server", false);
/* force fServer when running without GUI */
#if !defined(QT_GUI)
fServer = true;
#endif
- fPrintToConsole = GetBoolArg("-printtoconsole");
- fPrintToDebugger = GetBoolArg("-printtodebugger");
- fLogTimestamps = GetBoolArg("-logtimestamps");
+ fPrintToConsole = GetBoolArg("-printtoconsole", false);
+ fPrintToDebugger = GetBoolArg("-printtodebugger", false);
+ fLogTimestamps = GetBoolArg("-logtimestamps", false);
if (mapArgs.count("-timeout"))
{
@@ -583,6 +587,28 @@ bool AppInit2(boost::thread_group& threadGroup)
const char* pszP2SH = "/P2SH/";
COINBASE_FLAGS << std::vector<unsigned char>(pszP2SH, pszP2SH+strlen(pszP2SH));
+ // Fee-per-kilobyte amount considered the same as "free"
+ // If you are mining, be careful setting this:
+ // if you set it to zero then
+ // a transaction spammer can cheaply fill blocks using
+ // 1-satoshi-fee transactions. It should be set above the real
+ // cost to you of processing a transaction.
+ if (mapArgs.count("-mintxfee"))
+ {
+ int64 n = 0;
+ if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
+ CTransaction::nMinTxFee = n;
+ else
+ return InitError(strprintf(_("Invalid amount for -mintxfee=<amount>: '%s'"), mapArgs["-mintxfee"].c_str()));
+ }
+ if (mapArgs.count("-minrelaytxfee"))
+ {
+ int64 n = 0;
+ if (ParseMoney(mapArgs["-minrelaytxfee"], n) && n > 0)
+ CTransaction::nMinRelayTxFee = n;
+ else
+ return InitError(strprintf(_("Invalid amount for -minrelaytxfee=<amount>: '%s'"), mapArgs["-minrelaytxfee"].c_str()));
+ }
if (mapArgs.count("-paytxfee"))
{
@@ -612,7 +638,7 @@ bool AppInit2(boost::thread_group& threadGroup)
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("Used data directory %s\n", strDataDir.c_str());
+ printf("Using data directory %s\n", strDataDir.c_str());
printf("Using at most %i connections (%i file descriptors available)\n", nMaxConnections, nFD);
std::ostringstream strErrors;
@@ -633,11 +659,25 @@ bool AppInit2(boost::thread_group& threadGroup)
if (!bitdb.Open(GetDataDir()))
{
- string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
- return InitError(msg);
+ // try moving the database env out of the way
+ boost::filesystem::path pathDatabase = GetDataDir() / "database";
+ 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());
+ } catch(boost::filesystem::filesystem_error &error) {
+ // failure is ok (well, not really, but it's not worse than what we started with)
+ }
+
+ // try again
+ if (!bitdb.Open(GetDataDir())) {
+ // if it still fails, it probably means we can't even create the database env
+ string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
+ return InitError(msg);
+ }
}
- if (GetBoolArg("-salvagewallet"))
+ if (GetBoolArg("-salvagewallet", false))
{
// Recover readable keypairs:
if (!CWalletDB::Recover(bitdb, "wallet.dat", true))
@@ -759,16 +799,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 7: load block chain
- fReindex = GetBoolArg("-reindex");
-
- // Todo: Check if needed, because in step 5 we do the same
- if (!bitdb.Open(GetDataDir()))
- {
- string msg = strprintf(_("Error initializing database environment %s!"
- " To recover, BACKUP THAT DIRECTORY, then remove"
- " everything from it except for wallet.dat."), strDataDir.c_str());
- return InitError(msg);
- }
+ fReindex = GetBoolArg("-reindex", false);
// Upgrading to 0.8; hard-link the old blknnnn.dat files into /blocks/
filesystem::path blocksDir = GetDataDir() / "blocks";
@@ -836,6 +867,11 @@ bool AppInit2(boost::thread_group& threadGroup)
break;
}
+ // 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)
+ 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)
if (!InitBlockIndex()) {
strLoadError = _("Error initializing block database");
@@ -886,7 +922,7 @@ bool AppInit2(boost::thread_group& threadGroup)
}
printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
- if (GetBoolArg("-printblockindex") || GetBoolArg("-printblocktree"))
+ if (GetBoolArg("-printblockindex", false) || GetBoolArg("-printblocktree", false))
{
PrintBlockTree();
return false;
@@ -967,11 +1003,13 @@ bool AppInit2(boost::thread_group& threadGroup)
RandAddSeedPerfmon();
CPubKey newDefaultKey;
- if (!pwalletMain->GetKeyFromPool(newDefaultKey, false))
- strErrors << _("Cannot initialize keypool") << "\n";
- pwalletMain->SetDefaultKey(newDefaultKey);
- if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), ""))
- strErrors << _("Cannot write default address") << "\n";
+ if (pwalletMain->GetKeyFromPool(newDefaultKey, false)) {
+ pwalletMain->SetDefaultKey(newDefaultKey);
+ if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), ""))
+ strErrors << _("Cannot write default address") << "\n";
+ }
+
+ pwalletMain->SetBestChain(CBlockLocator(pindexBest));
}
printf("%s", strErrors.str().c_str());
@@ -980,7 +1018,7 @@ bool AppInit2(boost::thread_group& threadGroup)
RegisterWallet(pwalletMain);
CBlockIndex *pindexRescan = pindexBest;
- if (GetBoolArg("-rescan"))
+ if (GetBoolArg("-rescan", false))
pindexRescan = pindexGenesisBlock;
else
{
@@ -988,6 +1026,8 @@ bool AppInit2(boost::thread_group& threadGroup)
CBlockLocator locator;
if (walletdb.ReadBestBlock(locator))
pindexRescan = locator.GetBlockIndex();
+ else
+ pindexRescan = pindexGenesisBlock;
}
if (pindexBest && pindexBest != pindexRescan)
{
@@ -996,6 +1036,8 @@ bool AppInit2(boost::thread_group& threadGroup)
nStart = GetTimeMillis();
pwalletMain->ScanForWalletTransactions(pindexRescan, true);
printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
+ pwalletMain->SetBestChain(CBlockLocator(pindexBest));
+ nWalletDBUpdated++;
}
// ********************************************************* Step 9: import blocks
@@ -1033,6 +1075,9 @@ bool AppInit2(boost::thread_group& threadGroup)
if (!CheckDiskSpace())
return false;
+ if (!strErrors.str().empty())
+ return InitError(strErrors.str());
+
RandAddSeedPerfmon();
//// debug print
@@ -1044,6 +1089,8 @@ bool AppInit2(boost::thread_group& threadGroup)
StartNode(threadGroup);
+ // InitRPCMining is needed here so getwork/getblocktemplate in the GUI debug console works properly.
+ InitRPCMining();
if (fServer)
StartRPCThreads();
@@ -1054,9 +1101,6 @@ bool AppInit2(boost::thread_group& threadGroup)
uiInterface.InitMessage(_("Done loading"));
- if (!strErrors.str().empty())
- return InitError(strErrors.str());
-
// Add wallet transactions that aren't already in a block to mapTransactions
pwalletMain->ReacceptWalletTransactions();