Browse Source

simplify AddAddress,

readcompactsize limit, 
fixed a 64-bit compile error in serialize.h, 
change status "# blocks" to "# confirmations" and widen the column.

git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@53 1a98c847-1fd6-4fd8-948a-caf3550aa51b
miguelfreitas
s_nakamoto 15 years ago
parent
commit
cb0f89646f
  1. 4
      db.cpp
  2. 3
      irc.cpp
  3. 8
      main.cpp
  4. 10
      net.cpp
  5. 4
      net.h
  6. 16
      serialize.h
  7. 17
      ui.cpp
  8. 2
      uibase.h
  9. 4
      uiproject.fbp
  10. 77
      util.cpp
  11. 101
      util.h

4
db.cpp

@ -134,8 +134,6 @@ void CDB::Close()
CRITICAL_BLOCK(cs_db) CRITICAL_BLOCK(cs_db)
--mapFileUseCount[strFile]; --mapFileUseCount[strFile];
RandAddSeed();
} }
void CloseDb(const string& strFile) void CloseDb(const string& strFile)
@ -456,7 +454,7 @@ bool CAddrDB::LoadAddresses()
CAddress addr(psz, NODE_NETWORK); CAddress addr(psz, NODE_NETWORK);
addr.nTime = 0; // so it won't relay unless successfully connected addr.nTime = 0; // so it won't relay unless successfully connected
if (addr.IsValid()) if (addr.IsValid())
AddAddress(*this, addr); AddAddress(addr);
} }
} }
catch (...) { } catch (...) { }

3
irc.cpp

@ -265,8 +265,7 @@ void ThreadIRCSeed(void* parg)
if (DecodeAddress(pszName, addr)) if (DecodeAddress(pszName, addr))
{ {
addr.nTime = GetAdjustedTime() - 51 * 60; addr.nTime = GetAdjustedTime() - 51 * 60;
CAddrDB addrdb; if (AddAddress(addr))
if (AddAddress(addrdb, addr))
printf("IRC got new address\n"); printf("IRC got new address\n");
nGotIRCAddresses++; nGotIRCAddresses++;
} }

8
main.cpp

@ -1734,6 +1734,11 @@ bool ProcessMessages(CNode* pfrom)
// Allow exceptions from underlength message on vRecv // Allow exceptions from underlength message on vRecv
printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what()); printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught, normally caused by a message being shorter than its stated length\n", strCommand.c_str(), nMessageSize, e.what());
} }
else if (strstr(e.what(), ": size too large"))
{
// Allow exceptions from overlong size
printf("ProcessMessage(%s, %d bytes) : Exception '%s' caught\n", strCommand.c_str(), nMessageSize, e.what());
}
else else
{ {
PrintException(&e, "ProcessMessage()"); PrintException(&e, "ProcessMessage()");
@ -1840,7 +1845,6 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
return error("message addr size() = %d", vAddr.size()); return error("message addr size() = %d", vAddr.size());
// Store the new addresses // Store the new addresses
CAddrDB addrdb;
foreach(CAddress& addr, vAddr) foreach(CAddress& addr, vAddr)
{ {
if (fShutdown) if (fShutdown)
@ -1848,7 +1852,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
addr.nTime = GetAdjustedTime() - 2 * 60 * 60; addr.nTime = GetAdjustedTime() - 2 * 60 * 60;
if (pfrom->fGetAddr) if (pfrom->fGetAddr)
addr.nTime -= 5 * 24 * 60 * 60; addr.nTime -= 5 * 24 * 60 * 60;
AddAddress(addrdb, addr, false); AddAddress(addr, false);
pfrom->AddAddressKnown(addr); pfrom->AddAddressKnown(addr);
if (!pfrom->fGetAddr && addr.IsRoutable()) if (!pfrom->fGetAddr && addr.IsRoutable())
{ {

10
net.cpp

@ -223,7 +223,7 @@ bool GetMyExternalIP(unsigned int& ipRet)
bool AddAddress(CAddrDB& addrdb, CAddress addr, bool fCurrentlyOnline) bool AddAddress(CAddress addr, bool fCurrentlyOnline)
{ {
if (!addr.IsRoutable()) if (!addr.IsRoutable())
return false; return false;
@ -239,7 +239,7 @@ bool AddAddress(CAddrDB& addrdb, CAddress addr, bool fCurrentlyOnline)
// New address // New address
printf("AddAddress(%s)\n", addr.ToStringLog().c_str()); printf("AddAddress(%s)\n", addr.ToStringLog().c_str());
mapAddresses.insert(make_pair(addr.GetKey(), addr)); mapAddresses.insert(make_pair(addr.GetKey(), addr));
addrdb.WriteAddress(addr); CAddrDB().WriteAddress(addr);
return true; return true;
} }
else else
@ -260,7 +260,7 @@ bool AddAddress(CAddrDB& addrdb, CAddress addr, bool fCurrentlyOnline)
fUpdated = true; fUpdated = true;
} }
if (fUpdated) if (fUpdated)
addrdb.WriteAddress(addrFound); CAddrDB().WriteAddress(addrFound);
} }
} }
return false; return false;
@ -881,11 +881,11 @@ void ThreadOpenConnections2(void* parg)
vnThreadsRunning[1]--; vnThreadsRunning[1]--;
Sleep(500); Sleep(500);
const int nMaxConnections = 15; const int nMaxConnections = 15;
while (vNodes.size() >= nMaxConnections || vNodes.size() >= mapAddresses.size()) while (vNodes.size() >= nMaxConnections)
{ {
Sleep(2000);
if (fShutdown) if (fShutdown)
return; return;
Sleep(2000);
} }
vnThreadsRunning[1]++; vnThreadsRunning[1]++;
if (fShutdown) if (fShutdown)

