From e79110822efdc5fef27b1b24232a0655359e7df3 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Sat, 23 Feb 2013 23:51:02 +0100 Subject: remove duplicate bitdb.Open() code from init - remove code from step 7, which we already have in step 5 of init --- src/init.cpp | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index f6485c3b1..11b7554ed 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -733,15 +733,6 @@ bool AppInit2(boost::thread_group& threadGroup) 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); - } - // Upgrading to 0.8; hard-link the old blknnnn.dat files into /blocks/ filesystem::path blocksDir = GetDataDir() / "blocks"; if (!filesystem::exists(blocksDir)) -- cgit v1.2.3 From 1859aafef0a273e27e886646e36140cc5e375ee1 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 24 Apr 2013 00:41:04 +0200 Subject: Try moving database/ away in case of failure --- src/init.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 11b7554ed..48495c446 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -605,8 +605,22 @@ 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")) -- cgit v1.2.3 From 360cfe142c552ac5c4d904a1e970390188151ca8 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 25 Apr 2013 19:30:28 +0200 Subject: Allow the default key to be unavailable This solves the issue where no default key can be added after -salvagewallet. --- src/init.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index f6485c3b1..f35979bc0 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -939,11 +939,11 @@ 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"; + } } printf("%s", strErrors.str().c_str()); -- cgit v1.2.3 From 2aceeb01a9b0b0c6bfa0d56211453e87bc33d6e4 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 1 May 2013 19:09:04 +0200 Subject: Bugfix: if no bestblock record is present, do a -rescan It is possible to have a wallet.dat file without any bestblock record at all (if created offline, for example), which - when loaded into a client with a up-to-date chain - does no rescan and shows no transactions. Also make sure to write the current best block after a rescan, so it isn't necessary twice. --- src/init.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 3845cfad8..3c1040a79 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -988,6 +988,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 +998,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 -- cgit v1.2.3 From d605bc4cd13716fde9c34d79a01f4ee128f8814f Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 2 May 2013 12:26:33 -0400 Subject: Exit cleanly if AppInit2 returns false Bitcoin-Qt could core dump if application initialization failed in certain ways. I double-fixed this: 1) qt/bitcoin.cpp now shuts down core threads cleanly if AppInit2 returns false 2) init.cpp now exits before StartNode() if strErrors is set (no reason to StartNode if we're just going to exit immediately anyway). Tested by triggering all of the various ways AppInit2 can fail, either by passing bogus command-line arguments or just recompiling tweaked code to simulate failure. This is a partial fix for #2480 --- src/init.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 3845cfad8..c2259f1d7 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1033,6 +1033,9 @@ bool AppInit2(boost::thread_group& threadGroup) if (!CheckDiskSpace()) return false; + if (!strErrors.str().empty()) + return InitError(strErrors.str()); + RandAddSeedPerfmon(); //// debug print @@ -1054,9 +1057,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(); -- cgit v1.2.3 From 000dc55181f77cd96076c76b2cc13f8bcbe4146e Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 25 Apr 2013 20:11:27 -0400 Subject: Un-hardcode TX_FEE constants Allow setting of MIN_TX_FEE / MIN_RELAY_TX_FEE with -mintxfee / -mintxrelayfee Default values are the same (0.0001 BTC). --- src/init.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 3845cfad8..058b6a06e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -583,6 +583,28 @@ bool AppInit2(boost::thread_group& threadGroup) const char* pszP2SH = "/P2SH/"; COINBASE_FLAGS << std::vector(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=: '%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=: '%s'"), mapArgs["-minrelaytxfee"].c_str())); + } if (mapArgs.count("-paytxfee")) { -- cgit v1.2.3 From 110257a631455ac7b5c15c027d00bc41c04a2478 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Tue, 30 Apr 2013 18:16:07 +0200 Subject: small init.cpp changes (strings / Winsock init) - add a check that requested Winsock version is available - update some strings - remove -gen=0 from help-message as this is default --- src/init.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 3845cfad8..d82afd712 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -290,8 +290,7 @@ std::string HelpMessage() " -? " + _("This help message") + "\n" + " -conf= " + _("Specify configuration file (default: bitcoin.conf)") + "\n" + " -pid= " + _("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= " + _("Specify data directory") + "\n" + " -dbcache= " + _("Set database cache size in megabytes (default: 25)") + "\n" + " -timeout= " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" + @@ -358,7 +357,7 @@ std::string HelpMessage() " -txindex " + _("Maintain a full transaction index (default: 0)") + "\n" + " -loadblock= " + _("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= " + _("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= " + _("Set minimum block size in bytes (default: 0)") + "\n" + @@ -466,9 +465,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 @@ -612,7 +611,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; -- cgit v1.2.3 From 5d274c9927f01c590f187bbbc670a92e441c53b7 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 12 May 2013 12:03:32 +0200 Subject: Check for correct genesis At startup, check that the expected genesis is loaded. This should prevent cases where accidentally a datadir from the wrong network is loaded (testnet vs mainnet, e.g.). --- src/init.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index d619cb412..1e2ffe48e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -862,6 +862,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"); -- cgit v1.2.3 From 95c7db3dbf4012dd02e5f1f30e6d982341facaa6 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 22 May 2013 20:58:53 +0200 Subject: More bestblock records in wallets Write bestblock records in wallets: * Every 20160 blocks synced, no matter what (before: none during IBD) * Every 144 blocks after IBD (before: for every block, slow) * When creating a new wallet * At shutdown This should result in far fewer spurious rescans. --- src/init.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index d619cb412..ebd9dee7b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -100,6 +100,7 @@ void Shutdown() StopNode(); { LOCK(cs_main); + pwalletMain->SetBestChain(CBlockLocator(pindexBest)); if (pblocktree) pblocktree->Flush(); if (pcoinsTip) @@ -998,6 +999,8 @@ bool AppInit2(boost::thread_group& threadGroup) if (!pwalletMain->SetAddressBookName(pwalletMain->vchDefaultKey.GetID(), "")) strErrors << _("Cannot write default address") << "\n"; } + + pwalletMain->SetBestChain(CBlockLocator(pindexBest)); } printf("%s", strErrors.str().c_str()); -- cgit v1.2.3 From 03f498080a813850e1a3addf2c555450aa5e65c1 Mon Sep 17 00:00:00 2001 From: Robert Backhaus Date: Fri, 24 May 2013 23:40:51 +1000 Subject: Explictly cast calculation to int, to allow std::max to work. --- src/init.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index ebd9dee7b..3eb090ac0 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -530,7 +530,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.")); -- cgit v1.2.3 From f0d8a52cc039cda77730047db2443fbec016f852 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Fri, 24 May 2013 11:10:53 -0400 Subject: Replace repeated GetBoolArg() calls with Checkpoint::fEnabled variable set once at init time. --- src/init.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index ebd9dee7b..6dafcc4af 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 #include @@ -493,6 +494,7 @@ bool AppInit2(boost::thread_group& threadGroup) // ********************************************************* Step 2: parameter interactions fTestNet = GetBoolArg("-testnet"); + Checkpoints::fEnabled = GetBoolArg("-checkpoints", true); if (mapArgs.count("-bind")) { // when specifying an explicit binding address, you want to listen on it -- cgit v1.2.3 From dbc6dea1b27e87bca0ef496b8b74d6f027bb7de3 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Fri, 24 May 2013 15:52:52 -0400 Subject: Fix crash-at-shutdown if exiting before initializing wallet --- src/init.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index ebd9dee7b..59bf32eca 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -100,7 +100,8 @@ void Shutdown() StopNode(); { LOCK(cs_main); - pwalletMain->SetBestChain(CBlockLocator(pindexBest)); + if (pwalletMain) + pwalletMain->SetBestChain(CBlockLocator(pindexBest)); if (pblocktree) pblocktree->Flush(); if (pcoinsTip) -- cgit v1.2.3 From d98bf10f23e0e633ff2ff33075a353d30bf862b4 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 30 May 2013 15:51:41 +0200 Subject: Move pMiningKey init out of StartRPCThreads This commit decouples the pMiningKey initialization and shutdown from the RPC threads. `getwork` and `getblocktemplate` rely on pMiningKey, and can also be ran from the debug window in the UI even when the RPC server is not running. Solves issue #2706. --- src/init.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 767d7525a..9c807b288 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -96,6 +96,7 @@ void Shutdown() RenameThread("bitcoin-shutoff"); nTransactionsUpdated++; StopRPCThreads(); + ShutdownRPCMining(); bitdb.Flush(false); StopNode(); { @@ -1081,6 +1082,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(); -- cgit v1.2.3 From 3260b4c09006ea5c1b00c599a14e6c706ac760f8 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Sun, 28 Apr 2013 17:37:50 +0200 Subject: remove GetBoolArg() fDefault parameter defaulting to false - explicitly set the default of all GetBoolArg() calls - rework getarg_test.cpp and util_tests.cpp to cover this change - some indentation fixes - move macdockiconhandler.h include in bitcoin.cpp to the "our headers" section --- src/init.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 226e93e9a..78f838f7c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -198,7 +198,7 @@ bool AppInit(int argc, char* argv[]) exit(ret); } #if !defined(WIN32) - fDaemon = GetBoolArg("-daemon"); + fDaemon = GetBoolArg("-daemon", false); if (fDaemon) { // Daemonize @@ -495,7 +495,7 @@ 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")) { @@ -526,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); } @@ -543,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); @@ -559,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")) { @@ -677,7 +677,7 @@ bool AppInit2(boost::thread_group& threadGroup) } } - if (GetBoolArg("-salvagewallet")) + if (GetBoolArg("-salvagewallet", false)) { // Recover readable keypairs: if (!CWalletDB::Recover(bitdb, "wallet.dat", true)) @@ -799,7 +799,7 @@ bool AppInit2(boost::thread_group& threadGroup) // ********************************************************* Step 7: load block chain - fReindex = GetBoolArg("-reindex"); + fReindex = GetBoolArg("-reindex", false); // Upgrading to 0.8; hard-link the old blknnnn.dat files into /blocks/ filesystem::path blocksDir = GetDataDir() / "blocks"; @@ -922,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; @@ -1018,7 +1018,7 @@ bool AppInit2(boost::thread_group& threadGroup) RegisterWallet(pwalletMain); CBlockIndex *pindexRescan = pindexBest; - if (GetBoolArg("-rescan")) + if (GetBoolArg("-rescan", false)) pindexRescan = pindexGenesisBlock; else { -- cgit v1.2.3