Skip to content

Commit 2095df7

Browse files
committed
[addrman] Add Add_() inner function, fix Add() return semantics
Previously, Add() would return true if the function created a new AddressInfo object, even if that object could not be successfully entered into the new table and was deleted. That would happen if the new table position was already taken and the existing entry could not be removed. Instead, return true if the new AddressInfo object is successfully entered into the new table. This fixes a bug in the "Added %i addresses" log, which would not always accurately log how many addresses had been added. p2p_addrv2_relay.py and p2p_addr_relay.py need to be updated since they were incorrectly asserting on the buggy log (assuming that addresses are added to addrman, when there could in fact be new table position collisions that prevent some of those address records from being added).
1 parent 2658eb6 commit 2095df7

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

src/addrman.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
599599
if (!addr.IsRoutable())
600600
return false;
601601

602-
bool fNew = false;
603602
int nId;
604603
AddrInfo* pinfo = Find(addr, &nId);
605604

@@ -640,13 +639,12 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
640639
pinfo = Create(addr, source, &nId);
641640
pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
642641
nNew++;
643-
fNew = true;
644642
}
645643

646644
int nUBucket = pinfo->GetNewBucket(nKey, source, m_asmap);
647645
int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
646+
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
648647
if (vvNew[nUBucket][nUBucketPos] != nId) {
649-
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
650648
if (!fInsert) {
651649
AddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
652650
if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
@@ -666,7 +664,19 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
666664
}
667665
}
668666
}
669-
return fNew;
667+
return fInsert;
668+
}
669+
670+
bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
671+
{
672+
int added{0};
673+
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) {
674+
added += AddSingle(*it, source, nTimePenalty) ? 1 : 0;
675+
}
676+
if (added > 0) {
677+
LogPrint(BCLog::ADDRMAN, "Added %i addresses (of %i) from %s: %i tried, %i new\n", added, vAddr.size(), source.ToString(), nTried, nNew);
678+
}
679+
return added > 0;
670680
}
671681

672682
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
@@ -1031,15 +1041,10 @@ size_t AddrManImpl::size() const
10311041
bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
10321042
{
10331043
LOCK(cs);
1034-
int nAdd = 0;
10351044
Check();
1036-
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
1037-
nAdd += AddSingle(*it, source, nTimePenalty) ? 1 : 0;
1045+
auto ret = Add_(vAddr, source, nTimePenalty);
10381046
Check();
1039-
if (nAdd) {
1040-
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
1041-
}
1042-
return nAdd > 0;
1047+
return ret;
10431048
}
10441049

10451050
void AddrManImpl::Good(const CService& addr, int64_t nTime)

src/addrman.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class AddrMan
7676
* @param[in] source The address of the node that sent us these addr records.
7777
* @param[in] nTimePenalty A "time penalty" to apply to the address record's nTime. If a peer
7878
* sends us an address record with nTime=n, then we'll add it to our
79-
* addrman with nTime=(n - nTimePenalty). */
79+
* addrman with nTime=(n - nTimePenalty).
80+
* @return true if at least one address is successfully added. */
8081
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
8182

8283
//! Mark an entry as accessible, possibly moving it from "new" to "tried".

src/addrman_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ 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+
bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
252+
251253
void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
252254

253255
std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);

src/test/addrman_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
347347
//Test: tried table collision!
348348
CService addr1 = ResolveService("250.1.1." + ToString(++num_addrs));
349349
uint32_t collisions{1};
350-
BOOST_CHECK(addrman.Add({CAddress(addr1, NODE_NONE)}, source));
350+
BOOST_CHECK(!addrman.Add({CAddress(addr1, NODE_NONE)}, source));
351351
BOOST_CHECK_EQUAL(addrman.size(), num_addrs - collisions);
352352

353353
CService addr2 = ResolveService("250.1.1." + ToString(++num_addrs));

test/functional/p2p_addr_relay.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ def relay_tests(self):
152152
msg = self.setup_addr_msg(num_ipv4_addrs)
153153
with self.nodes[0].assert_debug_log(
154154
[
155-
'Added {} addresses from 127.0.0.1: 0 tried'.format(num_ipv4_addrs),
156155
'received: addr (301 bytes) peer=1',
157156
]
158157
):

test/functional/p2p_addrv2_relay.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ def run_test(self):
7272
addr_receiver = self.nodes[0].add_p2p_connection(AddrReceiver())
7373
msg.addrs = ADDRS
7474
with self.nodes[0].assert_debug_log([
75-
# The I2P address is not added to node's own addrman because it has no
76-
# I2P reachability (thus 10 - 1 = 9).
77-
'Added 9 addresses from 127.0.0.1: 0 tried',
7875
'received: addrv2 (159 bytes) peer=0',
7976
'sending addrv2 (159 bytes) peer=1',
8077
]):

0 commit comments

Comments
 (0)