Merge pull request #412 from sgimenez/db-transactions

Single DB transactions
This commit is contained in:
Pieter Wuille 2011-07-14 05:29:09 -07:00
commit 24271c542b
3 changed files with 22 additions and 6 deletions

View file

@ -1899,6 +1899,8 @@ bool static 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;
addrDB.TxnBegin();
int64 nNow = GetAdjustedTime(); int64 nNow = GetAdjustedTime();
int64 nSince = nNow - 10 * 60; int64 nSince = nNow - 10 * 60;
BOOST_FOREACH(CAddress& addr, vAddr) BOOST_FOREACH(CAddress& addr, vAddr)
@ -1910,7 +1912,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
continue; continue;
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60) if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
addr.nTime = nNow - 5 * 24 * 60 * 60; addr.nTime = nNow - 5 * 24 * 60 * 60;
AddAddress(addr, 2 * 60 * 60); AddAddress(addr, 2 * 60 * 60, &addrDB);
pfrom->AddAddressKnown(addr); pfrom->AddAddressKnown(addr);
if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable()) if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
{ {
@ -1941,6 +1943,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
} }
} }
} }
addrDB.TxnCommit(); // Save addresses (it's ok if this fails)
if (vAddr.size() < 1000) if (vAddr.size() < 1000)
pfrom->fGetAddr = false; pfrom->fGetAddr = false;
} }

View file

@ -440,7 +440,7 @@ void ThreadGetMyExternalIP(void* parg)
bool AddAddress(CAddress addr, int64 nTimePenalty) bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
{ {
if (!addr.IsRoutable()) if (!addr.IsRoutable())
return false; return false;
@ -455,6 +455,9 @@ bool AddAddress(CAddress addr, int64 nTimePenalty)
// New address // New address
printf("AddAddress(%s)\n", addr.ToString().c_str()); printf("AddAddress(%s)\n", addr.ToString().c_str());
mapAddresses.insert(make_pair(addr.GetKey(), addr)); mapAddresses.insert(make_pair(addr.GetKey(), addr));
if (pAddrDB)
pAddrDB->WriteAddress(addr);
else
CAddrDB().WriteAddress(addr); CAddrDB().WriteAddress(addr);
return true; return true;
} }
@ -477,9 +480,14 @@ bool AddAddress(CAddress addr, int64 nTimePenalty)
fUpdated = true; fUpdated = true;
} }
if (fUpdated) if (fUpdated)
{
if (pAddrDB)
pAddrDB->WriteAddress(addrFound);
else
CAddrDB().WriteAddress(addrFound); CAddrDB().WriteAddress(addrFound);
} }
} }
}
return false; return false;
} }
@ -1158,6 +1166,8 @@ void DNSAddressSeed()
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;
@ -1168,12 +1178,14 @@ void DNSAddressSeed()
if (addr.GetByte(3) != 127) if (addr.GetByte(3) != 127)
{ {
addr.nTime = 0; addr.nTime = 0;
AddAddress(addr); AddAddress(addr, 0, &addrDB);
found++; found++;
} }
} }
} }
} }
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);

View file

@ -14,6 +14,7 @@
class CMessageHeader; class CMessageHeader;
class CAddress; class CAddress;
class CAddrDB;
class CInv; class CInv;
class CRequestTracker; class CRequestTracker;
class CNode; class CNode;
@ -39,7 +40,7 @@ bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout
bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
bool GetMyExternalIP(unsigned int& ipRet); bool GetMyExternalIP(unsigned int& ipRet);
bool AddAddress(CAddress addr, int64 nTimePenalty=0); bool AddAddress(CAddress addr, int64 nTimePenalty=0, CAddrDB *pAddrDB=NULL);
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);