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