4
net.h

@ -23,7 +23,7 @@ enum
bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet); bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet);
bool GetMyExternalIP(unsigned int& ipRet); bool GetMyExternalIP(unsigned int& ipRet);
bool AddAddress(CAddrDB& addrdb, CAddress addr, bool fCurrentlyOnline=true); bool AddAddress(CAddress addr, bool fCurrentlyOnline=true);
void AddressCurrentlyConnected(const CAddress& addr); void AddressCurrentlyConnected(const CAddress& addr);
CNode* FindNode(unsigned int ip); CNode* FindNode(unsigned int ip);
CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0); CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
@ -627,7 +627,7 @@ public:
// We're using mapAskFor as a priority queue, // We're using mapAskFor as a priority queue,
// the key is the earliest time the request can be sent // the key is the earliest time the request can be sent
int64& nRequestTime = mapAlreadyAskedFor[inv]; int64& nRequestTime = mapAlreadyAskedFor[inv];
printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime); printf("askfor %s %"PRI64d"\n", inv.ToString().c_str(), nRequestTime);
// Make sure not to reuse time indexes to keep things in the same order // Make sure not to reuse time indexes to keep things in the same order
int64 nNow = (GetTime() - 1) * 1000000; int64 nNow = (GetTime() - 1) * 1000000;

16
serialize.h

@ -20,7 +20,7 @@ class CDataStream;
class CAutoFile; class CAutoFile;
static const int VERSION = 200; static const int VERSION = 200;
static const char* pszSubVer = " rc2"; static const char* pszSubVer = " test1";
@ -194,28 +194,32 @@ uint64 ReadCompactSize(Stream& is)
{ {
unsigned char chSize; unsigned char chSize;
READDATA(is, chSize); READDATA(is, chSize);
uint64 nSizeRet = 0;
if (chSize < UCHAR_MAX-2) if (chSize < UCHAR_MAX-2)
{ {
return chSize; nSizeRet = chSize;
} }
else if (chSize == UCHAR_MAX-2) else if (chSize == UCHAR_MAX-2)
{ {
unsigned short nSize; unsigned short nSize;
READDATA(is, nSize); READDATA(is, nSize);
return nSize; nSizeRet = nSize;
} }
else if (chSize == UCHAR_MAX-1) else if (chSize == UCHAR_MAX-1)
{ {
unsigned int nSize; unsigned int nSize;
READDATA(is, nSize); READDATA(is, nSize);
return nSize; nSizeRet = nSize;
} }
else else
{ {
uint64 nSize; uint64 nSize;
READDATA(is, nSize); READDATA(is, nSize);
return nSize; nSizeRet = nSize;
} }
if (nSizeRet > (uint64)INT_MAX)
throw std::ios_base::failure("ReadCompactSize() : size too large");
return nSizeRet;
} }
@ -460,7 +464,7 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion,
unsigned int i = 0; unsigned int i = 0;
while (i < nSize) while (i < nSize)
{ {
unsigned int blk = min(nSize - i, 1 + 4999999 / sizeof(T)); unsigned int blk = min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T)));
v.resize(i + blk); v.resize(i + blk);
is.read((char*)&v[i], blk * sizeof(T)); is.read((char*)&v[i], blk * sizeof(T));
i += blk; i += blk;

