1 // Copyright (c) 2009-2010 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
14 //////////////////////////////////////////////////////////////////////////////
\r
19 void ExitTimeout(void* parg)
\r
27 void Shutdown(void* parg)
\r
29 static CCriticalSection cs_Shutdown;
\r
32 CRITICAL_BLOCK(cs_Shutdown)
\r
34 fFirstThread = !fTaken;
\r
41 nTransactionsUpdated++;
\r
45 CreateThread(ExitTimeout, NULL);
\r
47 printf("Bitcoin exiting\n\n");
\r
65 //////////////////////////////////////////////////////////////////////////////
\r
71 int main(int argc, char* argv[])
\r
73 for (int i = 1; i < argc; i++)
\r
74 if (!IsSwitchChar(argv[i][0]))
\r
75 fCommandLine = true;
\r
76 fDaemon = !fCommandLine;
\r
85 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
\r
89 pthread_exit((void*)0);
\r
93 if (!AppInit(argc, argv))
\r
102 bool AppInit(int argc, char* argv[])
\r
107 fRet = AppInit2(argc, argv);
\r
109 catch (std::exception& e) {
\r
110 PrintException(&e, "AppInit()");
\r
112 PrintException(NULL, "AppInit()");
\r
119 bool AppInit2(int argc, char* argv[])
\r
122 // Turn off microsoft heap dump noise
\r
123 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
\r
124 _CrtSetReportFile(_CRT_WARN, CreateFileA("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0));
\r
126 #if _MSC_VER >= 1400
\r
127 // Disable confusing "helpful" text message on abort, ctrl-c
\r
128 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
\r
137 ParseParameters(argc, argv);
\r
139 if (mapArgs.count("-datadir"))
\r
141 filesystem::path pathDataDir = filesystem::system_complete(mapArgs["-datadir"]);
\r
142 strlcpy(pszSetDataDir, pathDataDir.string().c_str(), sizeof(pszSetDataDir));
\r
145 ReadConfigFile(mapArgs, mapMultiArgs); // Must be done after processing datadir
\r
147 if (mapArgs.count("-?") || mapArgs.count("--help"))
\r
149 string strUsage = string() +
\r
150 _("Usage:") + "\t\t\t\t\t\t\t\t\t\t\n" +
\r
151 " bitcoin [options] \t " + "\n" +
\r
152 " bitcoin [options] <command> [params]\t " + _("Send command to -server or bitcoind\n") +
\r
153 " bitcoin [options] help \t\t " + _("List commands\n") +
\r
154 " bitcoin [options] help <command> \t\t " + _("Get help for a command\n") +
\r
156 " -conf=<file> \t " + _("Specify configuration file (default: bitcoin.conf)\n") +
\r
157 " -gen \t " + _("Generate coins\n") +
\r
158 " -gen=0 \t " + _("Don't generate coins\n") +
\r
159 " -min \t " + _("Start minimized\n") +
\r
160 " -datadir=<dir> \t " + _("Specify data directory\n") +
\r
161 " -proxy=<ip:port>\t " + _("Connect through socks4 proxy\n") +
\r
162 " -addnode=<ip> \t " + _("Add a node to connect to\n") +
\r
163 " -connect=<ip> \t " + _("Connect only to the specified node\n") +
\r
164 " -server \t " + _("Accept command line and JSON-RPC commands\n") +
\r
165 " -daemon \t " + _("Run in the background as a daemon and accept commands\n") +
\r
166 " -? \t " + _("This help message\n");
\r
168 #if defined(__WXMSW__) && defined(GUI)
\r
169 // Tabs make the columns line up in the message box
\r
170 wxMessageBox(strUsage, "Bitcoin", wxOK);
\r
173 strUsage.erase(std::remove(strUsage.begin(), strUsage.end(), '\t'), strUsage.end());
\r
174 fprintf(stderr, "%s", strUsage.c_str());
\r
179 if (mapArgs.count("-debug"))
\r
182 if (mapArgs.count("-printtodebugger"))
\r
183 fPrintToDebugger = true;
\r
187 int ret = CommandLineRPC(argc, argv);
\r
191 if (!fDebug && !pszSetDataDir[0])
\r
193 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
\r
194 printf("Bitcoin version %d.%d.%d%s beta\n", VERSION/10000, (VERSION/100)%100, VERSION%100, pszSubVer);
\r
196 printf("OS version %s\n", ((string)wxGetOsDescription()).c_str());
\r
197 printf("System default language is %d %s\n", g_locale.GetSystemLanguage(), ((string)g_locale.GetSysName()).c_str());
\r
198 printf("Language file %s (%s)\n", (string("locale/") + (string)g_locale.GetCanonicalName() + "/LC_MESSAGES/bitcoin.mo").c_str(), ((string)g_locale.GetLocale()).c_str());
\r
200 printf("Default data directory %s\n", GetDefaultDataDir().c_str());
\r
202 if (mapArgs.count("-loadblockindextest"))
\r
205 txdb.LoadBlockIndex();
\r
211 // Limit to single instance per user
\r
212 // Required to protect the database files if we're going to keep deleting log.*
\r
214 #if defined(__WXMSW__) && defined(GUI)
\r
215 // todo: wxSingleInstanceChecker wasn't working on Linux, never deleted its lock file
\r
216 // maybe should go by whether successfully bind port 8333 instead
\r
217 wxString strMutexName = wxString("bitcoin_running.") + getenv("HOMEPATH");
\r
218 for (int i = 0; i < strMutexName.size(); i++)
\r
219 if (!isalnum(strMutexName[i]))
\r
220 strMutexName[i] = '.';
\r
221 wxSingleInstanceChecker* psingleinstancechecker = new wxSingleInstanceChecker(strMutexName);
\r
222 if (psingleinstancechecker->IsAnotherRunning())
\r
224 printf("Existing instance found\n");
\r
225 unsigned int nStart = GetTime();
\r
228 // TODO: find out how to do this in Linux, or replace with wxWidgets commands
\r
229 // Show the previous instance and exit
\r
230 HWND hwndPrev = FindWindowA("wxWindowClassNR", "Bitcoin");
\r
233 if (IsIconic(hwndPrev))
\r
234 ShowWindow(hwndPrev, SW_RESTORE);
\r
235 SetForegroundWindow(hwndPrev);
\r
239 if (GetTime() > nStart + 60)
\r
242 // Resume this instance if the other exits
\r
243 delete psingleinstancechecker;
\r
245 psingleinstancechecker = new wxSingleInstanceChecker(strMutexName);
\r
246 if (!psingleinstancechecker->IsAnotherRunning())
\r
252 // Bind to the port early so we can tell if another instance is already running.
\r
253 // This is a backup to wxSingleInstanceChecker, which doesn't work on Linux.
\r
255 if (!BindListenPort(strErrors))
\r
257 wxMessageBox(strErrors, "Bitcoin");
\r
265 fprintf(stdout, "bitcoin server starting\n");
\r
269 printf("Loading addresses...\n");
\r
270 nStart = GetTimeMillis();
\r
271 if (!LoadAddresses())
\r
272 strErrors += _("Error loading addr.dat \n");
\r
273 printf(" addresses %15"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
275 printf("Loading block index...\n");
\r
276 nStart = GetTimeMillis();
\r
277 if (!LoadBlockIndex())
\r
278 strErrors += _("Error loading blkindex.dat \n");
\r
279 printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
281 printf("Loading wallet...\n");
\r
282 nStart = GetTimeMillis();
\r
284 if (!LoadWallet(fFirstRun))
\r
285 strErrors += _("Error loading wallet.dat \n");
\r
286 printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);
\r
288 printf("Done loading\n");
\r
291 printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size());
\r
292 printf("nBestHeight = %d\n", nBestHeight);
\r
293 printf("mapKeys.size() = %d\n", mapKeys.size());
\r
294 printf("mapPubKeys.size() = %d\n", mapPubKeys.size());
\r
295 printf("mapWallet.size() = %d\n", mapWallet.size());
\r
296 printf("mapAddressBook.size() = %d\n", mapAddressBook.size());
\r
298 if (!strErrors.empty())
\r
300 wxMessageBox(strErrors, "Bitcoin");
\r
304 // Add wallet transactions that aren't already in a block to mapTransactions
\r
305 ReacceptWalletTransactions();
\r
310 if (mapArgs.count("-printblockindex") || mapArgs.count("-printblocktree"))
\r
316 if (mapArgs.count("-printblock"))
\r
318 string strMatch = mapArgs["-printblock"];
\r
320 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
\r
322 uint256 hash = (*mi).first;
\r
323 if (strncmp(hash.ToString().c_str(), strMatch.c_str(), strMatch.size()) == 0)
\r
325 CBlockIndex* pindex = (*mi).second;
\r
327 block.ReadFromDisk(pindex);
\r
328 block.BuildMerkleTree();
\r
335 printf("No blocks matching %s were found\n", strMatch.c_str());
\r
339 if (mapArgs.count("-gen"))
\r
341 if (mapArgs["-gen"].empty())
\r
342 fGenerateBitcoins = true;
\r
344 fGenerateBitcoins = (atoi(mapArgs["-gen"].c_str()) != 0);
\r
347 if (mapArgs.count("-proxy"))
\r
350 addrProxy = CAddress(mapArgs["-proxy"]);
\r
351 if (!addrProxy.IsValid())
\r
353 wxMessageBox(_("Invalid -proxy address"), "Bitcoin");
\r
358 if (mapArgs.count("-addnode"))
\r
360 foreach(string strAddr, mapMultiArgs["-addnode"])
\r
362 CAddress addr(strAddr, NODE_NETWORK);
\r
363 addr.nTime = 0; // so it won't relay unless successfully connected
\r
364 if (addr.IsValid())
\r
369 if (mapArgs.count("-paytxfee"))
\r
371 if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
\r
373 wxMessageBox(_("Invalid amount for -paytxfee=<amount>"), "Bitcoin");
\r
376 if (nTransactionFee > 1 * COIN)
\r
377 wxMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), "Bitcoin");
\r
381 // Create the main window and start the node
\r
385 CreateMainWindow();
\r
388 if (!CheckDiskSpace())
\r
391 RandAddSeedPerfmon();
\r
393 if (!CreateThread(StartNode, NULL))
\r
394 wxMessageBox("Error: CreateThread(StartNode) failed", "Bitcoin");
\r
396 if (mapArgs.count("-server") || fDaemon)
\r
397 CreateThread(ThreadRPCServer, NULL);
\r
399 #if defined(__WXMSW__) && defined(GUI)
\r
401 SetStartOnSystemStartup(true);
\r