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