Skip to content

Commit

Permalink
rpc: in/out peers can now return the setting's value
Browse files Browse the repository at this point in the history
  • Loading branch information
moneromooo-monero committed May 30, 2019
1 parent 5fbfa8a commit fcfcc3a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 21 deletions.
26 changes: 16 additions & 10 deletions src/daemon/command_parser_executor.cpp
Expand Up @@ -493,36 +493,42 @@ bool t_command_parser_executor::set_limit_down(const std::vector<std::string>& a

bool t_command_parser_executor::out_peers(const std::vector<std::string>& args)
{
if (args.empty()) return false;

unsigned int limit;
bool set = false;
uint32_t limit = 0;
try {
limit = std::stoi(args[0]);
if (!args.empty())
{
limit = std::stoi(args[0]);
set = true;
}
}

catch(const std::exception& ex) {
_erro("stoi exception");
return false;
}

return m_executor.out_peers(limit);
return m_executor.out_peers(set, limit);
}

bool t_command_parser_executor::in_peers(const std::vector<std::string>& args)
{
if (args.empty()) return false;

unsigned int limit;
bool set = false;
uint32_t limit = 0;
try {
limit = std::stoi(args[0]);
if (!args.empty())
{
limit = std::stoi(args[0]);
set = true;
}
}

catch(const std::exception& ex) {
_erro("stoi exception");
return false;
}

return m_executor.in_peers(limit);
return m_executor.in_peers(set, limit);
}

bool t_command_parser_executor::start_save_graph(const std::vector<std::string>& args)
Expand Down
12 changes: 8 additions & 4 deletions src/daemon/rpc_command_executor.cpp
Expand Up @@ -1465,13 +1465,14 @@ bool t_rpc_command_executor::get_limit_down()
return true;
}

bool t_rpc_command_executor::out_peers(uint64_t limit)
bool t_rpc_command_executor::out_peers(bool set, uint32_t limit)
{
cryptonote::COMMAND_RPC_OUT_PEERS::request req;
cryptonote::COMMAND_RPC_OUT_PEERS::response res;

epee::json_rpc::error error_resp;

req.set = set;
req.out_peers = limit;

std::string fail_message = "Unsuccessful";
Expand All @@ -1492,18 +1493,20 @@ bool t_rpc_command_executor::out_peers(uint64_t limit)
}
}

tools::msg_writer() << "Max number of out peers set to " << limit << std::endl;
const std::string s = res.out_peers == (uint32_t)-1 ? "unlimited" : std::to_string(res.out_peers);
tools::msg_writer() << "Max number of out peers set to " << s << std::endl;

return true;
}

bool t_rpc_command_executor::in_peers(uint64_t limit)
bool t_rpc_command_executor::in_peers(bool set, uint32_t limit)
{
cryptonote::COMMAND_RPC_IN_PEERS::request req;
cryptonote::COMMAND_RPC_IN_PEERS::response res;

epee::json_rpc::error error_resp;

req.set = set;
req.in_peers = limit;

std::string fail_message = "Unsuccessful";
Expand All @@ -1524,7 +1527,8 @@ bool t_rpc_command_executor::in_peers(uint64_t limit)
}
}

tools::msg_writer() << "Max number of in peers set to " << limit << std::endl;
const std::string s = res.in_peers == (uint32_t)-1 ? "unlimited" : std::to_string(res.in_peers);
tools::msg_writer() << "Max number of in peers set to " << s << std::endl;

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/rpc_command_executor.h
Expand Up @@ -123,9 +123,9 @@ class t_rpc_command_executor final {

bool set_limit(int64_t limit_down, int64_t limit_up);

bool out_peers(uint64_t limit);
bool out_peers(bool set, uint32_t limit);

bool in_peers(uint64_t limit);
bool in_peers(bool set, uint32_t limit);

bool start_save_graph();

Expand Down
2 changes: 2 additions & 0 deletions src/p2p/net_node.h
Expand Up @@ -245,7 +245,9 @@ namespace nodetool
size_t get_zone_count() const { return m_network_zones.size(); }

void change_max_out_public_peers(size_t count);
uint32_t get_max_out_public_peers() const;
void change_max_in_public_peers(size_t count);
uint32_t get_max_in_public_peers() const;
virtual bool block_host(const epee::net_utils::network_address &adress, time_t seconds = P2P_IP_BLOCKTIME);
virtual bool unblock_host(const epee::net_utils::network_address &address);
virtual std::map<std::string, time_t> get_blocked_hosts() { CRITICAL_REGION_LOCAL(m_blocked_hosts_lock); return m_blocked_hosts; }
Expand Down
18 changes: 18 additions & 0 deletions src/p2p/net_node.inl
Expand Up @@ -2216,6 +2216,15 @@ namespace nodetool
}
}

