Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace serialization functions #8867

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions contrib/epee/include/memwipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifdef __cplusplus
#include <array>
#include <cstddef>
#include "serialization/wire/fwd.h"

extern "C" {
#endif
Expand Down Expand Up @@ -74,6 +75,14 @@ namespace tools {
template<typename T>
const T& unwrap(scrubbed<T> const& src) { return src; }

template<typename R, typename T>
void read_bytes(R& source, scrubbed<T>& dest)
{ wire_read::bytes(source, unwrap(dest)); }

template<typename W, typename T>
void write_bytes(W& dest, const scrubbed<T>& source)
{ wire_write::bytes(dest, unwrap(source)); }

template <class T, size_t N>
using scrubbed_arr = scrubbed<std::array<T, N>>;
} // namespace tools
Expand Down
9 changes: 9 additions & 0 deletions contrib/epee/include/mlocker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <map>
#include <boost/thread/mutex.hpp>
#include "serialization/wire/fwd.h"

namespace epee
{
Expand Down Expand Up @@ -83,6 +84,14 @@ namespace epee
template<typename T>
const T& unwrap(mlocked<T> const& src) { return src; }

template<typename R, typename T>
void read_bytes(R& source, mlocked<T>& dest)
{ wire_read::bytes(source, unwrap(dest)); }

template<typename W, typename T>
void write_bytes(W& dest, const mlocked<T>& source)
{ wire_write::bytes(dest, unwrap(source)); }

template <class T, size_t N>
using mlocked_arr = mlocked<std::array<T, N>>;
}
1 change: 1 addition & 0 deletions contrib/epee/include/net/connection_basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <string>
#include <atomic>
#include <deque>
#include <memory>

#include <boost/asio.hpp>
Expand Down
91 changes: 39 additions & 52 deletions contrib/epee/include/net/http_server_handlers_map2.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#pragma once
#include "http_base.h"
#include "jsonrpc_structs.h"
#include "storages/portable_storage.h"
#include "storages/portable_storage_template_helper.h"

#undef MONERO_DEFAULT_LOG_CATEGORY
Expand Down Expand Up @@ -142,52 +141,40 @@
{ \
uint64_t ticks = epee::misc_utils::get_tick_count(); \
response_info.m_mime_tipe = "application/json"; \
epee::serialization::portable_storage ps; \
if(!ps.load_from_json(query_info.m_body)) \
epee::json_rpc::request_generic req{}; \
if(!epee::serialization::load_t_from_json(req, query_info.m_body)) \
{ \
boost::value_initialized<epee::json_rpc::error_response> rsp; \
static_cast<epee::json_rpc::error_response&>(rsp).jsonrpc = "2.0"; \
static_cast<epee::json_rpc::error_response&>(rsp).error.code = -32700; \
static_cast<epee::json_rpc::error_response&>(rsp).error.message = "Parse error"; \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_info.m_body); \
epee::json_rpc::response<std::string> rsp{};\
rsp.jsonrpc = "2.0"; \
rsp.error_.emplace(); \
rsp.error_->code = -32700; \
rsp.error_->message = "Parse error"; \
epee::serialization::store_t_to_json(rsp, response_info.m_body); \
return true; \
} \
epee::serialization::storage_entry id_; \
id_ = epee::serialization::storage_entry(std::string()); \
ps.get_value("id", id_, nullptr); \
std::string callback_name; \
if(!ps.get_value("method", callback_name, nullptr)) \
{ \
epee::json_rpc::error_response rsp; \
rsp.jsonrpc = "2.0"; \
rsp.error.code = -32600; \
rsp.error.message = "Invalid Request"; \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_info.m_body); \
return true; \
} \
const wire::basic_value id_ = req.id; \
const std::string callback_name = req.method; \
if(false) return true; //just a stub to have "else if"


#define PREPARE_OBJECTS_FROM_JSON(command_type) \
handled = true; \
response_info.m_mime_tipe = "application/json"; \
boost::value_initialized<epee::json_rpc::request<command_type::request> > req_; \
epee::json_rpc::request<command_type::request>& req = static_cast<epee::json_rpc::request<command_type::request>&>(req_);\
if(!req.load(ps)) \
epee::json_rpc::request_specific<command_type::request> req_{}; \
std::error_code req_error{}; \
epee::json_rpc::response<command_type::response> resp{}; \
resp.jsonrpc = "2.0"; \
resp.id = req.id; \
if((req_error = wire::json::from_bytes(epee::to_span(query_info.m_body), req_))) \
{ \
epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \
fail_resp.jsonrpc = "2.0"; \
fail_resp.id = req.id; \
fail_resp.error.code = -32602; \
fail_resp.error.message = "Invalid params"; \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \
resp.error_.emplace(); \
resp.error_->code = -32602; \
resp.error_->message = "Invalid params: " + req_error.message(); \
epee::serialization::store_t_to_json(resp, response_info.m_body); \
return true; \
} \
uint64_t ticks1 = epee::misc_utils::get_tick_count(); \
boost::value_initialized<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> > resp_; \
epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error>& resp = static_cast<epee::json_rpc::response<command_type::response, epee::json_rpc::dummy_error> &>(resp_); \
resp.jsonrpc = "2.0"; \
resp.id = req.id;
resp.result.emplace()

#define FINALIZE_OBJECTS_TO_JSON(method_name) \
uint64_t ticks2 = epee::misc_utils::get_tick_count(); \
Expand All @@ -200,19 +187,19 @@
#define MAP_JON_RPC_WE_IF(method_name, callback_f, command_type, cond) \
else if((callback_name == method_name) && (cond)) \
{ \
PREPARE_OBJECTS_FROM_JSON(command_type) \
epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \
fail_resp.jsonrpc = "2.0"; \
fail_resp.id = req.id; \
PREPARE_OBJECTS_FROM_JSON(command_type); \
resp.error_.emplace(); \
MINFO(m_conn_context << "Calling RPC method " << method_name); \
bool res = false; \
try { res = callback_f(req.params, resp.result, fail_resp.error, &m_conn_context); } \
try { res = callback_f(req_.params, *resp.result, *resp.error_, &m_conn_context); } \
catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \
if (!res) \
{ \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \
resp.result.reset(); \
epee::serialization::store_t_to_json(resp, response_info.m_body); \
return true; \
} \
resp.error_.reset(); \
FINALIZE_OBJECTS_TO_JSON(method_name) \
return true;\
}
Expand All @@ -222,32 +209,32 @@
#define MAP_JON_RPC(method_name, callback_f, command_type) \
else if(callback_name == method_name) \
{ \
PREPARE_OBJECTS_FROM_JSON(command_type) \
PREPARE_OBJECTS_FROM_JSON(command_type); \
MINFO(m_conn_context << "calling RPC method " << method_name); \
bool res = false; \
try { res = callback_f(req.params, resp.result, &m_conn_context); } \
try { res = callback_f(req_.params, *resp.result, &m_conn_context); } \
catch (const std::exception &e) { MERROR(m_conn_context << "Failed to " << #callback_f << "(): " << e.what()); } \
if (!res) \
{ \
epee::json_rpc::error_response fail_resp = AUTO_VAL_INIT(fail_resp); \
fail_resp.jsonrpc = "2.0"; \
fail_resp.id = req.id; \
fail_resp.error.code = -32603; \
fail_resp.error.message = "Internal error"; \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(fail_resp), response_info.m_body); \
resp.result.reset(); \
resp.error_.emplace(); \
resp.error_->code = -32603; \
resp.error_->message = "Internal error"; \
epee::serialization::store_t_to_json(resp, response_info.m_body); \
return true; \
} \
FINALIZE_OBJECTS_TO_JSON(method_name) \
return true;\
}

#define END_JSON_RPC_MAP() \
epee::json_rpc::error_response rsp; \
epee::json_rpc::response<std::string> rsp{}; \
rsp.id = id_; \
rsp.jsonrpc = "2.0"; \
rsp.error.code = -32601; \
rsp.error.message = "Method not found"; \
epee::serialization::store_t_to_json(static_cast<epee::json_rpc::error_response&>(rsp), response_info.m_body); \
rsp.error_.emplace(); \
rsp.error_->code = -32601; \
rsp.error_->message = "Method not found"; \
epee::serialization::store_t_to_json(rsp, response_info.m_body); \
return true; \
}

Expand Down
144 changes: 71 additions & 73 deletions contrib/epee/include/net/jsonrpc_structs.h
Original file line number Diff line number Diff line change
@@ -1,109 +1,107 @@
#ifndef JSONRPC_STRUCTS_H
#define JSONRPC_STRUCTS_H

#include <string>
#include <boost/optional/optional.hpp>
#include <cstdint>
#include "serialization/keyvalue_serialization.h"
#include "storages/portable_storage_base.h"
#include <string>
#include "serialization/wire/basic_value.h"
#include "serialization/wire/field.h"
#include "serialization/wire/fwd.h"
#include "serialization/wire/json/base.h"

//! Must be done in the `epee::json_rpc` namespace!
#define EPEE_JSONRPC_DECLARE(command) \
WIRE_JSON_DECLARE_CONVERSION(request_specific<command::request>); \
WIRE_JSON_DECLARE_CONVERSION(client_request<command::request>); \
WIRE_JSON_DECLARE_CONVERSION(response<command::response>)

//! Must be done in the `epee::json_rpc` namespace (cpp)!
#define EPEE_JSONRPC_DEFINE(command) \
WIRE_JSON_DEFINE_CONVERSION(request_specific<command::request>) \
WIRE_JSON_DEFINE_CONVERSION(client_request<command::request>) \
WIRE_JSON_DEFINE_CONVERSION(response<command::response>)

namespace epee
{
namespace json_rpc
{
template<typename t_param>
struct request
struct request_generic
{
std::string jsonrpc;
std::string method;
epee::serialization::storage_entry id;
t_param params;
wire::basic_value id;

request(): id{}, params{} {}
request_generic(): jsonrpc(), method(), id{} {}

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(jsonrpc)
KV_SERIALIZE(id)
KV_SERIALIZE(method)
KV_SERIALIZE(params)
END_KV_SERIALIZE_MAP()
WIRE_BEGIN_MAP(),
WIRE_FIELD(jsonrpc),
WIRE_OPTIONAL_FIELD(id),
WIRE_FIELD(method)
WIRE_END_MAP()
};
WIRE_JSON_DECLARE_CONVERSION(request_generic);

struct error
template<typename t_param>
struct request_specific
{
int64_t code;
std::string message;
t_param params;

error(): code(0) {}
request_specific() : params{} {}

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(code)
KV_SERIALIZE(message)
END_KV_SERIALIZE_MAP()
};

struct dummy_error
{
BEGIN_KV_SERIALIZE_MAP()
END_KV_SERIALIZE_MAP()
};

struct dummy_result
{
BEGIN_KV_SERIALIZE_MAP()
END_KV_SERIALIZE_MAP()
};

template<typename t_param, typename t_error>
struct response
{
std::string jsonrpc;
t_param result;
epee::serialization::storage_entry id;
t_error error;

response(): result{}, id(), error{} {}

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(jsonrpc)
KV_SERIALIZE(id)
KV_SERIALIZE(result)
KV_SERIALIZE(error)
END_KV_SERIALIZE_MAP()
WIRE_BEGIN_MAP(),
WIRE_FIELD(params)
WIRE_END_MAP()
};

template<typename t_param>
struct response<t_param, dummy_error>
struct client_request
{
std::string jsonrpc;
t_param result;
epee::serialization::storage_entry id;
std::string method;
wire::basic_value id;
t_param params;

response(): result{}, id{} {}
client_request() : jsonrpc(), method(), id{}, params{} {}

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(jsonrpc)
KV_SERIALIZE(id)
KV_SERIALIZE(result)
END_KV_SERIALIZE_MAP()
WIRE_BEGIN_MAP(),
WIRE_FIELD(jsonrpc),
WIRE_FIELD(method),
WIRE_FIELD(id),
WIRE_FIELD(params)
WIRE_END_MAP()
};

template<typename t_error>
struct response<dummy_result, t_error>
struct error
{
std::string jsonrpc;
t_error error;
epee::serialization::storage_entry id;
int64_t code;
std::string message;

response(): error{}, id{} {}
error(): code(0), message() {}

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(jsonrpc)
KV_SERIALIZE(id)
KV_SERIALIZE(error)
END_KV_SERIALIZE_MAP()
WIRE_BEGIN_MAP(),
WIRE_FIELD(code),
WIRE_FIELD(message)
WIRE_END_MAP()
};

typedef response<dummy_result, error> error_response;
template<typename t_param>
struct response
{
std::string jsonrpc;
wire::basic_value id;
boost::optional<t_param> result;
boost::optional<error> error_;

response(): jsonrpc(), id(), result(), error_() {}

WIRE_BEGIN_MAP(),
WIRE_FIELD(jsonrpc),
WIRE_OPTIONAL_FIELD(id),
WIRE_OPTIONAL_FIELD(result),
wire::optional_field("error", std::ref(self.error_))
WIRE_END_MAP()
};
WIRE_JSON_DECLARE_CONVERSION(response<std::string>); // used as default error response
}
}

Expand Down
Loading
Loading