Changeset View
Changeset View
Standalone View
Standalone View
src/net.cpp
Show First 20 Lines • Show All 889 Lines • ▼ Show 20 Lines | static bool CompareNodeTXTime(const NodeEvictionCandidate &a, | ||||
if (a.fBloomFilter != b.fBloomFilter) { | if (a.fBloomFilter != b.fBloomFilter) { | ||||
return a.fBloomFilter; | return a.fBloomFilter; | ||||
} | } | ||||
return a.nTimeConnected > b.nTimeConnected; | return a.nTimeConnected > b.nTimeConnected; | ||||
} | } | ||||
static bool CompareNodeProofTime(const NodeEvictionCandidate &a, | |||||
const NodeEvictionCandidate &b) { | |||||
// There is a fall-through here because it is common for a node to have more | |||||
// than a few peers that have not yet relayed proofs. This fallback is also | |||||
// used in the case avalanche is not enabled. | |||||
if (a.nLastProofTime != b.nLastProofTime) { | |||||
return a.nLastProofTime < b.nLastProofTime; | |||||
} | |||||
return a.nTimeConnected > b.nTimeConnected; | |||||
} | |||||
// Pick out the potential block-relay only peers, and sort them by last block | // Pick out the potential block-relay only peers, and sort them by last block | ||||
// time. | // time. | ||||
static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, | static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, | ||||
const NodeEvictionCandidate &b) { | const NodeEvictionCandidate &b) { | ||||
if (a.fRelayTxes != b.fRelayTxes) { | if (a.fRelayTxes != b.fRelayTxes) { | ||||
return a.fRelayTxes; | return a.fRelayTxes; | ||||
} | } | ||||
Show All 27 Lines | SelectNodeToEvict(std::vector<NodeEvictionCandidate> &&vEvictionCandidates) { | ||||
// Protect the 8 nodes with the lowest minimum ping time. | // Protect the 8 nodes with the lowest minimum ping time. | ||||
// An attacker cannot manipulate this metric without physically moving nodes | // An attacker cannot manipulate this metric without physically moving nodes | ||||
// closer to the target. | // closer to the target. | ||||
EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8); | EraseLastKElements(vEvictionCandidates, ReverseCompareNodeMinPingTime, 8); | ||||
// Protect 4 nodes that most recently sent us novel transactions accepted | // Protect 4 nodes that most recently sent us novel transactions accepted | ||||
// into our mempool. An attacker cannot manipulate this metric without | // into our mempool. An attacker cannot manipulate this metric without | ||||
// performing useful work. | // performing useful work. | ||||
EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4); | EraseLastKElements(vEvictionCandidates, CompareNodeTXTime, 4); | ||||
// Protect 4 nodes that most recently sent us novel proofs accepted | |||||
// into our proof pool. An attacker cannot manipulate this metric without | |||||
// performing useful work. | |||||
// TODO this filter must happen before the last tx time once avalanche is | |||||
// enabled for pre-consensus. | |||||
EraseLastKElements(vEvictionCandidates, CompareNodeProofTime, 4); | |||||
// Protect up to 8 non-tx-relay peers that have sent us novel blocks. | // Protect up to 8 non-tx-relay peers that have sent us novel blocks. | ||||
std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), | std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), | ||||
CompareNodeBlockRelayOnlyTime); | CompareNodeBlockRelayOnlyTime); | ||||
size_t erase_size = std::min(size_t(8), vEvictionCandidates.size()); | size_t erase_size = std::min(size_t(8), vEvictionCandidates.size()); | ||||
vEvictionCandidates.erase( | vEvictionCandidates.erase( | ||||
std::remove_if(vEvictionCandidates.end() - erase_size, | std::remove_if(vEvictionCandidates.end() - erase_size, | ||||
vEvictionCandidates.end(), | vEvictionCandidates.end(), | ||||
[](NodeEvictionCandidate const &n) { | [](NodeEvictionCandidate const &n) { | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | std::vector<NodeEvictionCandidate> vEvictionCandidates; | ||||
peer_relay_txes = node->m_tx_relay->fRelayTxes; | peer_relay_txes = node->m_tx_relay->fRelayTxes; | ||||
peer_filter_not_null = node->m_tx_relay->pfilter != nullptr; | peer_filter_not_null = node->m_tx_relay->pfilter != nullptr; | ||||
} | } | ||||
NodeEvictionCandidate candidate = { | NodeEvictionCandidate candidate = { | ||||
node->GetId(), | node->GetId(), | ||||
node->nTimeConnected, | node->nTimeConnected, | ||||
node->nMinPingUsecTime, | node->nMinPingUsecTime, | ||||
node->nLastBlockTime, | node->nLastBlockTime, | ||||
node->nLastProofTime, | |||||
node->nLastTXTime, | node->nLastTXTime, | ||||
HasAllDesirableServiceFlags(node->nServices), | HasAllDesirableServiceFlags(node->nServices), | ||||
peer_relay_txes, | peer_relay_txes, | ||||
peer_filter_not_null, | peer_filter_not_null, | ||||
node->nKeyedNetGroup, | node->nKeyedNetGroup, | ||||
node->m_prefer_evict, | node->m_prefer_evict, | ||||
node->addr.IsLocal()}; | node->addr.IsLocal()}; | ||||
vEvictionCandidates.push_back(candidate); | vEvictionCandidates.push_back(candidate); | ||||
▲ Show 20 Lines • Show All 2,143 Lines • Show Last 20 Lines |