Putting rate limiter inside sending keepalive function.
This commit is contained in:
parent
cf210c993e
commit
9ac1950ea0
4 changed files with 37 additions and 28 deletions
|
@ -49,7 +49,7 @@ namespace
|
|||
}
|
||||
bool constexpr log_to_cerr ()
|
||||
{
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -124,21 +124,23 @@ void rai::network::stop ()
|
|||
resolver.cancel ();
|
||||
}
|
||||
|
||||
void rai::network::send_keepalive (boost::asio::ip::udp::endpoint const & endpoint_a)
|
||||
void rai::network::maintain_keepalive (boost::asio::ip::udp::endpoint const & endpoint_a)
|
||||
{
|
||||
rai::keepalive_req message;
|
||||
client.peers.random_fill (message.peers);
|
||||
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
|
||||
if (!client.peers.contacting_peer (endpoint_a) && endpoint_a != endpoint ())
|
||||
{
|
||||
rai::vectorstream stream (*bytes);
|
||||
message.serialize (stream);
|
||||
}
|
||||
if (network_keepalive_logging ())
|
||||
{
|
||||
client.log.add (boost::str (boost::format ("Keepalive req sent from %1% to %2%") % endpoint ()% endpoint_a));
|
||||
}
|
||||
auto client_l (client.shared ());
|
||||
send_buffer (bytes->data (), bytes->size (), endpoint_a, [bytes, client_l, endpoint_a] (boost::system::error_code const & ec, size_t)
|
||||
rai::keepalive_req message;
|
||||
client.peers.random_fill (message.peers);
|
||||
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
|
||||
{
|
||||
rai::vectorstream stream (*bytes);
|
||||
message.serialize (stream);
|
||||
}
|
||||
if (network_keepalive_logging ())
|
||||
{
|
||||
client.log.add (boost::str (boost::format ("Keepalive req sent from %1% to %2%") % endpoint ()% endpoint_a));
|
||||
}
|
||||
auto client_l (client.shared ());
|
||||
send_buffer (bytes->data (), bytes->size (), endpoint_a, [bytes, client_l, endpoint_a] (boost::system::error_code const & ec, size_t)
|
||||
{
|
||||
if (network_logging ())
|
||||
{
|
||||
|
@ -148,6 +150,11 @@ void rai::network::send_keepalive (boost::asio::ip::udp::endpoint const & endpoi
|
|||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Skipping due to keepalive limiting
|
||||
}
|
||||
}
|
||||
|
||||
void rai::network::publish_block (boost::asio::ip::udp::endpoint const & endpoint_a, std::unique_ptr <rai::block> block)
|
||||
|
@ -217,7 +224,7 @@ void rai::network::receive_action (boost::system::error_code const & error, size
|
|||
auto known_peer (client.peers.known_peer (sender));
|
||||
if (!known_peer)
|
||||
{
|
||||
send_keepalive (sender);
|
||||
maintain_keepalive (sender);
|
||||
}
|
||||
client.peers.incoming_from_peer (sender);
|
||||
rai::bufferstream type_stream (buffer.data (), size_a);
|
||||
|
@ -372,9 +379,9 @@ void rai::network::merge_peers (std::array <rai::endpoint, 24> const & peers_a)
|
|||
{
|
||||
for (auto i (peers_a.begin ()), j (peers_a.end ()); i != j; ++i) // Amplify attack, send to the same IP many times
|
||||
{
|
||||
if (!client.peers.contacting_peer (*i) && *i != endpoint ())
|
||||
if (*i != endpoint ())
|
||||
{
|
||||
send_keepalive (*i);
|
||||
maintain_keepalive (*i);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1386,7 +1393,7 @@ service (new boost::asio::io_service)
|
|||
{
|
||||
auto starting1 ((*i)->peers.size ());
|
||||
auto starting2 ((*j)->peers.size ());
|
||||
(*j)->network.send_keepalive ((*i)->network.endpoint ());
|
||||
(*j)->network.maintain_keepalive ((*i)->network.endpoint ());
|
||||
do {
|
||||
service->run_one ();
|
||||
} while ((*i)->peers.size () == starting1 || (*j)->peers.size () == starting2);
|
||||
|
@ -2043,7 +2050,7 @@ void rai::processor::connect_bootstrap (std::vector <std::string> const & peers_
|
|||
{
|
||||
for (auto i (i_a), n (boost::asio::ip::udp::resolver::iterator {}); i != n; ++i)
|
||||
{
|
||||
client_l->network.send_keepalive (i->endpoint ());
|
||||
client_l->network.maintain_keepalive (i->endpoint ());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -2829,7 +2836,7 @@ void rai::processor::ongoing_keepalive ()
|
|||
auto peers (client.peers.purge_list (std::chrono::system_clock::now () - cutoff));
|
||||
for (auto i (peers.begin ()), j (peers.end ()); i != j && std::chrono::system_clock::now () - i->last_attempt > period; ++i)
|
||||
{
|
||||
client.network.send_keepalive (i->endpoint);
|
||||
client.network.maintain_keepalive (i->endpoint);
|
||||
}
|
||||
client.service.add (std::chrono::system_clock::now () + period, [this] () { ongoing_keepalive ();});
|
||||
}
|
||||
|
|
|
@ -443,7 +443,7 @@ namespace rai {
|
|||
void publish_block (rai::endpoint const &, std::unique_ptr <rai::block>);
|
||||
void confirm_block (std::unique_ptr <rai::block>, uint64_t);
|
||||
void merge_peers (std::array <rai::endpoint, 24> const &);
|
||||
void send_keepalive (rai::endpoint const &);
|
||||
void maintain_keepalive (rai::endpoint const &);
|
||||
void send_confirm_req (rai::endpoint const &, rai::block const &);
|
||||
void send_buffer (uint8_t const *, size_t, rai::endpoint const &, std::function <void (boost::system::error_code const &, size_t)>);
|
||||
void send_complete (boost::system::error_code const &, size_t);
|
||||
|
|
|
@ -136,7 +136,7 @@ TEST (client, auto_bootstrap)
|
|||
rai::keypair key2;
|
||||
client1->wallet.insert (key2.prv);
|
||||
ASSERT_FALSE (system.clients [0]->transactions.send (key2.pub, 100));
|
||||
client1->network.send_keepalive (system.clients [0]->network.endpoint ());
|
||||
client1->network.maintain_keepalive (system.clients [0]->network.endpoint ());
|
||||
client1->start ();
|
||||
auto iterations (0);
|
||||
do
|
||||
|
@ -161,7 +161,7 @@ TEST (client, auto_bootstrap_reverse)
|
|||
rai::keypair key2;
|
||||
client1->wallet.insert (key2.prv);
|
||||
ASSERT_FALSE (system.clients [0]->transactions.send (key2.pub, 100));
|
||||
system.clients [0]->network.send_keepalive (client1->network.endpoint ());
|
||||
system.clients [0]->network.maintain_keepalive (client1->network.endpoint ());
|
||||
client1->start ();
|
||||
auto iterations (0);
|
||||
do
|
||||
|
|
|
@ -101,7 +101,7 @@ TEST (network, send_keepalive)
|
|||
auto list1 (system.clients [0]->peers.list ());
|
||||
ASSERT_EQ (1, list1.size ());
|
||||
while (list1 [0].last_contact == std::chrono::system_clock::now ());
|
||||
system.clients [0]->network.send_keepalive (system.clients [1]->network.endpoint ());
|
||||
system.clients [0]->network.maintain_keepalive (system.clients [1]->network.endpoint ());
|
||||
auto initial (system.clients [0]->network.keepalive_ack_count);
|
||||
while (system.clients [0]->network.keepalive_ack_count == initial)
|
||||
{
|
||||
|
@ -125,9 +125,11 @@ TEST (network, multi_keepalive)
|
|||
auto client1 (std::make_shared <rai::client> (init1, system.service, 24001, system.processor, rai::test_genesis_key.pub));
|
||||
ASSERT_FALSE (init1.error ());
|
||||
client1->start ();
|
||||
client1->network.send_keepalive (system.clients [0]->network.endpoint ());
|
||||
ASSERT_EQ (0, client1->peers.size ());
|
||||
while (client1->peers.size () != 1 || system.clients [0]->peers.size () != 1)
|
||||
client1->network.maintain_keepalive (system.clients [0]->network.endpoint ());
|
||||
ASSERT_EQ (1, client1->peers.size ());
|
||||
ASSERT_EQ (0, system.clients [0]->peers.size ());
|
||||
while (system.clients [0]->peers.size () != 1)
|
||||
{
|
||||
system.service->run_one ();
|
||||
}
|
||||
|
@ -135,7 +137,7 @@ TEST (network, multi_keepalive)
|
|||
auto client2 (std::make_shared <rai::client> (init2, system.service, 24002, system.processor, rai::test_genesis_key.pub));
|
||||
ASSERT_FALSE (init2.error ());
|
||||
client2->start ();
|
||||
client2->network.send_keepalive (system.clients [0]->network.endpoint ());
|
||||
client2->network.maintain_keepalive (system.clients [0]->network.endpoint ());
|
||||
while (client1->peers.size () != 2 || system.clients [0]->peers.size () != 2 || client2->peers.size () != 2)
|
||||
{
|
||||
system.service->run_one ();
|
||||
|
@ -1046,7 +1048,7 @@ TEST (bulk, offline_send)
|
|||
rai::client_init init1;
|
||||
auto client1 (std::make_shared <rai::client> (init1, system.service, 24001, system.processor, rai::test_genesis_key.pub));
|
||||
ASSERT_FALSE (init1.error ());
|
||||
client1->network.send_keepalive (system.clients [0]->network.endpoint ());
|
||||
client1->network.maintain_keepalive (system.clients [0]->network.endpoint ());
|
||||
client1->start ();
|
||||
do
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue