Update License in File Headers
[novacoin.git] / src / init.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "db.h"
6 #include "walletdb.h"
7 #include "bitcoinrpc.h"
8 #include "net.h"
9 #include "init.h"
10 #include "util.h"
11 #include "ui_interface.h"
12 #include <boost/filesystem.hpp>
13 #include <boost/filesystem/fstream.hpp>
14 #include <boost/filesystem/convenience.hpp>
15 #include <boost/interprocess/sync/file_lock.hpp>
16
17 #ifndef WIN32
18 #include <signal.h>
19 #endif
20
21 using namespace std;
22 using namespace boost;
23
24 CWallet* pwalletMain;
25
26 //////////////////////////////////////////////////////////////////////////////
27 //
28 // Shutdown
29 //
30
31 void ExitTimeout(void* parg)
32 {
33 #ifdef WIN32
34     Sleep(5000);
35     ExitProcess(0);
36 #endif
37 }
38
39 void Shutdown(void* parg)
40 {
41     static CCriticalSection cs_Shutdown;
42     static bool fTaken;
43     bool fFirstThread = false;
44     {
45         TRY_LOCK(cs_Shutdown, lockShutdown);
46         if (lockShutdown)
47         {
48             fFirstThread = !fTaken;
49             fTaken = true;
50         }
51     }
52     static bool fExit;
53     if (fFirstThread)
54     {
55         fShutdown = true;
56         nTransactionsUpdated++;
57         DBFlush(false);
58         StopNode();
59         DBFlush(true);
60         boost::filesystem::remove(GetPidFile());
61         UnregisterWallet(pwalletMain);
62         delete pwalletMain;
63         CreateThread(ExitTimeout, NULL);
64         Sleep(50);
65         printf("Bitcoin exiting\n\n");
66         fExit = true;
67         exit(0);
68     }
69     else
70     {
71         while (!fExit)
72             Sleep(500);
73         Sleep(100);
74         ExitThread(0);
75     }
76 }
77
78 void HandleSIGTERM(int)
79 {
80     fRequestShutdown = true;
81 }
82
83
84
85
86
87
88 //////////////////////////////////////////////////////////////////////////////
89 //
90 // Start
91 //
92 #if !defined(QT_GUI)
93 int main(int argc, char* argv[])
94 {
95     bool fRet = false;
96     fRet = AppInit(argc, argv);
97
98     if (fRet && fDaemon)
99         return 0;
100
101     return 1;
102 }
103 #endif
104
105 bool AppInit(int argc, char* argv[])
106 {
107     bool fRet = false;
108     try
109     {
110         fRet = AppInit2(argc, argv);
111     }
112     catch (std::exception& e) {
113         PrintException(&e, "AppInit()");
114     } catch (...) {
115         PrintException(NULL, "AppInit()");
116     }
117     if (!fRet)
118         Shutdown(NULL);
119     return fRet;
120 }
121
122 bool AppInit2(int argc, char* argv[])
123 {
124 #ifdef _MSC_VER
125     // Turn off microsoft heap dump noise
126     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
127     _CrtSetReportFile(_CRT_WARN, CreateFileA("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0));
128 #endif
129 #if _MSC_VER >= 1400
130     // Disable confusing "helpful" text message on abort, ctrl-c
131     _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
132 #endif
133 #ifndef WIN32
134     umask(077);
135 #endif
136 #ifndef WIN32
137     // Clean shutdown on SIGTERM
138     struct sigaction sa;
139     sa.sa_handler = HandleSIGTERM;
140     sigemptyset(&sa.sa_mask);
141     sa.sa_flags = 0;
142     sigaction(SIGTERM, &sa, NULL);
143     sigaction(SIGINT, &sa, NULL);
144     sigaction(SIGHUP, &sa, NULL);
145 #endif
146
147     //
148     // Parameters
149     //
150     // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
151 #if !defined(QT_GUI)
152     ParseParameters(argc, argv);
153     if (!boost::filesystem::is_directory(GetDataDir(false)))
154     {
155         fprintf(stderr, "Error: Specified directory does not exist\n");
156         Shutdown(NULL);
157     }
158     ReadConfigFile(mapArgs, mapMultiArgs);
159 #endif
160
161     if (mapArgs.count("-?") || mapArgs.count("--help"))
162     {
163         string strUsage = string() +
164           _("Bitcoin version") + " " + FormatFullVersion() + "\n\n" +
165           _("Usage:") + "\t\t\t\t\t\t\t\t\t\t\n" +
166             "  bitcoind [options]                   \t  " + "\n" +
167             "  bitcoind [options] <command> [params]\t  " + _("Send command to -server or bitcoind") + "\n" +
168             "  bitcoind [options] help              \t\t  " + _("List commands") + "\n" +
169             "  bitcoind [options] help <command>    \t\t  " + _("Get help for a command") + "\n" +
170           _("Options:") + "\n" +
171             "  -conf=<file>     \t\t  " + _("Specify configuration file (default: bitcoin.conf)") + "\n" +
172             "  -pid=<file>      \t\t  " + _("Specify pid file (default: bitcoind.pid)") + "\n" +
173             "  -gen             \t\t  " + _("Generate coins") + "\n" +
174             "  -gen=0           \t\t  " + _("Don't generate coins") + "\n" +
175             "  -min             \t\t  " + _("Start minimized") + "\n" +
176             "  -splash          \t\t  " + _("Show splash screen on startup (default: 1)") + "\n" +
177             "  -datadir=<dir>   \t\t  " + _("Specify data directory") + "\n" +
178             "  -dbcache=<n>     \t\t  " + _("Set database cache size in megabytes (default: 25)") + "\n" +
179             "  -dblogsize=<n>   \t\t  " + _("Set database disk log size in megabytes (default: 100)") + "\n" +
180             "  -timeout=<n>     \t  "   + _("Specify connection timeout (in milliseconds)") + "\n" +
181             "  -proxy=<ip:port> \t  "   + _("Connect through socks4 proxy") + "\n" +
182             "  -dns             \t  "   + _("Allow DNS lookups for addnode and connect") + "\n" +
183             "  -port=<port>     \t\t  " + _("Listen for connections on <port> (default: 8333 or testnet: 18333)") + "\n" +
184             "  -maxconnections=<n>\t  " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" +
185             "  -addnode=<ip>    \t  "   + _("Add a node to connect to and attempt to keep the connection open") + "\n" +
186             "  -connect=<ip>    \t\t  " + _("Connect only to the specified node") + "\n" +
187             "  -irc             \t  "   + _("Find peers using internet relay chat (default: 0)") + "\n" +
188             "  -listen          \t  "   + _("Accept connections from outside (default: 1)") + "\n" +
189 #ifdef QT_GUI
190             "  -lang=<lang>     \t\t  " + _("Set language, for example \"de_DE\" (default: system locale)") + "\n" +
191 #endif
192             "  -dnsseed         \t  "   + _("Find peers using DNS lookup (default: 1)") + "\n" +
193             "  -banscore=<n>    \t  "   + _("Threshold for disconnecting misbehaving peers (default: 100)") + "\n" +
194             "  -bantime=<n>     \t  "   + _("Number of seconds to keep misbehaving peers from reconnecting (default: 86400)") + "\n" +
195             "  -maxreceivebuffer=<n>\t  " + _("Maximum per-connection receive buffer, <n>*1000 bytes (default: 10000)") + "\n" +
196             "  -maxsendbuffer=<n>\t  "   + _("Maximum per-connection send buffer, <n>*1000 bytes (default: 10000)") + "\n" +
197 #ifdef USE_UPNP
198 #if USE_UPNP
199             "  -upnp            \t  "   + _("Use Universal Plug and Play to map the listening port (default: 1)") + "\n" +
200 #else
201             "  -upnp            \t  "   + _("Use Universal Plug and Play to map the listening port (default: 0)") + "\n" +
202 #endif
203             "  -detachdb        \t  "   + _("Detach block and address databases. Increases shutdown time (default: 0)") + "\n" +
204 #endif
205             "  -paytxfee=<amt>  \t  "   + _("Fee per KB to add to transactions you send") + "\n" +
206 #ifdef QT_GUI
207             "  -server          \t\t  " + _("Accept command line and JSON-RPC commands") + "\n" +
208 #endif
209 #if !defined(WIN32) && !defined(QT_GUI)
210             "  -daemon          \t\t  " + _("Run in the background as a daemon and accept commands") + "\n" +
211 #endif
212             "  -testnet         \t\t  " + _("Use the test network") + "\n" +
213             "  -debug           \t\t  " + _("Output extra debugging information") + "\n" +
214             "  -logtimestamps   \t  "   + _("Prepend debug output with timestamp") + "\n" +
215             "  -printtoconsole  \t  "   + _("Send trace/debug info to console instead of debug.log file") + "\n" +
216 #ifdef WIN32
217             "  -printtodebugger \t  "   + _("Send trace/debug info to debugger") + "\n" +
218 #endif
219             "  -rpcuser=<user>  \t  "   + _("Username for JSON-RPC connections") + "\n" +
220             "  -rpcpassword=<pw>\t  "   + _("Password for JSON-RPC connections") + "\n" +
221             "  -rpcport=<port>  \t\t  " + _("Listen for JSON-RPC connections on <port> (default: 8332)") + "\n" +
222             "  -rpcallowip=<ip> \t\t  " + _("Allow JSON-RPC connections from specified IP address") + "\n" +
223             "  -rpcconnect=<ip> \t  "   + _("Send commands to node running on <ip> (default: 127.0.0.1)") + "\n" +
224             "  -blocknotify=<cmd> "     + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" +
225             "  -upgradewallet   \t  "   + _("Upgrade wallet to latest format") + "\n" +
226             "  -keypool=<n>     \t  "   + _("Set key pool size to <n> (default: 100)") + "\n" +
227             "  -rescan          \t  "   + _("Rescan the block chain for missing wallet transactions") + "\n" +
228             "  -checkblocks=<n> \t\t  " + _("How many blocks to check at startup (default: 2500, 0 = all)") + "\n" +
229             "  -checklevel=<n>  \t\t  " + _("How thorough the block verification is (0-6, default: 1)") + "\n";
230
231         strUsage += string() +
232             _("\nSSL options: (see the Bitcoin Wiki for SSL setup instructions)") + "\n" +
233             "  -rpcssl                                \t  " + _("Use OpenSSL (https) for JSON-RPC connections") + "\n" +
234             "  -rpcsslcertificatechainfile=<file.cert>\t  " + _("Server certificate file (default: server.cert)") + "\n" +
235             "  -rpcsslprivatekeyfile=<file.pem>       \t  " + _("Server private key (default: server.pem)") + "\n" +
236             "  -rpcsslciphers=<ciphers>               \t  " + _("Acceptable ciphers (default: TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH)") + "\n";
237
238         strUsage += string() +
239             "  -?               \t\t  " + _("This help message") + "\n";
240
241         // Remove tabs
242         strUsage.erase(std::remove(strUsage.begin(), strUsage.end(), '\t'), strUsage.end());
243 #if defined(QT_GUI) && defined(WIN32)
244         // On windows, show a message box, as there is no stderr
245         ThreadSafeMessageBox(strUsage, _("Usage"), wxOK | wxMODAL);
246 #else
247         fprintf(stderr, "%s", strUsage.c_str());
248 #endif
249         return false;
250     }
251
252     fTestNet = GetBoolArg("-testnet");
253     if (fTestNet)
254     {
255         SoftSetBoolArg("-irc", true);
256     }
257
258     fDebug = GetBoolArg("-debug");
259     fDetachDB = GetBoolArg("-detachdb", false);
260
261 #if !defined(WIN32) && !defined(QT_GUI)
262     fDaemon = GetBoolArg("-daemon");
263 #else
264     fDaemon = false;
265 #endif
266
267     if (fDaemon)
268         fServer = true;
269     else
270         fServer = GetBoolArg("-server");
271
272     /* force fServer when running without GUI */
273 #if !defined(QT_GUI)
274     fServer = true;
275 #endif
276     fPrintToConsole = GetBoolArg("-printtoconsole");
277     fPrintToDebugger = GetBoolArg("-printtodebugger");
278     fLogTimestamps = GetBoolArg("-logtimestamps");
279
280 #ifndef QT_GUI
281     for (int i = 1; i < argc; i++)
282         if (!IsSwitchChar(argv[i][0]) && !(strlen(argv[i]) > 7 && strncasecmp(argv[i], "bitcoin:", 8) == 0))
283             fCommandLine = true;
284
285     if (fCommandLine)
286     {
287         int ret = CommandLineRPC(argc, argv);
288         exit(ret);
289     }
290 #endif
291
292 #if !defined(WIN32) && !defined(QT_GUI)
293     if (fDaemon)
294     {
295         // Daemonize
296         pid_t pid = fork();
297         if (pid < 0)
298         {
299             fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
300             return false;
301         }
302         if (pid > 0)
303         {
304             CreatePidFile(GetPidFile(), pid);
305             return true;
306         }
307
308         pid_t sid = setsid();
309         if (sid < 0)
310             fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
311     }
312 #endif
313
314     if (!fDebug)
315         ShrinkDebugFile();
316     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
317     printf("Bitcoin version %s (%s)\n", FormatFullVersion().c_str(), CLIENT_DATE.c_str());
318     printf("Default data directory %s\n", GetDefaultDataDir().string().c_str());
319
320     if (GetBoolArg("-loadblockindextest"))
321     {
322         CTxDB txdb("r");
323         txdb.LoadBlockIndex();
324         PrintBlockTree();
325         return false;
326     }
327
328     // Make sure only a single bitcoin process is using the data directory.
329     boost::filesystem::path pathLockFile = GetDataDir() / ".lock";
330     FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
331     if (file) fclose(file);
332     static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
333     if (!lock.try_lock())
334     {
335         ThreadSafeMessageBox(strprintf(_("Cannot obtain a lock on data directory %s.  Bitcoin is probably already running."), GetDataDir().string().c_str()), _("Bitcoin"), wxOK|wxMODAL);
336         return false;
337     }
338
339     std::ostringstream strErrors;
340     //
341     // Load data files
342     //
343     if (fDaemon)
344         fprintf(stdout, "bitcoin server starting\n");
345     int64 nStart;
346
347     InitMessage(_("Loading addresses..."));
348     printf("Loading addresses...\n");
349     nStart = GetTimeMillis();
350     if (!LoadAddresses())
351         strErrors << _("Error loading addr.dat") << "\n";
352     printf(" addresses   %15"PRI64d"ms\n", GetTimeMillis() - nStart);
353
354     InitMessage(_("Loading block index..."));
355     printf("Loading block index...\n");
356     nStart = GetTimeMillis();
357     if (!LoadBlockIndex())
358         strErrors << _("Error loading blkindex.dat") << "\n";
359
360     // as LoadBlockIndex can take several minutes, it's possible the user
361     // requested to kill bitcoin-qt during the last operation. If so, exit.
362     // As the program has not fully started yet, Shutdown() is possibly overkill.
363     if (fRequestShutdown)
364     {
365         printf("Shutdown requested. Exiting.\n");
366         return false;
367     }
368     printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
369
370     InitMessage(_("Loading wallet..."));
371     printf("Loading wallet...\n");
372     nStart = GetTimeMillis();
373     bool fFirstRun;
374     pwalletMain = new CWallet("wallet.dat");
375     int nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
376     if (nLoadWalletRet != DB_LOAD_OK)
377     {
378         if (nLoadWalletRet == DB_CORRUPT)
379             strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n";
380         else if (nLoadWalletRet == DB_TOO_NEW)
381             strErrors << _("Error loading wallet.dat: Wallet requires newer version of Bitcoin") << "\n";
382         else if (nLoadWalletRet == DB_NEED_REWRITE)
383         {
384             strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n";
385             printf("%s", strErrors.str().c_str());
386             ThreadSafeMessageBox(strErrors.str(), _("Bitcoin"), wxOK | wxICON_ERROR | wxMODAL);
387             return false;
388         }
389         else
390             strErrors << _("Error loading wallet.dat") << "\n";
391     }
392
393     if (GetBoolArg("-upgradewallet", fFirstRun))
394     {
395         int nMaxVersion = GetArg("-upgradewallet", 0);
396         if (nMaxVersion == 0) // the -walletupgrade without argument case
397         {
398             printf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
399             nMaxVersion = CLIENT_VERSION;
400             pwalletMain->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
401         }
402         else
403             printf("Allowing wallet upgrade up to %i\n", nMaxVersion);
404         if (nMaxVersion < pwalletMain->GetVersion())
405             strErrors << _("Cannot downgrade wallet") << "\n";
406         pwalletMain->SetMaxVersion(nMaxVersion);
407     }
408
409     if (fFirstRun)
410     {
411         // Create new keyUser and set as default key
412         RandAddSeedPerfmon();
413
414         std::vector<unsigned char> newDefaultKey;
415         if (!pwalletMain->GetKeyFromPool(newDefaultKey, false))
416             strErrors << _("Cannot initialize keypool") << "\n";
417         pwalletMain->SetDefaultKey(newDefaultKey);
418         if (!pwalletMain->SetAddressBookName(CBitcoinAddress(pwalletMain->vchDefaultKey), ""))
419             strErrors << _("Cannot write default address") << "\n";
420     }
421
422     printf("%s", strErrors.str().c_str());
423     printf(" wallet      %15"PRI64d"ms\n", GetTimeMillis() - nStart);
424
425     RegisterWallet(pwalletMain);
426
427     CBlockIndex *pindexRescan = pindexBest;
428     if (GetBoolArg("-rescan"))
429         pindexRescan = pindexGenesisBlock;
430     else
431     {
432         CWalletDB walletdb("wallet.dat");
433         CBlockLocator locator;
434         if (walletdb.ReadBestBlock(locator))
435             pindexRescan = locator.GetBlockIndex();
436     }
437     if (pindexBest != pindexRescan)
438     {
439         InitMessage(_("Rescanning..."));
440         printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
441         nStart = GetTimeMillis();
442         pwalletMain->ScanForWalletTransactions(pindexRescan, true);
443         printf(" rescan      %15"PRI64d"ms\n", GetTimeMillis() - nStart);
444     }
445
446     InitMessage(_("Done loading"));
447     printf("Done loading\n");
448
449     //// debug print
450     printf("mapBlockIndex.size() = %d\n",   mapBlockIndex.size());
451     printf("nBestHeight = %d\n",            nBestHeight);
452     printf("setKeyPool.size() = %d\n",      pwalletMain->setKeyPool.size());
453     printf("mapWallet.size() = %d\n",       pwalletMain->mapWallet.size());
454     printf("mapAddressBook.size() = %d\n",  pwalletMain->mapAddressBook.size());
455
456     if (!strErrors.str().empty())
457     {
458         ThreadSafeMessageBox(strErrors.str(), _("Bitcoin"), wxOK | wxICON_ERROR | wxMODAL);
459         return false;
460     }
461
462     // Add wallet transactions that aren't already in a block to mapTransactions
463     pwalletMain->ReacceptWalletTransactions();
464
465     // Note: Bitcoin-QT stores several settings in the wallet, so we want
466     // to load the wallet BEFORE parsing command-line arguments, so
467     // the command-line/bitcoin.conf settings override GUI setting.
468
469     //
470     // Parameters
471     //
472     if (GetBoolArg("-printblockindex") || GetBoolArg("-printblocktree"))
473     {
474         PrintBlockTree();
475         return false;
476     }
477
478     if (mapArgs.count("-timeout"))
479     {
480         int nNewTimeout = GetArg("-timeout", 5000);
481         if (nNewTimeout > 0 && nNewTimeout < 600000)
482             nConnectTimeout = nNewTimeout;
483     }
484
485     if (mapArgs.count("-printblock"))
486     {
487         string strMatch = mapArgs["-printblock"];
488         int nFound = 0;
489         for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
490         {
491             uint256 hash = (*mi).first;
492             if (strncmp(hash.ToString().c_str(), strMatch.c_str(), strMatch.size()) == 0)
493             {
494                 CBlockIndex* pindex = (*mi).second;
495                 CBlock block;
496                 block.ReadFromDisk(pindex);
497                 block.BuildMerkleTree();
498                 block.print();
499                 printf("\n");
500                 nFound++;
501             }
502         }
503         if (nFound == 0)
504             printf("No blocks matching %s were found\n", strMatch.c_str());
505         return false;
506     }
507
508     if (mapArgs.count("-proxy"))
509     {
510         fUseProxy = true;
511         addrProxy = CService(mapArgs["-proxy"], 9050);
512         if (!addrProxy.IsValid())
513         {
514             ThreadSafeMessageBox(_("Invalid -proxy address"), _("Bitcoin"), wxOK | wxMODAL);
515             return false;
516         }
517     }
518
519     bool fTor = (fUseProxy && addrProxy.GetPort() == 9050);
520     if (fTor)
521     {
522         // Use SoftSetBoolArg here so user can override any of these if they wish.
523         // Note: the GetBoolArg() calls for all of these must happen later.
524         SoftSetBoolArg("-listen", false);
525         SoftSetBoolArg("-irc", false);
526         SoftSetBoolArg("-dnsseed", false);
527         SoftSetBoolArg("-upnp", false);
528         SoftSetBoolArg("-dns", false);
529     }
530
531     fAllowDNS = GetBoolArg("-dns");
532     fNoListen = !GetBoolArg("-listen", true);
533
534     // Continue to put "/P2SH/" in the coinbase to monitor
535     // BIP16 support.
536     // This can be removed eventually...
537     const char* pszP2SH = "/P2SH/";
538     COINBASE_FLAGS << std::vector<unsigned char>(pszP2SH, pszP2SH+strlen(pszP2SH));
539
540     if (!fNoListen)
541     {
542         std::string strError;
543         if (!BindListenPort(strError))
544         {
545             ThreadSafeMessageBox(strError, _("Bitcoin"), wxOK | wxMODAL);
546             return false;
547         }
548     }
549
550     if (mapArgs.count("-addnode"))
551     {
552         BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
553         {
554             CAddress addr(CService(strAddr, GetDefaultPort(), fAllowDNS));
555             addr.nTime = 0; // so it won't relay unless successfully connected
556             if (addr.IsValid())
557                 addrman.Add(addr, CNetAddr("127.0.0.1"));
558         }
559     }
560
561     if (mapArgs.count("-paytxfee"))
562     {
563         if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
564         {
565             ThreadSafeMessageBox(_("Invalid amount for -paytxfee=<amount>"), _("Bitcoin"), wxOK | wxMODAL);
566             return false;
567         }
568         if (nTransactionFee > 0.25 * COIN)
569             ThreadSafeMessageBox(_("Warning: -paytxfee is set very high.  This is the transaction fee you will pay if you send a transaction."), _("Bitcoin"), wxOK | wxICON_EXCLAMATION | wxMODAL);
570     }
571
572     //
573     // Start the node
574     //
575     if (!CheckDiskSpace())
576         return false;
577
578     RandAddSeedPerfmon();
579
580     if (!CreateThread(StartNode, NULL))
581         ThreadSafeMessageBox(_("Error: CreateThread(StartNode) failed"), _("Bitcoin"), wxOK | wxMODAL);
582
583     if (fServer)
584         CreateThread(ThreadRPCServer, NULL);
585
586 #ifdef QT_GUI
587     if (GetStartOnSystemStartup())
588         SetStartOnSystemStartup(true); // Remove startup links
589 #endif
590
591 #if !defined(QT_GUI)
592     while (1)
593         Sleep(5000);
594 #endif
595
596     return true;
597 }
598