Browse Source

Fix some address-handling deadlocks

Made three critical blocks for cs_mapAddresses smaller, and moved
writing to the database out of them. This should also improve the
concurrency of the code.
miguelfreitas
Pieter Wuille 13 years ago
parent
commit
a75d7066b8
  1. 59
      src/net.cpp

59
src/net.cpp

@ -496,21 +496,25 @@ bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
void AddressCurrentlyConnected(const CAddress& addr) void AddressCurrentlyConnected(const CAddress& addr)
{ {
CAddress *paddrFound = NULL;
CRITICAL_BLOCK(cs_mapAddresses) CRITICAL_BLOCK(cs_mapAddresses)
{ {
// Only if it's been published already // Only if it's been published already
map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey()); map<vector<unsigned char>, CAddress>::iterator it = mapAddresses.find(addr.GetKey());
if (it != mapAddresses.end()) if (it != mapAddresses.end())
paddrFound = &(*it).second;
}
if (paddrFound)
{
int64 nUpdateInterval = 20 * 60;
if (paddrFound->nTime < GetAdjustedTime() - nUpdateInterval)
{ {
CAddress& addrFound = (*it).second; // Periodically update most recently seen time
int64 nUpdateInterval = 20 * 60; paddrFound->nTime = GetAdjustedTime();
if (addrFound.nTime < GetAdjustedTime() - nUpdateInterval) CAddrDB addrdb;
{ addrdb.WriteAddress(*paddrFound);
// Periodically update most recently seen time
addrFound.nTime = GetAdjustedTime();
CAddrDB addrdb;
addrdb.WriteAddress(addrFound);
}
} }
} }
} }
@ -1271,13 +1275,13 @@ void ThreadDNSAddressSeed2(void* parg)
if (!fTestNet) if (!fTestNet)
{ {
printf("Loading addresses from DNS seeds (could take a while)\n"); printf("Loading addresses from DNS seeds (could take a while)\n");
CAddrDB addrDB;
addrDB.TxnBegin();
for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) { for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
vector<CAddress> vaddr; vector<CAddress> vaddr;
if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true)) if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true))
{ {
CAddrDB addrDB;
addrDB.TxnBegin();
BOOST_FOREACH (CAddress& addr, vaddr) BOOST_FOREACH (CAddress& addr, vaddr)
{ {
if (addr.GetByte(3) != 127) if (addr.GetByte(3) != 127)
@ -1287,10 +1291,9 @@ void ThreadDNSAddressSeed2(void* parg)
found++; found++;
} }
} }
addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
} }
} }
addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
} }
printf("%d addresses found from DNS seeds\n", found); printf("%d addresses found from DNS seeds\n", found);
@ -1462,28 +1465,32 @@ void ThreadOpenConnections2(void* parg)
if (fShutdown) if (fShutdown)
return; return;
bool fAddSeeds = false;
CRITICAL_BLOCK(cs_mapAddresses) CRITICAL_BLOCK(cs_mapAddresses)
{ {
// Add seed nodes if IRC isn't working // Add seed nodes if IRC isn't working
bool fTOR = (fUseProxy && addrProxy.port == htons(9050)); bool fTOR = (fUseProxy && addrProxy.port == htons(9050));
if (mapAddresses.empty() && (GetTime() - nStart > 60 || fTOR) && !fTestNet) if (mapAddresses.empty() && (GetTime() - nStart > 60 || fTOR) && !fTestNet)
fAddSeeds = true;
}
if (fAddSeeds)
{
for (int i = 0; i < ARRAYLEN(pnSeed); i++)
{ {
for (int i = 0; i < ARRAYLEN(pnSeed); i++) // It'll only connect to one or two seed nodes because once it connects,
{ // it'll get a pile of addresses with newer timestamps.
// It'll only connect to one or two seed nodes because once it connects, // Seed nodes are given a random 'last seen time' of between one and two
// it'll get a pile of addresses with newer timestamps. // weeks ago.
// Seed nodes are given a random 'last seen time' of between one and two const int64 nOneWeek = 7*24*60*60;
// weeks ago. CAddress addr;
const int64 nOneWeek = 7*24*60*60; addr.ip = pnSeed[i];
CAddress addr; addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
addr.ip = pnSeed[i]; AddAddress(addr);
addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
AddAddress(addr);
}
} }
} }
// //
// Choose an address to connect to based on most recently seen // Choose an address to connect to based on most recently seen
// //

Loading…
Cancel
Save