Skip to content

Commit c891967

Browse files
committedOct 23, 2010
bugfixes from Dean Gores,
addr system changes, make sure no gen before block 74000 git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@173 1a98c847-1fd6-4fd8-948a-caf3550aa51b
·
v29.0noversion
1 parent c285051 commit c891967

File tree

8 files changed

+109
-63
lines changed

8 files changed

+109
-63
lines changed
 

‎db.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ bool CAddrDB::WriteAddress(const CAddress& addr)
503503
return Write(make_pair(string("addr"), addr.GetKey()), addr);
504504
}
505505

506+
bool CAddrDB::EraseAddress(const CAddress& addr)
507+
{
508+
return Erase(make_pair(string("addr"), addr.GetKey()));
509+
}
510+
506511
bool CAddrDB::LoadAddresses()
507512
{
508513
CRITICAL_BLOCK(cs_mapAddresses)
@@ -554,11 +559,6 @@ bool CAddrDB::LoadAddresses()
554559
pcursor->close();
555560

556561
printf("Loaded %d addresses\n", mapAddresses.size());
557-
558-
// Fix for possible bug that manifests in mapAddresses.count in irc.cpp,
559-
// just need to call count here and it doesn't happen there. The bug was the
560-
// pack pragma in irc.cpp and has been fixed, but I'm not in a hurry to delete this.
561-
mapAddresses.count(vector<unsigned char>(18));
562562
}
563563

564564
return true;

‎db.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class CAddrDB : public CDB
298298
void operator=(const CAddrDB&);
299299
public:
300300
bool WriteAddress(const CAddress& addr);
301+
bool EraseAddress(const CAddress& addr);
301302
bool LoadAddresses();
302303
};
303304