17
ui.cpp

@ -349,7 +349,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent)
nDateWidth += 12; nDateWidth += 12;
m_listCtrl->InsertColumn(0, "", wxLIST_FORMAT_LEFT, dResize * 0); m_listCtrl->InsertColumn(0, "", wxLIST_FORMAT_LEFT, dResize * 0);
m_listCtrl->InsertColumn(1, "", wxLIST_FORMAT_LEFT, dResize * 0); m_listCtrl->InsertColumn(1, "", wxLIST_FORMAT_LEFT, dResize * 0);
m_listCtrl->InsertColumn(2, "Status", wxLIST_FORMAT_LEFT, dResize * 90); m_listCtrl->InsertColumn(2, "Status", wxLIST_FORMAT_LEFT, dResize * 110);
m_listCtrl->InsertColumn(3, "Date", wxLIST_FORMAT_LEFT, dResize * nDateWidth); m_listCtrl->InsertColumn(3, "Date", wxLIST_FORMAT_LEFT, dResize * nDateWidth);
m_listCtrl->InsertColumn(4, "Description", wxLIST_FORMAT_LEFT, dResize * 409 - nDateWidth); m_listCtrl->InsertColumn(4, "Description", wxLIST_FORMAT_LEFT, dResize * 409 - nDateWidth);
m_listCtrl->InsertColumn(5, "Debit", wxLIST_FORMAT_RIGHT, dResize * 79); m_listCtrl->InsertColumn(5, "Debit", wxLIST_FORMAT_RIGHT, dResize * 79);
@ -579,7 +579,7 @@ string FormatTxStatus(const CWalletTx& wtx)
else if (nDepth < 6) else if (nDepth < 6)
return strprintf("%d/unconfirmed", nDepth); return strprintf("%d/unconfirmed", nDepth);
else else
return strprintf("%d blocks", nDepth); return strprintf("%d confirmations", nDepth);
} }
} }
@ -3706,13 +3706,12 @@ bool CMyApp::OnInit2()
if (mapArgs.count("-addnode")) if (mapArgs.count("-addnode"))
{ {
CAddrDB addrdb;
foreach(string strAddr, mapMultiArgs["-addnode"]) foreach(string strAddr, mapMultiArgs["-addnode"])
{ {
CAddress addr(strAddr, NODE_NETWORK); CAddress addr(strAddr, NODE_NETWORK);
addr.nTime = 0; // so it won't relay unless successfully connected addr.nTime = 0; // so it won't relay unless successfully connected
if (addr.IsValid()) if (addr.IsValid())
AddAddress(addrdb, addr); AddAddress(addr);
} }
} }
@ -3934,3 +3933,13 @@ void SetStartOnSystemStartup(bool fAutoStart)
bool GetStartOnSystemStartup() { return false; } bool GetStartOnSystemStartup() { return false; }
void SetStartOnSystemStartup(bool fAutoStart) { } void SetStartOnSystemStartup(bool fAutoStart) { }
#endif #endif

2
uibase.h

