genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #include "headers.h"
genesis 6 #include "db.h"
genesis 7 #include "bitcoinrpc.h"
genesis 8 #include "net.h"
genesis 9 #include "init.h"
genesis 10 #include "strlcpy.h"
genesis 11 #include <boost/filesystem.hpp>
genesis 12 #include <boost/filesystem/fstream.hpp>
genesis 13 #include <boost/interprocess/sync/file_lock.hpp>
genesis 14
genesis 15
genesis 16 using namespace std;
genesis 17 using namespace boost;
genesis 18
genesis 19 CWallet* pwalletMain;
genesis 20
genesis 21
genesis 22
genesis 23
genesis 24
genesis 25
genesis 26 void ExitTimeout(void* parg)
genesis 27 {
genesis 28 #ifdef WIN32
genesis 29 Sleep(5000);
genesis 30 ExitProcess(0);
genesis 31 #endif
genesis 32 }
genesis 33
genesis 34 void Shutdown(void* parg)
genesis 35 {
genesis 36 static CCriticalSection cs_Shutdown;
genesis 37 static bool fTaken;
genesis 38 bool fFirstThread = false;
genesis 39 TRY_CRITICAL_BLOCK(cs_Shutdown)
genesis 40 {
genesis 41 fFirstThread = !fTaken;
genesis 42 fTaken = true;
genesis 43 }
genesis 44 static bool fExit;
genesis 45 if (fFirstThread)
genesis 46 {
genesis 47 fShutdown = true;
genesis 48 nTransactionsUpdated++;
genesis 49 DBFlush(false);
genesis 50 StopNode();
genesis 51 DBFlush(true);
genesis 52 boost::filesystem::remove(GetPidFile());
genesis 53 UnregisterWallet(pwalletMain);
genesis 54 delete pwalletMain;
genesis 55 CreateThread(ExitTimeout, NULL);
genesis 56 Sleep(50);
genesis 57 printf("Bitcoin exiting\n\n");
genesis 58 fExit = true;
genesis 59 exit(0);
genesis 60 }
genesis 61 else
genesis 62 {
genesis 63 while (!fExit)
genesis 64 Sleep(500);
genesis 65 Sleep(100);
genesis 66 ExitThread(0);
genesis 67 }
genesis 68 }
genesis 69
genesis 70 void HandleSIGTERM(int)
genesis 71 {
genesis 72 fRequestShutdown = true;
genesis 73 }
genesis 74
genesis 75
genesis 76
genesis 77
genesis 78
genesis 79
genesis 80
genesis 81
genesis 82
genesis 83
bitcoin-asciilife... 84
genesis 85 int main(int argc, char* argv[])
genesis 86 {
genesis 87 bool fRet = false;
genesis 88 fRet = AppInit(argc, argv);
genesis 89
genesis 90 if (fRet && fDaemon)
genesis 91 return 0;
genesis 92
genesis 93 return 1;
genesis 94 }
bitcoin-asciilife... 95
genesis 96
genesis 97 bool AppInit(int argc, char* argv[])
genesis 98 {
genesis 99 bool fRet = false;
genesis 100 try
genesis 101 {
genesis 102 fRet = AppInit2(argc, argv);
genesis 103 }
genesis 104 catch (std::exception& e) {
genesis 105 PrintException(&e, "AppInit()");
genesis 106 } catch (...) {
genesis 107 PrintException(NULL, "AppInit()");
genesis 108 }
genesis 109 if (!fRet)
genesis 110 Shutdown(NULL);
genesis 111 return fRet;
genesis 112 }
genesis 113
genesis 114 bool AppInit2(int argc, char* argv[])
genesis 115 {
genesis 116 #ifdef _MSC_VER
genesis 117
genesis 118 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
genesis 119 _CrtSetReportFile(_CRT_WARN, CreateFileA("NUL", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0));
genesis 120 #endif
genesis 121 #if _MSC_VER >= 1400
genesis 122
genesis 123 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
genesis 124 #endif
genesis 125 #ifndef WIN32
genesis 126 umask(077);
genesis 127 #endif
genesis 128 #ifndef WIN32
genesis 129
genesis 130 struct sigaction sa;
genesis 131 sa.sa_handler = HandleSIGTERM;
genesis 132 sigemptyset(&sa.sa_mask);
genesis 133 sa.sa_flags = 0;
genesis 134 sigaction(SIGTERM, &sa, NULL);
genesis 135 sigaction(SIGINT, &sa, NULL);
genesis 136 sigaction(SIGHUP, &sa, NULL);
genesis 137 #endif
genesis 138
genesis 139
genesis 140
genesis 141
genesis 142 ParseParameters(argc, argv);
genesis 143
genesis 144 if (mapArgs.count("-datadir"))
genesis 145 {
genesis 146 if (filesystem::is_directory(filesystem::system_complete(mapArgs["-datadir"])))
genesis 147 {
genesis 148 filesystem::path pathDataDir = filesystem::system_complete(mapArgs["-datadir"]);
genesis 149 strlcpy(pszSetDataDir, pathDataDir.string().c_str(), sizeof(pszSetDataDir));
genesis 150 }
genesis 151 else
genesis 152 {
genesis 153 fprintf(stderr, "Error: Specified directory does not exist\n");
genesis 154 Shutdown(NULL);
genesis 155 }
genesis 156 }
genesis 157
genesis 158
genesis 159 ReadConfigFile(mapArgs, mapMultiArgs);
genesis 160
genesis 161 if (mapArgs.count("-?") || mapArgs.count("--help"))
genesis 162 {
genesis 163 string strUsage = string() +
genesis 164 _("Bitcoin version") + " " + FormatFullVersion() + "\n\n" +
genesis 165 _("Usage:") + "\t\t\t\t\t\t\t\t\t\t\n" +
genesis 166 " bitcoind [options] \t " + "\n" +
genesis 167 " bitcoind [options] <command> [params]\t " + _("Send command to -server or bitcoind\n") +
genesis 168 " bitcoind [options] help \t\t " + _("List commands\n") +
genesis 169 " bitcoind [options] help <command> \t\t " + _("Get help for a command\n") +
genesis 170 _("Options:\n") +
genesis 171 " -conf=<file> \t\t " + _("Specify configuration file (default: bitcoin.conf)\n") +
genesis 172 " -pid=<file> \t\t " + _("Specify pid file (default: bitcoind.pid)\n") +
genesis 173 " -gen \t\t " + _("Generate coins\n") +
genesis 174 " -gen=0 \t\t " + _("Don't generate coins\n") +
genesis 175 " -min \t\t " + _("Start minimized\n") +
genesis 176 " -datadir=<dir> \t\t " + _("Specify data directory\n") +
genesis 177 " -timeout=<n> \t " + _("Specify connection timeout (in milliseconds)\n") +
genesis 178 " -proxy=<ip:port> \t " + _("Connect through socks4 proxy\n") +
genesis 179 " -dns \t " + _("Allow DNS lookups for addnode and connect\n") +
genesis 180 " -port=<port> \t\t " + _("Listen for connections on <port> (default: 8333 or testnet: 18333)\n") +
genesis 181 " -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)\n") +
genesis 182 " -addnode=<ip> \t " + _("Add a node to connect to\n") +
genesis 183 " -connect=<ip> \t\t " + _("Connect only to the specified node\n") +
genesis 184 " -noirc \t " + _("Don't find peers using internet relay chat\n") +
genesis 185 " -nolisten \t " + _("Don't accept connections from outside\n") +
genesis 186 " -nodnsseed \t " + _("Don't bootstrap list of peers using DNS\n") +
genesis 187 " -banscore=<n> \t " + _("Threshold for disconnecting misbehaving peers (default: 100)\n") +
genesis 188 " -bantime=<n> \t " + _("Number of seconds to keep misbehaving peers from reconnecting (default: 86400)\n") +
genesis 189 " -maxreceivebuffer=<n>\t " + _("Maximum per-connection receive buffer, <n>*1000 bytes (default: 10000)\n") +
genesis 190 " -maxsendbuffer=<n>\t " + _("Maximum per-connection send buffer, <n>*1000 bytes (default: 10000)\n") +
genesis 191 " -paytxfee=<amt> \t " + _("Fee per kB to add to transactions you send\n") +
bitcoin-asciilife... 192 #if !defined(WIN32)
genesis 193 " -daemon \t\t " + _("Run in the background as a daemon and accept commands\n") +
genesis 194 #endif
genesis 195 " -testnet \t\t " + _("Use the test network\n") +
genesis 196 " -debug \t\t " + _("Output extra debugging information\n") +
genesis 197 " -logtimestamps \t " + _("Prepend debug output with timestamp\n") +
genesis 198 " -printtoconsole \t " + _("Send trace/debug info to console instead of debug.log file\n") +
genesis 199 #ifdef WIN32
genesis 200 " -printtodebugger \t " + _("Send trace/debug info to debugger\n") +
genesis 201 #endif
genesis 202 " -rpcuser=<user> \t " + _("Username for JSON-RPC connections\n") +
genesis 203 " -rpcpassword=<pw>\t " + _("Password for JSON-RPC connections\n") +
genesis 204 " -rpcport=<port> \t\t " + _("Listen for JSON-RPC connections on <port> (default: 8332)\n") +
genesis 205 " -rpcallowip=<ip> \t\t " + _("Allow JSON-RPC connections from specified IP address\n") +
genesis 206 " -rpcconnect=<ip> \t " + _("Send commands to node running on <ip> (default: 127.0.0.1)\n") +
genesis 207 " -keypool=<n> \t " + _("Set key pool size to <n> (default: 100)\n") +
genesis 208 " -rescan \t " + _("Rescan the block chain for missing wallet transactions\n");
genesis 209
genesis 210 strUsage += string() +
genesis 211 " -? \t\t " + _("This help message\n");
genesis 212
genesis 213
genesis 214 strUsage.erase(std::remove(strUsage.begin(), strUsage.end(), '\t'), strUsage.end());
genesis 215 fprintf(stderr, "%s", strUsage.c_str());
genesis 216 return false;
genesis 217 }
genesis 218
genesis 219 fTestNet = GetBoolArg("-testnet");
genesis 220 fDebug = GetBoolArg("-debug");
genesis 221
bitcoin-asciilife... 222 #if !defined(WIN32)
genesis 223 fDaemon = GetBoolArg("-daemon");
genesis 224 #else
genesis 225 fDaemon = false;
genesis 226 #endif
genesis 227
genesis 228 if (fDaemon)
genesis 229 fServer = true;
genesis 230 else
genesis 231 fServer = GetBoolArg("-server");
genesis 232
genesis 233
genesis 234 fServer = true;
genesis 235 fPrintToConsole = GetBoolArg("-printtoconsole");
genesis 236 fPrintToDebugger = GetBoolArg("-printtodebugger");
genesis 237 fLogTimestamps = GetBoolArg("-logtimestamps");
genesis 238
genesis 239 for (int i = 1; i < argc; i++)
genesis 240 if (!IsSwitchChar(argv[i][0]))
genesis 241 fCommandLine = true;
genesis 242
genesis 243 if (fCommandLine)
genesis 244 {
genesis 245 int ret = CommandLineRPC(argc, argv);
genesis 246 exit(ret);
genesis 247 }
genesis 248
bitcoin-asciilife... 249 #if !defined(WIN32)
genesis 250 if (fDaemon)
genesis 251 {
genesis 252
genesis 253 pid_t pid = fork();
genesis 254 if (pid < 0)
genesis 255 {
genesis 256 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
genesis 257 return false;
genesis 258 }
genesis 259 if (pid > 0)
genesis 260 {
genesis 261 CreatePidFile(GetPidFile(), pid);
genesis 262 return true;
genesis 263 }
genesis 264
genesis 265 pid_t sid = setsid();
genesis 266 if (sid < 0)
genesis 267 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
genesis 268 }
genesis 269 #endif
genesis 270
genesis 271 if (!fDebug && !pszSetDataDir[0])
genesis 272 ShrinkDebugFile();
genesis 273 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
genesis 274 printf("Bitcoin version %s\n", FormatFullVersion().c_str());
genesis 275 printf("Default data directory %s\n", GetDefaultDataDir().c_str());
genesis 276
genesis 277 if (GetBoolArg("-loadblockindextest"))
genesis 278 {
genesis 279 CTxDB txdb("r");
genesis 280 txdb.LoadBlockIndex();
genesis 281 PrintBlockTree();
genesis 282 return false;
genesis 283 }
genesis 284
genesis 285
genesis 286 string strLockFile = GetDataDir() + "/.lock";
genesis 287 FILE* file = fopen(strLockFile.c_str(), "a");
genesis 288 if (file) fclose(file);
genesis 289 static boost::interprocess::file_lock lock(strLockFile.c_str());
genesis 290 if (!lock.try_lock())
genesis 291 {
genesis 292 wxMessageBox(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), GetDataDir().c_str()), "Bitcoin");
genesis 293 return false;
genesis 294 }
genesis 295
genesis 296 string strErrors;
genesis 297
genesis 298
genesis 299
genesis 300
genesis 301 if (fDaemon)
genesis 302 fprintf(stdout, "bitcoin server starting\n");
genesis 303 strErrors = "";
genesis 304 int64 nStart;
genesis 305
genesis 306 InitMessage(_("Loading addresses..."));
genesis 307 printf("Loading addresses...\n");
genesis 308 nStart = GetTimeMillis();
genesis 309 if (!LoadAddresses())
genesis 310 strErrors += _("Error loading addr.dat \n");
genesis 311 printf(" addresses %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 312
genesis 313 InitMessage(_("Loading block index..."));
genesis 314 printf("Loading block index...\n");
genesis 315 nStart = GetTimeMillis();
genesis 316 if (!LoadBlockIndex())
genesis 317 strErrors += _("Error loading blkindex.dat \n");
genesis 318 printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 319
genesis 320 InitMessage(_("Loading wallet..."));
genesis 321 printf("Loading wallet...\n");
genesis 322 nStart = GetTimeMillis();
genesis 323 bool fFirstRun;
genesis 324 pwalletMain = new CWallet("wallet.dat");
genesis 325 int nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
genesis 326 if (nLoadWalletRet != DB_LOAD_OK)
genesis 327 {
genesis 328 if (nLoadWalletRet == DB_CORRUPT)
genesis 329 strErrors += _("Error loading wallet.dat: Wallet corrupted \n");
genesis 330 else if (nLoadWalletRet == DB_TOO_NEW)
genesis 331 strErrors += _("Error loading wallet.dat: Wallet requires newer version of Bitcoin \n");
genesis 332 else if (nLoadWalletRet == DB_NEED_REWRITE)
genesis 333 {
genesis 334 strErrors += _("Wallet needed to be rewritten: restart Bitcoin to complete \n");
genesis 335 wxMessageBox(strErrors, "Bitcoin", wxOK | wxICON_ERROR);
genesis 336 return false;
genesis 337 }
genesis 338 else
genesis 339 strErrors += _("Error loading wallet.dat \n");
genesis 340 }
genesis 341 printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 342
genesis 343 RegisterWallet(pwalletMain);
genesis 344
genesis 345 CBlockIndex *pindexRescan = pindexBest;
genesis 346 if (GetBoolArg("-rescan"))
genesis 347 pindexRescan = pindexGenesisBlock;
genesis 348 else
genesis 349 {
genesis 350 CWalletDB walletdb("wallet.dat");
genesis 351 CBlockLocator locator;
genesis 352 if (walletdb.ReadBestBlock(locator))
genesis 353 pindexRescan = locator.GetBlockIndex();
genesis 354 }
genesis 355 if (pindexBest != pindexRescan)
genesis 356 {
genesis 357 InitMessage(_("Rescanning..."));
genesis 358 printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
genesis 359 nStart = GetTimeMillis();
genesis 360 pwalletMain->ScanForWalletTransactions(pindexRescan, true);
genesis 361 printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 362 }
genesis 363
genesis 364 InitMessage(_("Done loading"));
genesis 365 printf("Done loading\n");
genesis 366
genesis 367
genesis 368 printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size());
genesis 369 printf("nBestHeight = %d\n", nBestHeight);
genesis 370 printf("setKeyPool.size() = %d\n", pwalletMain->setKeyPool.size());
genesis 371 printf("mapWallet.size() = %d\n", pwalletMain->mapWallet.size());
genesis 372 printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size());
genesis 373
genesis 374 if (!strErrors.empty())
genesis 375 {
genesis 376 wxMessageBox(strErrors, "Bitcoin", wxOK | wxICON_ERROR);
genesis 377 return false;
genesis 378 }
genesis 379
genesis 380
genesis 381 pwalletMain->ReacceptWalletTransactions();
genesis 382
genesis 383
genesis 384
genesis 385
genesis 386
genesis 387
genesis 388
genesis 389
genesis 390 if (GetBoolArg("-printblockindex") || GetBoolArg("-printblocktree"))
genesis 391 {
genesis 392 PrintBlockTree();
genesis 393 return false;
genesis 394 }
genesis 395
genesis 396 if (mapArgs.count("-timeout"))
genesis 397 {
genesis 398 int nNewTimeout = GetArg("-timeout", 5000);
genesis 399 if (nNewTimeout > 0 && nNewTimeout < 600000)
genesis 400 nConnectTimeout = nNewTimeout;
genesis 401 }
genesis 402
genesis 403 if (mapArgs.count("-printblock"))
genesis 404 {
genesis 405 string strMatch = mapArgs["-printblock"];
genesis 406 int nFound = 0;
genesis 407 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
genesis 408 {
genesis 409 uint256 hash = (*mi).first;
genesis 410 if (strncmp(hash.ToString().c_str(), strMatch.c_str(), strMatch.size()) == 0)
genesis 411 {
genesis 412 CBlockIndex* pindex = (*mi).second;
genesis 413 CBlock block;
genesis 414 block.ReadFromDisk(pindex);
genesis 415 block.BuildMerkleTree();
genesis 416 block.print();
genesis 417 printf("\n");
genesis 418 nFound++;
genesis 419 }
genesis 420 }
genesis 421 if (nFound == 0)
genesis 422 printf("No blocks matching %s were found\n", strMatch.c_str());
genesis 423 return false;
genesis 424 }
genesis 425
genesis 426 fGenerateBitcoins = GetBoolArg("-gen");
genesis 427
genesis 428 if (mapArgs.count("-proxy"))
genesis 429 {
genesis 430 fUseProxy = true;
genesis 431 addrProxy = CAddress(mapArgs["-proxy"]);
genesis 432 if (!addrProxy.IsValid())
genesis 433 {
genesis 434 wxMessageBox(_("Invalid -proxy address"), "Bitcoin");
genesis 435 return false;
genesis 436 }
genesis 437 }
genesis 438
genesis 439 bool fTor = (fUseProxy && addrProxy.port == htons(9050));
genesis 440 if (fTor)
genesis 441 {
genesis 442
genesis 443
genesis 444 SoftSetArg("-nolisten", true);
genesis 445 SoftSetArg("-noirc", true);
genesis 446 SoftSetArg("-nodnsseed", true);
genesis 447 SoftSetArg("-dns", false);
genesis 448 }
genesis 449
genesis 450 fAllowDNS = GetBoolArg("-dns");
genesis 451 fNoListen = GetBoolArg("-nolisten");
genesis 452
genesis 453
genesis 454
genesis 455 if (!fNoListen)
genesis 456 {
genesis 457 if (!BindListenPort(strErrors))
genesis 458 {
genesis 459 wxMessageBox(strErrors, "Bitcoin");
genesis 460 return false;
genesis 461 }
genesis 462 }
genesis 463
genesis 464 if (mapArgs.count("-addnode"))
genesis 465 {
genesis 466 BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
genesis 467 {
genesis 468 CAddress addr(strAddr, fAllowDNS);
genesis 469 addr.nTime = 0;
genesis 470 if (addr.IsValid())
genesis 471 AddAddress(addr);
genesis 472 }
genesis 473 }
genesis 474
genesis 475 if (mapArgs.count("-paytxfee"))
genesis 476 {
genesis 477 if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
genesis 478 {
genesis 479 wxMessageBox(_("Invalid amount for -paytxfee=<amount>"), "Bitcoin");
genesis 480 return false;
genesis 481 }
genesis 482 if (nTransactionFee > 0.25 * COIN)
genesis 483 wxMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), "Bitcoin", wxOK | wxICON_EXCLAMATION);
genesis 484 }
genesis 485
genesis 486
genesis 487
genesis 488
genesis 489 if (!CheckDiskSpace())
genesis 490 return false;
genesis 491
genesis 492 RandAddSeedPerfmon();
genesis 493
genesis 494 if (!CreateThread(StartNode, NULL))
genesis 495 wxMessageBox(_("Error: CreateThread(StartNode) failed"), "Bitcoin");
genesis 496
genesis 497 if (fServer)
genesis 498 CreateThread(ThreadRPCServer, NULL);
genesis 499
genesis 500 while (1)
genesis 501 Sleep(5000);
genesis 502
genesis 503 return true;
genesis 504 }