Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | struct Peer { | ||||
ProofRef proof; | ProofRef proof; | ||||
bool hasFinalized = false; | bool hasFinalized = false; | ||||
// The network stack uses timestamp in seconds, so we oblige. | // The network stack uses timestamp in seconds, so we oblige. | ||||
std::chrono::seconds registration_time; | std::chrono::seconds registration_time; | ||||
std::chrono::seconds nextPossibleConflictTime; | std::chrono::seconds nextPossibleConflictTime; | ||||
double availabilityScore = 0.0; | |||||
/** | /** | ||||
* Consider dropping the peer if no node is attached after this timeout | * Consider dropping the peer if no node is attached after this timeout | ||||
* expired. | * expired. | ||||
*/ | */ | ||||
static constexpr auto DANGLING_TIMEOUT = 15min; | static constexpr auto DANGLING_TIMEOUT = 15min; | ||||
Peer(PeerId peerid_, ProofRef proof_, | Peer(PeerId peerid_, ProofRef proof_, | ||||
std::chrono::seconds nextPossibleConflictTime_) | std::chrono::seconds nextPossibleConflictTime_) | ||||
▲ Show 20 Lines • Show All 269 Lines • ▼ Show 20 Lines | public: | ||||
auto getUnbroadcastProofs() const { return m_unbroadcast_proofids; } | auto getUnbroadcastProofs() const { return m_unbroadcast_proofids; } | ||||
/* | /* | ||||
* Quorum management | * Quorum management | ||||
*/ | */ | ||||
uint32_t getTotalPeersScore() const { return totalPeersScore; } | uint32_t getTotalPeersScore() const { return totalPeersScore; } | ||||
uint32_t getConnectedPeersScore() const { return connectedPeersScore; } | uint32_t getConnectedPeersScore() const { return connectedPeersScore; } | ||||
template <typename Callable> | |||||
void updateAvailabilityScores(const double decayFactor, | |||||
Callable &&getNodeAvailabilityScore) { | |||||
for (auto it = peers.begin(); it != peers.end(); it++) { | |||||
peers.modify(it, [&](Peer &peer) { | |||||
// Calculate average of current node scores | |||||
double peerScore{0.0}; | |||||
forEachNode(peer, [&](const avalanche::Node &node) { | |||||
peerScore += getNodeAvailabilityScore(node.nodeid); | |||||
}); | |||||
peerScore /= peer.node_count; | |||||
// Calculate exponential moving average of averaged node scores | |||||
peer.availabilityScore = | |||||
decayFactor * peerScore + | |||||
(1. - decayFactor) * peer.availabilityScore; | |||||
}); | |||||
} | |||||
} | |||||
/**************************************************** | /**************************************************** | ||||
* Functions which are public for testing purposes. * | * Functions which are public for testing purposes. * | ||||
****************************************************/ | ****************************************************/ | ||||
/** | /** | ||||
* Remove an existing peer. | * Remove an existing peer. | ||||
*/ | */ | ||||
bool removePeer(const PeerId peerid); | bool removePeer(const PeerId peerid); | ||||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |