From 8c41469140584f3cbdd09fb62a1287da0216f431 Mon Sep 17 00:00:00 2001 From: Patrick Varilly Date: Thu, 14 Jul 2011 02:45:34 +0200 Subject: [PATCH 1/2] Single DB transaction for all addresses in a message Cuts disk activity at startup immensely --- src/main.cpp | 5 ++++- src/net.cpp | 14 +++++++++++--- src/net.h | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6902194016..e3ad35044e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1899,6 +1899,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) return error("message addr size() = %d", vAddr.size()); // Store the new addresses + CAddrDB addrDB; + addrDB.TxnBegin(); int64 nNow = GetAdjustedTime(); int64 nSince = nNow - 10 * 60; BOOST_FOREACH(CAddress& addr, vAddr) @@ -1910,7 +1912,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) continue; if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60) addr.nTime = nNow - 5 * 24 * 60 * 60; - AddAddress(addr, 2 * 60 * 60); + AddAddress(addr, 2 * 60 * 60, &addrDB); pfrom->AddAddressKnown(addr); 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) pfrom->fGetAddr = false; } diff --git a/src/net.cpp b/src/net.cpp index ac5a2834b0..dcfff934b6 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -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()) return false; @@ -455,7 +455,10 @@ bool AddAddress(CAddress addr, int64 nTimePenalty) // New address printf("AddAddress(%s)\n", addr.ToString().c_str()); mapAddresses.insert(make_pair(addr.GetKey(), addr)); - CAddrDB().WriteAddress(addr); + if (pAddrDB) + pAddrDB->WriteAddress(addr); + else + CAddrDB().WriteAddress(addr); return true; } else @@ -477,7 +480,12 @@ bool AddAddress(CAddress addr, int64 nTimePenalty) fUpdated = true; } if (fUpdated) - CAddrDB().WriteAddress(addrFound); + { + if (pAddrDB) + pAddrDB->WriteAddress(addrFound); + else + CAddrDB().WriteAddress(addrFound); + } } } return false; diff --git a/src/net.h b/src/net.h index afa264b723..78055bfc69 100644 --- a/src/net.h +++ b/src/net.h @@ -14,6 +14,7 @@ class CMessageHeader; class CAddress; +class CAddrDB; class CInv; class CRequestTracker; class CNode; @@ -39,7 +40,7 @@ bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout bool Lookup(const char *pszName, std::vector& 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 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); CNode* FindNode(unsigned int ip); CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0); From d655a26c9dd157a9e4bf08bff14bfaa69791287a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gimenez?= Date: Thu, 14 Jul 2011 02:57:39 +0200 Subject: [PATCH 2/2] Single DB transaction for addresses from DNS seeds --- src/net.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/net.cpp b/src/net.cpp index dcfff934b6..2a90f6d0cc 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1166,6 +1166,8 @@ void DNSAddressSeed() if (!fTestNet) { 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++) { vector vaddr; @@ -1176,12 +1178,14 @@ void DNSAddressSeed() if (addr.GetByte(3) != 127) { addr.nTime = 0; - AddAddress(addr); + AddAddress(addr, 0, &addrDB); found++; } } } } + + addrDB.TxnCommit(); // Save addresses (it's ok if this fails) } printf("%d addresses found from DNS seeds\n", found);