Skip to content

Commit

Permalink
Add DNS resolution support for work peers
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed May 14, 2018
1 parent f48597f commit 8707a26
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
10 changes: 5 additions & 5 deletions rai/core_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ TEST (rpc, work_peer_bad)
system.wallet (0)->insert_adhoc (key.prv);
rai::rpc rpc (system.service, node1, rai::rpc_config (true));
rpc.start ();
node2.config.work_peers.push_back (std::make_pair (boost::asio::ip::address_v6::any (), 0));
node2.config.work_peers.push_back (std::make_pair (boost::asio::ip::address_v6::any ().to_string (), 0));
rai::block_hash hash1 (1);
std::atomic<uint64_t> work (0);
node2.generate_work (hash1, [&work](uint64_t work_a) {
Expand All @@ -1617,7 +1617,7 @@ TEST (rpc, work_peer_one)
system.wallet (0)->insert_adhoc (key.prv);
rai::rpc rpc (system.service, node1, rai::rpc_config (true));
rpc.start ();
node2.config.work_peers.push_back (std::make_pair (node1.network.endpoint ().address (), rpc.config.port));
node2.config.work_peers.push_back (std::make_pair (node1.network.endpoint ().address ().to_string (), rpc.config.port));
rai::keypair key1;
uint64_t work (0);
node2.generate_work (key1.pub, [&work](uint64_t work_a) {
Expand Down Expand Up @@ -1653,9 +1653,9 @@ TEST (rpc, work_peer_many)
config4.port += 2;
rai::rpc rpc4 (system4.service, node4, config4);
rpc4.start ();
node1.config.work_peers.push_back (std::make_pair (node2.network.endpoint ().address (), rpc2.config.port));
node1.config.work_peers.push_back (std::make_pair (node3.network.endpoint ().address (), rpc3.config.port));
node1.config.work_peers.push_back (std::make_pair (node4.network.endpoint ().address (), rpc4.config.port));
node1.config.work_peers.push_back (std::make_pair (node2.network.endpoint ().address ().to_string (), rpc2.config.port));
node1.config.work_peers.push_back (std::make_pair (node3.network.endpoint ().address ().to_string (), rpc3.config.port));
node1.config.work_peers.push_back (std::make_pair (node4.network.endpoint ().address ().to_string (), rpc4.config.port));
for (auto i (0); i < 10; ++i)
{
rai::keypair key1;
Expand Down
69 changes: 59 additions & 10 deletions rai/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,12 +992,18 @@ bool rai::node_config::deserialize_json (bool & upgraded_a, boost::property_tree
for (auto i (work_peers_l.begin ()), n (work_peers_l.end ()); i != n; ++i)
{
auto work_peer (i->second.get<std::string> (""));
boost::asio::ip::address address;
uint16_t port;
result |= rai::parse_address_port (work_peer, address, port);
auto port_position (work_peer.rfind (':'));
result |= port_position == -1;
if (!result)
{
work_peers.push_back (std::make_pair (address, port));
auto port_str (work_peer.substr (port_position + 1));
uint16_t port;
result |= parse_port (port_str, port);
if (!result)
{
auto address (work_peer.substr (0, port_position));
work_peers.push_back (std::make_pair (address, port));
}
}
}
auto preconfigured_peers_l (tree_a.get_child ("preconfigured_peers"));
Expand Down Expand Up @@ -1882,8 +1888,15 @@ bool rai::parse_port (std::string const & string_a, uint16_t & port_a)
{
bool result;
size_t converted;
port_a = std::stoul (string_a, &converted);
result = converted != string_a.size () || converted > std::numeric_limits<uint16_t>::max ();
try
{
port_a = std::stoul (string_a, &converted);
result = converted != string_a.size () || converted > std::numeric_limits<uint16_t>::max ();
}
catch (...)
{
result = true;
}
return result;
}

Expand Down Expand Up @@ -2164,15 +2177,50 @@ class distributed_work : public std::enable_shared_from_this<distributed_work>
callback (callback_a),
node (node_a),
root (root_a),
backoff (backoff_a)
backoff (backoff_a),
need_resolve (node_a->config.work_peers)
{
completed.clear ();
for (auto & i : node_a->config.work_peers)
}
void start ()
{
if (need_resolve.empty ())
{
outstanding[i.first] = i.second;
start_work ();
}
else
{
auto current (need_resolve.back ());
need_resolve.pop_back ();
auto this_l (shared_from_this ());
boost::system::error_code ec;
auto parsed_address (boost::asio::ip::address_v6::from_string (current.first, ec));
if (!ec)
{
outstanding[parsed_address] = current.second;
start ();
}
else
{
node->network.resolver.async_resolve (boost::asio::ip::udp::resolver::query (current.first, std::to_string (current.second)), [current, this_l](boost::system::error_code const & ec, boost::asio::ip::udp::resolver::iterator i_a) {
if (!ec)
{
for (auto i (i_a), n (boost::asio::ip::udp::resolver::iterator{}); i != n; ++i)
{
auto endpoint (i->endpoint ());
this_l->outstanding[endpoint.address ()] = endpoint.port ();
}
}
else
{
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("Error resolving work peer: %1%:%2%: %3%") % current.first % current.second % ec.message ());
}
this_l->start ();
});
}
}
}
void start ()
void start_work ()
{
if (!outstanding.empty ())
{
Expand Down Expand Up @@ -2371,6 +2419,7 @@ class distributed_work : public std::enable_shared_from_this<distributed_work>
rai::block_hash root;
std::mutex mutex;
std::map<boost::asio::ip::address, uint16_t> outstanding;
std::vector<std::pair<std::string, uint16_t>> need_resolve;
std::atomic_flag completed;
};
}
Expand Down
2 changes: 1 addition & 1 deletion rai/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class node_config
rai::account random_representative ();
uint16_t peering_port;
rai::logging logging;
std::vector<std::pair<boost::asio::ip::address, uint16_t>> work_peers;
std::vector<std::pair<std::string, uint16_t>> work_peers;
std::vector<std::string> preconfigured_peers;
std::vector<rai::account> preconfigured_representatives;
unsigned bootstrap_fraction_numerator;
Expand Down
23 changes: 7 additions & 16 deletions rai/node/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4430,26 +4430,17 @@ void rai::rpc_handler::work_peer_add ()
{
std::string address_text = request.get<std::string> ("address");
std::string port_text = request.get<std::string> ("port");
boost::system::error_code ec;
auto address (boost::asio::ip::address_v6::from_string (address_text, ec));
if (!ec)
uint16_t port;
if (!rai::parse_port (port_text, port))
{
uint16_t port;
if (!rai::parse_port (port_text, port))
{
node.config.work_peers.push_back (std::make_pair (address, port));
boost::property_tree::ptree response_l;
response_l.put ("success", "");
response (response_l);
}
else
{
error_response (response, "Invalid port");
}
node.config.work_peers.push_back (std::make_pair (address_text, port));
boost::property_tree::ptree response_l;
response_l.put ("success", "");
response (response_l);
}
else
{
error_response (response, "Invalid address");
error_response (response, "Invalid port");
}
}
else
Expand Down

0 comments on commit 8707a26

Please sign in to comment.