@ -158,7 +158,7 @@ class CMainFrameBase : public wxFrame
wxListCtrl* m_listCtrlOrdersSent; wxListCtrl* m_listCtrlOrdersSent;
wxListCtrl* m_listCtrlProductsSent; wxListCtrl* m_listCtrlProductsSent;
wxListCtrl* m_listCtrlOrdersReceived; wxListCtrl* m_listCtrlOrdersReceived;
CMainFrameBase( wxWindow* parent, wxWindowID id = wxID_MAINFRAME, const wxString& title = wxT("Bitcoin"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 705,484 ), long style = wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER|wxTAB_TRAVERSAL ); CMainFrameBase( wxWindow* parent, wxWindowID id = wxID_MAINFRAME, const wxString& title = wxT("Bitcoin"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 725,484 ), long style = wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER|wxTAB_TRAVERSAL );
~CMainFrameBase(); ~CMainFrameBase();
}; };

4
uiproject.fbp

@ -32,7 +32,7 @@
<property name="minimum_size"></property> <property name="minimum_size"></property>
<property name="name">CMainFrameBase</property> <property name="name">CMainFrameBase</property>
<property name="pos"></property> <property name="pos"></property>
<property name="size">705,484</property> <property name="size">725,484</property>
<property name="style">wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER</property> <property name="style">wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER</property>
<property name="subclass"></property> <property name="subclass"></property>
<property name="title">Bitcoin</property> <property name="title">Bitcoin</property>
@ -1737,7 +1737,7 @@
<event name="OnSetFocus"></event> <event name="OnSetFocus"></event>
<event name="OnSize"></event> <event name="OnSize"></event>
<event name="OnUpdateUI"></event> <event name="OnUpdateUI"></event>
<object class="wxBoxSizer" expanded="1"> <object class="wxBoxSizer" expanded="0">
<property name="minimum_size"></property> <property name="minimum_size"></property>
<property name="name">bSizer55</property> <property name="name">bSizer55</property>
<property name="orient">wxVERTICAL</property> <property name="orient">wxVERTICAL</property>

77
util.cpp

