Skip to content

Commit

Permalink
Separate election state for the broadcasting block fallback (#2720)
Browse files Browse the repository at this point in the history
Instead of basing only on time, a hard limit on confirmation requests helps avoid resorting to fallback mechanisms (block broadcasting, escalating to dependents) too soon.

This is implemented with a new state broadcasting , and before moving to this state at least two confirmation requests must be done. This indirectly extends the time before moving to backtracking by 10 seconds.

Time between broadcasts reduced to 15 from 20 seconds.
  • Loading branch information
guilhermelawless committed Apr 22, 2020
1 parent 86f3cd8 commit e64f0b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
28 changes: 24 additions & 4 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
using namespace std::chrono;

int constexpr nano::election::passive_duration_factor;
int constexpr nano::election::active_duration_factor;
int constexpr nano::election::active_request_count_min;
int constexpr nano::election::active_broadcasting_duration_factor;
int constexpr nano::election::confirmed_duration_factor;

std::chrono::milliseconds nano::election::base_latency () const
Expand Down Expand Up @@ -98,6 +99,18 @@ bool nano::election::valid_change (nano::election::state_t expected_a, nano::ele
}
break;
case nano::election::state_t::active:
switch (desired_a)
{
case nano::election::state_t::idle:
case nano::election::state_t::broadcasting:
case nano::election::state_t::confirmed:
case nano::election::state_t::expired_unconfirmed:
result = true;
break;
default:
break;
}
case nano::election::state_t::broadcasting:
switch (desired_a)
{
case nano::election::state_t::idle:
Expand Down Expand Up @@ -246,7 +259,7 @@ void nano::election::activate_dependencies ()

void nano::election::broadcast_block (nano::confirmation_solicitor & solicitor_a)
{
if (base_latency () * 20 < std::chrono::steady_clock::now () - last_block)
if (base_latency () * 15 < std::chrono::steady_clock::now () - last_block)
{
if (!solicitor_a.broadcast (*this))
{
Expand All @@ -273,11 +286,18 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a
break;
}
case nano::election::state_t::active:
send_confirm_req (solicitor_a);
if (confirmation_request_count > active_request_count_min)
{
state_change (nano::election::state_t::active, nano::election::state_t::broadcasting);
}
break;
case nano::election::state_t::broadcasting:
broadcast_block (solicitor_a);
send_confirm_req (solicitor_a);
if (base_latency () * active_duration_factor < std::chrono::steady_clock::now () - state_start)
if (base_latency () * active_broadcasting_duration_factor < std::chrono::steady_clock::now () - state_start)
{
state_change (nano::election::state_t::active, nano::election::state_t::backtracking);
state_change (nano::election::state_t::broadcasting, nano::election::state_t::backtracking);
lock.unlock ();
activate_dependencies ();
lock.lock ();
Expand Down
12 changes: 7 additions & 5 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class election final : public std::enable_shared_from_this<nano::election>
enum class state_t
{
idle,
passive,
active,
backtracking,
confirmed,
passive, // only listening for incoming votes
active, // actively request confirmations
broadcasting, // request confirmations and broadcast the winner
backtracking, // start an election for unconfirmed dependent blocks
confirmed, // confirmed but still listening for votes
expired_confirmed,
expired_unconfirmed
};
static int constexpr passive_duration_factor = 5;
static int constexpr active_duration_factor = 30;
static int constexpr active_request_count_min = 2;
static int constexpr active_broadcasting_duration_factor = 30;
static int constexpr confirmed_duration_factor = 5;
std::atomic<nano::election::state_t> state_m = { state_t::idle };

Expand Down

0 comments on commit e64f0b0

Please sign in to comment.