Always use a 50% chance to choose between tried and new entries

This change was suggested as Countermeasure 2 in
Eclipse Attacks on Bitcoin’s Peer-to-Peer Network, Ethan Heilman,
Alison Kendler, Aviv Zohar, Sharon Goldberg. ePrint Archive Report
2015/263. March 2015.
pull/262/head
Pieter Wuille 10 years ago
parent f68ba3f67b
commit c6a63ceeb4

@ -332,14 +332,13 @@ void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
info.nAttempts++; info.nAttempts++;
} }
CAddress CAddrMan::Select_(int nUnkBias) CAddress CAddrMan::Select_()
{ {
if (size() == 0) if (size() == 0)
return CAddress(); return CAddress();
double nCorTried = sqrt(nTried) * (100.0 - nUnkBias); // Use a 50% chance for choosing between tried and new table entries.
double nCorNew = sqrt(nNew) * nUnkBias; if (nTried > 0 && (nNew == 0 || GetRandInt(2) == 0)) {
if ((nCorTried + nCorNew) * GetRandInt(1 << 30) / (1 << 30) < nCorTried) {
// use a tried node // use a tried node
double fChanceFactor = 1.0; double fChanceFactor = 1.0;
while (1) { while (1) {

@ -231,7 +231,7 @@ protected:
//! Select an address to connect to. //! Select an address to connect to.
//! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100) //! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
CAddress Select_(int nUnkBias); CAddress Select_();
#ifdef DEBUG_ADDRMAN #ifdef DEBUG_ADDRMAN
//! Perform consistency check. Returns an error code or zero. //! Perform consistency check. Returns an error code or zero.
@ -533,13 +533,13 @@ public:
* Choose an address to connect to. * Choose an address to connect to.
* nUnkBias determines how much "new" entries are favored over "tried" ones (0-100). * nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
*/ */
CAddress Select(int nUnkBias = 50) CAddress Select()
{ {
CAddress addrRet; CAddress addrRet;
{ {
LOCK(cs); LOCK(cs);
Check(); Check();
addrRet = Select_(nUnkBias); addrRet = Select_();
Check(); Check();
} }
return addrRet; return addrRet;

@ -1221,8 +1221,7 @@ void ThreadOpenConnections()
int nTries = 0; int nTries = 0;
while (true) while (true)
{ {
// use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections) CAddress addr = addrman.Select();
CAddress addr = addrman.Select(10 + min(nOutbound,8)*10);
// if we selected an invalid address, restart // if we selected an invalid address, restart
if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr)) if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))

Loading…
Cancel
Save