raw
experimental-genesis    1 //  /****************************\
experimental-genesis 2 // * EXPERIMENTAL BRANCH. *
experimental-genesis 3 // * FOR LABORATORY USE ONLY. *
experimental-genesis 4 // ********************************
experimental-genesis 5 // ************
experimental-genesis 6 // **************
experimental-genesis 7 // ****************
experimental-genesis 8 // **** **** ****
experimental-genesis 9 // *** *** ***
experimental-genesis 10 // *** *** ***
experimental-genesis 11 // *** * * **
experimental-genesis 12 // ******** ********
experimental-genesis 13 // ******* ******
experimental-genesis 14 // *** **
experimental-genesis 15 // * ******* **
experimental-genesis 16 // ** * * * * *
experimental-genesis 17 // ** * * ***
experimental-genesis 18 // **** * * * * ****
experimental-genesis 19 // **** *** * * ** ***
experimental-genesis 20 // **** ********* ******
experimental-genesis 21 // ******* ***** *******
experimental-genesis 22 // ********* ****** **
experimental-genesis 23 // ** ****** ******
experimental-genesis 24 // ** ******* **
experimental-genesis 25 // ** ******* ***
experimental-genesis 26 // **** ******** ************
experimental-genesis 27 // ************ ************
experimental-genesis 28 // ******** *******
experimental-genesis 29 // ****** ****
experimental-genesis 30 // *** ***
experimental-genesis 31 // ********************************
experimental-genesis 32 // Copyright (c) 2009-2010 Satoshi Nakamoto
experimental-genesis 33 // Copyright (c) 2009-2012 The Bitcoin developers
experimental-genesis 34 // Distributed under the MIT/X11 software license, see the accompanying
experimental-genesis 35 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
experimental-genesis 36 #include "headers.h"
experimental-genesis 37 #include "strlcpy.h"
experimental-genesis 38 #include <boost/program_options/detail/config_file.hpp>
experimental-genesis 39 #include <boost/program_options/parsers.hpp>
experimental-genesis 40 #include <boost/filesystem.hpp>
experimental-genesis 41 #include <boost/filesystem/fstream.hpp>
experimental-genesis 42 #include <boost/interprocess/sync/interprocess_mutex.hpp>
experimental-genesis 43 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
experimental-genesis 44 #include <boost/foreach.hpp>
experimental-genesis 45
experimental-genesis 46 using namespace std;
experimental-genesis 47 using namespace boost;
experimental-genesis 48
experimental-genesis 49 map<string, string> mapArgs;
experimental-genesis 50 map<string, vector<string> > mapMultiArgs;
experimental-genesis 51 bool fDebug = false;
experimental-genesis 52 bool fPrintToConsole = false;
experimental-genesis 53 bool fPrintToDebugger = false;
experimental-genesis 54 char pszSetDataDir[MAX_PATH] = "";
experimental-genesis 55 bool fRequestShutdown = false;
experimental-genesis 56 bool fShutdown = false;
experimental-genesis 57 bool fDaemon = false;
experimental-genesis 58 bool fServer = false;
experimental-genesis 59 bool fCommandLine = false;
experimental-genesis 60 string strMiscWarning;
experimental-genesis 61 bool fTestNet = false;
experimental-genesis 62 bool fNoListen = false;
experimental-genesis 63 bool fLogTimestamps = false;
experimental-genesis 64
experimental-genesis 65
experimental-genesis 66
experimental-genesis 67
experimental-genesis 68 // Workaround for "multiple definition of `_tls_used'"
experimental-genesis 69 // http://svn.boost.org/trac/boost/ticket/4258
experimental-genesis 70 extern "C" void tss_cleanup_implemented() { }
experimental-genesis 71
experimental-genesis 72
experimental-genesis 73
experimental-genesis 74
experimental-genesis 75
experimental-genesis 76 // Init openssl library multithreading support
experimental-genesis 77 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
experimental-genesis 78 void locking_callback(int mode, int i, const char* file, int line)
experimental-genesis 79 {
experimental-genesis 80 if (mode & CRYPTO_LOCK)
experimental-genesis 81 ppmutexOpenSSL[i]->lock();
experimental-genesis 82 else
experimental-genesis 83 ppmutexOpenSSL[i]->unlock();
experimental-genesis 84 }
experimental-genesis 85
experimental-genesis 86 // Init
experimental-genesis 87 class CInit
experimental-genesis 88 {
experimental-genesis 89 public:
experimental-genesis 90 CInit()
experimental-genesis 91 {
experimental-genesis 92 // Init openssl library multithreading support
experimental-genesis 93 ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
experimental-genesis 94 for (int i = 0; i < CRYPTO_num_locks(); i++)
experimental-genesis 95 ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
experimental-genesis 96 CRYPTO_set_locking_callback(locking_callback);
experimental-genesis 97
experimental-genesis 98 #ifdef WIN32
experimental-genesis 99 // Seed random number generator with screen scrape and other hardware sources
experimental-genesis 100 RAND_screen();
experimental-genesis 101 #endif
experimental-genesis 102
experimental-genesis 103 // Seed random number generator with performance counter
experimental-genesis 104 RandAddSeed();
experimental-genesis 105 }
experimental-genesis 106 ~CInit()
experimental-genesis 107 {
experimental-genesis 108 // Shutdown openssl library multithreading support
experimental-genesis 109 CRYPTO_set_locking_callback(NULL);
experimental-genesis 110 for (int i = 0; i < CRYPTO_num_locks(); i++)
experimental-genesis 111 delete ppmutexOpenSSL[i];
experimental-genesis 112 OPENSSL_free(ppmutexOpenSSL);
experimental-genesis 113 }
experimental-genesis 114 }
experimental-genesis 115 instance_of_cinit;
experimental-genesis 116
experimental-genesis 117
experimental-genesis 118
experimental-genesis 119
experimental-genesis 120
experimental-genesis 121
experimental-genesis 122
experimental-genesis 123
experimental-genesis 124 void RandAddSeed()
experimental-genesis 125 {
experimental-genesis 126 // Seed with CPU performance counter
experimental-genesis 127 int64 nCounter = GetPerformanceCounter();
experimental-genesis 128 RAND_add(&nCounter, sizeof(nCounter), 1.5);
experimental-genesis 129 memset(&nCounter, 0, sizeof(nCounter));
experimental-genesis 130 }
experimental-genesis 131
experimental-genesis 132 void RandAddSeedPerfmon()
experimental-genesis 133 {
experimental-genesis 134 RandAddSeed();
experimental-genesis 135
experimental-genesis 136 // This can take up to 2 seconds, so only do it every 10 minutes
experimental-genesis 137 static int64 nLastPerfmon;
experimental-genesis 138 if (GetTime() < nLastPerfmon + 10 * 60)
experimental-genesis 139 return;
experimental-genesis 140 nLastPerfmon = GetTime();
experimental-genesis 141
experimental-genesis 142 #ifdef WIN32
experimental-genesis 143 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
experimental-genesis 144 // Seed with the entire set of perfmon data
experimental-genesis 145 unsigned char pdata[250000];
experimental-genesis 146 memset(pdata, 0, sizeof(pdata));
experimental-genesis 147 unsigned long nSize = sizeof(pdata);
experimental-genesis 148 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
experimental-genesis 149 RegCloseKey(HKEY_PERFORMANCE_DATA);
experimental-genesis 150 if (ret == ERROR_SUCCESS)
experimental-genesis 151 {
experimental-genesis 152 RAND_add(pdata, nSize, nSize/100.0);
experimental-genesis 153 memset(pdata, 0, nSize);
experimental-genesis 154 printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
experimental-genesis 155 }
experimental-genesis 156 #endif
experimental-genesis 157 }
experimental-genesis 158
experimental-genesis 159 uint64 GetRand(uint64 nMax)
experimental-genesis 160 {
experimental-genesis 161 if (nMax == 0)
experimental-genesis 162 return 0;
experimental-genesis 163
experimental-genesis 164 // The range of the random source must be a multiple of the modulus
experimental-genesis 165 // to give every possible output value an equal possibility
experimental-genesis 166 uint64 nRange = (UINT64_MAX / nMax) * nMax;
experimental-genesis 167 uint64 nRand = 0;
experimental-genesis 168 do
experimental-genesis 169 RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
experimental-genesis 170 while (nRand >= nRange);
experimental-genesis 171 return (nRand % nMax);
experimental-genesis 172 }
experimental-genesis 173
experimental-genesis 174 int GetRandInt(int nMax)
experimental-genesis 175 {
experimental-genesis 176 return GetRand(nMax);
experimental-genesis 177 }
experimental-genesis 178
experimental-genesis 179
experimental-genesis 180
experimental-genesis 181
experimental-genesis 182
experimental-genesis 183
experimental-genesis 184
experimental-genesis 185
experimental-genesis 186
experimental-genesis 187
experimental-genesis 188
experimental-genesis 189 inline int OutputDebugStringF(const char* pszFormat, ...)
experimental-genesis 190 {
experimental-genesis 191 int ret = 0;
experimental-genesis 192 if (fPrintToConsole)
experimental-genesis 193 {
experimental-genesis 194 // print to console
experimental-genesis 195 va_list arg_ptr;
experimental-genesis 196 va_start(arg_ptr, pszFormat);
experimental-genesis 197 ret = vprintf(pszFormat, arg_ptr);
experimental-genesis 198 va_end(arg_ptr);
experimental-genesis 199 }
experimental-genesis 200 else
experimental-genesis 201 {
experimental-genesis 202 // print to debug.log
experimental-genesis 203 static FILE* fileout = NULL;
experimental-genesis 204
experimental-genesis 205 if (!fileout)
experimental-genesis 206 {
experimental-genesis 207 char pszFile[MAX_PATH+100];
experimental-genesis 208 GetDataDir(pszFile);
experimental-genesis 209 strlcat(pszFile, "/debug.log", sizeof(pszFile));
experimental-genesis 210 fileout = fopen(pszFile, "a");
experimental-genesis 211 if (fileout) setbuf(fileout, NULL); // unbuffered
experimental-genesis 212 }
experimental-genesis 213 if (fileout)
experimental-genesis 214 {
experimental-genesis 215 static bool fStartedNewLine = true;
experimental-genesis 216
experimental-genesis 217 // Debug print useful for profiling
experimental-genesis 218 if (fLogTimestamps && fStartedNewLine)
experimental-genesis 219 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
experimental-genesis 220 if (pszFormat[strlen(pszFormat) - 1] == '\n')
experimental-genesis 221 fStartedNewLine = true;
experimental-genesis 222 else
experimental-genesis 223 fStartedNewLine = false;
experimental-genesis 224
experimental-genesis 225 va_list arg_ptr;
experimental-genesis 226 va_start(arg_ptr, pszFormat);
experimental-genesis 227 ret = vfprintf(fileout, pszFormat, arg_ptr);
experimental-genesis 228 va_end(arg_ptr);
experimental-genesis 229 }
experimental-genesis 230 }
experimental-genesis 231
experimental-genesis 232 #ifdef WIN32
experimental-genesis 233 if (fPrintToDebugger)
experimental-genesis 234 {
experimental-genesis 235 static CCriticalSection cs_OutputDebugStringF;
experimental-genesis 236
experimental-genesis 237 // accumulate a line at a time
experimental-genesis 238 CRITICAL_BLOCK(cs_OutputDebugStringF)
experimental-genesis 239 {
experimental-genesis 240 static char pszBuffer[50000];
experimental-genesis 241 static char* pend;
experimental-genesis 242 if (pend == NULL)
experimental-genesis 243 pend = pszBuffer;
experimental-genesis 244 va_list arg_ptr;
experimental-genesis 245 va_start(arg_ptr, pszFormat);
experimental-genesis 246 int limit = END(pszBuffer) - pend - 2;
experimental-genesis 247 int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
experimental-genesis 248 va_end(arg_ptr);
experimental-genesis 249 if (ret < 0 || ret >= limit)
experimental-genesis 250 {
experimental-genesis 251 pend = END(pszBuffer) - 2;
experimental-genesis 252 *pend++ = '\n';
experimental-genesis 253 }
experimental-genesis 254 else
experimental-genesis 255 pend += ret;
experimental-genesis 256 *pend = '\0';
experimental-genesis 257 char* p1 = pszBuffer;
experimental-genesis 258 char* p2;
experimental-genesis 259 while (p2 = strchr(p1, '\n'))
experimental-genesis 260 {
experimental-genesis 261 p2++;
experimental-genesis 262 char c = *p2;
experimental-genesis 263 *p2 = '\0';
experimental-genesis 264 OutputDebugStringA(p1);
experimental-genesis 265 *p2 = c;
experimental-genesis 266 p1 = p2;
experimental-genesis 267 }
experimental-genesis 268 if (p1 != pszBuffer)
experimental-genesis 269 memmove(pszBuffer, p1, pend - p1 + 1);
experimental-genesis 270 pend -= (p1 - pszBuffer);
experimental-genesis 271 }
experimental-genesis 272 }
experimental-genesis 273 #endif
experimental-genesis 274 return ret;
experimental-genesis 275 }
experimental-genesis 276
experimental-genesis 277
experimental-genesis 278 // Safer snprintf
experimental-genesis 279 // - prints up to limit-1 characters
experimental-genesis 280 // - output string is always null terminated even if limit reached
experimental-genesis 281 // - return value is the number of characters actually printed
experimental-genesis 282 int my_snprintf(char* buffer, size_t limit, const char* format, ...)
experimental-genesis 283 {
experimental-genesis 284 if (limit == 0)
experimental-genesis 285 return 0;
experimental-genesis 286 va_list arg_ptr;
experimental-genesis 287 va_start(arg_ptr, format);
experimental-genesis 288 int ret = _vsnprintf(buffer, limit, format, arg_ptr);
experimental-genesis 289 va_end(arg_ptr);
experimental-genesis 290 if (ret < 0 || ret >= limit)
experimental-genesis 291 {
experimental-genesis 292 ret = limit - 1;
experimental-genesis 293 buffer[limit-1] = 0;
experimental-genesis 294 }
experimental-genesis 295 return ret;
experimental-genesis 296 }
experimental-genesis 297
experimental-genesis 298 string strprintf(const std::string &format, ...)
experimental-genesis 299 {
experimental-genesis 300 char buffer[50000];
experimental-genesis 301 char* p = buffer;
experimental-genesis 302 int limit = sizeof(buffer);
experimental-genesis 303 int ret;
experimental-genesis 304 loop
experimental-genesis 305 {
experimental-genesis 306 va_list arg_ptr;
experimental-genesis 307 va_start(arg_ptr, format);
experimental-genesis 308 ret = _vsnprintf(p, limit, format.c_str(), arg_ptr);
experimental-genesis 309 va_end(arg_ptr);
experimental-genesis 310 if (ret >= 0 && ret < limit)
experimental-genesis 311 break;
experimental-genesis 312 if (p != buffer)
experimental-genesis 313 delete[] p;
experimental-genesis 314 limit *= 2;
experimental-genesis 315 p = new char[limit];
experimental-genesis 316 if (p == NULL)
experimental-genesis 317 throw std::bad_alloc();
experimental-genesis 318 }
experimental-genesis 319 string str(p, p+ret);
experimental-genesis 320 if (p != buffer)
experimental-genesis 321 delete[] p;
experimental-genesis 322 return str;
experimental-genesis 323 }
experimental-genesis 324
experimental-genesis 325 bool error(const std::string &format, ...)
experimental-genesis 326 {
experimental-genesis 327 char buffer[50000];
experimental-genesis 328 int limit = sizeof(buffer);
experimental-genesis 329 va_list arg_ptr;
experimental-genesis 330 va_start(arg_ptr, format);
experimental-genesis 331 int ret = _vsnprintf(buffer, limit, format.c_str(), arg_ptr);
experimental-genesis 332 va_end(arg_ptr);
experimental-genesis 333 if (ret < 0 || ret >= limit)
experimental-genesis 334 {
experimental-genesis 335 ret = limit - 1;
experimental-genesis 336 buffer[limit-1] = 0;
experimental-genesis 337 }
experimental-genesis 338 printf("ERROR: %s\n", buffer);
experimental-genesis 339 return false;
experimental-genesis 340 }
experimental-genesis 341
experimental-genesis 342
experimental-genesis 343 void ParseString(const string& str, char c, vector<string>& v)
experimental-genesis 344 {
experimental-genesis 345 if (str.empty())
experimental-genesis 346 return;
experimental-genesis 347 string::size_type i1 = 0;
experimental-genesis 348 string::size_type i2;
experimental-genesis 349 loop
experimental-genesis 350 {
experimental-genesis 351 i2 = str.find(c, i1);
experimental-genesis 352 if (i2 == str.npos)
experimental-genesis 353 {
experimental-genesis 354 v.push_back(str.substr(i1));
experimental-genesis 355 return;
experimental-genesis 356 }
experimental-genesis 357 v.push_back(str.substr(i1, i2-i1));
experimental-genesis 358 i1 = i2+1;
experimental-genesis 359 }
experimental-genesis 360 }
experimental-genesis 361
experimental-genesis 362
experimental-genesis 363 string FormatMoney(int64 n, bool fPlus)
experimental-genesis 364 {
experimental-genesis 365 // Note: not using straight sprintf here because we do NOT want
experimental-genesis 366 // localized number formatting.
experimental-genesis 367 int64 n_abs = (n > 0 ? n : -n);
experimental-genesis 368 int64 quotient = n_abs/COIN;
experimental-genesis 369 int64 remainder = n_abs%COIN;
experimental-genesis 370 string str = strprintf("%"PRI64d".%08"PRI64d, quotient, remainder);
experimental-genesis 371
experimental-genesis 372 // Right-trim excess 0's before the decimal point:
experimental-genesis 373 int nTrim = 0;
experimental-genesis 374 for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
experimental-genesis 375 ++nTrim;
experimental-genesis 376 if (nTrim)
experimental-genesis 377 str.erase(str.size()-nTrim, nTrim);
experimental-genesis 378
experimental-genesis 379 if (n < 0)
experimental-genesis 380 str.insert((unsigned int)0, 1, '-');
experimental-genesis 381 else if (fPlus && n > 0)
experimental-genesis 382 str.insert((unsigned int)0, 1, '+');
experimental-genesis 383 return str;
experimental-genesis 384 }
experimental-genesis 385
experimental-genesis 386
experimental-genesis 387 bool ParseMoney(const string& str, int64& nRet)
experimental-genesis 388 {
experimental-genesis 389 return ParseMoney(str.c_str(), nRet);
experimental-genesis 390 }
experimental-genesis 391
experimental-genesis 392 bool ParseMoney(const char* pszIn, int64& nRet)
experimental-genesis 393 {
experimental-genesis 394 string strWhole;
experimental-genesis 395 int64 nUnits = 0;
experimental-genesis 396 const char* p = pszIn;
experimental-genesis 397 while (isspace(*p))
experimental-genesis 398 p++;
experimental-genesis 399 for (; *p; p++)
experimental-genesis 400 {
experimental-genesis 401 if (*p == '.')
experimental-genesis 402 {
experimental-genesis 403 p++;
experimental-genesis 404 int64 nMult = CENT*10;
experimental-genesis 405 while (isdigit(*p) && (nMult > 0))
experimental-genesis 406 {
experimental-genesis 407 nUnits += nMult * (*p++ - '0');
experimental-genesis 408 nMult /= 10;
experimental-genesis 409 }
experimental-genesis 410 break;
experimental-genesis 411 }
experimental-genesis 412 if (isspace(*p))
experimental-genesis 413 break;
experimental-genesis 414 if (!isdigit(*p))
experimental-genesis 415 return false;
experimental-genesis 416 strWhole.insert(strWhole.end(), *p);
experimental-genesis 417 }
experimental-genesis 418 for (; *p; p++)
experimental-genesis 419 if (!isspace(*p))
experimental-genesis 420 return false;
experimental-genesis 421 if (strWhole.size() > 10) // guard against 63 bit overflow
experimental-genesis 422 return false;
experimental-genesis 423 if (nUnits < 0 || nUnits > COIN)
experimental-genesis 424 return false;
experimental-genesis 425 int64 nWhole = atoi64(strWhole);
experimental-genesis 426 int64 nValue = nWhole*COIN + nUnits;
experimental-genesis 427
experimental-genesis 428 nRet = nValue;
experimental-genesis 429 return true;
experimental-genesis 430 }
experimental-genesis 431
experimental-genesis 432
experimental-genesis 433 vector<unsigned char> ParseHex(const char* psz)
experimental-genesis 434 {
experimental-genesis 435 static char phexdigit[256] =
experimental-genesis 436 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 437 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 438 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 439 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
experimental-genesis 440 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 441 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 442 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1
experimental-genesis 443 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 444 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 445 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 446 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 447 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 448 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 449 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 450 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
experimental-genesis 451 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
experimental-genesis 452
experimental-genesis 453 // convert hex dump to vector
experimental-genesis 454 vector<unsigned char> vch;
experimental-genesis 455 loop
experimental-genesis 456 {
experimental-genesis 457 while (isspace(*psz))
experimental-genesis 458 psz++;
experimental-genesis 459 char c = phexdigit[(unsigned char)*psz++];
experimental-genesis 460 if (c == (char)-1)
experimental-genesis 461 break;
experimental-genesis 462 unsigned char n = (c << 4);
experimental-genesis 463 c = phexdigit[(unsigned char)*psz++];
experimental-genesis 464 if (c == (char)-1)
experimental-genesis 465 break;
experimental-genesis 466 n |= c;
experimental-genesis 467 vch.push_back(n);
experimental-genesis 468 }
experimental-genesis 469 return vch;
experimental-genesis 470 }
experimental-genesis 471
experimental-genesis 472 vector<unsigned char> ParseHex(const string& str)
experimental-genesis 473 {
experimental-genesis 474 return ParseHex(str.c_str());
experimental-genesis 475 }
experimental-genesis 476
experimental-genesis 477 void ParseParameters(int argc, char* argv[])
experimental-genesis 478 {
experimental-genesis 479 mapArgs.clear();
experimental-genesis 480 mapMultiArgs.clear();
experimental-genesis 481 for (int i = 1; i < argc; i++)
experimental-genesis 482 {
experimental-genesis 483 char psz[10000];
experimental-genesis 484 strlcpy(psz, argv[i], sizeof(psz));
experimental-genesis 485 char* pszValue = (char*)"";
experimental-genesis 486 if (strchr(psz, '='))
experimental-genesis 487 {
experimental-genesis 488 pszValue = strchr(psz, '=');
experimental-genesis 489 *pszValue++ = '\0';
experimental-genesis 490 }
experimental-genesis 491 #ifdef WIN32
experimental-genesis 492 _strlwr(psz);
experimental-genesis 493 if (psz[0] == '/')
experimental-genesis 494 psz[0] = '-';
experimental-genesis 495 #endif
experimental-genesis 496 if (psz[0] != '-')
experimental-genesis 497 break;
experimental-genesis 498 mapArgs[psz] = pszValue;
experimental-genesis 499 mapMultiArgs[psz].push_back(pszValue);
experimental-genesis 500 }
experimental-genesis 501 }
experimental-genesis 502
experimental-genesis 503 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
experimental-genesis 504 {
experimental-genesis 505 if (mapArgs.count(strArg))
experimental-genesis 506 return false;
experimental-genesis 507 mapArgs[strArg] = strValue;
experimental-genesis 508 return true;
experimental-genesis 509 }
experimental-genesis 510
experimental-genesis 511 bool SoftSetArg(const std::string& strArg, bool fValue)
experimental-genesis 512 {
experimental-genesis 513 if (fValue)
experimental-genesis 514 return SoftSetArg(strArg, std::string("1"));
experimental-genesis 515 else
experimental-genesis 516 return SoftSetArg(strArg, std::string("0"));
experimental-genesis 517 }
experimental-genesis 518
experimental-genesis 519
experimental-genesis 520 string EncodeBase64(const unsigned char* pch, size_t len)
experimental-genesis 521 {
experimental-genesis 522 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
experimental-genesis 523
experimental-genesis 524 string strRet="";
experimental-genesis 525 strRet.reserve((len+2)/3*4);
experimental-genesis 526
experimental-genesis 527 int mode=0, left=0;
experimental-genesis 528 const unsigned char *pchEnd = pch+len;
experimental-genesis 529
experimental-genesis 530 while (pch<pchEnd)
experimental-genesis 531 {
experimental-genesis 532 int enc = *(pch++);
experimental-genesis 533 switch (mode)
experimental-genesis 534 {
experimental-genesis 535 case 0: // we have no bits
experimental-genesis 536 strRet += pbase64[enc >> 2];
experimental-genesis 537 left = (enc & 3) << 4;
experimental-genesis 538 mode = 1;
experimental-genesis 539 break;
experimental-genesis 540
experimental-genesis 541 case 1: // we have two bits
experimental-genesis 542 strRet += pbase64[left | (enc >> 4)];
experimental-genesis 543 left = (enc & 15) << 2;
experimental-genesis 544 mode = 2;
experimental-genesis 545 break;
experimental-genesis 546
experimental-genesis 547 case 2: // we have four bits
experimental-genesis 548 strRet += pbase64[left | (enc >> 6)];
experimental-genesis 549 strRet += pbase64[enc & 63];
experimental-genesis 550 mode = 0;
experimental-genesis 551 break;
experimental-genesis 552 }
experimental-genesis 553 }
experimental-genesis 554
experimental-genesis 555 if (mode)
experimental-genesis 556 {
experimental-genesis 557 strRet += pbase64[left];
experimental-genesis 558 strRet += '=';
experimental-genesis 559 if (mode == 1)
experimental-genesis 560 strRet += '=';
experimental-genesis 561 }
experimental-genesis 562
experimental-genesis 563 return strRet;
experimental-genesis 564 }
experimental-genesis 565
experimental-genesis 566 string EncodeBase64(const string& str)
experimental-genesis 567 {
experimental-genesis 568 return EncodeBase64((const unsigned char*)str.c_str(), str.size());
experimental-genesis 569 }
experimental-genesis 570
experimental-genesis 571 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
experimental-genesis 572 {
experimental-genesis 573 static const int decode64_table[256] =
experimental-genesis 574 {
experimental-genesis 575 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 576 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 577 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
experimental-genesis 578 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
experimental-genesis 579 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
experimental-genesis 580 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
experimental-genesis 581 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 582 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 583 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 584 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 585 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 586 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
experimental-genesis 587 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
experimental-genesis 588 };
experimental-genesis 589
experimental-genesis 590 if (pfInvalid)
experimental-genesis 591 *pfInvalid = false;
experimental-genesis 592
experimental-genesis 593 vector<unsigned char> vchRet;
experimental-genesis 594 vchRet.reserve(strlen(p)*3/4);
experimental-genesis 595
experimental-genesis 596 int mode = 0;
experimental-genesis 597 int left = 0;
experimental-genesis 598
experimental-genesis 599 while (1)
experimental-genesis 600 {
experimental-genesis 601 int dec = decode64_table[*p];
experimental-genesis 602 if (dec == -1) break;
experimental-genesis 603 p++;
experimental-genesis 604 switch (mode)
experimental-genesis 605 {
experimental-genesis 606 case 0: // we have no bits and get 6
experimental-genesis 607 left = dec;
experimental-genesis 608 mode = 1;
experimental-genesis 609 break;
experimental-genesis 610
experimental-genesis 611 case 1: // we have 6 bits and keep 4
experimental-genesis 612 vchRet.push_back((left<<2) | (dec>>4));
experimental-genesis 613 left = dec & 15;
experimental-genesis 614 mode = 2;
experimental-genesis 615 break;
experimental-genesis 616
experimental-genesis 617 case 2: // we have 4 bits and get 6, we keep 2
experimental-genesis 618 vchRet.push_back((left<<4) | (dec>>2));
experimental-genesis 619 left = dec & 3;
experimental-genesis 620 mode = 3;
experimental-genesis 621 break;
experimental-genesis 622
experimental-genesis 623 case 3: // we have 2 bits and get 6
experimental-genesis 624 vchRet.push_back((left<<6) | dec);
experimental-genesis 625 mode = 0;
experimental-genesis 626 break;
experimental-genesis 627 }
experimental-genesis 628 }
experimental-genesis 629
experimental-genesis 630 if (pfInvalid)
experimental-genesis 631 switch (mode)
experimental-genesis 632 {
experimental-genesis 633 case 0: // 4n base64 characters processed: ok
experimental-genesis 634 break;
experimental-genesis 635
experimental-genesis 636 case 1: // 4n+1 base64 character processed: impossible
experimental-genesis 637 *pfInvalid = true;
experimental-genesis 638 break;
experimental-genesis 639
experimental-genesis 640 case 2: // 4n+2 base64 characters processed: require '=='
experimental-genesis 641 if (left || p[0] != '=' || p[1] != '=' || decode64_table[p[2]] != -1)
experimental-genesis 642 *pfInvalid = true;
experimental-genesis 643 break;
experimental-genesis 644
experimental-genesis 645 case 3: // 4n+3 base64 characters processed: require '='
experimental-genesis 646 if (left || p[0] != '=' || decode64_table[p[1]] != -1)
experimental-genesis 647 *pfInvalid = true;
experimental-genesis 648 break;
experimental-genesis 649 }
experimental-genesis 650
experimental-genesis 651 return vchRet;
experimental-genesis 652 }
experimental-genesis 653
experimental-genesis 654 string DecodeBase64(const string& str)
experimental-genesis 655 {
experimental-genesis 656 vector<unsigned char> vchRet = DecodeBase64(str.c_str());
experimental-genesis 657 return string((const char*)&vchRet[0], vchRet.size());
experimental-genesis 658 }
experimental-genesis 659
experimental-genesis 660
experimental-genesis 661 bool WildcardMatch(const char* psz, const char* mask)
experimental-genesis 662 {
experimental-genesis 663 loop
experimental-genesis 664 {
experimental-genesis 665 switch (*mask)
experimental-genesis 666 {
experimental-genesis 667 case '\0':
experimental-genesis 668 return (*psz == '\0');
experimental-genesis 669 case '*':
experimental-genesis 670 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
experimental-genesis 671 case '?':
experimental-genesis 672 if (*psz == '\0')
experimental-genesis 673 return false;
experimental-genesis 674 break;
experimental-genesis 675 default:
experimental-genesis 676 if (*psz != *mask)
experimental-genesis 677 return false;
experimental-genesis 678 break;
experimental-genesis 679 }
experimental-genesis 680 psz++;
experimental-genesis 681 mask++;
experimental-genesis 682 }
experimental-genesis 683 }
experimental-genesis 684
experimental-genesis 685 bool WildcardMatch(const string& str, const string& mask)
experimental-genesis 686 {
experimental-genesis 687 return WildcardMatch(str.c_str(), mask.c_str());
experimental-genesis 688 }
experimental-genesis 689
experimental-genesis 690
experimental-genesis 691
experimental-genesis 692
experimental-genesis 693
experimental-genesis 694
experimental-genesis 695
experimental-genesis 696
experimental-genesis 697 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
experimental-genesis 698 {
experimental-genesis 699 #ifdef WIN32
experimental-genesis 700 char pszModule[MAX_PATH];
experimental-genesis 701 pszModule[0] = '\0';
experimental-genesis 702 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
experimental-genesis 703 #else
experimental-genesis 704 const char* pszModule = "bitcoin";
experimental-genesis 705 #endif
experimental-genesis 706 if (pex)
experimental-genesis 707 snprintf(pszMessage, 1000,
experimental-genesis 708 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
experimental-genesis 709 else
experimental-genesis 710 snprintf(pszMessage, 1000,
experimental-genesis 711 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
experimental-genesis 712 }
experimental-genesis 713
experimental-genesis 714 void LogException(std::exception* pex, const char* pszThread)
experimental-genesis 715 {
experimental-genesis 716 char pszMessage[10000];
experimental-genesis 717 FormatException(pszMessage, pex, pszThread);
experimental-genesis 718 printf("\n%s", pszMessage);
experimental-genesis 719 }
experimental-genesis 720
experimental-genesis 721 void PrintException(std::exception* pex, const char* pszThread)
experimental-genesis 722 {
experimental-genesis 723 char pszMessage[10000];
experimental-genesis 724 FormatException(pszMessage, pex, pszThread);
experimental-genesis 725 printf("\n\n************************\n%s\n", pszMessage);
experimental-genesis 726 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
experimental-genesis 727 strMiscWarning = pszMessage;
experimental-genesis 728 throw;
experimental-genesis 729 }
experimental-genesis 730
experimental-genesis 731 void ThreadOneMessageBox(string strMessage)
experimental-genesis 732 {
experimental-genesis 733 // Skip message boxes if one is already open
experimental-genesis 734 static bool fMessageBoxOpen;
experimental-genesis 735 if (fMessageBoxOpen)
experimental-genesis 736 return;
experimental-genesis 737 fMessageBoxOpen = true;
experimental-genesis 738 ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
experimental-genesis 739 fMessageBoxOpen = false;
experimental-genesis 740 }
experimental-genesis 741
experimental-genesis 742 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
experimental-genesis 743 {
experimental-genesis 744 char pszMessage[10000];
experimental-genesis 745 FormatException(pszMessage, pex, pszThread);
experimental-genesis 746 printf("\n\n************************\n%s\n", pszMessage);
experimental-genesis 747 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
experimental-genesis 748 strMiscWarning = pszMessage;
experimental-genesis 749 }
experimental-genesis 750
experimental-genesis 751
experimental-genesis 752
experimental-genesis 753
experimental-genesis 754
experimental-genesis 755
experimental-genesis 756
experimental-genesis 757
experimental-genesis 758 #ifdef WIN32
experimental-genesis 759 typedef WINSHELLAPI BOOL (WINAPI *PSHGETSPECIALFOLDERPATHA)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate);
experimental-genesis 760
experimental-genesis 761 string MyGetSpecialFolderPath(int nFolder, bool fCreate)
experimental-genesis 762 {
experimental-genesis 763 char pszPath[MAX_PATH+100] = "";
experimental-genesis 764
experimental-genesis 765 // SHGetSpecialFolderPath isn't always available on old Windows versions
experimental-genesis 766 HMODULE hShell32 = LoadLibraryA("shell32.dll");
experimental-genesis 767 if (hShell32)
experimental-genesis 768 {
experimental-genesis 769 PSHGETSPECIALFOLDERPATHA pSHGetSpecialFolderPath =
experimental-genesis 770 (PSHGETSPECIALFOLDERPATHA)GetProcAddress(hShell32, "SHGetSpecialFolderPathA");
experimental-genesis 771 bool fSuccess = false;
experimental-genesis 772 if (pSHGetSpecialFolderPath)
experimental-genesis 773 fSuccess =
experimental-genesis 774 (*pSHGetSpecialFolderPath)(NULL, pszPath, nFolder, fCreate);
experimental-genesis 775 FreeModule(hShell32);
experimental-genesis 776 if (fSuccess)
experimental-genesis 777 return pszPath;
experimental-genesis 778 }
experimental-genesis 779
experimental-genesis 780 // Backup option
experimental-genesis 781 std::string strPath;
experimental-genesis 782 {
experimental-genesis 783 const char *pszEnv;
experimental-genesis 784 if (nFolder == CSIDL_STARTUP)
experimental-genesis 785 {
experimental-genesis 786 pszEnv = getenv("USERPROFILE");
experimental-genesis 787 if (pszEnv)
experimental-genesis 788 strPath = pszEnv;
experimental-genesis 789 strPath += "\\Start Menu\\Programs\\Startup";
experimental-genesis 790 }
experimental-genesis 791 else if (nFolder == CSIDL_APPDATA)
experimental-genesis 792 {
experimental-genesis 793 pszEnv = getenv("APPDATA");
experimental-genesis 794 if (pszEnv)
experimental-genesis 795 strPath = pszEnv;
experimental-genesis 796 }
experimental-genesis 797 }
experimental-genesis 798
experimental-genesis 799 return strPath;
experimental-genesis 800 }
experimental-genesis 801 #endif
experimental-genesis 802
experimental-genesis 803 string GetDefaultDataDir()
experimental-genesis 804 {
experimental-genesis 805 // Windows: C:\Documents and Settings\username\Application Data\Bitcoin
experimental-genesis 806 // Mac: ~/Library/Application Support/Bitcoin
experimental-genesis 807 // Unix: ~/.bitcoin
experimental-genesis 808 #ifdef WIN32
experimental-genesis 809 // Windows
experimental-genesis 810 return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin";
experimental-genesis 811 #else
experimental-genesis 812 char* pszHome = getenv("HOME");
experimental-genesis 813 if (pszHome == NULL || strlen(pszHome) == 0)
experimental-genesis 814 pszHome = (char*)"/";
experimental-genesis 815 string strHome = pszHome;
experimental-genesis 816 if (strHome[strHome.size()-1] != '/')
experimental-genesis 817 strHome += '/';
experimental-genesis 818 #ifdef MAC_OSX
experimental-genesis 819 // Mac
experimental-genesis 820 strHome += "Library/Application Support/";
experimental-genesis 821 filesystem::create_directory(strHome.c_str());
experimental-genesis 822 return strHome + "Bitcoin";
experimental-genesis 823 #else
experimental-genesis 824 // Unix
experimental-genesis 825 return strHome + ".bitcoin";
experimental-genesis 826 #endif
experimental-genesis 827 #endif
experimental-genesis 828 }
experimental-genesis 829
experimental-genesis 830 void GetDataDir(char* pszDir)
experimental-genesis 831 {
experimental-genesis 832 // pszDir must be at least MAX_PATH length.
experimental-genesis 833 int nVariation;
experimental-genesis 834 if (pszSetDataDir[0] != 0)
experimental-genesis 835 {
experimental-genesis 836 strlcpy(pszDir, pszSetDataDir, MAX_PATH);
experimental-genesis 837 nVariation = 0;
experimental-genesis 838 }
experimental-genesis 839 else
experimental-genesis 840 {
experimental-genesis 841 // This can be called during exceptions by printf, so we cache the
experimental-genesis 842 // value so we don't have to do memory allocations after that.
experimental-genesis 843 static char pszCachedDir[MAX_PATH];
experimental-genesis 844 if (pszCachedDir[0] == 0)
experimental-genesis 845 strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir));
experimental-genesis 846 strlcpy(pszDir, pszCachedDir, MAX_PATH);
experimental-genesis 847 nVariation = 1;
experimental-genesis 848 }
experimental-genesis 849 if (fTestNet)
experimental-genesis 850 {
experimental-genesis 851 char* p = pszDir + strlen(pszDir);
experimental-genesis 852 if (p > pszDir && p[-1] != '/' && p[-1] != '\\')
experimental-genesis 853 *p++ = '/';
experimental-genesis 854 strcpy(p, "testnet");
experimental-genesis 855 nVariation += 2;
experimental-genesis 856 }
experimental-genesis 857 static bool pfMkdir[4];
experimental-genesis 858 if (!pfMkdir[nVariation])
experimental-genesis 859 {
experimental-genesis 860 pfMkdir[nVariation] = true;
experimental-genesis 861 boost::filesystem::create_directory(pszDir);
experimental-genesis 862 }
experimental-genesis 863 }
experimental-genesis 864
experimental-genesis 865 string GetDataDir()
experimental-genesis 866 {
experimental-genesis 867 char pszDir[MAX_PATH];
experimental-genesis 868 GetDataDir(pszDir);
experimental-genesis 869 return pszDir;
experimental-genesis 870 }
experimental-genesis 871
experimental-genesis 872 string GetConfigFile()
experimental-genesis 873 {
experimental-genesis 874 namespace fs = boost::filesystem;
experimental-genesis 875 fs::path pathConfig(GetArg("-conf", "bitcoin.conf"));
experimental-genesis 876 if (!pathConfig.is_complete())
experimental-genesis 877 pathConfig = fs::path(GetDataDir()) / pathConfig;
experimental-genesis 878 return pathConfig.string();
experimental-genesis 879 }
experimental-genesis 880
experimental-genesis 881 void ReadConfigFile(map<string, string>& mapSettingsRet,
experimental-genesis 882 map<string, vector<string> >& mapMultiSettingsRet)
experimental-genesis 883 {
experimental-genesis 884 namespace fs = boost::filesystem;
experimental-genesis 885 namespace pod = boost::program_options::detail;
experimental-genesis 886
experimental-genesis 887 fs::ifstream streamConfig(GetConfigFile());
experimental-genesis 888 if (!streamConfig.good())
experimental-genesis 889 return;
experimental-genesis 890
experimental-genesis 891 set<string> setOptions;
experimental-genesis 892 setOptions.insert("*");
experimental-genesis 893
experimental-genesis 894 for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
experimental-genesis 895 {
experimental-genesis 896 // Don't overwrite existing settings so command line settings override bitcoin.conf
experimental-genesis 897 string strKey = string("-") + it->string_key;
experimental-genesis 898 if (mapSettingsRet.count(strKey) == 0)
experimental-genesis 899 mapSettingsRet[strKey] = it->value[0];
experimental-genesis 900 mapMultiSettingsRet[strKey].push_back(it->value[0]);
experimental-genesis 901 }
experimental-genesis 902 }
experimental-genesis 903
experimental-genesis 904 string GetPidFile()
experimental-genesis 905 {
experimental-genesis 906 namespace fs = boost::filesystem;
experimental-genesis 907 fs::path pathConfig(GetArg("-pid", "bitcoind.pid"));
experimental-genesis 908 if (!pathConfig.is_complete())
experimental-genesis 909 pathConfig = fs::path(GetDataDir()) / pathConfig;
experimental-genesis 910 return pathConfig.string();
experimental-genesis 911 }
experimental-genesis 912
experimental-genesis 913 void CreatePidFile(string pidFile, pid_t pid)
experimental-genesis 914 {
experimental-genesis 915 FILE* file = fopen(pidFile.c_str(), "w");
experimental-genesis 916 if (file)
experimental-genesis 917 {
experimental-genesis 918 fprintf(file, "%d\n", pid);
experimental-genesis 919 fclose(file);
experimental-genesis 920 }
experimental-genesis 921 }
experimental-genesis 922
experimental-genesis 923 int GetFilesize(FILE* file)
experimental-genesis 924 {
experimental-genesis 925 int nSavePos = ftell(file);
experimental-genesis 926 int nFilesize = -1;
experimental-genesis 927 if (fseek(file, 0, SEEK_END) == 0)
experimental-genesis 928 nFilesize = ftell(file);
experimental-genesis 929 fseek(file, nSavePos, SEEK_SET);
experimental-genesis 930 return nFilesize;
experimental-genesis 931 }
experimental-genesis 932
experimental-genesis 933 void ShrinkDebugFile()
experimental-genesis 934 {
experimental-genesis 935 // Scroll debug.log if it's getting too big
experimental-genesis 936 string strFile = GetDataDir() + "/debug.log";
experimental-genesis 937 FILE* file = fopen(strFile.c_str(), "r");
experimental-genesis 938 if (file && GetFilesize(file) > 10 * 1000000)
experimental-genesis 939 {
experimental-genesis 940 // Restart the file with some of the end
experimental-genesis 941 char pch[200000];
experimental-genesis 942 fseek(file, -sizeof(pch), SEEK_END);
experimental-genesis 943 int nBytes = fread(pch, 1, sizeof(pch), file);
experimental-genesis 944 fclose(file);
experimental-genesis 945
experimental-genesis 946 file = fopen(strFile.c_str(), "w");
experimental-genesis 947 if (file)
experimental-genesis 948 {
experimental-genesis 949 fwrite(pch, 1, nBytes, file);
experimental-genesis 950 fclose(file);
experimental-genesis 951 }
experimental-genesis 952 }
experimental-genesis 953 }
experimental-genesis 954
experimental-genesis 955
experimental-genesis 956
experimental-genesis 957
experimental-genesis 958
experimental-genesis 959
experimental-genesis 960
experimental-genesis 961
experimental-genesis 962 //
experimental-genesis 963 // "Never go to sea with two chronometers; take one or three."
experimental-genesis 964 // Our three time sources are:
experimental-genesis 965 // - System clock
experimental-genesis 966 // - Median of other nodes's clocks
experimental-genesis 967 // - The user (asking the user to fix the system clock if the first two disagree)
experimental-genesis 968 //
experimental-genesis 969 static int64 nMockTime = 0; // For unit testing
experimental-genesis 970
experimental-genesis 971 int64 GetTime()
experimental-genesis 972 {
experimental-genesis 973 if (nMockTime) return nMockTime;
experimental-genesis 974
experimental-genesis 975 return time(NULL);
experimental-genesis 976 }
experimental-genesis 977
experimental-genesis 978 void SetMockTime(int64 nMockTimeIn)
experimental-genesis 979 {
experimental-genesis 980 nMockTime = nMockTimeIn;
experimental-genesis 981 }
experimental-genesis 982
experimental-genesis 983 static int64 nTimeOffset = 0;
experimental-genesis 984
experimental-genesis 985 int64 GetAdjustedTime()
experimental-genesis 986 {
experimental-genesis 987 return GetTime() + nTimeOffset;
experimental-genesis 988 }
experimental-genesis 989
experimental-genesis 990 void AddTimeData(unsigned int ip, int64 nTime)
experimental-genesis 991 {
experimental-genesis 992 int64 nOffsetSample = nTime - GetTime();
experimental-genesis 993
experimental-genesis 994 // Ignore duplicates
experimental-genesis 995 static set<unsigned int> setKnown;
experimental-genesis 996 if (!setKnown.insert(ip).second)
experimental-genesis 997 return;
experimental-genesis 998
experimental-genesis 999 // Add data
experimental-genesis 1000 static vector<int64> vTimeOffsets;
experimental-genesis 1001 if (vTimeOffsets.empty())
experimental-genesis 1002 vTimeOffsets.push_back(0);
experimental-genesis 1003 vTimeOffsets.push_back(nOffsetSample);
experimental-genesis 1004 printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60);
experimental-genesis 1005 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
experimental-genesis 1006 {
experimental-genesis 1007 sort(vTimeOffsets.begin(), vTimeOffsets.end());
experimental-genesis 1008 int64 nMedian = vTimeOffsets[vTimeOffsets.size()/2];
experimental-genesis 1009 // Only let other nodes change our time by so much
experimental-genesis 1010 if (abs64(nMedian) < 70 * 60)
experimental-genesis 1011 {
experimental-genesis 1012 nTimeOffset = nMedian;
experimental-genesis 1013 }
experimental-genesis 1014 else
experimental-genesis 1015 {
experimental-genesis 1016 nTimeOffset = 0;
experimental-genesis 1017
experimental-genesis 1018 static bool fDone;
experimental-genesis 1019 if (!fDone)
experimental-genesis 1020 {
experimental-genesis 1021 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
experimental-genesis 1022 bool fMatch = false;
experimental-genesis 1023 BOOST_FOREACH(int64 nOffset, vTimeOffsets)
experimental-genesis 1024 if (nOffset != 0 && abs64(nOffset) < 5 * 60)
experimental-genesis 1025 fMatch = true;
experimental-genesis 1026
experimental-genesis 1027 if (!fMatch)
experimental-genesis 1028 {
experimental-genesis 1029 fDone = true;
experimental-genesis 1030 string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly.");
experimental-genesis 1031 strMiscWarning = strMessage;
experimental-genesis 1032 printf("*** %s\n", strMessage.c_str());
experimental-genesis 1033 boost::thread(boost::bind(ThreadSafeMessageBox, strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION, (wxWindow*)NULL, -1, -1));
experimental-genesis 1034 }
experimental-genesis 1035 }
experimental-genesis 1036 }
experimental-genesis 1037 BOOST_FOREACH(int64 n, vTimeOffsets)
experimental-genesis 1038 printf("%+"PRI64d" ", n);
experimental-genesis 1039 printf("| nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
experimental-genesis 1040 }
experimental-genesis 1041 }
experimental-genesis 1042
experimental-genesis 1043
experimental-genesis 1044
experimental-genesis 1045
experimental-genesis 1046
experimental-genesis 1047
experimental-genesis 1048
experimental-genesis 1049
experimental-genesis 1050
experimental-genesis 1051 string FormatVersion(int nVersion)
experimental-genesis 1052 {
experimental-genesis 1053 if (nVersion%100 == 0)
experimental-genesis 1054 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
experimental-genesis 1055 else
experimental-genesis 1056 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
experimental-genesis 1057 }
experimental-genesis 1058
experimental-genesis 1059 string FormatFullVersion()
experimental-genesis 1060 {
experimental-genesis 1061 string s = FormatVersion(VERSION) + pszSubVer;
experimental-genesis 1062 if (VERSION_IS_BETA) {
experimental-genesis 1063 s += "-";
experimental-genesis 1064 s += _("beta");
experimental-genesis 1065 }
experimental-genesis 1066 return s;
experimental-genesis 1067 }
experimental-genesis 1068
experimental-genesis 1069
experimental-genesis 1070
experimental-genesis 1071
experimental-genesis 1072 #ifdef DEBUG_LOCKORDER
experimental-genesis 1073 //
experimental-genesis 1074 // Early deadlock detection.
experimental-genesis 1075 // Problem being solved:
experimental-genesis 1076 // Thread 1 locks A, then B, then C
experimental-genesis 1077 // Thread 2 locks D, then C, then A
experimental-genesis 1078 // --> may result in deadlock between the two threads, depending on when they run.
experimental-genesis 1079 // Solution implemented here:
experimental-genesis 1080 // Keep track of pairs of locks: (A before B), (A before C), etc.
experimental-genesis 1081 // Complain if any thread trys to lock in a different order.
experimental-genesis 1082 //
experimental-genesis 1083
experimental-genesis 1084 struct CLockLocation
experimental-genesis 1085 {
experimental-genesis 1086 CLockLocation(const char* pszName, const char* pszFile, int nLine)
experimental-genesis 1087 {
experimental-genesis 1088 mutexName = pszName;
experimental-genesis 1089 sourceFile = pszFile;
experimental-genesis 1090 sourceLine = nLine;
experimental-genesis 1091 }
experimental-genesis 1092
experimental-genesis 1093 std::string ToString() const
experimental-genesis 1094 {
experimental-genesis 1095 return mutexName+" "+sourceFile+":"+itostr(sourceLine);
experimental-genesis 1096 }
experimental-genesis 1097
experimental-genesis 1098 private:
experimental-genesis 1099 std::string mutexName;
experimental-genesis 1100 std::string sourceFile;
experimental-genesis 1101 int sourceLine;
experimental-genesis 1102 };
experimental-genesis 1103
experimental-genesis 1104 typedef std::vector< std::pair<CCriticalSection*, CLockLocation> > LockStack;
experimental-genesis 1105
experimental-genesis 1106 static boost::interprocess::interprocess_mutex dd_mutex;
experimental-genesis 1107 static std::map<std::pair<CCriticalSection*, CCriticalSection*>, LockStack> lockorders;
experimental-genesis 1108 static boost::thread_specific_ptr<LockStack> lockstack;
experimental-genesis 1109
experimental-genesis 1110
experimental-genesis 1111 static void potential_deadlock_detected(const std::pair<CCriticalSection*, CCriticalSection*>& mismatch, const LockStack& s1, const LockStack& s2)
experimental-genesis 1112 {
experimental-genesis 1113 printf("POTENTIAL DEADLOCK DETECTED\n");
experimental-genesis 1114 printf("Previous lock order was:\n");
experimental-genesis 1115 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s2)
experimental-genesis 1116 {
experimental-genesis 1117 if (i.first == mismatch.first) printf(" (1)");
experimental-genesis 1118 if (i.first == mismatch.second) printf(" (2)");
experimental-genesis 1119 printf(" %s\n", i.second.ToString().c_str());
experimental-genesis 1120 }
experimental-genesis 1121 printf("Current lock order is:\n");
experimental-genesis 1122 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s1)
experimental-genesis 1123 {
experimental-genesis 1124 if (i.first == mismatch.first) printf(" (1)");
experimental-genesis 1125 if (i.first == mismatch.second) printf(" (2)");
experimental-genesis 1126 printf(" %s\n", i.second.ToString().c_str());
experimental-genesis 1127 }
experimental-genesis 1128 }
experimental-genesis 1129
experimental-genesis 1130 static void push_lock(CCriticalSection* c, const CLockLocation& locklocation)
experimental-genesis 1131 {
experimental-genesis 1132 bool fOrderOK = true;
experimental-genesis 1133 if (lockstack.get() == NULL)
experimental-genesis 1134 lockstack.reset(new LockStack);
experimental-genesis 1135
experimental-genesis 1136 if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
experimental-genesis 1137 dd_mutex.lock();
experimental-genesis 1138
experimental-genesis 1139 (*lockstack).push_back(std::make_pair(c, locklocation));
experimental-genesis 1140
experimental-genesis 1141 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, (*lockstack))
experimental-genesis 1142 {
experimental-genesis 1143 if (i.first == c) break;
experimental-genesis 1144
experimental-genesis 1145 std::pair<CCriticalSection*, CCriticalSection*> p1 = std::make_pair(i.first, c);
experimental-genesis 1146 if (lockorders.count(p1))
experimental-genesis 1147 continue;
experimental-genesis 1148 lockorders[p1] = (*lockstack);
experimental-genesis 1149
experimental-genesis 1150 std::pair<CCriticalSection*, CCriticalSection*> p2 = std::make_pair(c, i.first);
experimental-genesis 1151 if (lockorders.count(p2))
experimental-genesis 1152 {
experimental-genesis 1153 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
experimental-genesis 1154 break;
experimental-genesis 1155 }
experimental-genesis 1156 }
experimental-genesis 1157 dd_mutex.unlock();
experimental-genesis 1158 }
experimental-genesis 1159
experimental-genesis 1160 static void pop_lock()
experimental-genesis 1161 {
experimental-genesis 1162 if (fDebug)
experimental-genesis 1163 {
experimental-genesis 1164 const CLockLocation& locklocation = (*lockstack).rbegin()->second;
experimental-genesis 1165 printf("Unlocked: %s\n", locklocation.ToString().c_str());
experimental-genesis 1166 }
experimental-genesis 1167 dd_mutex.lock();
experimental-genesis 1168 (*lockstack).pop_back();
experimental-genesis 1169 dd_mutex.unlock();
experimental-genesis 1170 }
experimental-genesis 1171
experimental-genesis 1172 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
experimental-genesis 1173 {
experimental-genesis 1174 push_lock(this, CLockLocation(pszName, pszFile, nLine));
experimental-genesis 1175 mutex.lock();
experimental-genesis 1176 }
experimental-genesis 1177 void CCriticalSection::Leave()
experimental-genesis 1178 {
experimental-genesis 1179 mutex.unlock();
experimental-genesis 1180 pop_lock();
experimental-genesis 1181 }
experimental-genesis 1182 bool CCriticalSection::TryEnter(const char* pszName, const char* pszFile, int nLine)
experimental-genesis 1183 {
experimental-genesis 1184 push_lock(this, CLockLocation(pszName, pszFile, nLine));
experimental-genesis 1185 bool result = mutex.try_lock();
experimental-genesis 1186 if (!result) pop_lock();
experimental-genesis 1187 return result;
experimental-genesis 1188 }
experimental-genesis 1189
experimental-genesis 1190 #else
experimental-genesis 1191
experimental-genesis 1192 void CCriticalSection::Enter(const char*, const char*, int)
experimental-genesis 1193 {
experimental-genesis 1194 mutex.lock();
experimental-genesis 1195 }
experimental-genesis 1196
experimental-genesis 1197 void CCriticalSection::Leave()
experimental-genesis 1198 {
experimental-genesis 1199 mutex.unlock();
experimental-genesis 1200 }
experimental-genesis 1201
experimental-genesis 1202 bool CCriticalSection::TryEnter(const char*, const char*, int)
experimental-genesis 1203 {
experimental-genesis 1204 bool result = mutex.try_lock();
experimental-genesis 1205 return result;
experimental-genesis 1206 }
experimental-genesis 1207
experimental-genesis 1208 #endif /* DEBUG_LOCKORDER */