@@ -4847,26 +4847,35 @@ static void RelayTransaction(const CTransaction& tx, CConnman& connman)
4847
4847
4848
4848
static void RelayAddress (const CAddress& addr, bool fReachable , CConnman& connman)
4849
4849
{
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)
4851
4851
4852
4852
// Relay to a limited number of other nodes
4853
4853
// Use deterministic randomness to send to the same nodes for 24 hours
4854
4854
// at a time so the addrKnowns of the chosen nodes prevent repeats
4855
4855
uint64_t hashAddr = addr.GetHash ();
4856
- std::multimap<uint64_t , CNode*> mapMix;
4857
4856
const CSipHasher hasher = connman.GetDeterministicRandomizer (RANDOMIZER_ID_ADDRESS_RELAY).Write (hashAddr << 32 ).Write ((GetTime () + hashAddr) / (24 *60 *60 ));
4858
4857
FastRandomContext insecure_rand;
4859
4858
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) {
4861
4863
if (pnode->nVersion >= CADDR_TIME_VERSION) {
4862
4864
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
+ }
4864
4872
}
4865
4873
};
4866
4874
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
+ }
4870
4879
};
4871
4880
4872
4881
connman.ForEachNodeThen (std::move (sortfunc), std::move (pushfunc));
0 commit comments