Introduce CHashVerifier to hash read data
[bitcoinplatinum.git] / src / bitcoind.cpp
blob31680a8ec765ca85916f66df1e97ba567aaca72f
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
10 #include "chainparams.h"
11 #include "clientversion.h"
12 #include "compat.h"
13 #include "fs.h"
14 #include "rpc/server.h"
15 #include "init.h"
16 #include "noui.h"
17 #include "scheduler.h"
18 #include "util.h"
19 #include "httpserver.h"
20 #include "httprpc.h"
21 #include "utilstrencodings.h"
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/thread.hpp>
26 #include <stdio.h>
28 /* Introduction text for doxygen: */
30 /*! \mainpage Developer documentation
32 * \section intro_sec Introduction
34 * This is the developer documentation of the reference client for an experimental new digital currency called Bitcoin (https://www.bitcoin.org/),
35 * which enables instant payments to anyone, anywhere in the world. Bitcoin uses peer-to-peer technology to operate
36 * with no central authority: managing transactions and issuing money are carried out collectively by the network.
38 * The software is a community-driven open source project, released under the MIT license.
40 * \section Navigation
41 * Use the buttons <code>Namespaces</code>, <code>Classes</code> or <code>Files</code> at the top of the page to start navigating the code.
44 void WaitForShutdown(boost::thread_group* threadGroup)
46 bool fShutdown = ShutdownRequested();
47 // Tell the main threads to shutdown.
48 while (!fShutdown)
50 MilliSleep(200);
51 fShutdown = ShutdownRequested();
53 if (threadGroup)
55 Interrupt(*threadGroup);
56 threadGroup->join_all();
60 //////////////////////////////////////////////////////////////////////////////
62 // Start
64 bool AppInit(int argc, char* argv[])
66 boost::thread_group threadGroup;
67 CScheduler scheduler;
69 bool fRet = false;
72 // Parameters
74 // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
75 ParseParameters(argc, argv);
77 // Process help and version before taking care about datadir
78 if (IsArgSet("-?") || IsArgSet("-h") || IsArgSet("-help") || IsArgSet("-version"))
80 std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
82 if (IsArgSet("-version"))
84 strUsage += FormatParagraph(LicenseInfo());
86 else
88 strUsage += "\n" + _("Usage:") + "\n" +
89 " bitcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
91 strUsage += "\n" + HelpMessage(HMM_BITCOIND);
94 fprintf(stdout, "%s", strUsage.c_str());
95 return true;
98 try
100 if (!fs::is_directory(GetDataDir(false)))
102 fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", GetArg("-datadir", "").c_str());
103 return false;
107 ReadConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME));
108 } catch (const std::exception& e) {
109 fprintf(stderr,"Error reading configuration file: %s\n", e.what());
110 return false;
112 // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
113 try {
114 SelectParams(ChainNameFromCommandLine());
115 } catch (const std::exception& e) {
116 fprintf(stderr, "Error: %s\n", e.what());
117 return false;
120 // Command-line RPC
121 bool fCommandLine = false;
122 for (int i = 1; i < argc; i++)
123 if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "bitcoin:"))
124 fCommandLine = true;
126 if (fCommandLine)
128 fprintf(stderr, "Error: There is no RPC client functionality in bitcoind anymore. Use the bitcoin-cli utility instead.\n");
129 exit(EXIT_FAILURE);
131 // -server defaults to true for bitcoind but not for the GUI so do this here
132 SoftSetBoolArg("-server", true);
133 // Set this early so that parameter interactions go to console
134 InitLogging();
135 InitParameterInteraction();
136 if (!AppInitBasicSetup())
138 // InitError will have been called with detailed error, which ends up on console
139 exit(EXIT_FAILURE);
141 if (!AppInitParameterInteraction())
143 // InitError will have been called with detailed error, which ends up on console
144 exit(EXIT_FAILURE);
146 if (!AppInitSanityChecks())
148 // InitError will have been called with detailed error, which ends up on console
149 exit(EXIT_FAILURE);
151 if (GetBoolArg("-daemon", false))
153 #if HAVE_DECL_DAEMON
154 fprintf(stdout, "Bitcoin server starting\n");
156 // Daemonize
157 if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
158 fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
159 return false;
161 #else
162 fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
163 return false;
164 #endif // HAVE_DECL_DAEMON
167 fRet = AppInitMain(threadGroup, scheduler);
169 catch (const std::exception& e) {
170 PrintExceptionContinue(&e, "AppInit()");
171 } catch (...) {
172 PrintExceptionContinue(NULL, "AppInit()");
175 if (!fRet)
177 Interrupt(threadGroup);
178 // threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
179 // the startup-failure cases to make sure they don't result in a hang due to some
180 // thread-blocking-waiting-for-another-thread-during-startup case
181 } else {
182 WaitForShutdown(&threadGroup);
184 Shutdown();
186 return fRet;
189 int main(int argc, char* argv[])
191 SetupEnvironment();
193 // Connect bitcoind signal handlers
194 noui_connect();
196 return (AppInit(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE);