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 #ifdef USE_SSL
genesis 211 strUsage += string() +
genesis 212 _("\nSSL options: (see the Bitcoin Wiki for SSL setup instructions)\n") +
genesis 213 " -rpcssl \t " + _("Use OpenSSL (https) for JSON-RPC connections\n") +
genesis 214 " -rpcsslcertificatechainfile=<file.cert>\t " + _("Server certificate file (default: server.cert)\n") +
genesis 215 " -rpcsslprivatekeyfile=<file.pem> \t " + _("Server private key (default: server.pem)\n") +
genesis 216 " -rpcsslciphers=<ciphers> \t " + _("Acceptable ciphers (default: TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH)\n");
genesis 217 #endif
genesis 218
genesis 219 strUsage += string() +
genesis 220 " -? \t\t " + _("This help message\n");
genesis 221
genesis 222
genesis 223 strUsage.erase(std::remove(strUsage.begin(), strUsage.end(), '\t'), strUsage.end());
genesis 224 fprintf(stderr, "%s", strUsage.c_str());
genesis 225 return false;
genesis 226 }
genesis 227
genesis 228 fTestNet = GetBoolArg("-testnet");
genesis 229 fDebug = GetBoolArg("-debug");
genesis 230
bitcoin-asciilife... 231 #if !defined(WIN32)
genesis 232 fDaemon = GetBoolArg("-daemon");
genesis 233 #else
genesis 234 fDaemon = false;
genesis 235 #endif
genesis 236
genesis 237 if (fDaemon)
genesis 238 fServer = true;
genesis 239 else
genesis 240 fServer = GetBoolArg("-server");
genesis 241
genesis 242
genesis 243 fServer = true;
genesis 244 fPrintToConsole = GetBoolArg("-printtoconsole");
genesis 245 fPrintToDebugger = GetBoolArg("-printtodebugger");
genesis 246 fLogTimestamps = GetBoolArg("-logtimestamps");
genesis 247
genesis 248 for (int i = 1; i < argc; i++)
genesis 249 if (!IsSwitchChar(argv[i][0]))
genesis 250 fCommandLine = true;
genesis 251
genesis 252 if (fCommandLine)
genesis 253 {
genesis 254 int ret = CommandLineRPC(argc, argv);
genesis 255 exit(ret);
genesis 256 }
genesis 257
bitcoin-asciilife... 258 #if !defined(WIN32)
genesis 259 if (fDaemon)
genesis 260 {
genesis 261
genesis 262 pid_t pid = fork();
genesis 263 if (pid < 0)
genesis 264 {
genesis 265 fprintf(stderr, "Error: fork() returned %d errno %d\n", pid, errno);
genesis 266 return false;
genesis 267 }
genesis 268 if (pid > 0)
genesis 269 {
genesis 270 CreatePidFile(GetPidFile(), pid);
genesis 271 return true;
genesis 272 }
genesis 273
genesis 274 pid_t sid = setsid();
genesis 275 if (sid < 0)
genesis 276 fprintf(stderr, "Error: setsid() returned %d errno %d\n", sid, errno);
genesis 277 }
genesis 278 #endif
genesis 279
genesis 280 if (!fDebug && !pszSetDataDir[0])
genesis 281 ShrinkDebugFile();
genesis 282 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
genesis 283 printf("Bitcoin version %s\n", FormatFullVersion().c_str());
genesis 284 printf("Default data directory %s\n", GetDefaultDataDir().c_str());
genesis 285
genesis 286 if (GetBoolArg("-loadblockindextest"))
genesis 287 {
genesis 288 CTxDB txdb("r");
genesis 289 txdb.LoadBlockIndex();
genesis 290 PrintBlockTree();
genesis 291 return false;
genesis 292 }
genesis 293
genesis 294
genesis 295 string strLockFile = GetDataDir() + "/.lock";
genesis 296 FILE* file = fopen(strLockFile.c_str(), "a");
genesis 297 if (file) fclose(file);
genesis 298 static boost::interprocess::file_lock lock(strLockFile.c_str());
genesis 299 if (!lock.try_lock())
genesis 300 {
genesis 301 wxMessageBox(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), GetDataDir().c_str()), "Bitcoin");
genesis 302 return false;
genesis 303 }
genesis 304
genesis 305 string strErrors;
genesis 306
genesis 307
genesis 308
genesis 309
genesis 310 if (fDaemon)
genesis 311 fprintf(stdout, "bitcoin server starting\n");
genesis 312 strErrors = "";
genesis 313 int64 nStart;
genesis 314
genesis 315 InitMessage(_("Loading addresses..."));
genesis 316 printf("Loading addresses...\n");
genesis 317 nStart = GetTimeMillis();
genesis 318 if (!LoadAddresses())
genesis 319 strErrors += _("Error loading addr.dat \n");
genesis 320 printf(" addresses %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 321
genesis 322 InitMessage(_("Loading block index..."));
genesis 323 printf("Loading block index...\n");
genesis 324 nStart = GetTimeMillis();
genesis 325 if (!LoadBlockIndex())
genesis 326 strErrors += _("Error loading blkindex.dat \n");
genesis 327 printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 328
genesis 329 InitMessage(_("Loading wallet..."));
genesis 330 printf("Loading wallet...\n");
genesis 331 nStart = GetTimeMillis();
genesis 332 bool fFirstRun;
genesis 333 pwalletMain = new CWallet("wallet.dat");
genesis 334 int nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
genesis 335 if (nLoadWalletRet != DB_LOAD_OK)
genesis 336 {
genesis 337 if (nLoadWalletRet == DB_CORRUPT)
genesis 338 strErrors += _("Error loading wallet.dat: Wallet corrupted \n");
genesis 339 else if (nLoadWalletRet == DB_TOO_NEW)
genesis 340 strErrors += _("Error loading wallet.dat: Wallet requires newer version of Bitcoin \n");
genesis 341 else if (nLoadWalletRet == DB_NEED_REWRITE)
genesis 342 {
genesis 343 strErrors += _("Wallet needed to be rewritten: restart Bitcoin to complete \n");
genesis 344 wxMessageBox(strErrors, "Bitcoin", wxOK | wxICON_ERROR);
genesis 345 return false;
genesis 346 }
genesis 347 else
genesis 348 strErrors += _("Error loading wallet.dat \n");
genesis 349 }
genesis 350 printf(" wallet %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 351
genesis 352 RegisterWallet(pwalletMain);
genesis 353
genesis 354 CBlockIndex *pindexRescan = pindexBest;
genesis 355 if (GetBoolArg("-rescan"))
genesis 356 pindexRescan = pindexGenesisBlock;
genesis 357 else
genesis 358 {
genesis 359 CWalletDB walletdb("wallet.dat");
genesis 360 CBlockLocator locator;
genesis 361 if (walletdb.ReadBestBlock(locator))
genesis 362 pindexRescan = locator.GetBlockIndex();
genesis 363 }
genesis 364 if (pindexBest != pindexRescan)
genesis 365 {
genesis 366 InitMessage(_("Rescanning..."));
genesis 367 printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
genesis 368 nStart = GetTimeMillis();
genesis 369 pwalletMain->ScanForWalletTransactions(pindexRescan, true);
genesis 370 printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
genesis 371 }
genesis 372
genesis 373 InitMessage(_("Done loading"));
genesis 374 printf("Done loading\n");
genesis 375
genesis 376
genesis 377 printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size());
genesis 378 printf("nBestHeight = %d\n", nBestHeight);
genesis 379 printf("setKeyPool.size() = %d\n", pwalletMain->setKeyPool.size());
genesis 380 printf("mapWallet.size() = %d\n", pwalletMain->mapWallet.size());
genesis 381 printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size());
genesis 382
genesis 383 if (!strErrors.empty())
genesis 384 {
genesis 385 wxMessageBox(strErrors, "Bitcoin", wxOK | wxICON_ERROR);
genesis 386 return false;
genesis 387 }
genesis 388
genesis 389
genesis 390 pwalletMain->ReacceptWalletTransactions();
genesis 391
genesis 392
genesis 393
genesis 394
genesis 395
genesis 396
genesis 397
genesis 398
genesis 399 if (GetBoolArg("-printblockindex") || GetBoolArg("-printblocktree"))
genesis 400 {
genesis 401 PrintBlockTree();
genesis 402 return false;
genesis 403 }
genesis 404
genesis 405 if (mapArgs.count("-timeout"))
genesis 406 {
genesis 407 int nNewTimeout = GetArg("-timeout", 5000);
genesis 408 if (nNewTimeout > 0 && nNewTimeout < 600000)
genesis 409 nConnectTimeout = nNewTimeout;
genesis 410 }
genesis 411
genesis 412 if (mapArgs.count("-printblock"))
genesis 413 {
genesis 414 string strMatch = mapArgs["-printblock"];
genesis 415 int nFound = 0;
genesis 416 for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
genesis 417 {
genesis 418 uint256 hash = (*mi).first;
genesis 419 if (strncmp(hash.ToString().c_str(), strMatch.c_str(), strMatch.size()) == 0)
genesis 420 {
genesis 421 CBlockIndex* pindex = (*mi).second;
genesis 422 CBlock block;
genesis 423 block.ReadFromDisk(pindex);
genesis 424 block.BuildMerkleTree();
genesis 425 block.print();
genesis 426 printf("\n");
genesis 427 nFound++;
genesis 428 }
genesis 429 }
genesis 430 if (nFound == 0)
genesis 431 printf("No blocks matching %s were found\n", strMatch.c_str());
genesis 432 return false;
genesis 433 }
genesis 434
genesis 435 fGenerateBitcoins = GetBoolArg("-gen");
genesis 436
genesis 437 if (mapArgs.count("-proxy"))
genesis 438 {
genesis 439 fUseProxy = true;
genesis 440 addrProxy = CAddress(mapArgs["-proxy"]);
genesis 441 if (!addrProxy.IsValid())
genesis 442 {
genesis 443 wxMessageBox(_("Invalid -proxy address"), "Bitcoin");
genesis 444 return false;
genesis 445 }
genesis 446 }
genesis 447
genesis 448 bool fTor = (fUseProxy && addrProxy.port == htons(9050));
genesis 449 if (fTor)
genesis 450 {
genesis 451
genesis 452
genesis 453 SoftSetArg("-nolisten", true);
genesis 454 SoftSetArg("-noirc", true);
genesis 455 SoftSetArg("-nodnsseed", true);
genesis 456 SoftSetArg("-dns", false);
genesis 457 }
genesis 458
genesis 459 fAllowDNS = GetBoolArg("-dns");
genesis 460 fNoListen = GetBoolArg("-nolisten");
genesis 461
genesis 462
genesis 463
genesis 464 if (!fNoListen)
genesis 465 {
genesis 466 if (!BindListenPort(strErrors))
genesis 467 {
genesis 468 wxMessageBox(strErrors, "Bitcoin");
genesis 469 return false;
genesis 470 }
genesis 471 }
genesis 472
genesis 473 if (mapArgs.count("-addnode"))
genesis 474 {
genesis 475 BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"])
genesis 476 {
genesis 477 CAddress addr(strAddr, fAllowDNS);
genesis 478 addr.nTime = 0;
genesis 479 if (addr.IsValid())
genesis 480 AddAddress(addr);
genesis 481 }
genesis 482 }
genesis 483
genesis 484 if (mapArgs.count("-paytxfee"))
genesis 485 {
genesis 486 if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
genesis 487 {
genesis 488 wxMessageBox(_("Invalid amount for -paytxfee=<amount>"), "Bitcoin");
genesis 489 return false;
genesis 490 }
genesis 491 if (nTransactionFee > 0.25 * COIN)
genesis 492 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 493 }
genesis 494
genesis 495
genesis 496
genesis 497
genesis 498 if (!CheckDiskSpace())
genesis 499 return false;
genesis 500
genesis 501 RandAddSeedPerfmon();
genesis 502
genesis 503 if (!CreateThread(StartNode, NULL))
genesis 504 wxMessageBox(_("Error: CreateThread(StartNode) failed"), "Bitcoin");
genesis 505
genesis 506 if (fServer)
genesis 507 CreateThread(ThreadRPCServer, NULL);
genesis 508
genesis 509 while (1)
genesis 510 Sleep(5000);
genesis 511
genesis 512 return true;
genesis 513 }