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