Skip to content

Commit 3612b8a

Browse files
Merge dashpay#6024: backport: Merge bitcoin#22496, 21985
c3f8acf Merge bitcoin#21985: net: Return IPv6 scope id in `CNetAddr::ToStringIP()` (W. J. van der Laan) 28daf0d Merge bitcoin#22496: addrman: Remove addrman hotfixes (fanquake) Pull request description: bitcoin backports Top commit has no ACKs. Tree-SHA512: e208aee446ce5d4b29b43b9f86236b6f8ac1805572edcdf6355246c186947a5e783ecf77869525249436aa7fa2e10ae00e6b13aba7f6f04c8543e8fd382b9c8d
2 parents 5945c37 + c3f8acf commit 3612b8a

File tree

4 files changed

+19
-41
lines changed

4 files changed

+19
-41
lines changed

src/addrman.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,6 @@ double CAddrInfo::GetChance(int64_t nNow) const
7979
return fChance;
8080
}
8181

82-
void CAddrMan::RemoveInvalid()
83-
{
84-
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
85-
for (size_t i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
86-
const auto id = vvNew[bucket][i];
87-
if (id != -1 && !mapInfo[id].IsValid()) {
88-
ClearNew(bucket, i);
89-
}
90-
}
91-
}
92-
93-
for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; ++bucket) {
94-
for (size_t i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
95-
const auto id = vvTried[bucket][i];
96-
if (id == -1) {
97-
continue;
98-
}
99-
const auto& addr_info = mapInfo[id];
100-
if (addr_info.IsValid()) {
101-
continue;
102-
}
103-
vvTried[bucket][i] = -1;
104-
--nTried;
105-
SwapRandom(addr_info.nRandomPos, vRandom.size() - 1);
106-
vRandom.pop_back();
107-
mapAddr.erase(addr_info);
108-
mapInfo.erase(id);
109-
m_tried_collisions.erase(id);
110-
}
111-
}
112-
}
11382

11483
CAddrInfo* CAddrMan::Find(const CService& addr, int* pnId)
11584
{

src/addrman.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ class CAddrMan
371371
s >> info;
372372
int nKBucket = info.GetTriedBucket(nKey, m_asmap);
373373
int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
374-
if (vvTried[nKBucket][nKBucketPos] == -1) {
374+
if (info.IsValid()
375+
&& vvTried[nKBucket][nKBucketPos] == -1) {
375376
info.nRandomPos = vRandom.size();
376377
info.fInTried = true;
377378
vRandom.push_back(nIdCount);
@@ -425,6 +426,9 @@ class CAddrMan
425426
const int entry_index{bucket_entry.second};
426427
CAddrInfo& info = mapInfo[entry_index];
427428

429+
// Don't store the entry in the new bucket if it's not a valid address for our addrman
430+
if (!info.IsValid()) continue;
431+
428432
// The entry shouldn't appear in more than
429433
// ADDRMAN_NEW_BUCKETS_PER_ADDRESS. If it has already, just skip
430434
// this bucket_entry.
@@ -447,7 +451,7 @@ class CAddrMan
447451
}
448452
}
449453

450-
// Prune new entries with refcount 0 (as a result of collisions).
454+
// Prune new entries with refcount 0 (as a result of collisions or invalid address).
451455
int nLostUnk = 0;
452456
for (auto it = mapInfo.cbegin(); it != mapInfo.cend(); ) {
453457
if (it->second.fInTried == false && it->second.nRefCount == 0) {
@@ -459,10 +463,9 @@ class CAddrMan
459463
}
460464
}
461465
if (nLost + nLostUnk > 0) {
462-
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
466+
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
463467
}
464468

465-
RemoveInvalid();
466469

467470
Check();
468471
}
@@ -795,8 +798,6 @@ class CAddrMan
795798
//! Get address info for address
796799
CAddrInfo GetAddressInfo_(const CService& addr) EXCLUSIVE_LOCKS_REQUIRED(cs);
797800

798-
//! Remove invalid addresses.
799-
void RemoveInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs);
800801

801802
friend class CAddrManTest;
802803
friend class CAddrManDeterministic;

src/netaddress.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ static std::string IPv4ToString(Span<const uint8_t> a)
544544

545545
// Return an IPv6 address text representation with zero compression as described in RFC 5952
546546
// ("A Recommendation for IPv6 Address Text Representation").
547-
static std::string IPv6ToString(Span<const uint8_t> a)
547+
static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
548548
{
549549
assert(a.size() == ADDR_IPV6_SIZE);
550550
const std::array groups{
@@ -592,6 +592,10 @@ static std::string IPv6ToString(Span<const uint8_t> a)
592592
r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
593593
}
594594

595+
if (scope_id != 0) {
596+
r += strprintf("%%%u", scope_id);
597+
}
598+
595599
return r;
596600
}
597601

@@ -612,14 +616,14 @@ std::string CNetAddr::ToStringIP() const
612616
case NET_IPV4:
613617
return IPv4ToString(m_addr);
614618
case NET_IPV6: {
615-
return IPv6ToString(m_addr);
619+
return IPv6ToString(m_addr, m_scope_id);
616620
}
617621
case NET_ONION:
618622
return OnionToString(m_addr);
619623
case NET_I2P:
620624
return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
621625
case NET_CJDNS:
622-
return IPv6ToString(m_addr);
626+
return IPv6ToString(m_addr, 0);
623627
case NET_INTERNAL:
624628
return EncodeBase32(m_addr) + ".internal";
625629
case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE

src/test/net_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,17 @@ BOOST_AUTO_TEST_CASE(cnetaddr_basic)
302302

303303
// IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
304304
// We support non-negative decimal integers (uint32_t) as zone id indices.
305-
// Test with a fairly-high value, e.g. 32, to avoid locally reserved ids.
305+
// Normal link-local scoped address functionality is to append "%" plus the
306+
// zone id, for example, given a link-local address of "fe80::1" and a zone
307+
// id of "32", return the address as "fe80::1%32".
306308
const std::string link_local{"fe80::1"};
307309
const std::string scoped_addr{link_local + "%32"};
308310
BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
309311
BOOST_REQUIRE(addr.IsValid());
310312
BOOST_REQUIRE(addr.IsIPv6());
311313
BOOST_CHECK(!addr.IsBindAny());
314+
BOOST_CHECK_EQUAL(addr.ToString(), scoped_addr);
315+
312316
// Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
313317
BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
314318
BOOST_REQUIRE(addr.IsValid());

0 commit comments

Comments
 (0)