Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 2,144 Lines • ▼ Show 20 Lines | for (const auto &[step, tau, decayFactor] : testCases) { | ||||
}); | }); | ||||
// Expect a monotonic rise | // Expect a monotonic rise | ||||
double currentScore = getAvailabilityScore(); | double currentScore = getAvailabilityScore(); | ||||
BOOST_CHECK_GE(currentScore, previousScore); | BOOST_CHECK_GE(currentScore, previousScore); | ||||
previousScore = currentScore; | previousScore = currentScore; | ||||
} | } | ||||
// We expect (1 - e^-i) after i * tau. The tolerance is expressed | // We expect (1 - e^-i) * numNodesPerPeer after i * tau. The | ||||
// as a percentage, and we add a (large) 0.1% margin to account for | // tolerance is expressed as a percentage, and we add a (large) | ||||
// floating point errors. | // 0.1% margin to account for floating point errors. | ||||
BOOST_CHECK_CLOSE(previousScore, -1 * std::expm1(-1. * i), | BOOST_CHECK_CLOSE(previousScore, | ||||
-1 * std::expm1(-1. * i) * numNodesPerPeer, | |||||
100.1 / tau); | 100.1 / tau); | ||||
} | } | ||||
// After 10 tau we should be very close to 100% (about 99.995%) | // After 10 tau we should be very close to 100% (about 99.995%) | ||||
BOOST_CHECK_CLOSE(previousScore, 1., 0.01); | BOOST_CHECK_CLOSE(previousScore, numNodesPerPeer, 0.01); | ||||
// Make the proof invalid | // Make the proof invalid | ||||
BOOST_CHECK(pm.rejectProof( | BOOST_CHECK(pm.rejectProof( | ||||
proofid, avalanche::PeerManager::RejectionMode::INVALIDATE)); | proofid, avalanche::PeerManager::RejectionMode::INVALIDATE)); | ||||
BOOST_CHECK(!pm.isBoundToPeer(proofid)); | BOOST_CHECK(!pm.isBoundToPeer(proofid)); | ||||
BOOST_CHECK(!pm.exists(proofid)); | BOOST_CHECK(!pm.exists(proofid)); | ||||
// Re-register the proof | // Re-register the proof | ||||
Show All 13 Lines | for (const auto &[step, tau, decayFactor] : testCases) { | ||||
for (size_t i = 1; i <= 10; i++) { | for (size_t i = 1; i <= 10; i++) { | ||||
for (uint32_t j = 0; j < tau; j += step) { | for (uint32_t j = 0; j < tau; j += step) { | ||||
pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | ||||
return getNodeAvailabilityScore(1.0, nodeid); | return getNodeAvailabilityScore(1.0, nodeid); | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
previousScore = getAvailabilityScore(); | previousScore = getAvailabilityScore(); | ||||
BOOST_CHECK_CLOSE(previousScore, 1., 0.01); | BOOST_CHECK_CLOSE(previousScore, numNodesPerPeer, 0.01); | ||||
for (size_t i = 1; i <= 3; i++) { | for (size_t i = 1; i <= 3; i++) { | ||||
for (uint32_t j = 0; j < tau; j += step) { | for (uint32_t j = 0; j < tau; j += step) { | ||||
// Nodes only respond to polls 50% of the time (0 score) | // Nodes only respond to polls 50% of the time (0 score) | ||||
pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | ||||
return getNodeAvailabilityScore(0.0, nodeid); | return getNodeAvailabilityScore(0.0, nodeid); | ||||
}); | }); | ||||
// Expect a monotonic fall | // Expect a monotonic fall | ||||
double currentScore = getAvailabilityScore(); | double currentScore = getAvailabilityScore(); | ||||
BOOST_CHECK_LE(currentScore, previousScore); | BOOST_CHECK_LE(currentScore, previousScore); | ||||
previousScore = currentScore; | previousScore = currentScore; | ||||
} | } | ||||
// There is a slight error in the expected value because we did not | // There is a slight error in the expected value because we did not | ||||
// start the decay at exactly 100%, but the 0.1% margin is at least | // start the decay at exactly 100%, but the 0.1% margin is at least | ||||
// an order of magnitude larger than the expected error so it | // an order of magnitude larger than the expected error so it | ||||
// doesn't matter. | // doesn't matter. | ||||
BOOST_CHECK_CLOSE(previousScore, 1. + std::expm1(-1. * i), | BOOST_CHECK_CLOSE(previousScore, | ||||
(1. + std::expm1(-1. * i)) * numNodesPerPeer, | |||||
100.1 / tau); | 100.1 / tau); | ||||
} | } | ||||
// After 3 more tau we should be under 5% | // After 3 more tau we should be under 5% | ||||
BOOST_CHECK_LT(previousScore, .05); | BOOST_CHECK_LT(previousScore, .05 * numNodesPerPeer); | ||||
for (size_t i = 1; i <= 100; i++) { | for (size_t i = 1; i <= 100; i++) { | ||||
// Nodes respond to polls < 50% of the time (negative score) | // Nodes respond to polls < 50% of the time (negative score) | ||||
pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | pm.updateAvailabilityScores(decayFactor, [&](auto nodeid) { | ||||
return getNodeAvailabilityScore(-10.0, nodeid); | return getNodeAvailabilityScore(-10.0, nodeid); | ||||
}); | }); | ||||
// It's still a monotonic fall, and the score should turn negative. | // It's still a monotonic fall, and the score should turn negative. | ||||
Show All 9 Lines |