diff --git a/src/avalanche/peermanager.h b/src/avalanche/peermanager.h --- a/src/avalanche/peermanager.h +++ b/src/avalanche/peermanager.h @@ -17,13 +17,13 @@ uint64_t score; public: - Slot(uint64_t startIn, uint64_t stop) - : start(startIn), score(stop - start) {} + Slot(uint64_t startIn, uint64_t scoreIn) : start(startIn), score(scoreIn) {} - Slot withScore(uint64_t scoreIn) { return Slot(start, start + scoreIn); } + Slot withScore(uint64_t scoreIn) { return Slot(start, scoreIn); } uint64_t getStart() const { return start; } uint64_t getStop() const { return start + score; } + uint64_t getScore() const { return score; } bool contains(uint64_t slot) const { return getStart() <= slot && slot < getStop(); diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -8,9 +8,8 @@ void PeerManager::addPeer(uint64_t score) { const uint64_t start = slotCount; - const uint64_t stop = start + score; - slots.emplace_back(start, stop); - slotCount = stop; + slots.emplace_back(start, score); + slotCount = start + score; } void PeerManager::rescorePeer(size_t i, uint64_t score) { diff --git a/src/avalanche/test/peermanager_tests.cpp b/src/avalanche/test/peermanager_tests.cpp --- a/src/avalanche/test/peermanager_tests.cpp +++ b/src/avalanche/test/peermanager_tests.cpp @@ -16,7 +16,7 @@ BOOST_CHECK_EQUAL(selectPeerImpl({}, 1, 3), NO_PEER); // One peer - const std::vector oneslot = {{100, 200}}; + const std::vector oneslot = {{100, 100}}; // Undershoot BOOST_CHECK_EQUAL(selectPeerImpl(oneslot, 0, 300), NO_PEER); @@ -34,7 +34,7 @@ BOOST_CHECK_EQUAL(selectPeerImpl(oneslot, 299, 300), NO_PEER); // Two peers - const std::vector twoslots = {{100, 200}, {300, 400}}; + const std::vector twoslots = {{100, 100}, {300, 100}}; // Undershoot BOOST_CHECK_EQUAL(selectPeerImpl(twoslots, 0, 500), NO_PEER); @@ -68,7 +68,7 @@ // 100 peers of size 1 with 1 empty element apart. uint64_t max = 1; for (int i = 0; i < 100; i++) { - slots.emplace_back(max, max + 1); + slots.emplace_back(max, 1); max += 2; } @@ -99,11 +99,13 @@ // Update the slots to be heavily skewed toward the first element. for (int i = 0; i < 100; i++) { - slots[i] = Slot(slots[i].getStart() + 100, slots[i].getStop() + 100); + slots[i] = Slot(slots[i].getStart() + 100, slots[i].getScore()); } - slots[0] = Slot(1, slots[0].getStop()); - slots[99] = Slot(slots[99].getStart(), 300); + slots[0] = Slot(1, slots[0].getStop() - 1); + slots[99] = slots[99].withScore(1); + max = slots[99].getStop(); + BOOST_CHECK_EQUAL(max, 300); BOOST_CHECK_EQUAL(selectPeerImpl(slots, 0, max), NO_PEER); BOOST_CHECK_EQUAL(selectPeerImpl(slots, 1, max), 0); @@ -129,9 +131,10 @@ }; for (size_t i = 0; i < size; i++) { - uint64_t start = next(); - uint64_t stop = next(); - slots.emplace_back(start, stop); + const uint64_t start = next(); + const uint32_t score = InsecureRandBits(3); + max += score; + slots.emplace_back(start, score); } for (int k = 0; k < 100; k++) {