Switch addrman key from vector to uint256

This commit is contained in:
Pieter Wuille 2015-03-08 06:30:05 -07:00
parent f3948a30cd
commit b23add5521
2 changed files with 23 additions and 12 deletions

View File

@ -10,30 +10,30 @@
using namespace std; using namespace std;
int CAddrInfo::GetTriedBucket(const std::vector<unsigned char>& nKey) const int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{ {
CDataStream ss1(SER_GETHASH, 0); CDataStream ss1(SER_GETHASH, 0);
std::vector<unsigned char> vchKey = GetKey(); std::vector<unsigned char> vchKey = GetKey();
ss1 << nKey << vchKey; ss1 << ((unsigned char)32) << nKey << vchKey;
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash(); uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash();
CDataStream ss2(SER_GETHASH, 0); CDataStream ss2(SER_GETHASH, 0);
std::vector<unsigned char> vchGroupKey = GetGroup(); std::vector<unsigned char> vchGroupKey = GetGroup();
ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP); ss2 << ((unsigned char)32) << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP);
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash(); uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash();
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
} }
int CAddrInfo::GetNewBucket(const std::vector<unsigned char>& nKey, const CNetAddr& src) const int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const
{ {
CDataStream ss1(SER_GETHASH, 0); CDataStream ss1(SER_GETHASH, 0);
std::vector<unsigned char> vchGroupKey = GetGroup(); std::vector<unsigned char> vchGroupKey = GetGroup();
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(); std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
ss1 << nKey << vchGroupKey << vchSourceGroupKey; ss1 << ((unsigned char)32) << nKey << vchGroupKey << vchSourceGroupKey;
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash(); uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash();
CDataStream ss2(SER_GETHASH, 0); CDataStream ss2(SER_GETHASH, 0);
ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP); ss2 << ((unsigned char)32) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP);
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash(); uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash();
return hash2 % ADDRMAN_NEW_BUCKET_COUNT; return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
} }
@ -483,6 +483,8 @@ int CAddrMan::Check_()
return -13; return -13;
if (mapNew.size()) if (mapNew.size())
return -15; return -15;
if (nKey.IsNull())
return -16;
return 0; return 0;
} }

View File

@ -10,6 +10,7 @@
#include "random.h" #include "random.h"
#include "sync.h" #include "sync.h"
#include "timedata.h" #include "timedata.h"
#include "uint256.h"
#include "util.h" #include "util.h"
#include <map> #include <map>
@ -79,13 +80,13 @@ public:
} }
//! Calculate in which "tried" bucket this entry belongs //! Calculate in which "tried" bucket this entry belongs
int GetTriedBucket(const std::vector<unsigned char> &nKey) const; int GetTriedBucket(const uint256 &nKey) const;
//! Calculate in which "new" bucket this entry belongs, given a certain source //! Calculate in which "new" bucket this entry belongs, given a certain source
int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const; int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;
//! Calculate in which "new" bucket this entry belongs, using its default source //! Calculate in which "new" bucket this entry belongs, using its default source
int GetNewBucket(const std::vector<unsigned char> &nKey) const int GetNewBucket(const uint256 &nKey) const
{ {
return GetNewBucket(nKey, source); return GetNewBucket(nKey, source);
} }
@ -176,7 +177,7 @@ private:
mutable CCriticalSection cs; mutable CCriticalSection cs;
//! secret key to randomize bucket select with //! secret key to randomize bucket select with
std::vector<unsigned char> nKey; uint256 nKey;
//! last used nId //! last used nId
int nIdCount; int nIdCount;
@ -284,6 +285,7 @@ public:
unsigned char nVersion = 0; unsigned char nVersion = 0;
s << nVersion; s << nVersion;
s << ((unsigned char)32);
s << nKey; s << nKey;
s << nNew; s << nNew;
s << nTried; s << nTried;
@ -328,6 +330,9 @@ public:
unsigned char nVersion; unsigned char nVersion;
s >> nVersion; s >> nVersion;
unsigned char nKeySize;
s >> nKeySize;
if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman");
s >> nKey; s >> nKey;
s >> nNew; s >> nNew;
s >> nTried; s >> nTried;
@ -393,14 +398,18 @@ public:
CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>()) CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
{ {
nKey.resize(32); nKey = GetRandHash();
GetRandBytes(&nKey[0], 32);
nIdCount = 0; nIdCount = 0;
nTried = 0; nTried = 0;
nNew = 0; nNew = 0;
} }
~CAddrMan()
{
nKey.SetNull();
}
//! Return the number of (unique) addresses in all tables. //! Return the number of (unique) addresses in all tables.
int size() int size()
{ {