Skip to content

Commit c6a63ce

Browse files
committed
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.
1 parent f68ba3f commit c6a63ce

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

src/addrman.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,13 @@ void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
332332
info.nAttempts++;
333333
}
334334

335-
CAddress CAddrMan::Select_(int nUnkBias)
335+
CAddress CAddrMan::Select_()
336336
{
337337
if (size() == 0)
338338
return CAddress();
339339

340-
double nCorTried = sqrt(nTried) * (100.0 - nUnkBias);
341-
double nCorNew = sqrt(nNew) * nUnkBias;
342-
if ((nCorTried + nCorNew) * GetRandInt(1 << 30) / (1 << 30) < nCorTried) {
340+
// Use a 50% chance for choosing between tried and new table entries.
341+
if (nTried > 0 && (nNew == 0 || GetRandInt(2) == 0)) {
343342
// use a tried node
344343
double fChanceFactor = 1.0;
345344
while (1) {

src/addrman.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class CAddrMan
231231

232232
//! Select an address to connect to.
233233
//! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
234-
CAddress Select_(int nUnkBias);
234+
CAddress Select_();
235235

236236
#ifdef DEBUG_ADDRMAN
237237
//! Perform consistency check. Returns an error code or zero.
@@ -533,13 +533,13 @@ class CAddrMan
533533
* Choose an address to connect to.
534534
* nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
535535
*/
536-
CAddress Select(int nUnkBias = 50)
536+
CAddress Select()
537537
{
538538
CAddress addrRet;
539539
{
540540
LOCK(cs);
541541
Check();
542-
addrRet = Select_(nUnkBias);
542+
addrRet = Select_();
543543
Check();
544544
}
545545
return addrRet;

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,7 @@ void ThreadOpenConnections()
12211221
int nTries = 0;
12221222
while (true)
12231223
{
1224-
// use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections)
1225-
CAddress addr = addrman.Select(10 + min(nOutbound,8)*10);
1224+
CAddress addr = addrman.Select();
12261225

12271226
// if we selected an invalid address, restart
12281227
if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))

0 commit comments

Comments
 (0)