Skip to content

Commit 791b58d

Browse files
committed
Merge #8690: Do not fully sort all nodes for addr relay
a33b169 Do not fully sort all nodes for addr relay (Pieter Wuille)
2 parents 5ea5e04 + a33b169 commit 791b58d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/main.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4847,26 +4847,35 @@ static void RelayTransaction(const CTransaction& tx, CConnman& connman)
48474847

48484848
static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
48494849
{
4850-
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
4850+
unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
48514851

48524852
// Relay to a limited number of other nodes
48534853
// Use deterministic randomness to send to the same nodes for 24 hours
48544854
// at a time so the addrKnowns of the chosen nodes prevent repeats
48554855
uint64_t hashAddr = addr.GetHash();
4856-
std::multimap<uint64_t, CNode*> mapMix;
48574856
const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
48584857
FastRandomContext insecure_rand;
48594858

4860-
auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
4859+
std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
4860+
assert(nRelayNodes <= best.size());
4861+
4862+
auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
48614863
if (pnode->nVersion >= CADDR_TIME_VERSION) {
48624864
uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
4863-
mapMix.emplace(hashKey, pnode);
4865+
for (unsigned int i = 0; i < nRelayNodes; i++) {
4866+
if (hashKey > best[i].first) {
4867+
std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
4868+
best[i] = std::make_pair(hashKey, pnode);
4869+
break;
4870+
}
4871+
}
48644872
}
48654873
};
48664874

4867-
auto pushfunc = [&addr, &mapMix, &nRelayNodes, &insecure_rand] {
4868-
for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
4869-
mi->second->PushAddress(addr, insecure_rand);
4875+
auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
4876+
for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
4877+
best[i].second->PushAddress(addr, insecure_rand);
4878+
}
48704879
};
48714880

48724881
connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));

0 commit comments

Comments
 (0)