Various improvements to the ZMQ JSON-RPC handling:

- Finding handling function in ZMQ JSON-RPC now uses binary search
  - Temporary `std::vector`s in JSON output now use `epee::span` to
    prevent allocations.
  - Binary -> hex in JSON output no longer allocates temporary buffer
  - C++ structs -> JSON skips intermediate DOM creation, and instead
    write directly to an output stream.
This commit is contained in:
Lee Clagett 2019-11-07 05:45:06 +00:00
parent b4e1dc83d2
commit 0f78b06e8c
12 changed files with 832 additions and 1029 deletions

View file

@ -28,6 +28,10 @@
#include "daemon_handler.h"
#include <algorithm>
#include <cstring>
#include <stdexcept>
#include <boost/uuid/nil_generator.hpp>
// likely included by daemon_handler.h's includes,
// but including here for clarity
@ -42,6 +46,74 @@ namespace cryptonote
namespace rpc
{
namespace
{
using handler_function = std::string(DaemonHandler& handler, const rapidjson::Value& id, const rapidjson::Value& msg);
struct handler_map
{
const char* method_name;
handler_function* call;
};
bool operator<(const handler_map& lhs, const handler_map& rhs) noexcept
{
return std::strcmp(lhs.method_name, rhs.method_name) < 0;
}
bool operator<(const handler_map& lhs, const std::string& rhs) noexcept
{
return std::strcmp(lhs.method_name, rhs.c_str()) < 0;
}
template<typename Message>
std::string handle_message(DaemonHandler& handler, const rapidjson::Value& id, const rapidjson::Value& parameters)
{
typename Message::Request request{};
request.fromJson(parameters);
typename Message::Response response{};
handler.handle(request, response);
return FullMessage::getResponse(response, id);
}
constexpr const handler_map handlers[] =
{
{u8"get_block_hash", handle_message<GetBlockHash>},
{u8"get_block_header_by_hash", handle_message<GetBlockHeaderByHash>},
{u8"get_block_header_by_height", handle_message<GetBlockHeaderByHeight>},
{u8"get_block_headers_by_height", handle_message<GetBlockHeadersByHeight>},
{u8"get_blocks_fast", handle_message<GetBlocksFast>},
{u8"get_dynamic_fee_estimate", handle_message<GetFeeEstimate>},
{u8"get_hashes_fast", handle_message<GetHashesFast>},
{u8"get_height", handle_message<GetHeight>},
{u8"get_info", handle_message<GetInfo>},
{u8"get_last_block_header", handle_message<GetLastBlockHeader>},
{u8"get_output_distribution", handle_message<GetOutputDistribution>},
{u8"get_output_histogram", handle_message<GetOutputHistogram>},
{u8"get_output_keys", handle_message<GetOutputKeys>},
{u8"get_peer_list", handle_message<GetPeerList>},
{u8"get_rpc_version", handle_message<GetRPCVersion>},
{u8"get_transaction_pool", handle_message<GetTransactionPool>},
{u8"get_transactions", handle_message<GetTransactions>},
{u8"get_tx_global_output_indices", handle_message<GetTxGlobalOutputIndices>},
{u8"hard_fork_info", handle_message<HardForkInfo>},
{u8"key_images_spent", handle_message<KeyImagesSpent>},
{u8"mining_status", handle_message<MiningStatus>},
{u8"save_bc", handle_message<SaveBC>},
{u8"send_raw_tx", handle_message<SendRawTxHex>},
{u8"set_log_level", handle_message<SetLogLevel>},
{u8"start_mining", handle_message<StartMining>},
{u8"stop_mining", handle_message<StopMining>}
};
} // anonymous
DaemonHandler::DaemonHandler(cryptonote::core& c, t_p2p& p2p)
: m_core(c), m_p2p(p2p)
{
const auto last_sorted = std::is_sorted_until(std::begin(handlers), std::end(handlers));
if (last_sorted != std::end(handlers))
throw std::logic_error{std::string{"ZMQ JSON-RPC handlers map is not properly sorted, see "} + last_sorted->method_name};
}
void DaemonHandler::handle(const GetHeight::Request& req, GetHeight::Response& res)
{
@ -840,68 +912,21 @@ namespace rpc
{
MDEBUG("Handling RPC request: " << request);
Message* resp_message = NULL;
try
{
FullMessage req_full(request, true);
rapidjson::Value& req_json = req_full.getMessage();
const std::string request_type = req_full.getRequestType();
// create correct Message subclass and call handle() on it
REQ_RESP_TYPES_MACRO(request_type, GetHeight, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetBlocksFast, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetHashesFast, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetTransactions, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, KeyImagesSpent, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetTxGlobalOutputIndices, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, SendRawTx, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, SendRawTxHex, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetInfo, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, StartMining, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, StopMining, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, MiningStatus, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, SaveBC, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetBlockHash, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetLastBlockHeader, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHash, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHeight, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetBlockHeadersByHeight, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetPeerList, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, SetLogLevel, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetTransactionPool, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, HardForkInfo, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetOutputHistogram, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetOutputKeys, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetRPCVersion, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetFeeEstimate, req_json, resp_message, handle);
REQ_RESP_TYPES_MACRO(request_type, GetOutputDistribution, req_json, resp_message, handle);
// if none of the request types matches
if (resp_message == NULL)
{
const auto matched_handler = std::lower_bound(std::begin(handlers), std::end(handlers), request_type);
if (matched_handler == std::end(handlers) || matched_handler->method_name != request_type)
return BAD_REQUEST(request_type, req_full.getID());
}
FullMessage resp_full = FullMessage::responseMessage(resp_message, req_full.getID());
const std::string response = resp_full.getJson();
delete resp_message;
resp_message = NULL;
std::string response = matched_handler->call(*this, req_full.getID(), req_full.getMessage());
MDEBUG("Returning RPC response: " << response);
return response;
}
catch (const std::exception& e)
{
if (resp_message)
{
delete resp_message;
}
return BAD_JSON(e.what());
}
}