Rename to fake_work_peer

This commit is contained in:
Piotr Wójcik 2024-05-12 17:17:47 +02:00
commit d8c6a79129
2 changed files with 16 additions and 16 deletions

View file

@ -132,7 +132,7 @@ TEST (distributed_work, peer)
work = work_a; work = work_a;
done = true; done = true;
}; };
auto work_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::good)); auto work_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::good));
work_peer->start (); work_peer->start ();
decltype (node->config.work_peers) peers; decltype (node->config.work_peers) peers;
peers.emplace_back ("::ffff:127.0.0.1", work_peer->port ()); peers.emplace_back ("::ffff:127.0.0.1", work_peer->port ());
@ -158,7 +158,7 @@ TEST (distributed_work, peer_malicious)
work = work_a; work = work_a;
done = true; done = true;
}; };
auto malicious_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::malicious)); auto malicious_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::malicious));
malicious_peer->start (); malicious_peer->start ();
decltype (node->config.work_peers) peers; decltype (node->config.work_peers) peers;
peers.emplace_back ("::ffff:127.0.0.1", malicious_peer->port ()); peers.emplace_back ("::ffff:127.0.0.1", malicious_peer->port ());
@ -176,7 +176,7 @@ TEST (distributed_work, peer_malicious)
// Test again with no local work generation enabled to make sure the malicious peer is sent more than one request // Test again with no local work generation enabled to make sure the malicious peer is sent more than one request
node->config.work_threads = 0; node->config.work_threads = 0;
ASSERT_FALSE (node->local_work_generation_enabled ()); ASSERT_FALSE (node->local_work_generation_enabled ());
auto malicious_peer2 (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::malicious)); auto malicious_peer2 (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::malicious));
malicious_peer2->start (); malicious_peer2->start ();
peers[0].second = malicious_peer2->port (); peers[0].second = malicious_peer2->port ();
ASSERT_FALSE (node->distributed_work.make (nano::work_version::work_1, hash, peers, node->network_params.work.base, {}, nano::account ())); ASSERT_FALSE (node->distributed_work.make (nano::work_version::work_1, hash, peers, node->network_params.work.base, {}, nano::account ()));
@ -201,9 +201,9 @@ TEST (distributed_work, DISABLED_peer_multi)
work = work_a; work = work_a;
done = true; done = true;
}; };
auto good_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::good)); auto good_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::good));
auto malicious_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::malicious)); auto malicious_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::malicious));
auto slow_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), work_peer_type::slow)); auto slow_peer (std::make_shared<fake_work_peer> (node->work, node->io_ctx, system.get_available_port (), fake_work_peer_type::slow));
good_peer->start (); good_peer->start ();
malicious_peer->start (); malicious_peer->start ();
slow_peer->start (); slow_peer->start ();

View file

@ -23,19 +23,19 @@ using tcp = boost::asio::ip::tcp;
namespace namespace
{ {
enum class work_peer_type enum class fake_work_peer_type
{ {
good, good,
malicious, malicious,
slow slow
}; };
class work_peer_connection : public std::enable_shared_from_this<work_peer_connection> class fake_work_peer_connection : public std::enable_shared_from_this<fake_work_peer_connection>
{ {
std::string const generic_error = "Unable to parse JSON"; std::string const generic_error = "Unable to parse JSON";
public: public:
work_peer_connection (asio::io_context & ioc_a, work_peer_type const type_a, nano::work_version const version_a, nano::work_pool & pool_a, std::function<void (bool const)> on_generation_a, std::function<void ()> on_cancel_a) : fake_work_peer_connection (asio::io_context & ioc_a, fake_work_peer_type const type_a, nano::work_version const version_a, nano::work_pool & pool_a, std::function<void (bool const)> on_generation_a, std::function<void ()> on_cancel_a) :
socket (ioc_a), socket (ioc_a),
type (type_a), type (type_a),
version (version_a), version (version_a),
@ -52,7 +52,7 @@ public:
tcp::socket socket; tcp::socket socket;
private: private:
work_peer_type type; fake_work_peer_type type;
nano::work_version version; nano::work_version version;
nano::work_pool & work_pool; nano::work_pool & work_pool;
beast::flat_buffer buffer{ 8192 }; beast::flat_buffer buffer{ 8192 };
@ -138,7 +138,7 @@ private:
void handle_generate (nano::block_hash const & hash_a) void handle_generate (nano::block_hash const & hash_a)
{ {
if (type == work_peer_type::good) if (type == fake_work_peer_type::good)
{ {
auto hash = hash_a; auto hash = hash_a;
auto request_difficulty = work_pool.network_constants.work.threshold_base (version); auto request_difficulty = work_pool.network_constants.work.threshold_base (version);
@ -154,7 +154,7 @@ private:
ptree::write_json (ostream, message_l); ptree::write_json (ostream, message_l);
beast::ostream (this_l->response.body ()) << ostream.str (); beast::ostream (this_l->response.body ()) << ostream.str ();
// Delay response by 500ms as a slow peer, immediate async call for a good peer // Delay response by 500ms as a slow peer, immediate async call for a good peer
this_l->timer.expires_from_now (boost::posix_time::milliseconds (this_l->type == work_peer_type::slow ? 500 : 0)); this_l->timer.expires_from_now (boost::posix_time::milliseconds (this_l->type == fake_work_peer_type::slow ? 500 : 0));
this_l->timer.async_wait ([this_l, result] (boost::system::error_code const & ec) { this_l->timer.async_wait ([this_l, result] (boost::system::error_code const & ec) {
if (this_l->on_generation) if (this_l->on_generation)
{ {
@ -164,7 +164,7 @@ private:
}); });
}); });
} }
else if (type == work_peer_type::malicious) else if (type == fake_work_peer_type::malicious)
{ {
// Respond immediately with no work // Respond immediately with no work
on_generation (false); on_generation (false);
@ -197,7 +197,7 @@ class fake_work_peer : public std::enable_shared_from_this<fake_work_peer>
{ {
public: public:
fake_work_peer () = delete; fake_work_peer () = delete;
fake_work_peer (nano::work_pool & pool_a, asio::io_context & ioc_a, unsigned short port_a, work_peer_type const type_a, nano::work_version const version_a = nano::work_version::work_1) : fake_work_peer (nano::work_pool & pool_a, asio::io_context & ioc_a, unsigned short port_a, fake_work_peer_type const type_a, nano::work_version const version_a = nano::work_version::work_1) :
pool (pool_a), pool (pool_a),
ioc (ioc_a), ioc (ioc_a),
acceptor (ioc_a, tcp::endpoint{ tcp::v4 (), port_a }), acceptor (ioc_a, tcp::endpoint{ tcp::v4 (), port_a }),
@ -221,7 +221,7 @@ private:
void listen () void listen ()
{ {
std::weak_ptr<fake_work_peer> this_w (shared_from_this ()); std::weak_ptr<fake_work_peer> this_w (shared_from_this ());
auto connection (std::make_shared<work_peer_connection> ( auto connection (std::make_shared<fake_work_peer_connection> (
ioc, type, version, pool, ioc, type, version, pool,
[this_w] (bool const good_generation) { [this_w] (bool const good_generation) {
if (auto this_l = this_w.lock ()) if (auto this_l = this_w.lock ())
@ -257,7 +257,7 @@ private:
nano::work_pool & pool; nano::work_pool & pool;
asio::io_context & ioc; asio::io_context & ioc;
tcp::acceptor acceptor; tcp::acceptor acceptor;
work_peer_type const type; fake_work_peer_type const type;
nano::work_version version; nano::work_version version;
}; };
} }