Skip to content

Commit

Permalink
Apply logic change of bitcoin#23306 (multiple ports per IP)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzumsande committed Oct 31, 2021
1 parent c079ee7 commit bd0a52f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
36 changes: 18 additions & 18 deletions src/addrman_multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ void AddrManMultiImpl::Unserialize(Stream& s_)
// one tried entry for a given address, and there can't be both tried and
// new ones simultaneously).
bool alias = false;
auto it_addr = m_index.get<ByAddress>().lower_bound(std::pair<const CNetAddr&, bool>(info, false));
if (it_addr != m_index.get<ByAddress>().end() && static_cast<const CNetAddr&>(*it_addr) == info) {
auto it_addr = m_index.get<ByAddress>().lower_bound(std::pair<const CService&, bool>(info, false));
if (it_addr != m_index.get<ByAddress>().end() && static_cast<const CService&>(*it_addr) == info) {
if (info.fInTried) {
do {
Erase(it_addr);
it_addr = m_index.get<ByAddress>().lower_bound(std::pair<const CNetAddr&, bool>(info, false));
} while (it_addr != m_index.get<ByAddress>().end() && static_cast<const CNetAddr&>(*it_addr) == info);
it_addr = m_index.get<ByAddress>().lower_bound(std::pair<const CService&, bool>(info, false));
} while (it_addr != m_index.get<ByAddress>().end() && static_cast<const CService&>(*it_addr) == info);
} else {
alias = true;
}
Expand Down Expand Up @@ -319,12 +319,12 @@ void AddrManMultiImpl::Unserialize(Stream& s_)
}
}

int AddrManMultiImpl::CountAddr(const CNetAddr& addr) const
int AddrManMultiImpl::CountAddr(const CService& addr) const
{
AssertLockHeld(cs);
auto it = m_index.get<ByAddress>().lower_bound(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().lower_bound(std::pair<const CService&, bool>(addr, false));
if (it == m_index.get<ByAddress>().end()) return 0;
auto it_end = m_index.get<ByAddress>().upper_bound(std::pair<const CNetAddr&, bool>(addr, true));
auto it_end = m_index.get<ByAddress>().upper_bound(std::pair<const CService&, bool>(addr, true));
return std::distance(it, it_end);
}

Expand All @@ -346,7 +346,7 @@ void AddrManMultiImpl::EraseInner(AddrManIndex::index<ByAddress>::type::iterator
// In case the entry being deleted has an alias, we don't delete the requested one, but
// the alias instead. The alias' source IP is moved to the actual entry however, so
// it is preserved.
auto it_alias = m_index.get<ByAddress>().find(std::make_pair<const CNetAddr&, bool>(*it, true));
auto it_alias = m_index.get<ByAddress>().find(std::make_pair<const CService&, bool>(*it, true));
if (it_alias != m_index.get<ByAddress>().end()) {
if (m_tried_collisions.count(&*it_alias)) m_tried_collisions.insert(&*it);
Modify(it, [&](AddrInfo& info) { info.source = it_alias->source; });
Expand Down Expand Up @@ -393,8 +393,8 @@ void AddrManMultiImpl::MakeTried(AddrManIndex::index<ByAddress>::type::iterator
Erase(it);
// remove the entry from all new buckets
while (true) {
auto it_existing = m_index.get<ByAddress>().lower_bound(std::pair<const CNetAddr&, bool>(info, false));
if (it_existing == m_index.get<ByAddress>().end() || *it_existing != static_cast<const CNetAddr&>(info)) break;
auto it_existing = m_index.get<ByAddress>().lower_bound(std::pair<const CService&, bool>(info, false));
if (it_existing == m_index.get<ByAddress>().end() || *it_existing != static_cast<const CService&>(info)) break;
Erase(it_existing);
}

Expand Down Expand Up @@ -433,7 +433,7 @@ void AddrManMultiImpl::Good_(const CService& addr, bool test_before_evict, int64

nLastGood = nTime;

auto it = m_index.get<ByAddress>().find(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().find(std::pair<const CService&, bool>(addr, false));

// if not found, bail out
if (it == m_index.get<ByAddress>().end()) return;
Expand Down Expand Up @@ -484,7 +484,7 @@ bool AddrManMultiImpl::Add_(const CAddress& addr, const CNetAddr& source, int64_
if (!addr.IsRoutable())
return false;

auto it = m_index.get<ByAddress>().find(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().find(std::pair<const CService&, bool>(addr, false));

// Do not set a penalty for a source's self-announcement
if (addr == source) {
Expand Down Expand Up @@ -535,7 +535,7 @@ bool AddrManMultiImpl::Add_(const CAddress& addr, const CNetAddr& source, int64_

info.Rebucket(nKey, m_asmap);
auto it_existing = m_index.get<ByBucket>().find(ByBucketExtractor()(info));
if (it_existing == m_index.get<ByBucket>().end() || static_cast<const CNetAddr&>(*it_existing) != addr) {
if (it_existing == m_index.get<ByBucket>().end() || static_cast<const CService&>(*it_existing) != addr) {
bool fInsert = it_existing == m_index.get<ByBucket>().end();
if (!fInsert) {
const AddrInfo& infoExisting = *it_existing;
Expand All @@ -559,7 +559,7 @@ void AddrManMultiImpl::Attempt_(const CService& addr, bool fCountFailure, int64_
{
AssertLockHeld(cs);

auto it = m_index.get<ByAddress>().find(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().find(std::pair<const CService&, bool>(addr, false));

// if not found, bail out
if (it == m_index.get<ByAddress>().end()) return;
Expand Down Expand Up @@ -678,7 +678,7 @@ void AddrManMultiImpl::Connected_(const CService& addr, int64_t nTime)
{
AssertLockHeld(cs);

auto it = m_index.get<ByAddress>().find(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().find(std::pair<const CService&, bool>(addr, false));

// if not found, bail out
if (it == m_index.get<ByAddress>().end()) return;
Expand All @@ -696,7 +696,7 @@ void AddrManMultiImpl::SetServices_(const CService& addr, ServiceFlags nServices
{
AssertLockHeld(cs);

auto it = m_index.get<ByAddress>().find(std::pair<const CNetAddr&, bool>(addr, false));
auto it = m_index.get<ByAddress>().find(std::pair<const CService&, bool>(addr, false));

// if not found, bail out
if (it == m_index.get<ByAddress>().end()) return;
Expand Down Expand Up @@ -815,7 +815,7 @@ int AddrManMultiImpl::ForceCheckAddrman() const
// Tried entries cannot have aliases.
if (info.fInTried) return -1;
// Aliases must have the same address as their precessor in this iteration order.
if (it == m_index.get<ByAddress>().begin() || static_cast<const CNetAddr&>(info) != *std::prev(it)) return -2;
if (it == m_index.get<ByAddress>().begin() || static_cast<const CService&>(info) != *std::prev(it)) return -2;
} else {
if ((size_t) info.nRandomPos >= vRandom.size()) return -22;
if (vRandom[info.nRandomPos] != it) return -23;
Expand All @@ -825,7 +825,7 @@ int AddrManMultiImpl::ForceCheckAddrman() const
counted_new++;
}
// Non-alias entries must have a different address as their predecessor in this iteration order.
if (it != m_index.get<ByAddress>().begin() && static_cast<const CNetAddr&>(info) == *std::prev(it)) return -3;
if (it != m_index.get<ByAddress>().begin() && static_cast<const CService&>(info) == *std::prev(it)) return -3;
}

AddrInfo copy = info;
Expand Down
4 changes: 2 additions & 2 deletions src/addrman_multi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class AddrManMultiImpl

struct ByAddressExtractor
{
using result_type = std::pair<const CNetAddr&, bool>;
using result_type = std::pair<const CService&, bool>;
result_type operator()(const AddrInfo& info) const { return {info, info.nRandomPos == -1}; }
};

Expand Down Expand Up @@ -252,7 +252,7 @@ class AddrManMultiImpl
const std::vector<bool> m_asmap;

//! Count the number of occurrences of entries with this address (including aliases).
int CountAddr(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(cs);
int CountAddr(const CService& addr) const EXCLUSIVE_LOCKS_REQUIRED(cs);

void UpdateStat(const AddrInfo& info, int inc) EXCLUSIVE_LOCKS_REQUIRED(cs);

Expand Down

0 comments on commit bd0a52f

Please sign in to comment.