Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirmation requests and broadcasts if available vote is for a conflicting block #2784

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions nano/core_test/confirmation_solicitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ TEST (confirmation_solicitor, batches)
nano::system system;
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
node_flags.disable_rep_crawler = true;
node_flags.disable_udp = false;
auto & node1 = *system.add_node (node_flags);
// This tests instantiates a solicitor
node_flags.disable_request_loop = true;
auto & node2 = *system.add_node (node_flags);
// Solicitor will only solicit from this representative
auto channel1 (node2.network.udp_channels.create (node1.network.endpoint ()));
// Solicitor will only solicit from this representative
nano::representative representative (nano::test_genesis_key.pub, nano::genesis_amount, channel1);

std::vector<nano::representative> representatives{ representative };
nano::confirmation_solicitor solicitor (node2.network, node2.network_params.network);
solicitor.prepare (representatives);
// Ensure the representatives are correct
ASSERT_EQ (1, representatives.size ());
ASSERT_EQ (channel1, representatives.front ().channel);
ASSERT_EQ (nano::test_genesis_key.pub, representatives.front ().account);
ASSERT_TIMELY (3s, node2.network.size () == 1);
auto send (std::make_shared<nano::send_block> (nano::genesis_hash, nano::keypair ().pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash)));
{
nano::lock_guard<std::mutex> guard (node2.active.mutex);
Expand All @@ -44,17 +44,44 @@ TEST (confirmation_solicitor, batches)
ASSERT_EQ (0, node2.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out));
ASSERT_FALSE (solicitor.broadcast (*election));
}
system.deadline_set (5s);
while (node2.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out) < 1)
{
ASSERT_NO_ERROR (system.poll ());
}
// From rep crawler
system.deadline_set (5s);
// One publish through directed broadcasting and another through random flooding
ASSERT_EQ (2, node2.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out));
solicitor.flush ();
while (node2.stats.count (nano::stat::type::message, nano::stat::detail::confirm_req, nano::stat::dir::out) == 1)
ASSERT_EQ (1, node2.stats.count (nano::stat::type::message, nano::stat::detail::confirm_req, nano::stat::dir::out));
}

TEST (confirmation_solicitor, different_hash)
{
nano::system system;
nano::node_flags node_flags;
node_flags.disable_request_loop = true;
node_flags.disable_rep_crawler = true;
node_flags.disable_udp = false;
auto & node1 = *system.add_node (node_flags);
auto & node2 = *system.add_node (node_flags);
auto channel1 (node2.network.udp_channels.create (node1.network.endpoint ()));
// Solicitor will only solicit from this representative
nano::representative representative (nano::test_genesis_key.pub, nano::genesis_amount, channel1);
std::vector<nano::representative> representatives{ representative };
nano::confirmation_solicitor solicitor (node2.network, node2.network_params.network);
solicitor.prepare (representatives);
// Ensure the representatives are correct
ASSERT_EQ (1, representatives.size ());
ASSERT_EQ (channel1, representatives.front ().channel);
ASSERT_EQ (nano::test_genesis_key.pub, representatives.front ().account);
ASSERT_TIMELY (3s, node2.network.size () == 1);
auto send (std::make_shared<nano::send_block> (nano::genesis_hash, nano::keypair ().pub, nano::genesis_amount - 100, nano::test_genesis_key.prv, nano::test_genesis_key.pub, *system.work.generate (nano::genesis_hash)));
{
ASSERT_NO_ERROR (system.poll ());
nano::lock_guard<std::mutex> guard (node2.active.mutex);
auto election (std::make_shared<nano::election> (node2, send, nullptr, false));
// Add a vote for something else, not the winner
election->last_votes[representative.account] = { std::chrono::steady_clock::now (), 1, 1 };
// Ensure the request and broadcast goes through
ASSERT_FALSE (solicitor.add (*election));
ASSERT_FALSE (solicitor.broadcast (*election));
}
ASSERT_LE (2, node2.stats.count (nano::stat::type::message, nano::stat::detail::confirm_req, nano::stat::dir::out));
// One publish through directed broadcasting and another through random flooding
ASSERT_EQ (2, node2.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out));
solicitor.flush ();
ASSERT_EQ (1, node2.stats.count (nano::stat::type::message, nano::stat::detail::confirm_req, nano::stat::dir::out));
}
10 changes: 7 additions & 3 deletions nano/node/confirmation_solicitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nano::confirmation_solicitor::confirmation_solicitor (nano::network & network_a,
max_confirm_req_batches (params_a.is_test_network () ? 1 : 20),
max_block_broadcasts (params_a.is_test_network () ? 4 : 30),
max_election_requests (30),
max_election_broadcasts (network_a.fanout () / 2),
max_election_broadcasts (std::max<size_t> (network_a.fanout () / 2, 1)),
network (network_a)
{
}
Expand All @@ -29,12 +29,14 @@ bool nano::confirmation_solicitor::broadcast (nano::election const & election_a)
bool error (true);
if (rebroadcasted++ < max_block_broadcasts)
{
auto const & hash (election_a.status.winner->hash ());
nano::publish winner (election_a.status.winner);
unsigned count = 0;
// Directed broadcasting to principal representatives
for (auto i (representatives_broadcasts.begin ()), n (representatives_broadcasts.end ()); i != n && count < max_election_broadcasts; ++i)
{
if (election_a.last_votes.find (i->account) == election_a.last_votes.end ())
auto existing (election_a.last_votes.find (i->account));
if (existing == election_a.last_votes.end () || existing->second.hash != hash)
{
i->channel->send (winner);
++count;
Expand All @@ -52,11 +54,13 @@ bool nano::confirmation_solicitor::add (nano::election const & election_a)
debug_assert (prepared);
auto const max_channel_requests (max_confirm_req_batches * nano::network::confirm_req_hashes_max);
unsigned count = 0;
auto const & hash (election_a.status.winner->hash ());
for (auto i (representatives_requests.begin ()); i != representatives_requests.end () && count < max_election_requests;)
{
bool full_queue (false);
auto rep (*i);
if (election_a.last_votes.find (rep.account) == election_a.last_votes.end ())
auto existing (election_a.last_votes.find (rep.account));
if (existing == election_a.last_votes.end () || existing->second.hash != hash)
{
auto & request_queue (requests[rep.channel]);
if (request_queue.size () < max_channel_requests)
Expand Down