14
14
#include " clientversion.h"
15
15
#include " consensus/consensus.h"
16
16
#include " crypto/common.h"
17
+ #include " crypto/sha256.h"
17
18
#include " hash.h"
18
19
#include " primitives/transaction.h"
19
20
#include " scheduler.h"
@@ -838,6 +839,7 @@ struct NodeEvictionCandidate
838
839
int64_t nTimeConnected;
839
840
int64_t nMinPingUsecTime;
840
841
CAddress addr;
842
+ std::vector<unsigned char > vchKeyedNetGroup;
841
843
};
842
844
843
845
static bool ReverseCompareNodeMinPingTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
@@ -850,36 +852,8 @@ static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, cons
850
852
return a.nTimeConnected > b.nTimeConnected ;
851
853
}
852
854
853
- class CompareNetGroupKeyed
854
- {
855
- std::vector<unsigned char > vchSecretKey;
856
- public:
857
- CompareNetGroupKeyed ()
858
- {
859
- vchSecretKey.resize (32 , 0 );
860
- GetRandBytes (vchSecretKey.data (), vchSecretKey.size ());
861
- }
862
-
863
- bool operator ()(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
864
- {
865
- std::vector<unsigned char > vchGroupA, vchGroupB;
866
- CSHA256 hashA, hashB;
867
- std::vector<unsigned char > vchA (32 ), vchB (32 );
868
-
869
- vchGroupA = a.addr .GetGroup ();
870
- vchGroupB = b.addr .GetGroup ();
871
-
872
- hashA.Write (begin_ptr (vchGroupA), vchGroupA.size ());
873
- hashB.Write (begin_ptr (vchGroupB), vchGroupB.size ());
874
-
875
- hashA.Write (begin_ptr (vchSecretKey), vchSecretKey.size ());
876
- hashB.Write (begin_ptr (vchSecretKey), vchSecretKey.size ());
877
-
878
- hashA.Finalize (begin_ptr (vchA));
879
- hashB.Finalize (begin_ptr (vchB));
880
-
881
- return vchA < vchB;
882
- }
855
+ static bool CompareNetGroupKeyed (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
856
+ return a.vchKeyedNetGroup < b.vchKeyedNetGroup ;
883
857
};
884
858
885
859
/* * Try to find a connection to evict when the node is full.
@@ -902,7 +876,7 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
902
876
continue ;
903
877
if (node->fDisconnect )
904
878
continue ;
905
- NodeEvictionCandidate candidate = {node->id , node->nTimeConnected , node->nMinPingUsecTime , node->addr };
879
+ NodeEvictionCandidate candidate = {node->id , node->nTimeConnected , node->nMinPingUsecTime , node->addr , node-> vchKeyedNetGroup };
906
880
vEvictionCandidates.push_back (candidate);
907
881
}
908
882
}
@@ -912,9 +886,8 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
912
886
// Protect connections with certain characteristics
913
887
914
888
// Deterministically select 4 peers to protect by netgroup.
915
- // An attacker cannot predict which netgroups will be protected.
916
- static CompareNetGroupKeyed comparerNetGroupKeyed;
917
- std::sort (vEvictionCandidates.begin (), vEvictionCandidates.end (), comparerNetGroupKeyed);
889
+ // An attacker cannot predict which netgroups will be protected
890
+ std::sort (vEvictionCandidates.begin (), vEvictionCandidates.end (), CompareNetGroupKeyed);
918
891
vEvictionCandidates.erase (vEvictionCandidates.end () - std::min (4 , static_cast <int >(vEvictionCandidates.size ())), vEvictionCandidates.end ());
919
892
920
893
if (vEvictionCandidates.empty ()) return false ;
@@ -2392,6 +2365,8 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
2392
2365
lastSentFeeFilter = 0 ;
2393
2366
nextSendTimeFeeFilter = 0 ;
2394
2367
2368
+ CalculateKeyedNetGroup ();
2369
+
2395
2370
BOOST_FOREACH (const std::string &msg, getAllNetMessageTypes ())
2396
2371
mapRecvBytesPerMsgCmd[msg] = 0 ;
2397
2372
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0 ;
0 commit comments