Make addrman.h little compact
[novacoin.git] / src / addrman.cpp
index e985b1f..18da0ed 100644 (file)
@@ -7,6 +7,26 @@
 
 using namespace std;
 
+void CAddrInfo::Init()
+{
+    nLastSuccess = 0;
+    nLastTry = 0;
+    nAttempts = 0;
+    nRefCount = 0;
+    fInTried = false;
+    nRandomPos = -1;
+}
+
+CAddrInfo::CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
+{
+    Init();
+}
+
+CAddrInfo::CAddrInfo() : CAddress(), source()
+{
+    Init();
+}
+
 int CAddrInfo::GetTriedBucket(const std::vector<unsigned char> &nKey) const
 {
     CDataStream ss1(SER_GETHASH, 0);
@@ -35,6 +55,11 @@ int CAddrInfo::GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAd
     return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
 }
 
+int CAddrInfo::GetNewBucket(const std::vector<unsigned char> &nKey) const
+{
+    return GetNewBucket(nKey, source);
+}
+
 bool CAddrInfo::IsTerrible(int64_t nNow) const
 {
     if (nLastTry && nLastTry >= nNow-60) // never remove things tried the last minute
@@ -537,3 +562,125 @@ void CAddrMan::Connected_(const CService &addr, int64_t nTime)
     if (nTime - info.nTime > nUpdateInterval)
         info.nTime = nTime;
 }
+
+CAddrMan::CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
+{
+     nKey.resize(32);
+     RAND_bytes(&nKey[0], 32);
+
+     nIdCount = 0;
+     nTried = 0;
+     nNew = 0;
+}
+
+int CAddrMan::size()
+{
+    return (int) vRandom.size();
+}
+
+void CAddrMan::Check()
+{
+#ifdef DEBUG_ADDRMAN
+    {
+        LOCK(cs);
+        int err;
+        if ((err=Check_()))
+            printf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
+    }
+#endif
+}
+
+bool CAddrMan::Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty)
+{
+    bool fRet = false;
+    {
+        LOCK(cs);
+        Check();
+        fRet |= Add_(addr, source, nTimePenalty);
+        Check();
+    }
+    if (fRet)
+        printf("Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort().c_str(), source.ToString().c_str(), nTried, nNew);
+    return fRet;
+}
+
+bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
+{
+    int nAdd = 0;
+    {
+        LOCK(cs);
+        Check();
+        for (auto it = vAddr.begin(); it != vAddr.end(); it++)
+            nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
+        Check();
+    }
+    if (nAdd)
+        printf("Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString().c_str(), nTried, nNew);
+    return nAdd > 0;
+}
+
+void CAddrMan::Good(const CService &addr, int64_t nTime)
+{
+    {
+        LOCK(cs);
+        Check();
+        Good_(addr, nTime);
+        Check();
+    }
+}
+
+void CAddrMan::Attempt(const CService &addr, int64_t nTime)
+{
+    {
+        LOCK(cs);
+        Check();
+        Attempt_(addr, nTime);
+        Check();
+    }
+}
+
+CAddress CAddrMan::Select(int nUnkBias)
+{
+    CAddress addrRet;
+    {
+        LOCK(cs);
+        Check();
+        addrRet = Select_(nUnkBias);
+        Check();
+    }
+    return addrRet;
+}
+
+std::vector<CAddress> CAddrMan::GetAddr()
+{
+    Check();
+    std::vector<CAddress> vAddr;
+    {
+        LOCK(cs);
+        GetAddr_(vAddr);
+    }
+    Check();
+    return vAddr;
+}
+
+std::vector<CAddrInfo> CAddrMan::GetOnlineAddr()
+{
+    Check();
+    std::vector<CAddrInfo> vAddr;
+    {
+        LOCK(cs);
+        GetOnlineAddr_(vAddr);
+    }
+    Check();
+    return vAddr;
+}
+
+void CAddrMan::Connected(const CService &addr, int64_t nTime)
+{
+    {
+        LOCK(cs);
+        Check();
+        Connected_(addr, nTime);
+        Check();
+    }
+}