From dd519206a684c772a4a06ceecc87c665ad09d8be Mon Sep 17 00:00:00 2001 From: s_nakamoto Date: Thu, 29 Oct 2009 02:52:48 +0000 Subject: addr relaying fixes, proxy option and privacy patches, detect connect to self, non-final tx locktime changes, fix hide unconfirmed generated git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@18 1a98c847-1fd6-4fd8-948a-caf3550aa51b --- util.cpp | 170 ++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 119 insertions(+), 51 deletions(-) (limited to 'util.cpp') diff --git a/util.cpp b/util.cpp index 8271474ee..6ba84a516 100644 --- a/util.cpp +++ b/util.cpp @@ -5,8 +5,9 @@ #include "headers.h" - bool fDebug = false; +bool fPrintToDebugger = false; +bool fPrintToConsole = false; @@ -37,8 +38,8 @@ public: // Seed random number generator with screen scrape and other hardware sources RAND_screen(); - // Seed random number generator with perfmon data - RandAddSeed(true); + // Seed random number generator with performance counter + RandAddSeed(); } ~CInit() { @@ -54,40 +55,43 @@ instance_of_cinit; -void RandAddSeed(bool fPerfmon) +void RandAddSeed() { // Seed with CPU performance counter LARGE_INTEGER PerformanceCount; QueryPerformanceCounter(&PerformanceCount); RAND_add(&PerformanceCount, sizeof(PerformanceCount), 1.5); memset(&PerformanceCount, 0, sizeof(PerformanceCount)); +} +void RandAddSeedPerfmon() +{ + // This can take up to 2 seconds, so only do it every 10 minutes static int64 nLastPerfmon; - if (fPerfmon || GetTime() > nLastPerfmon + 5 * 60) + if (GetTime() < nLastPerfmon + 10 * 60) + return; + nLastPerfmon = GetTime(); + + // Seed with the entire set of perfmon data + unsigned char pdata[250000]; + memset(pdata, 0, sizeof(pdata)); + unsigned long nSize = sizeof(pdata); + long ret = RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); + RegCloseKey(HKEY_PERFORMANCE_DATA); + if (ret == ERROR_SUCCESS) { - nLastPerfmon = GetTime(); - - // Seed with the entire set of perfmon data - unsigned char pdata[250000]; - memset(pdata, 0, sizeof(pdata)); - unsigned long nSize = sizeof(pdata); - long ret = RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); - RegCloseKey(HKEY_PERFORMANCE_DATA); - if (ret == ERROR_SUCCESS) - { - uint256 hash; - SHA256(pdata, nSize, (unsigned char*)&hash); - RAND_add(&hash, sizeof(hash), min(nSize/500.0, (double)sizeof(hash))); - hash = 0; - memset(pdata, 0, nSize); - - time_t nTime; - time(&nTime); - struct tm* ptmTime = gmtime(&nTime); - char pszTime[200]; - strftime(pszTime, sizeof(pszTime), "%x %H:%M:%S", ptmTime); - printf("%s RandAddSeed() %d bytes\n", pszTime, nSize); - } + uint256 hash; + SHA256(pdata, nSize, (unsigned char*)&hash); + RAND_add(&hash, sizeof(hash), min(nSize/500.0, (double)sizeof(hash))); + hash = 0; + memset(pdata, 0, nSize); + + time_t nTime; + time(&nTime); + struct tm* ptmTime = gmtime(&nTime); + char pszTime[200]; + strftime(pszTime, sizeof(pszTime), "%x %H:%M:%S", ptmTime); + printf("%s RandAddSeed() %d bytes\n", pszTime, nSize); } } @@ -99,7 +103,6 @@ void RandAddSeed(bool fPerfmon) - // Safer snprintf // - prints up to limit-1 characters // - output string is always null terminated even if limit reached @@ -172,27 +175,6 @@ bool error(const char* format, ...) } -void PrintException(std::exception* pex, const char* pszThread) -{ - char pszModule[MAX_PATH]; - pszModule[0] = '\0'; - GetModuleFileName(NULL, pszModule, sizeof(pszModule)); - _strlwr(pszModule); - char pszMessage[1000]; - if (pex) - snprintf(pszMessage, sizeof(pszMessage), - "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); - else - snprintf(pszMessage, sizeof(pszMessage), - "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); - printf("\n\n************************\n%s", pszMessage); - if (wxTheApp) - wxMessageBox(pszMessage, "Error", wxOK | wxICON_ERROR); - throw; - //DebugBreak(); -} - - void ParseString(const string& str, char c, vector& v) { unsigned int i1 = 0; @@ -268,6 +250,92 @@ bool ParseMoney(const char* pszIn, int64& nRet) } +vector ParseHex(const char* psz) +{ + vector vch; + while (isspace(*psz)) + psz++; + vch.reserve((strlen(psz)+1)/3); + + static char phexdigit[256] = + { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, + -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1 + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; + + while (*psz) + { + char c = phexdigit[(unsigned char)*psz++]; + if (c == -1) + break; + unsigned char n = (c << 4); + if (*psz) + { + char c = phexdigit[(unsigned char)*psz++]; + if (c == -1) + break; + n |= c; + vch.push_back(n); + } + while (isspace(*psz)) + psz++; + } + + return vch; +} + +vector ParseHex(const std::string& str) +{ + return ParseHex(str.c_str()); +} + + + + + + +void FormatException(char* pszMessage, std::exception* pex, const char* pszThread) +{ + char pszModule[MAX_PATH]; + pszModule[0] = '\0'; + GetModuleFileName(NULL, pszModule, sizeof(pszModule)); + if (pex) + snprintf(pszMessage, 1000, + "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); + else + snprintf(pszMessage, 1000, + "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); +} + +void LogException(std::exception* pex, const char* pszThread) +{ + char pszMessage[1000]; + FormatException(pszMessage, pex, pszThread); + printf("\n%s", pszMessage); +} + +void PrintException(std::exception* pex, const char* pszThread) +{ + char pszMessage[1000]; + FormatException(pszMessage, pex, pszThread); + printf("\n\n************************\n%s\n", pszMessage); + if (wxTheApp) + wxMessageBox(pszMessage, "Error", wxOK | wxICON_ERROR); + throw; + //DebugBreak(); +} @@ -363,7 +431,7 @@ void AddTimeData(unsigned int ip, int64 nTime) if (vTimeOffsets.empty()) vTimeOffsets.push_back(0); vTimeOffsets.push_back(nOffsetSample); - printf("Added time data, samples %d, ip %08x, offset %+I64d (%+I64d minutes)\n", vTimeOffsets.size(), ip, vTimeOffsets.back(), vTimeOffsets.back()/60); + printf("Added time data, samples %d, offset %+I64d (%+I64d minutes)\n", vTimeOffsets.size(), vTimeOffsets.back(), vTimeOffsets.back()/60); if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) { sort(vTimeOffsets.begin(), vTimeOffsets.end()); -- cgit v1.2.3