1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 #include <boost/algorithm/string/join.hpp>
8 #include <boost/program_options/detail/config_file.hpp>
9 #include <boost/program_options/parsers.hpp>
10 #include <boost/filesystem.hpp>
11 #include <boost/filesystem/fstream.hpp>
12 #include <boost/interprocess/sync/interprocess_mutex.hpp>
13 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
14 #include <boost/foreach.hpp>
17 using namespace boost;
19 map<string, string> mapArgs;
20 map<string, vector<string> > mapMultiArgs;
22 bool fPrintToConsole = false;
23 bool fPrintToDebugger = false;
24 char pszSetDataDir[MAX_PATH] = "";
25 bool fRequestShutdown = false;
26 bool fShutdown = false;
29 bool fCommandLine = false;
30 string strMiscWarning;
31 bool fTestNet = false;
32 bool fNoListen = false;
33 bool fLogTimestamps = false;
34 CMedianFilter<int64> vTimeOffsets(200,0);
38 // Workaround for "multiple definition of `_tls_used'"
39 // http://svn.boost.org/trac/boost/ticket/4258
40 extern "C" void tss_cleanup_implemented() { }
46 // Init openssl library multithreading support
47 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
48 void locking_callback(int mode, int i, const char* file, int line)
50 if (mode & CRYPTO_LOCK)
51 ppmutexOpenSSL[i]->lock();
53 ppmutexOpenSSL[i]->unlock();
62 // Init openssl library multithreading support
63 ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
64 for (int i = 0; i < CRYPTO_num_locks(); i++)
65 ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
66 CRYPTO_set_locking_callback(locking_callback);
69 // Seed random number generator with screen scrape and other hardware sources
73 // Seed random number generator with performance counter
78 // Shutdown openssl library multithreading support
79 CRYPTO_set_locking_callback(NULL);
80 for (int i = 0; i < CRYPTO_num_locks(); i++)
81 delete ppmutexOpenSSL[i];
82 OPENSSL_free(ppmutexOpenSSL);
96 // Seed with CPU performance counter
97 int64 nCounter = GetPerformanceCounter();
98 RAND_add(&nCounter, sizeof(nCounter), 1.5);
99 memset(&nCounter, 0, sizeof(nCounter));
102 void RandAddSeedPerfmon()
106 // This can take up to 2 seconds, so only do it every 10 minutes
107 static int64 nLastPerfmon;
108 if (GetTime() < nLastPerfmon + 10 * 60)
110 nLastPerfmon = GetTime();
113 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
114 // Seed with the entire set of perfmon data
115 unsigned char pdata[250000];
116 memset(pdata, 0, sizeof(pdata));
117 unsigned long nSize = sizeof(pdata);
118 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
119 RegCloseKey(HKEY_PERFORMANCE_DATA);
120 if (ret == ERROR_SUCCESS)
122 RAND_add(pdata, nSize, nSize/100.0);
123 memset(pdata, 0, nSize);
124 printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
129 uint64 GetRand(uint64 nMax)
134 // The range of the random source must be a multiple of the modulus
135 // to give every possible output value an equal possibility
136 uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
139 RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
140 while (nRand >= nRange);
141 return (nRand % nMax);
144 int GetRandInt(int nMax)
146 return GetRand(nMax);
159 inline int OutputDebugStringF(const char* pszFormat, ...)
166 va_start(arg_ptr, pszFormat);
167 ret = vprintf(pszFormat, arg_ptr);
172 // print to debug.log
173 static FILE* fileout = NULL;
177 char pszFile[MAX_PATH+100];
179 strlcat(pszFile, "/debug.log", sizeof(pszFile));
180 fileout = fopen(pszFile, "a");
181 if (fileout) setbuf(fileout, NULL); // unbuffered
185 static bool fStartedNewLine = true;
187 // Debug print useful for profiling
188 if (fLogTimestamps && fStartedNewLine)
189 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
190 if (pszFormat[strlen(pszFormat) - 1] == '\n')
191 fStartedNewLine = true;
193 fStartedNewLine = false;
196 va_start(arg_ptr, pszFormat);
197 ret = vfprintf(fileout, pszFormat, arg_ptr);
203 if (fPrintToDebugger)
205 static CCriticalSection cs_OutputDebugStringF;
207 // accumulate a line at a time
208 CRITICAL_BLOCK(cs_OutputDebugStringF)
210 static char pszBuffer[50000];
215 va_start(arg_ptr, pszFormat);
216 int limit = END(pszBuffer) - pend - 2;
217 int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
219 if (ret < 0 || ret >= limit)
221 pend = END(pszBuffer) - 2;
227 char* p1 = pszBuffer;
229 while (p2 = strchr(p1, '\n'))
234 OutputDebugStringA(p1);
239 memmove(pszBuffer, p1, pend - p1 + 1);
240 pend -= (p1 - pszBuffer);
249 // - prints up to limit-1 characters
250 // - output string is always null terminated even if limit reached
251 // - return value is the number of characters actually printed
252 int my_snprintf(char* buffer, size_t limit, const char* format, ...)
257 va_start(arg_ptr, format);
258 int ret = _vsnprintf(buffer, limit, format, arg_ptr);
260 if (ret < 0 || ret >= limit)
268 string strprintf(const std::string &format, ...)
272 int limit = sizeof(buffer);
277 va_start(arg_ptr, format);
278 ret = _vsnprintf(p, limit, format.c_str(), arg_ptr);
280 if (ret >= 0 && ret < limit)
287 throw std::bad_alloc();
289 string str(p, p+ret);
295 bool error(const std::string &format, ...)
298 int limit = sizeof(buffer);
300 va_start(arg_ptr, format);
301 int ret = _vsnprintf(buffer, limit, format.c_str(), arg_ptr);
303 if (ret < 0 || ret >= limit)
308 printf("ERROR: %s\n", buffer);
313 void ParseString(const string& str, char c, vector<string>& v)
317 string::size_type i1 = 0;
318 string::size_type i2;
321 i2 = str.find(c, i1);
324 v.push_back(str.substr(i1));
327 v.push_back(str.substr(i1, i2-i1));
333 string FormatMoney(int64 n, bool fPlus)
335 // Note: not using straight sprintf here because we do NOT want
336 // localized number formatting.
337 int64 n_abs = (n > 0 ? n : -n);
338 int64 quotient = n_abs/COIN;
339 int64 remainder = n_abs%COIN;
340 string str = strprintf("%"PRI64d".%08"PRI64d, quotient, remainder);
342 // Right-trim excess 0's before the decimal point:
344 for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
347 str.erase(str.size()-nTrim, nTrim);
350 str.insert((unsigned int)0, 1, '-');
351 else if (fPlus && n > 0)
352 str.insert((unsigned int)0, 1, '+');
357 bool ParseMoney(const string& str, int64& nRet)
359 return ParseMoney(str.c_str(), nRet);
362 bool ParseMoney(const char* pszIn, int64& nRet)
366 const char* p = pszIn;
374 int64 nMult = CENT*10;
375 while (isdigit(*p) && (nMult > 0))
377 nUnits += nMult * (*p++ - '0');
386 strWhole.insert(strWhole.end(), *p);
391 if (strWhole.size() > 10) // guard against 63 bit overflow
393 if (nUnits < 0 || nUnits > COIN)
395 int64 nWhole = atoi64(strWhole);
396 int64 nValue = nWhole*COIN + nUnits;
403 static char phexdigit[256] =
404 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
405 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
406 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
407 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
408 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
410 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1
411 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
412 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
413 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
414 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
415 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
416 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
417 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
418 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
419 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
421 bool IsHex(const string& str)
423 BOOST_FOREACH(unsigned char c, str)
425 if (phexdigit[c] < 0)
428 return (str.size() > 0) && (str.size()%2 == 0);
431 vector<unsigned char> ParseHex(const char* psz)
433 // convert hex dump to vector
434 vector<unsigned char> vch;
437 while (isspace(*psz))
439 char c = phexdigit[(unsigned char)*psz++];
442 unsigned char n = (c << 4);
443 c = phexdigit[(unsigned char)*psz++];
452 vector<unsigned char> ParseHex(const string& str)
454 return ParseHex(str.c_str());
457 void ParseParameters(int argc, const char*const argv[])
460 mapMultiArgs.clear();
461 for (int i = 1; i < argc; i++)
464 strlcpy(psz, argv[i], sizeof(psz));
465 char* pszValue = (char*)"";
466 if (strchr(psz, '='))
468 pszValue = strchr(psz, '=');
479 mapArgs[psz] = pszValue;
480 mapMultiArgs[psz].push_back(pszValue);
484 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
486 string name = entry.first;
488 // interpret --foo as -foo (as long as both are not set)
489 if (name.find("--") == 0)
491 std::string singleDash(name.begin()+1, name.end());
492 if (mapArgs.count(singleDash) == 0)
493 mapArgs[singleDash] = entry.second;
497 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1, as long as -foo not set)
498 if (name.find("-no") == 0)
500 std::string positive("-");
501 positive.append(name.begin()+3, name.end());
502 if (mapArgs.count(positive) == 0)
504 bool value = !GetBoolArg(name);
505 mapArgs[positive] = (value ? "1" : "0");
511 std::string GetArg(const std::string& strArg, const std::string& strDefault)
513 if (mapArgs.count(strArg))
514 return mapArgs[strArg];
518 int64 GetArg(const std::string& strArg, int64 nDefault)
520 if (mapArgs.count(strArg))
521 return atoi64(mapArgs[strArg]);
525 bool GetBoolArg(const std::string& strArg, bool fDefault)
527 if (mapArgs.count(strArg))
529 if (mapArgs[strArg].empty())
531 return (atoi(mapArgs[strArg]) != 0);
536 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
538 if (mapArgs.count(strArg))
540 mapArgs[strArg] = strValue;
544 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
547 return SoftSetArg(strArg, std::string("1"));
549 return SoftSetArg(strArg, std::string("0"));
553 string EncodeBase64(const unsigned char* pch, size_t len)
555 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
558 strRet.reserve((len+2)/3*4);
561 const unsigned char *pchEnd = pch+len;
568 case 0: // we have no bits
569 strRet += pbase64[enc >> 2];
570 left = (enc & 3) << 4;
574 case 1: // we have two bits
575 strRet += pbase64[left | (enc >> 4)];
576 left = (enc & 15) << 2;
580 case 2: // we have four bits
581 strRet += pbase64[left | (enc >> 6)];
582 strRet += pbase64[enc & 63];
590 strRet += pbase64[left];
599 string EncodeBase64(const string& str)
601 return EncodeBase64((const unsigned char*)str.c_str(), str.size());
604 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
606 static const int decode64_table[256] =
608 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
609 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
610 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
611 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
612 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
613 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
614 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
615 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
616 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
617 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
618 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
619 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
620 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
626 vector<unsigned char> vchRet;
627 vchRet.reserve(strlen(p)*3/4);
634 int dec = decode64_table[*p];
635 if (dec == -1) break;
639 case 0: // we have no bits and get 6
644 case 1: // we have 6 bits and keep 4
645 vchRet.push_back((left<<2) | (dec>>4));
650 case 2: // we have 4 bits and get 6, we keep 2
651 vchRet.push_back((left<<4) | (dec>>2));
656 case 3: // we have 2 bits and get 6
657 vchRet.push_back((left<<6) | dec);
666 case 0: // 4n base64 characters processed: ok
669 case 1: // 4n+1 base64 character processed: impossible
673 case 2: // 4n+2 base64 characters processed: require '=='
674 if (left || p[0] != '=' || p[1] != '=' || decode64_table[p[2]] != -1)
678 case 3: // 4n+3 base64 characters processed: require '='
679 if (left || p[0] != '=' || decode64_table[p[1]] != -1)
687 string DecodeBase64(const string& str)
689 vector<unsigned char> vchRet = DecodeBase64(str.c_str());
690 return string((const char*)&vchRet[0], vchRet.size());
694 bool WildcardMatch(const char* psz, const char* mask)
701 return (*psz == '\0');
703 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
718 bool WildcardMatch(const string& str, const string& mask)
720 return WildcardMatch(str.c_str(), mask.c_str());
730 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
733 char pszModule[MAX_PATH];
735 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
737 const char* pszModule = "bitcoin";
740 snprintf(pszMessage, 1000,
741 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
743 snprintf(pszMessage, 1000,
744 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
747 void LogException(std::exception* pex, const char* pszThread)
749 char pszMessage[10000];
750 FormatException(pszMessage, pex, pszThread);
751 printf("\n%s", pszMessage);
754 void PrintException(std::exception* pex, const char* pszThread)
756 char pszMessage[10000];
757 FormatException(pszMessage, pex, pszThread);
758 printf("\n\n************************\n%s\n", pszMessage);
759 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
760 strMiscWarning = pszMessage;
764 void ThreadOneMessageBox(string strMessage)
766 // Skip message boxes if one is already open
767 static bool fMessageBoxOpen;
770 fMessageBoxOpen = true;
771 ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
772 fMessageBoxOpen = false;
775 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
777 char pszMessage[10000];
778 FormatException(pszMessage, pex, pszThread);
779 printf("\n\n************************\n%s\n", pszMessage);
780 fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
781 strMiscWarning = pszMessage;
792 typedef WINSHELLAPI BOOL (WINAPI *PSHGETSPECIALFOLDERPATHA)(HWND hwndOwner, LPSTR lpszPath, int nFolder, BOOL fCreate);
794 string MyGetSpecialFolderPath(int nFolder, bool fCreate)
796 char pszPath[MAX_PATH+100] = "";
798 // SHGetSpecialFolderPath isn't always available on old Windows versions
799 HMODULE hShell32 = LoadLibraryA("shell32.dll");
802 PSHGETSPECIALFOLDERPATHA pSHGetSpecialFolderPath =
803 (PSHGETSPECIALFOLDERPATHA)GetProcAddress(hShell32, "SHGetSpecialFolderPathA");
804 if (pSHGetSpecialFolderPath)
805 (*pSHGetSpecialFolderPath)(NULL, pszPath, nFolder, fCreate);
806 FreeModule(hShell32);
810 if (pszPath[0] == '\0')
812 if (nFolder == CSIDL_STARTUP)
814 strcpy(pszPath, getenv("USERPROFILE"));
815 strcat(pszPath, "\\Start Menu\\Programs\\Startup");
817 else if (nFolder == CSIDL_APPDATA)
819 strcpy(pszPath, getenv("APPDATA"));
827 string GetDefaultDataDir()
829 // Windows: C:\Documents and Settings\username\Application Data\Bitcoin
830 // Mac: ~/Library/Application Support/Bitcoin
834 return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin";
836 char* pszHome = getenv("HOME");
837 if (pszHome == NULL || strlen(pszHome) == 0)
838 pszHome = (char*)"/";
839 string strHome = pszHome;
840 if (strHome[strHome.size()-1] != '/')
844 strHome += "Library/Application Support/";
845 filesystem::create_directory(strHome.c_str());
846 return strHome + "Bitcoin";
849 return strHome + ".bitcoin";
854 void GetDataDir(char* pszDir)
856 // pszDir must be at least MAX_PATH length.
858 if (pszSetDataDir[0] != 0)
860 strlcpy(pszDir, pszSetDataDir, MAX_PATH);
865 // This can be called during exceptions by printf, so we cache the
866 // value so we don't have to do memory allocations after that.
867 static char pszCachedDir[MAX_PATH];
868 if (pszCachedDir[0] == 0)
869 strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir));
870 strlcpy(pszDir, pszCachedDir, MAX_PATH);
875 char* p = pszDir + strlen(pszDir);
876 if (p > pszDir && p[-1] != '/' && p[-1] != '\\')
878 strcpy(p, "testnet");
881 static bool pfMkdir[4];
882 if (!pfMkdir[nVariation])
884 pfMkdir[nVariation] = true;
885 boost::filesystem::create_directory(pszDir);
891 char pszDir[MAX_PATH];
896 string GetConfigFile()
898 namespace fs = boost::filesystem;
899 fs::path pathConfig(GetArg("-conf", "bitcoin.conf"));
900 if (!pathConfig.is_complete())
901 pathConfig = fs::path(GetDataDir()) / pathConfig;
902 return pathConfig.string();
905 void ReadConfigFile(map<string, string>& mapSettingsRet,
906 map<string, vector<string> >& mapMultiSettingsRet)
908 namespace fs = boost::filesystem;
909 namespace pod = boost::program_options::detail;
911 fs::ifstream streamConfig(GetConfigFile());
912 if (!streamConfig.good())
915 set<string> setOptions;
916 setOptions.insert("*");
918 for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
920 // Don't overwrite existing settings so command line settings override bitcoin.conf
921 string strKey = string("-") + it->string_key;
922 if (mapSettingsRet.count(strKey) == 0)
923 mapSettingsRet[strKey] = it->value[0];
924 mapMultiSettingsRet[strKey].push_back(it->value[0]);
930 namespace fs = boost::filesystem;
931 fs::path pathConfig(GetArg("-pid", "bitcoind.pid"));
932 if (!pathConfig.is_complete())
933 pathConfig = fs::path(GetDataDir()) / pathConfig;
934 return pathConfig.string();
937 void CreatePidFile(string pidFile, pid_t pid)
939 FILE* file = fopen(pidFile.c_str(), "w");
942 fprintf(file, "%d\n", pid);
947 int GetFilesize(FILE* file)
949 int nSavePos = ftell(file);
951 if (fseek(file, 0, SEEK_END) == 0)
952 nFilesize = ftell(file);
953 fseek(file, nSavePos, SEEK_SET);
957 void ShrinkDebugFile()
959 // Scroll debug.log if it's getting too big
960 string strFile = GetDataDir() + "/debug.log";
961 FILE* file = fopen(strFile.c_str(), "r");
962 if (file && GetFilesize(file) > 10 * 1000000)
964 // Restart the file with some of the end
966 fseek(file, -sizeof(pch), SEEK_END);
967 int nBytes = fread(pch, 1, sizeof(pch), file);
970 file = fopen(strFile.c_str(), "w");
973 fwrite(pch, 1, nBytes, file);
987 // "Never go to sea with two chronometers; take one or three."
988 // Our three time sources are:
990 // - Median of other nodes's clocks
991 // - The user (asking the user to fix the system clock if the first two disagree)
993 static int64 nMockTime = 0; // For unit testing
997 if (nMockTime) return nMockTime;
1002 void SetMockTime(int64 nMockTimeIn)
1004 nMockTime = nMockTimeIn;
1007 static int64 nTimeOffset = 0;
1009 int64 GetAdjustedTime()
1011 return GetTime() + nTimeOffset;
1014 void AddTimeData(const CNetAddr& ip, int64 nTime)
1016 int64 nOffsetSample = nTime - GetTime();
1018 // Ignore duplicates
1019 static set<CNetAddr> setKnown;
1020 if (!setKnown.insert(ip).second)
1024 vTimeOffsets.input(nOffsetSample);
1025 printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
1026 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
1028 int64 nMedian = vTimeOffsets.median();
1029 std::vector<int64> vSorted = vTimeOffsets.sorted();
1030 // Only let other nodes change our time by so much
1031 if (abs64(nMedian) < 70 * 60)
1033 nTimeOffset = nMedian;
1042 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
1043 bool fMatch = false;
1044 BOOST_FOREACH(int64 nOffset, vSorted)
1045 if (nOffset != 0 && abs64(nOffset) < 5 * 60)
1051 string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly.");
1052 strMiscWarning = strMessage;
1053 printf("*** %s\n", strMessage.c_str());
1054 boost::thread(boost::bind(ThreadSafeMessageBox, strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION, (wxWindow*)NULL, -1, -1));
1059 BOOST_FOREACH(int64 n, vSorted)
1060 printf("%+"PRI64d" ", n);
1063 printf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
1074 string FormatVersion(int nVersion)
1076 if (nVersion%100 == 0)
1077 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1079 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1082 string FormatFullVersion()
1084 string s = FormatVersion(CLIENT_VERSION);
1085 if (VERSION_IS_BETA) {
1092 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
1093 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
1095 std::ostringstream ss;
1097 ss << name << ":" << FormatVersion(nClientVersion);
1098 if (!comments.empty())
1099 ss << "(" << boost::algorithm::join(comments, "; ") << ")";
1106 #ifdef DEBUG_LOCKORDER
1108 // Early deadlock detection.
1109 // Problem being solved:
1110 // Thread 1 locks A, then B, then C
1111 // Thread 2 locks D, then C, then A
1112 // --> may result in deadlock between the two threads, depending on when they run.
1113 // Solution implemented here:
1114 // Keep track of pairs of locks: (A before B), (A before C), etc.
1115 // Complain if any thread trys to lock in a different order.
1118 struct CLockLocation
1120 CLockLocation(const char* pszName, const char* pszFile, int nLine)
1122 mutexName = pszName;
1123 sourceFile = pszFile;
1127 std::string ToString() const
1129 return mutexName+" "+sourceFile+":"+itostr(sourceLine);
1133 std::string mutexName;
1134 std::string sourceFile;
1138 typedef std::vector< std::pair<CCriticalSection*, CLockLocation> > LockStack;
1140 static boost::interprocess::interprocess_mutex dd_mutex;
1141 static std::map<std::pair<CCriticalSection*, CCriticalSection*>, LockStack> lockorders;
1142 static boost::thread_specific_ptr<LockStack> lockstack;
1145 static void potential_deadlock_detected(const std::pair<CCriticalSection*, CCriticalSection*>& mismatch, const LockStack& s1, const LockStack& s2)
1147 printf("POTENTIAL DEADLOCK DETECTED\n");
1148 printf("Previous lock order was:\n");
1149 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s2)
1151 if (i.first == mismatch.first) printf(" (1)");
1152 if (i.first == mismatch.second) printf(" (2)");
1153 printf(" %s\n", i.second.ToString().c_str());
1155 printf("Current lock order is:\n");
1156 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s1)
1158 if (i.first == mismatch.first) printf(" (1)");
1159 if (i.first == mismatch.second) printf(" (2)");
1160 printf(" %s\n", i.second.ToString().c_str());
1164 static void push_lock(CCriticalSection* c, const CLockLocation& locklocation)
1166 bool fOrderOK = true;
1167 if (lockstack.get() == NULL)
1168 lockstack.reset(new LockStack);
1170 if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
1173 (*lockstack).push_back(std::make_pair(c, locklocation));
1175 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, (*lockstack))
1177 if (i.first == c) break;
1179 std::pair<CCriticalSection*, CCriticalSection*> p1 = std::make_pair(i.first, c);
1180 if (lockorders.count(p1))
1182 lockorders[p1] = (*lockstack);
1184 std::pair<CCriticalSection*, CCriticalSection*> p2 = std::make_pair(c, i.first);
1185 if (lockorders.count(p2))
1187 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
1194 static void pop_lock()
1198 const CLockLocation& locklocation = (*lockstack).rbegin()->second;
1199 printf("Unlocked: %s\n", locklocation.ToString().c_str());
1202 (*lockstack).pop_back();
1206 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
1208 push_lock(this, CLockLocation(pszName, pszFile, nLine));
1209 #ifdef DEBUG_LOCKCONTENTION
1210 bool result = mutex.try_lock();
1213 printf("LOCKCONTENTION: %s\n", pszName);
1214 printf("Locker: %s:%d\n", pszFile, nLine);
1222 void CCriticalSection::Leave()
1227 bool CCriticalSection::TryEnter(const char* pszName, const char* pszFile, int nLine)
1229 push_lock(this, CLockLocation(pszName, pszFile, nLine));
1230 bool result = mutex.try_lock();
1231 if (!result) pop_lock();
1237 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine)
1239 #ifdef DEBUG_LOCKCONTENTION
1240 bool result = mutex.try_lock();
1243 printf("LOCKCONTENTION: %s\n", pszName);
1244 printf("Locker: %s:%d\n", pszFile, nLine);
1252 void CCriticalSection::Leave()
1257 bool CCriticalSection::TryEnter(const char*, const char*, int)
1259 bool result = mutex.try_lock();
1263 #endif /* DEBUG_LOCKORDER */