template<class t_payload_net_handler>
uint32_t node_server<t_payload_net_handler>::get_max_out_public_peers() const
{
const auto public_zone = m_network_zones.find(epee::net_utils::zone::public_);
if (public_zone == m_network_zones.end())
return 0;
return public_zone->second.m_config.m_net_config.max_out_connection_count;
}

template<class t_payload_net_handler>
void node_server<t_payload_net_handler>::change_max_in_public_peers(size_t count)
{
Expand All @@ -2229,6 +2238,15 @@ namespace nodetool
}
}

template<class t_payload_net_handler>
uint32_t node_server<t_payload_net_handler>::get_max_in_public_peers() const
{
const auto public_zone = m_network_zones.find(epee::net_utils::zone::public_);
if (public_zone == m_network_zones.end())
return 0;
return public_zone->second.m_config.m_net_config.max_in_connection_count;
}

template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::set_tos_flag(const boost::program_options::variables_map& vm, int flag)
{
Expand Down
8 changes: 6 additions & 2 deletions src/rpc/core_rpc_server.cpp
Expand Up @@ -2075,15 +2075,19 @@ namespace cryptonote
bool core_rpc_server::on_out_peers(const COMMAND_RPC_OUT_PEERS::request& req, COMMAND_RPC_OUT_PEERS::response& res, const connection_context *ctx)
{
PERF_TIMER(on_out_peers);
m_p2p.change_max_out_public_peers(req.out_peers);
if (req.set)
m_p2p.change_max_out_public_peers(req.out_peers);
res.out_peers = m_p2p.get_max_out_public_peers();
res.status = CORE_RPC_STATUS_OK;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_in_peers(const COMMAND_RPC_IN_PEERS::request& req, COMMAND_RPC_IN_PEERS::response& res, const connection_context *ctx)
{
PERF_TIMER(on_in_peers);
m_p2p.change_max_in_public_peers(req.in_peers);
if (req.set)
m_p2p.change_max_in_public_peers(req.in_peers);
res.in_peers = m_p2p.get_max_in_public_peers();
res.status = CORE_RPC_STATUS_OK;
return true;
}
Expand Down
14 changes: 11 additions & 3 deletions src/rpc/core_rpc_server_commands_defs.h
Expand Up @@ -84,7 +84,7 @@ namespace cryptonote
// advance which version they will stop working with
// Don't go over 32767 for any of these
#define CORE_RPC_VERSION_MAJOR 2
#define CORE_RPC_VERSION_MINOR 6
#define CORE_RPC_VERSION_MINOR 7
#define MAKE_CORE_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define CORE_RPC_VERSION MAKE_CORE_RPC_VERSION(CORE_RPC_VERSION_MAJOR, CORE_RPC_VERSION_MINOR)

Expand Down Expand Up @@ -1682,18 +1682,22 @@ namespace cryptonote
{
struct request_t
{
uint64_t out_peers;
bool set;
uint32_t out_peers;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE_OPT(set, true)
KV_SERIALIZE(out_peers)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;

struct response_t
{
uint32_t out_peers;
std::string status;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(out_peers)
KV_SERIALIZE(status)
END_KV_SERIALIZE_MAP()
};
Expand All @@ -1704,18 +1708,22 @@ namespace cryptonote
{
struct request_t
{
uint64_t in_peers;
bool set;
uint32_t in_peers;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE_OPT(set, true)
KV_SERIALIZE(in_peers)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;

struct response_t
{
uint32_t in_peers;
std::string status;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(in_peers)
KV_SERIALIZE(status)
END_KV_SERIALIZE_MAP()
};
Expand Down

0 comments on commit fcfcc3a

Please sign in to comment.