@ -8,8 +8,8 @@
map<string, string> mapArgs; map<string, string> mapArgs;
map<string, vector<string> > mapMultiArgs; map<string, vector<string> > mapMultiArgs;
bool fDebug = false; bool fDebug = false;
bool fPrintToDebugger = false;
bool fPrintToConsole = false; bool fPrintToConsole = false;
bool fPrintToDebugger = false;
char pszSetDataDir[MAX_PATH] = ""; char pszSetDataDir[MAX_PATH] = "";
bool fShutdown = false; bool fShutdown = false;
@ -75,6 +75,8 @@ void RandAddSeed()
void RandAddSeedPerfmon() void RandAddSeedPerfmon()
{ {
RandAddSeed();
// This can take up to 2 seconds, so only do it every 10 minutes // This can take up to 2 seconds, so only do it every 10 minutes
static int64 nLastPerfmon; static int64 nLastPerfmon;
if (GetTime() < nLastPerfmon + 10 * 60) if (GetTime() < nLastPerfmon + 10 * 60)
@ -129,6 +131,79 @@ uint64 GetRand(uint64 nMax)
inline int OutputDebugStringF(const char* pszFormat, ...)
{
int ret = 0;
if (fPrintToConsole || wxTheApp == NULL)
{
// print to console
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
ret = vprintf(pszFormat, arg_ptr);
va_end(arg_ptr);
}
else
{
// print to debug.log
char pszFile[MAX_PATH+100];
GetDataDir(pszFile);
strlcat(pszFile, "/debug.log", sizeof(pszFile));
FILE* fileout = fopen(pszFile, "a");
if (fileout)
{
//// Debug print useful for profiling
//fprintf(fileout, " %"PRI64d" ", wxGetLocalTimeMillis().GetValue());
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
ret = vfprintf(fileout, pszFormat, arg_ptr);
va_end(arg_ptr);
fclose(fileout);
}
}
#ifdef __WXMSW__
if (fPrintToDebugger)
{
// accumulate a line at a time
static CCriticalSection cs_OutputDebugStringF;
CRITICAL_BLOCK(cs_OutputDebugStringF)
{
static char pszBuffer[50000];
static char* pend;
if (pend == NULL)
pend = pszBuffer;
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
int limit = END(pszBuffer) - pend - 2;
int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
va_end(arg_ptr);
if (ret < 0 || ret >= limit)
{
pend = END(pszBuffer) - 2;
*pend++ = '\n';
}
else
pend += ret;
*pend = '\0';
char* p1 = pszBuffer;
char* p2;
while (p2 = strchr(p1, '\n'))
{
p2++;
char c = *p2;
*p2 = '\0';
OutputDebugString(p1);
*p2 = c;
p1 = p2;
}
if (p1 != pszBuffer)
memmove(pszBuffer, p1, pend - p1 + 1);
pend -= (p1 - pszBuffer);
}
}
#endif
return ret;
}
// Safer snprintf // Safer snprintf

101
util.h

@ -112,13 +112,14 @@ inline int myclosesocket(SOCKET& hSocket)
extern map<string, string> mapArgs; extern map<string, string> mapArgs;
extern map<string, vector<string> > mapMultiArgs; extern map<string, vector<string> > mapMultiArgs;
extern bool fDebug; extern bool fDebug;
extern bool fPrintToDebugger;
extern bool fPrintToConsole; extern bool fPrintToConsole;
extern bool fPrintToDebugger;
extern char pszSetDataDir[MAX_PATH]; extern char pszSetDataDir[MAX_PATH];
extern bool fShutdown; extern bool fShutdown;
void RandAddSeed(); void RandAddSeed();
void RandAddSeedPerfmon(); void RandAddSeedPerfmon();
int OutputDebugStringF(const char* pszFormat, ...);
int my_snprintf(char* buffer, size_t limit, const char* format, ...); int my_snprintf(char* buffer, size_t limit, const char* format, ...);
string strprintf(const char* format, ...); string strprintf(const char* format, ...);
bool error(const char* format, ...); bool error(const char* format, ...);
@ -219,92 +220,6 @@ public:
inline int OutputDebugStringF(const char* pszFormat, ...)
{
int ret = 0;
#ifdef __WXDEBUG__
if (!fPrintToConsole)
{
// print to debug.log
char pszFile[MAX_PATH+100];
GetDataDir(pszFile);
strlcat(pszFile, "/debug.log", sizeof(pszFile));
FILE* fileout = fopen(pszFile, "a");
if (fileout)
{
//// Debug print useful for profiling
//fprintf(fileout, " %"PRI64d" ", wxGetLocalTimeMillis().GetValue());
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
ret = vfprintf(fileout, pszFormat, arg_ptr);
va_end(arg_ptr);
fclose(fileout);
}
}
#ifdef __WXMSW__
if (fPrintToDebugger)
{
// accumulate a line at a time
static CCriticalSection cs_OutputDebugStringF;
CRITICAL_BLOCK(cs_OutputDebugStringF)
{
static char pszBuffer[50000];
static char* pend;
if (pend == NULL)
pend = pszBuffer;
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
int limit = END(pszBuffer) - pend - 2;
int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
va_end(arg_ptr);
if (ret < 0 || ret >= limit)
{
pend = END(pszBuffer) - 2;
*pend++ = '\n';
}
else
pend += ret;
*pend = '\0';
char* p1 = pszBuffer;
char* p2;
while (p2 = strchr(p1, '\n'))
{
p2++;
char c = *p2;
*p2 = '\0';
OutputDebugString(p1);
*p2 = c;
p1 = p2;
}
if (p1 != pszBuffer)
memmove(pszBuffer, p1, pend - p1 + 1);
pend -= (p1 - pszBuffer);
}
}
#endif
#endif
if (fPrintToConsole)
{
// print to console
va_list arg_ptr;
va_start(arg_ptr, pszFormat);
ret = vprintf(pszFormat, arg_ptr);
va_end(arg_ptr);
}
return ret;
}
inline string i64tostr(int64 n) inline string i64tostr(int64 n)
{ {
return strprintf("%"PRI64d, n); return strprintf("%"PRI64d, n);
@ -415,11 +330,19 @@ inline string DateTimeStrFormat(const char* pszFormat, int64 nTime)
inline void heapchk() inline void heapchk()
{ {
#ifdef __WXMSW__ #ifdef __WXMSW__
if (_heapchk() != _HEAPOK) /// for debugging
DebugBreak(); //if (_heapchk() != _HEAPOK)
// DebugBreak();
#endif #endif
} }

Loading…
Cancel
Save