‎irc.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,31 @@ bool RecvLine(SOCKET hSocket, string& strLine)
8484
}
8585
else if (nBytes <= 0)
8686
{
87+
if (fShutdown)
88+
return false;
89+
if (nBytes < 0)
90+
{
91+
int nErr = WSAGetLastError();
92+
if (nErr == WSAEMSGSIZE)
93+
continue;
94+
if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
95+
{
96+
Sleep(10);
97+
continue;
98+
}
99+
}
87100
if (!strLine.empty())
88101
return true;
89-
// socket closed
90-
printf("IRC socket closed\n");
91-
return false;
92-
}
93-
else
94-
{
95-
// socket error
96-
int nErr = WSAGetLastError();
97-
if (nErr != WSAEMSGSIZE && nErr != WSAEINTR && nErr != WSAEINPROGRESS)
102+
if (nBytes == 0)
103+
{
104+
// socket closed
105+
printf("IRC socket closed\n");
106+
return false;
107+
}
108+
else
98109
{
110+
// socket error
111+
int nErr = WSAGetLastError();
99112
printf("IRC recv failed: %d\n", nErr);
100113
return false;
101114
}
@@ -293,8 +306,8 @@ void ThreadIRCSeed2(void* parg)
293306
CAddress addr;
294307
if (DecodeAddress(pszName, addr))
295308
{
296-
addr.nTime = GetAdjustedTime() - 51 * 60;
297-
if (AddAddress(addr))
309+
addr.nTime = GetAdjustedTime();
310+
if (AddAddress(addr, 51 * 60))
298311
printf("IRC got new address\n");
299312
nGotIRCAddresses++;
300313
}

‎main.cpp

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
929929

930930
bool IsInitialBlockDownload()
931931
{
932-
if (pindexBest == NULL)
932+
if (pindexBest == NULL || nBestHeight < 74000)
933933
return true;
934934
static int64 nLastUpdate;
935935
static CBlockIndex* pindexLastBest;
@@ -2172,6 +2172,24 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
21722172
if (pfrom->nVersion < 209)
21732173
pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
21742174

2175+
if (!pfrom->fInbound)
2176+
{
2177+
// Advertise our address
2178+
if (addrLocalHost.IsRoutable() && !fUseProxy)
2179+
{
2180+
CAddress addr(addrLocalHost);
2181+
addr.nTime = GetAdjustedTime();
2182+
pfrom->PushAddress(addr);
2183+
}
2184+
2185+
// Get recent addresses
2186+
if (pfrom->nVersion >= 31402 || mapAddresses.size() < 1000)
2187+
{
2188+
pfrom->PushMessage("getaddr");
2189+
pfrom->fGetAddr = true;
2190+
}
2191+
}
2192+
21752193
// Ask the first connected node for block updates
21762194
static int nAskedForBlocks;
21772195
if (!pfrom->fClient && (nAskedForBlocks < 1 || vNodes.size() <= 1))
@@ -2208,27 +2226,30 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
22082226
{
22092227
vector<CAddress> vAddr;
22102228
vRecv >> vAddr;
2211-
if (pfrom->nVersion < 200) // don't want addresses from 0.1.5
2229+
2230+
// Don't want addr from older versions unless seeding
2231+
if (pfrom->nVersion < 209)
22122232
return true;
2213-
if (pfrom->nVersion < 209 && mapAddresses.size() > 1000) // don't want addr from 0.2.0 unless seeding
2233+
if (pfrom->nVersion < 31402 && mapAddresses.size() > 1000)
22142234
return true;
22152235
if (vAddr.size() > 1000)
22162236
return error("message addr size() = %d", vAddr.size());
22172237

22182238
// Store the new addresses
2239+
int64 nNow = GetAdjustedTime();
2240+
int64 nSince = nNow - 10 * 60;
22192241
foreach(CAddress& addr, vAddr)
22202242
{
22212243
if (fShutdown)
22222244
return true;
22232245
// ignore IPv6 for now, since it isn't implemented anyway
22242246
if (!addr.IsIPv4())
22252247
continue;
2226-
addr.nTime = GetAdjustedTime() - 2 * 60 * 60;
2227-
if (pfrom->fGetAddr || vAddr.size() > 10)
2228-
addr.nTime -= 5 * 24 * 60 * 60;
2229-
AddAddress(addr);
2248+
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
2249+
addr.nTime = nNow - 5 * 24 * 60 * 60;
2250+
AddAddress(addr, 2 * 60 * 60);
22302251
pfrom->AddAddressKnown(addr);
2231-
if (!pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
2252+
if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
22322253
{
22332254
// Relay to a limited number of other nodes
22342255
CRITICAL_BLOCK(cs_vNodes)
@@ -2243,6 +2264,8 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
22432264
multimap<uint256, CNode*> mapMix;
22442265
foreach(CNode* pnode, vNodes)
22452266
{
2267+
if (pnode->nVersion < 31402)
2268+
continue;
22462269
unsigned int nPointer;
22472270
memcpy(&nPointer, &pnode, sizeof(nPointer));
22482271
uint256 hashKey = hashRand ^ nPointer;
@@ -2610,9 +2633,12 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
26102633
if (pto->nLastSend && GetTime() - pto->nLastSend > 30 * 60 && pto->vSend.empty())
26112634
pto->PushMessage("ping");
26122635

2636+
// Resend wallet transactions that haven't gotten in a block yet
2637+
ResendWalletTransactions();
2638+
26132639
// Address refresh broadcast
26142640
static int64 nLastRebroadcast;
2615-
if (GetTime() - nLastRebroadcast > 24 * 60 * 60) // every 24 hours
2641+
if (GetTime() - nLastRebroadcast > 24 * 60 * 60)
26162642
{
26172643
nLastRebroadcast = GetTime();
26182644
CRITICAL_BLOCK(cs_vNodes)
@@ -2624,13 +2650,42 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
26242650

26252651
// Rebroadcast our address
26262652
if (addrLocalHost.IsRoutable() && !fUseProxy)
2627-
pnode->PushAddress(addrLocalHost);
2653+
{
2654+
CAddress addr(addrLocalHost);
2655+
addr.nTime = GetAdjustedTime();
2656+
pnode->PushAddress(addr);
2657+
}
26282658
}
26292659
}
26302660
}
26312661

2632-
// Resend wallet transactions that haven't gotten in a block yet
2633-
ResendWalletTransactions();
2662+
// Clear out old addresses periodically so it's not too much work at once
2663+
static int64 nLastClear;
2664+
if (nLastClear == 0)
2665+
nLastClear = GetTime();
2666+
if (GetTime() - nLastClear > 10 * 60 && vNodes.size() >= 3)
2667+
{
2668+
nLastClear = GetTime();
2669+
CRITICAL_BLOCK(cs_mapAddresses)
2670+
{
2671+
CAddrDB addrdb;
2672+
int64 nSince = GetAdjustedTime() - 14 * 24 * 60 * 60;
2673+
for (map<vector<unsigned char>, CAddress>::iterator mi = mapAddresses.begin();
2674+
mi != mapAddresses.end();)
2675+
{
2676+
const CAddress& addr = (*mi).second;
2677+
if (addr.nTime < nSince)
2678+
{
2679+
if (mapAddresses.size() < 1000 || GetTime() > nLastClear + 20)
2680+
break;
2681+
addrdb.EraseAddress(addr);
2682+
mapAddresses.erase(mi++);
2683+
}
2684+
else
2685+
mi++;
2686+
}
2687+
}
2688+
}
26342689

26352690

26362691
//

‎net.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ bool GetMyExternalIP2(const CAddress& addrConnect, const char* pszGet, const cha
144144
}
145145
}
146146
closesocket(hSocket);
147-
if (strLine.find("<"))
147+
if (strLine.find("<") != -1)
148148
strLine = strLine.substr(0, strLine.find("<"));
149149
strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
150150
while (strLine.size() > 0 && isspace(strLine[strLine.size()-1]))
@@ -224,12 +224,13 @@ bool GetMyExternalIP(unsigned int& ipRet)
224224

225225

226226

227-
bool AddAddress(CAddress addr)
227+
bool AddAddress(CAddress addr, int64 nTimePenalty)
228228
{
229229
if (!addr.IsRoutable())
230230
return false;
231231
if (addr.ip == addrLocalHost.ip)
232232
return false;
233+
addr.nTime = max((int64)0, (int64)addr.nTime - nTimePenalty);
233234
CRITICAL_BLOCK(cs_mapAddresses)
234235
{
235236
map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
@@ -1073,25 +1074,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect)
10731074
return false;
10741075
pnode->fNetworkNode = true;
10751076

1076-
if (addrLocalHost.IsRoutable() && !fUseProxy)
1077-
{
1078-
// Advertise our address
1079-
vector<CAddress> vAddr;
1080-
vAddr.push_back(addrLocalHost);
1081-
pnode->PushMessage("addr", vAddr);
1082-
}
1083-
1084-
// Get as many addresses as we can
1085-
pnode->PushMessage("getaddr");
1086-
pnode->fGetAddr = true; // don't relay the results of the getaddr
1087-
1088-
////// should the one on the receiving end do this too?
1089-
// Subscribe our local subscription list
1090-
const unsigned int nHops = 0;
1091-
for (unsigned int nChannel = 0; nChannel < pnodeLocalHost->vfSubscribe.size(); nChannel++)
1092-
if (pnodeLocalHost->vfSubscribe[nChannel])
1093-
pnode->PushMessage("subscribe", nChannel, nHops);
1094-
10951077
return true;
10961078
}
10971079

‎net.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ enum
2424

2525
bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet);
2626
bool GetMyExternalIP(unsigned int& ipRet);
27-
bool AddAddress(CAddress addr);
27+
bool AddAddress(CAddress addr, int64 nTimePenalty=0);
2828
void AddressCurrentlyConnected(const CAddress& addr);
2929
CNode* FindNode(unsigned int ip);
3030
CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0);
@@ -139,7 +139,7 @@ class CAddress
139139
unsigned int ip;
140140
unsigned short port;
141141

142-
// disk only
142+
// disk and network only
143143
unsigned int nTime;
144144

145145
// memory only
@@ -186,7 +186,7 @@ class CAddress
186186
memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
187187
ip = INADDR_NONE;
188188
port = GetDefaultPort();
189-
nTime = GetAdjustedTime();
189+
nTime = 100000000;
190190
nLastTry = 0;
191191
}
192192

@@ -218,11 +218,12 @@ class CAddress
218218

219219
IMPLEMENT_SERIALIZE
220220
(
221+
if (fRead)
222+
const_cast<CAddress*>(this)->Init();
221223
if (nType & SER_DISK)
222-
{
223224
READWRITE(nVersion);
225+
if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
224226
READWRITE(nTime);
225-
}
226227
READWRITE(nServices);
227228
READWRITE(FLATDATA(pchReserved)); // for IPv6
228229
READWRITE(ip);
@@ -415,7 +416,7 @@ class CInv
415416
const char* GetCommand() const
416417
{
417418
if (!IsKnownType())
418-
throw std::out_of_range(strprintf("CInv::GetCommand() : type=% unknown type", type));
419+
throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
419420
return ppszTypeName[type];
420421
}
421422

@@ -732,13 +733,6 @@ class CNode
732733
AbortMessage();
733734
}
734735

735-
const char* GetMessageCommand() const
736-
{
737-
if (nHeaderStart == -1)
738-
return "";
739-
return &vSend[nHeaderStart] + offsetof(CMessageHeader, pchCommand);
740-
}
741-
742736

743737

744738

‎rpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ Value backupwallet(const Array& params, bool fHelp)
656656
return Value::null;
657657
}
658658

659+
659660
Value validateaddress(const Array& params, bool fHelp)
660661
{
661662
if (fHelp || params.size() != 1)

‎serialize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CDataStream;
2222
class CAutoFile;
2323
static const unsigned int MAX_SIZE = 0x02000000;
2424

25-
static const int VERSION = 31401;
25+
static const int VERSION = 31402;
2626
static const char* pszSubVer = "";
2727

2828

0 commit comments

Comments
 (0)
Please sign in to comment.