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

Republish unconfirmed blocks with delay #1232

Merged
merged 3 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 41 additions & 2 deletions rai/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,38 @@ void rai::network::republish_block (rai::transaction const & transaction, std::s
}
}

void rai::network::republish_block_batch (std::deque<std::shared_ptr<rai::block>> blocks_a, unsigned delay_a)
{
auto block (blocks_a.front ());
blocks_a.pop_front ();
auto hash (block->hash ());
auto list (node.peers.list_fanout ());
rai::publish message (block);
std::shared_ptr<std::vector<uint8_t>> bytes (new std::vector<uint8_t>);
{
rai::vectorstream stream (*bytes);
message.serialize (stream);
}
for (auto i (list.begin ()), n (list.end ()); i != n; ++i)
{
republish (hash, bytes, *i);
}
if (node.config.logging.network_logging ())
{
BOOST_LOG (node.log) << boost::str (boost::format ("Block %1% was republished to peers") % hash.to_string ());
}
if (!blocks_a.empty ())
{
std::weak_ptr<rai::node> node_w (node.shared ());
node.alarm.add (std::chrono::steady_clock::now () + std::chrono::milliseconds (delay_a), [node_w, blocks_a, delay_a]() {
if (auto node_l = node_w.lock ())
{
node_l->network.republish_block_batch (blocks_a, delay_a);
}
});
}
}

// In order to rate limit network traffic we republish:
// 1) Only if they are a non-replay vote of a block that's actively settling. Settling blocks are limited by block PoW
// 2) The rep has a weight > Y to prevent creating a lot of small-weight accounts to send out votes
Expand Down Expand Up @@ -3836,6 +3868,7 @@ void rai::active_transactions::announce_votes ()
unsigned unconfirmed_announcements (0);
unsigned mass_request_count (0);
std::vector<rai::block_hash> blocks_bundle;
std::deque<std::shared_ptr<rai::block>> rebroadcast_bundle;

for (auto i (roots.begin ()), n (roots.end ()); i != n; ++i)
{
Expand Down Expand Up @@ -3872,7 +3905,7 @@ void rai::active_transactions::announce_votes ()
{
if (node.config.enable_voting && std::chrono::system_clock::now () >= node.config.generate_hash_votes_at)
{
node.network.republish_block (transaction, election_l->status.winner, false);
rebroadcast_bundle.push_back (election_l->status.winner);
blocks_bundle.push_back (election_l->status.winner->hash ());
if (blocks_bundle.size () >= 12)
{
Expand All @@ -3886,7 +3919,7 @@ void rai::active_transactions::announce_votes ()
else
{
election_l->compute_rep_votes (transaction);
node.network.republish_block (transaction, election_l->status.winner);
rebroadcast_bundle.push_back (election_l->status.winner);
}
}
else
Expand Down Expand Up @@ -3941,6 +3974,12 @@ void rai::active_transactions::announce_votes ()
++info_a.announcements;
});
}
// Rebroadcast unconfirmed blocks
if (!rebroadcast_bundle.empty ())
{
node.network.republish_block_batch (rebroadcast_bundle, 100);
}
// Request votes for unconfirmed blocks
if (node.config.enable_voting && !blocks_bundle.empty ())
{
node.wallets.foreach_representative (transaction, [&](rai::public_key const & pub_a, rai::raw_key const & prv_a) {
Expand Down
1 change: 1 addition & 0 deletions rai/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ class network
void rpc_action (boost::system::error_code const &, size_t);
void republish_vote (std::shared_ptr<rai::vote>);
void republish_block (rai::transaction const &, std::shared_ptr<rai::block>, bool = true);
void republish_block_batch (std::deque<std::shared_ptr<rai::block>>, unsigned = 100);
void republish (rai::block_hash const &, std::shared_ptr<std::vector<uint8_t>>, rai::endpoint);
void publish_broadcast (std::vector<rai::peer_information> &, std::unique_ptr<rai::block>);
void confirm_send (rai::confirm_ack const &, std::shared_ptr<std::vector<uint8_t>>, rai::endpoint const &);
Expand Down
12 changes: 8 additions & 4 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,7 @@ void rai::rpc_handler::republish ()
auto block (node.store.block_get (transaction, hash));
if (block != nullptr)
{
std::deque<std::shared_ptr<rai::block>> republish_bundle;
for (auto i (0); !hash.is_zero () && i < count; ++i)
{
block = node.store.block_get (transaction, hash);
Expand All @@ -2589,13 +2590,13 @@ void rai::rpc_handler::republish ()
for (auto & hash_l : hashes)
{
block_a = node.store.block_get (transaction, hash_l);
node.network.republish_block (transaction, std::move (block_a));
republish_bundle.push_back (std::move (block_a));
boost::property_tree::ptree entry_l;
entry_l.put ("", hash_l.to_string ());
blocks.push_back (std::make_pair ("", entry_l));
}
}
node.network.republish_block (transaction, std::move (block)); // Republish block
republish_bundle.push_back (std::move (block)); // Republish block
boost::property_tree::ptree entry;
entry.put ("", hash.to_string ());
blocks.push_back (std::make_pair ("", entry));
Expand Down Expand Up @@ -2626,7 +2627,7 @@ void rai::rpc_handler::republish ()
for (auto & hash_l : hashes)
{
block_d = node.store.block_get (transaction, hash_l);
node.network.republish_block (transaction, std::move (block_d));
republish_bundle.push_back (std::move (block_d));
boost::property_tree::ptree entry_l;
entry_l.put ("", hash_l.to_string ());
blocks.push_back (std::make_pair ("", entry_l));
Expand All @@ -2636,6 +2637,7 @@ void rai::rpc_handler::republish ()
}
hash = node.store.block_successor (transaction, hash);
}
node.network.republish_block_batch (republish_bundle, 25);
response_l.put ("success", ""); // obsolete
response_l.add_child ("blocks", blocks);
}
Expand Down Expand Up @@ -3348,6 +3350,7 @@ void rai::rpc_handler::wallet_republish ()
if (!ec)
{
boost::property_tree::ptree blocks;
std::deque<std::shared_ptr<rai::block>> republish_bundle;
auto transaction (node.store.tx_begin_read ());
for (auto i (wallet->store.begin (transaction)), n (wallet->store.end ()); i != n; ++i)
{
Expand All @@ -3365,12 +3368,13 @@ void rai::rpc_handler::wallet_republish ()
for (auto & hash : hashes)
{
block = node.store.block_get (transaction, hash);
node.network.republish_block (transaction, std::move (block));
republish_bundle.push_back (std::move (block));
boost::property_tree::ptree entry;
entry.put ("", hash.to_string ());
blocks.push_back (std::make_pair ("", entry));
}
}
node.network.republish_block_batch (republish_bundle, 25);
response_l.add_child ("blocks", blocks);
}
response_errors ();
Expand Down