Skip to content

Commit 207f1c8

Browse files
committed
refactor: make AddrMan::Good return bool
If AddrMan::Good is unable to add an entry to tried (for a number of reasons), return false. This makes it much easier and cleaner to directly test for tried collisions. It also allows anyone calling Good() to handle the case where adding an address to tried is unsuccessful. Update docs to doxygen style.
1 parent 5dd28e5 commit 207f1c8

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/addrman.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
614614
return fInsert;
615615
}
616616

617-
void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
617+
bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
618618
{
619619
AssertLockHeld(cs);
620620

@@ -625,8 +625,7 @@ void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
625625
AddrInfo* pinfo = Find(addr, &nId);
626626

627627
// if not found, bail out
628-
if (!pinfo)
629-
return;
628+
if (!pinfo) return false;
630629

631630
AddrInfo& info = *pinfo;
632631

@@ -638,13 +637,11 @@ void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
638637
// currently-connected peers.
639638

640639
// if it is already in the tried set, don't do anything else
641-
if (info.fInTried)
642-
return;
640+
if (info.fInTried) return false;
643641

644642
// if it is not in new, something bad happened
645-
if (!Assume(info.nRefCount > 0)) {
646-
return;
647-
}
643+
if (!Assume(info.nRefCount > 0)) return false;
644+
648645

649646
// which tried bucket to move the entry to
650647
int tried_bucket = info.GetTriedBucket(nKey, m_asmap);
@@ -661,11 +658,13 @@ void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
661658
colliding_entry != mapInfo.end() ? colliding_entry->second.ToString() : "",
662659
addr.ToString(),
663660
m_tried_collisions.size());
661+
return false;
664662
} else {
665663
// move nId to the tried tables
666664
MakeTried(info, nId);
667665
LogPrint(BCLog::ADDRMAN, "Moved %s mapped to AS%i to tried[%i][%i]\n",
668666
addr.ToString(), addr.GetMappedAS(m_asmap), tried_bucket, tried_bucket_pos);
667+
return true;
669668
}
670669
}
671670

@@ -1049,12 +1048,13 @@ bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source
10491048
return ret;
10501049
}
10511050

1052-
void AddrManImpl::Good(const CService& addr, int64_t nTime)
1051+
bool AddrManImpl::Good(const CService& addr, int64_t nTime)
10531052
{
10541053
LOCK(cs);
10551054
Check();
1056-
Good_(addr, /* test_before_evict */ true, nTime);
1055+
auto ret = Good_(addr, /*test_before_evict=*/true, nTime);
10571056
Check();
1057+
return ret;
10581058
}
10591059

10601060
void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
@@ -1157,9 +1157,9 @@ bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, in
11571157
return m_impl->Add(vAddr, source, nTimePenalty);
11581158
}
11591159

1160-
void AddrMan::Good(const CService& addr, int64_t nTime)
1160+
bool AddrMan::Good(const CService& addr, int64_t nTime)
11611161
{
1162-
m_impl->Good(addr, nTime);
1162+
return m_impl->Good(addr, nTime);
11631163
}
11641164

11651165
void AddrMan::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)

src/addrman.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@ class AddrMan
8181
* @return true if at least one address is successfully added. */
8282
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
8383

84-
//! Mark an entry as accessible, possibly moving it from "new" to "tried".
85-
void Good(const CService& addr, int64_t nTime = GetAdjustedTime());
84+
/**
85+
* Mark an address record as accessible and attempt to move it to addrman's tried table.
86+
*
87+
* @param[in] addr Address record to attempt to move to tried table.
88+
* @param[in] nTime The time that we were last connected to this peer.
89+
* @return true if the address is successfully moved from the new table to the tried table.
90+
*/
91+
bool Good(const CService& addr, int64_t nTime = GetAdjustedTime());
8692

8793
//! Mark an entry as connection attempted to.
8894
void Attempt(const CService& addr, bool fCountFailure, int64_t nTime = GetAdjustedTime());

src/addrman_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class AddrManImpl
115115
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
116116
EXCLUSIVE_LOCKS_REQUIRED(!cs);
117117

118-
void Good(const CService& addr, int64_t nTime)
118+
bool Good(const CService& addr, int64_t nTime)
119119
EXCLUSIVE_LOCKS_REQUIRED(!cs);
120120

121121
void Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
@@ -248,7 +248,7 @@ class AddrManImpl
248248
* @see AddrMan::Add() for parameters. */
249249
bool AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
250250

251-
void Good_(const CService& addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
251+
bool Good_(const CService& addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
252252

253253
bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
254254

0 commit comments

Comments
 (0)