From 2416631c6a300345ab1d534470594f25a62cac13 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 3 Sep 2023 20:37:58 +0100 Subject: [PATCH] Ensure limiter has space for at least 1 item. --- nano/core_test/scheduler_limiter.cpp | 6 +++--- nano/node/scheduler/buckets.cpp | 4 +++- nano/node/scheduler/hinted.cpp | 2 +- nano/node/scheduler/limiter.cpp | 1 + nano/node/scheduler/optimistic.cpp | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/nano/core_test/scheduler_limiter.cpp b/nano/core_test/scheduler_limiter.cpp index 4beee2ae08..4b81f0dbcd 100644 --- a/nano/core_test/scheduler_limiter.cpp +++ b/nano/core_test/scheduler_limiter.cpp @@ -7,9 +7,9 @@ TEST (scheduler_limiter, construction) { - auto occupancy = std::make_shared (nano::test::active_transactions_insert_null, 0, nano::election_behavior::normal); - ASSERT_EQ (0, occupancy->limit ()); - ASSERT_FALSE (occupancy->available ()); + auto occupancy = std::make_shared (nano::test::active_transactions_insert_null, 1, nano::election_behavior::normal); + ASSERT_EQ (1, occupancy->limit ()); + ASSERT_TRUE (occupancy->available ()); } TEST (scheduler_limiter, limit) diff --git a/nano/node/scheduler/buckets.cpp b/nano/node/scheduler/buckets.cpp index e7b462bcf2..e817ca9aab 100644 --- a/nano/node/scheduler/buckets.cpp +++ b/nano/node/scheduler/buckets.cpp @@ -76,7 +76,9 @@ nano::scheduler::buckets::buckets (insert_t const & insert, uint64_t maximum) : minimums.push_back (uint128_t{ 1 } << 120); for (auto i = 0u; i < minimums.size (); ++i) { - auto limiter_l = std::make_shared (insert, maximum / minimums.size (), nano::election_behavior::normal); + auto count = std::max (maximum / minimums.size (), 1u); + debug_assert (count > 0); + auto limiter_l = std::make_shared (insert, count, nano::election_behavior::normal); buckets_m.emplace_back (limiter_l); } populate_schedule (); diff --git a/nano/node/scheduler/hinted.cpp b/nano/node/scheduler/hinted.cpp index a6ea48f6cc..926d40f8bf 100644 --- a/nano/node/scheduler/hinted.cpp +++ b/nano/node/scheduler/hinted.cpp @@ -12,7 +12,7 @@ nano::scheduler::hinted::hinted (config const & config_a, nano::node & node_a, n config_m{ config_a }, node{ node_a }, inactive_vote_cache{ inactive_vote_cache_a }, - limiter{ std::make_shared (node.active.insert_fn (), node.config.active_elections_hinted_limit_percentage * node.config.active_elections_size / 100, nano::election_behavior::hinted) }, + limiter{ std::make_shared (node.active.insert_fn (), std::max (node.config.active_elections_hinted_limit_percentage * node.config.active_elections_size / 100, 1u), nano::election_behavior::hinted) }, online_reps{ online_reps_a }, stats{ stats_a } { } diff --git a/nano/node/scheduler/limiter.cpp b/nano/node/scheduler/limiter.cpp index 90b8e466bc..4cde8e61fe 100644 --- a/nano/node/scheduler/limiter.cpp +++ b/nano/node/scheduler/limiter.cpp @@ -10,6 +10,7 @@ nano::scheduler::limiter::limiter (insert_t const & insert, size_t limit, nano:: limit_m{ limit }, behavior{ behavior } { + debug_assert (limit > 0); } size_t nano::scheduler::limiter::limit () const diff --git a/nano/node/scheduler/optimistic.cpp b/nano/node/scheduler/optimistic.cpp index 14124afd09..be6f4dab7f 100644 --- a/nano/node/scheduler/optimistic.cpp +++ b/nano/node/scheduler/optimistic.cpp @@ -8,7 +8,7 @@ nano::scheduler::optimistic::optimistic (optimistic_config const & config_a, nan config{ config_a }, node{ node_a }, ledger{ ledger_a }, - limiter{ std::make_shared (node.active.insert_fn (), node.config.active_elections_optimistic_limit_percentage * node.config.active_elections_size / 100, nano::election_behavior::optimistic) }, + limiter{ std::make_shared (node.active.insert_fn (), std::max (node.config.active_elections_optimistic_limit_percentage * node.config.active_elections_size / 100, 1u), nano::election_behavior::optimistic) }, network_constants{ network_constants_a }, stats{ stats_a } {