From 3faf249d6aec8ed8599030bb2848a0a3c05ad09f Mon Sep 17 00:00:00 2001 From: MoneroOcean Date: Wed, 15 Jul 2020 10:55:34 -0700 Subject: [PATCH] Bug fix --- .../epee/include/serialization/enableable.h | 2 +- .../serialization/keyvalue_serialization.h | 27 ++++- .../keyvalue_serialization_overloads.h | 105 ++++++++++++++++-- .../include/storages/http_abstract_invoke.h | 17 +-- .../include/storages/levin_abstract_invoke2.h | 57 ++-------- .../include/storages/parserse_base_utils.h | 62 +---------- .../epee/include/storages/portable_storage.h | 52 ++++----- .../include/storages/portable_storage_base.h | 15 +-- .../storages/portable_storage_from_bin.h | 10 +- .../storages/portable_storage_from_json.h | 18 +-- .../storages/portable_storage_to_bin.h | 12 +- 11 files changed, 178 insertions(+), 199 deletions(-) diff --git a/src/contrib/epee/include/serialization/enableable.h b/src/contrib/epee/include/serialization/enableable.h index ab1d799..e1be2a7 100644 --- a/src/contrib/epee/include/serialization/enableable.h +++ b/src/contrib/epee/include/serialization/enableable.h @@ -50,4 +50,4 @@ namespace epee { // construct from specified values } }; -} \ No newline at end of file +} diff --git a/src/contrib/epee/include/serialization/keyvalue_serialization.h b/src/contrib/epee/include/serialization/keyvalue_serialization.h index 27fb0f1..5459c84 100644 --- a/src/contrib/epee/include/serialization/keyvalue_serialization.h +++ b/src/contrib/epee/include/serialization/keyvalue_serialization.h @@ -31,6 +31,10 @@ #include "misc_log_ex.h" #include "enableable.h" #include "keyvalue_serialization_overloads.h" + +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "serialization" + namespace epee { /************************************************************************/ @@ -41,12 +45,12 @@ public: \ template \ bool store( t_storage& st, typename t_storage::hsection hparent_section = nullptr) const\ {\ - return serialize_map(*this, st, hparent_section);\ + return serialize_map(*this, st, hparent_section);\ }\ template \ bool _load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\ {\ - return serialize_map(*this, stg, hparent_section);\ + return serialize_map(*this, stg, hparent_section);\ }\ template \ bool load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\ @@ -68,6 +72,15 @@ public: \ #define KV_SERIALIZE_N(varialble, val_name) \ epee::serialization::selector::serialize(this_ref.varialble, stg, hparent_section, val_name); + template inline void serialize_default(const T &t, T v) { } + template inline void serialize_default(T &t, T v) { t = v; } + +#define KV_SERIALIZE_OPT_N(variable, val_name, default_value) \ + do { \ + if (!epee::serialization::selector::serialize(this_ref.variable, stg, hparent_section, val_name)) \ + epee::serialize_default(this_ref.variable, default_value); \ + } while (0); + #define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) \ epee::serialization::selector::serialize_t_val_as_blob(this_ref.varialble, stg, hparent_section, val_name); @@ -75,6 +88,14 @@ public: \ static_assert(std::is_pod::value, "t_type must be a POD type."); \ KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) +#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, val_name, default_value) \ + do { \ + static_assert(std::is_pod::value, "t_type must be a POD type."); \ + bool ret = KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name); \ + if (!ret) \ + epee::serialize_default(this_ref.varialble, default_value); \ + } while(0); + #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, val_name) \ epee::serialization::selector::serialize_stl_container_pod_val_as_blob(this_ref.varialble, stg, hparent_section, val_name); @@ -82,8 +103,10 @@ public: \ #define KV_SERIALIZE(varialble) KV_SERIALIZE_N(varialble, #varialble) #define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, #varialble) +#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT(varialble, def) KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, #varialble, def) #define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_pod compile time check #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB(varialble) KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, #varialble) +#define KV_SERIALIZE_OPT(variable,default_value) KV_SERIALIZE_OPT_N(variable, #variable, default_value) } diff --git a/src/contrib/epee/include/serialization/keyvalue_serialization_overloads.h b/src/contrib/epee/include/serialization/keyvalue_serialization_overloads.h index 33486d9..fc8b90a 100644 --- a/src/contrib/epee/include/serialization/keyvalue_serialization_overloads.h +++ b/src/contrib/epee/include/serialization/keyvalue_serialization_overloads.h @@ -26,8 +26,23 @@ #pragma once +#include +#include +#include +#include +#include +#include + +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "serialization" + namespace epee { + namespace + { + template void hint_resize(C &container, size_t size) {} + template void hint_resize(std::vector &container, size_t size) { container.reserve(size); } + } namespace serialization { @@ -73,7 +88,7 @@ namespace epee template static bool unserialize_t_obj(serializible_type& obj, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { - typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section, true); + typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section, false); if(!hchild_section) return false; return obj._load(stg, hchild_section); } @@ -90,7 +105,7 @@ namespace epee static bool unserialize_t_obj(enableable& obj, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { obj.enabled = false; - typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section, true); + typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section, false); if(!hchild_section) return false; obj.enabled = true; return obj.v._load(stg, hchild_section); @@ -117,16 +132,15 @@ namespace epee typename stl_container::value_type exchange_val; typename t_storage::harray hval_array = stg.get_first_value(pname, exchange_val, hparent_section); if(!hval_array) return false; - container.push_back(std::move(exchange_val)); + container.insert(container.end(), std::move(exchange_val)); while(stg.get_next_value(hval_array, exchange_val)) - container.push_back(std::move(exchange_val)); + container.insert(container.end(), std::move(exchange_val)); return true; }//-------------------------------------------------------------------------------------------------------------------- template static bool serialize_stl_container_pod_val_as_blob(const stl_container& container, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { if(!container.size()) return true; - typename stl_container::const_iterator it = container.begin(); std::string mb; mb.resize(sizeof(typename stl_container::value_type)*container.size()); typename stl_container::value_type* p_elem = (typename stl_container::value_type*)mb.data(); @@ -150,10 +164,11 @@ namespace epee typename stl_container::value_type* pelem = (typename stl_container::value_type*)buff.data(); CHECK_AND_ASSERT_MES(!(loaded_size%sizeof(typename stl_container::value_type)), false, - "size in blob " << loaded_size << " not have not zero modulo for sizeof(value_type) = " << sizeof(typename stl_container::value_type)); + "size in blob " << loaded_size << " not have not zero modulo for sizeof(value_type) = " << sizeof(typename stl_container::value_type) << ", type " << typeid(typename stl_container::value_type).name()); size_t count = (loaded_size/sizeof(typename stl_container::value_type)); + hint_resize(container, count); for(size_t i = 0; i < count; i++) - container.push_back(*(pelem++)); + container.insert(container.end(), *(pelem++)); } return res; } @@ -187,12 +202,12 @@ namespace epee typename t_storage::harray hsec_array = stg.get_first_section(pname, hchild_section, hparent_section); if(!hsec_array || !hchild_section) return false; res = val._load(stg, hchild_section); - container.push_back(val); + container.insert(container.end(), val); while(stg.get_next_section(hsec_array, hchild_section)) { typename stl_container::value_type val_l = typename stl_container::value_type(); res |= val_l._load(stg, hchild_section); - container.push_back(std::move(val_l)); + container.insert(container.end(), std::move(val_l)); } return res; } @@ -228,6 +243,18 @@ namespace epee } //------------------------------------------------------------------------------------------------------------------- template + static bool kv_serialize(const std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_unserialize(std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template static bool kv_serialize(const std::list& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { return serialize_stl_container_t_val(d, stg, hparent_section, pname); @@ -239,6 +266,18 @@ namespace epee return unserialize_stl_container_t_val(d, stg, hparent_section, pname); } //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_serialize(const std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_unserialize(std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_val(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- }; template<> struct kv_serialization_overloads_impl_is_base_serializable_types @@ -269,6 +308,18 @@ namespace epee } //------------------------------------------------------------------------------------------------------------------- template + static bool kv_serialize(const std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_obj(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_unserialize(std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template static bool kv_serialize(const std::list& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { return serialize_stl_container_t_obj(d, stg, hparent_section, pname); @@ -279,6 +330,18 @@ namespace epee { return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); } + //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_serialize(const std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return serialize_stl_container_t_obj(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + static bool kv_unserialize(std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); + } }; template struct base_serializable_types: public boost::mpl::vector::type @@ -354,6 +417,18 @@ namespace epee } //------------------------------------------------------------------------------------------------------------------- template + bool kv_serialize(const std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_serialize(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + bool kv_unserialize(std::deque& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_unserialize(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template bool kv_serialize(const std::list& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) { return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_serialize(d, stg, hparent_section, pname); @@ -364,5 +439,17 @@ namespace epee { return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_unserialize(d, stg, hparent_section, pname); } + //------------------------------------------------------------------------------------------------------------------- + template + bool kv_serialize(const std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_serialize(d, stg, hparent_section, pname); + } + //------------------------------------------------------------------------------------------------------------------- + template + bool kv_unserialize(std::set& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) + { + return kv_serialization_overloads_impl_is_base_serializable_types, typename std::remove_const::type>::value>::kv_unserialize(d, stg, hparent_section, pname); + } } } diff --git a/src/contrib/epee/include/storages/http_abstract_invoke.h b/src/contrib/epee/include/storages/http_abstract_invoke.h index a8bc945..18b7f10 100644 --- a/src/contrib/epee/include/storages/http_abstract_invoke.h +++ b/src/contrib/epee/include/storages/http_abstract_invoke.h @@ -38,7 +38,7 @@ namespace epee namespace net_utils { template - bool invoke_http_json(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST") + bool invoke_http_json(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "GET") { std::string req_param; if(!serialization::store_t_to_json(out_struct, req_param)) @@ -72,7 +72,7 @@ namespace epee template - bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST") + bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "GET") { std::string req_param; if(!serialization::store_t_to_binary(out_struct, req_param)) @@ -101,7 +101,7 @@ namespace epee } template - bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, epee::json_rpc::error &error_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") + bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0") { epee::json_rpc::request req_t = AUTO_VAL_INIT(req_t); req_t.jsonrpc = "2.0"; @@ -111,12 +111,10 @@ namespace epee epee::json_rpc::response resp_t = AUTO_VAL_INIT(resp_t); if(!epee::net_utils::invoke_http_json(uri, req_t, resp_t, transport, timeout, http_method)) { - error_struct = {}; return false; } if(resp_t.error.code || resp_t.error.message.size()) { - error_struct = resp_t.error; LOG_ERROR("RPC call of \"" << req_t.method << "\" returned error: " << resp_t.error.code << ", message: " << resp_t.error.message); return false; } @@ -124,15 +122,8 @@ namespace epee return true; } - template - bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") - { - epee::json_rpc::error error_struct; - return invoke_http_json_rpc(uri, method_name, out_struct, result_struct, error_struct, transport, timeout, http_method, req_id); - } - template - bool invoke_http_json_rpc(const boost::string_ref uri, typename t_command::request& out_struct, typename t_command::response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0") + bool invoke_http_json_rpc(const boost::string_ref uri, typename t_command::request& out_struct, typename t_command::response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0") { return invoke_http_json_rpc(uri, t_command::methodname(), out_struct, result_struct, transport, timeout, http_method, req_id); } diff --git a/src/contrib/epee/include/storages/levin_abstract_invoke2.h b/src/contrib/epee/include/storages/levin_abstract_invoke2.h index 4633fa5..06eb9bd 100644 --- a/src/contrib/epee/include/storages/levin_abstract_invoke2.h +++ b/src/contrib/epee/include/storages/levin_abstract_invoke2.h @@ -28,35 +28,16 @@ #include "portable_storage_template_helper.h" #include -#include #include "span.h" #include "net/levin_base.h" #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "net" -namespace -{ - template - void on_levin_traffic(const context_t &context, bool initiator, bool sent, bool error, size_t bytes, const char *category) - { - MCINFO("net.p2p.traffic", context << bytes << " bytes " << (sent ? "sent" : "received") << (error ? "/corrupt" : "") - << " for category " << category << " initiated by " << (initiator ? "us" : "peer")); - } - template - void on_levin_traffic(const context_t &context, bool initiator, bool sent, bool error, size_t bytes, int command) - { - char buf[32]; - snprintf(buf, sizeof(buf), "command-%u", command); - return on_levin_traffic(context, initiator, sent, error, bytes, buf); - } -} - namespace epee { namespace net_utils { -#if 0 template bool invoke_remote_command2(int command, const t_arg& out_struct, t_result& result_struct, t_transport& transport) { @@ -102,18 +83,16 @@ namespace epee } return true; } -#endif template - bool invoke_remote_command2(const epee::net_utils::connection_context_base context, int command, const t_arg& out_struct, t_result& result_struct, t_transport& transport) + bool invoke_remote_command2(boost::uuids::uuid conn_id, int command, const t_arg& out_struct, t_result& result_struct, t_transport& transport) { - const boost::uuids::uuid &conn_id = context.m_connection_id; + typename serialization::portable_storage stg; out_struct.store(stg); std::string buff_to_send, buff_to_recv; stg.store_to_binary(buff_to_send); - on_levin_traffic(context, true, true, false, buff_to_send.size(), command); int res = transport.invoke(command, buff_to_send, buff_to_recv, conn_id); if( res <=0 ) { @@ -123,30 +102,24 @@ namespace epee typename serialization::portable_storage stg_ret; if(!stg_ret.load_from_binary(buff_to_recv)) { - on_levin_traffic(context, true, false, true, buff_to_recv.size(), command); LOG_ERROR("Failed to load_from_binary on command " << command); return false; } - on_levin_traffic(context, true, false, false, buff_to_recv.size(), command); return result_struct.load(stg_ret); } template - bool async_invoke_remote_command2(const epee::net_utils::connection_context_base &context, int command, const t_arg& out_struct, t_transport& transport, const callback_t &cb, size_t inv_timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED) + bool async_invoke_remote_command2(boost::uuids::uuid conn_id, int command, const t_arg& out_struct, t_transport& transport, const callback_t &cb, size_t inv_timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED) { - const boost::uuids::uuid &conn_id = context.m_connection_id; typename serialization::portable_storage stg; const_cast(out_struct).store(stg);//TODO: add true const support to searilzation std::string buff_to_send; stg.store_to_binary(buff_to_send); - on_levin_traffic(context, true, true, false, buff_to_send.size(), command); int res = transport.invoke_async(command, epee::strspan(buff_to_send), conn_id, [cb, command](int code, const epee::span buff, typename t_transport::connection_context& context)->bool { t_result result_struct = AUTO_VAL_INIT(result_struct); if( code <=0 ) { - if (!buff.empty()) - on_levin_traffic(context, true, false, true, buff.size(), command); LOG_PRINT_L1("Failed to invoke command " << command << " return code " << code); cb(code, result_struct, context); return false; @@ -154,19 +127,16 @@ namespace epee serialization::portable_storage stg_ret; if(!stg_ret.load_from_binary(buff)) { - on_levin_traffic(context, true, false, true, buff.size(), command); LOG_ERROR("Failed to load_from_binary on command " << command); cb(LEVIN_ERROR_FORMAT, result_struct, context); return false; } if (!result_struct.load(stg_ret)) { - on_levin_traffic(context, true, false, true, buff.size(), command); LOG_ERROR("Failed to load result struct on command " << command); cb(LEVIN_ERROR_FORMAT, result_struct, context); return false; } - on_levin_traffic(context, true, false, false, buff.size(), command); cb(code, result_struct, context); return true; }, inv_timeout); @@ -179,15 +149,14 @@ namespace epee } template - bool notify_remote_command2(const typename t_transport::connection_context &context, int command, const t_arg& out_struct, t_transport& transport) + bool notify_remote_command2(boost::uuids::uuid conn_id, int command, const t_arg& out_struct, t_transport& transport) { - const boost::uuids::uuid &conn_id = context.m_connection_id; + serialization::portable_storage stg; out_struct.store(stg); std::string buff_to_send; stg.store_to_binary(buff_to_send); - on_levin_traffic(context, true, true, false, buff_to_send.size(), command); int res = transport.notify(command, epee::strspan(buff_to_send), conn_id); if(res <=0 ) { @@ -204,7 +173,6 @@ namespace epee serialization::portable_storage strg; if(!strg.load_from_binary(in_buff)) { - on_levin_traffic(context, false, false, true, in_buff.size(), command); LOG_ERROR("Failed to load_from_binary in command " << command); return -1; } @@ -213,11 +181,9 @@ namespace epee if (!static_cast(in_struct).load(strg)) { - on_levin_traffic(context, false, false, true, in_buff.size(), command); LOG_ERROR("Failed to load in_struct in command " << command); return -1; } - on_levin_traffic(context, false, false, false, in_buff.size(), command); int res = cb(command, static_cast(in_struct), static_cast(out_struct), context); serialization::portable_storage strg_out; static_cast(out_struct).store(strg_out); @@ -227,7 +193,6 @@ namespace epee LOG_ERROR("Failed to store_to_binary in command" << command); return -1; } - on_levin_traffic(context, false, true, false, buff_out.size(), command); return res; } @@ -238,18 +203,15 @@ namespace epee serialization::portable_storage strg; if(!strg.load_from_binary(in_buff)) { - on_levin_traffic(context, false, false, true, in_buff.size(), command); LOG_ERROR("Failed to load_from_binary in notify " << command); return -1; } boost::value_initialized in_struct; if (!static_cast(in_struct).load(strg)) { - on_levin_traffic(context, false, false, true, in_buff.size(), command); LOG_ERROR("Failed to load in_struct in notify " << command); return -1; } - on_levin_traffic(context, false, false, false, in_buff.size(), command); return cb(command, in_struct, context); } @@ -295,20 +257,20 @@ namespace epee #define HANDLE_INVOKE2(command_id, func, type_name_in, typename_out) \ if(!is_notify && command_id == command) \ - {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);} + {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, buff_out, boost::bind(func, this, _1, _2, _3, _4), context);} #define HANDLE_INVOKE_T2(COMMAND, func) \ if(!is_notify && COMMAND::ID == command) \ - {handled=true;return epee::net_utils::buff_to_t_adapter(command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);} + {handled=true;return epee::net_utils::buff_to_t_adapter(command, in_buff, buff_out, boost::bind(func, this, _1, _2, _3, _4), context);} #define HANDLE_NOTIFY2(command_id, func, type_name_in) \ if(is_notify && command_id == command) \ - {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);} + {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, boost::bind(func, this, _1, _2, _3), context);} #define HANDLE_NOTIFY_T2(NOTIFY, func) \ if(is_notify && NOTIFY::ID == command) \ - {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);} + {handled=true;return epee::net_utils::buff_to_t_adapter(this, command, in_buff, boost::bind(func, this, _1, _2, _3), context);} #define CHAIN_INVOKE_MAP2(func) \ @@ -334,7 +296,6 @@ namespace epee #define END_INVOKE_MAP2() \ LOG_ERROR("Unknown command:" << command); \ - on_levin_traffic(context, false, false, true, in_buff.size(), "invalid-command"); \ return LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED; \ } } diff --git a/src/contrib/epee/include/storages/parserse_base_utils.h b/src/contrib/epee/include/storages/parserse_base_utils.h index 5a6cc0b..b5c4138 100644 --- a/src/contrib/epee/include/storages/parserse_base_utils.h +++ b/src/contrib/epee/include/storages/parserse_base_utils.h @@ -31,11 +31,6 @@ #include #include -#include "misc_log_ex.h" - -#undef MONERO_DEFAULT_LOG_CATEGORY -#define MONERO_DEFAULT_LOG_CATEGORY "serialization" - namespace epee { namespace misc_utils @@ -67,26 +62,6 @@ namespace misc_utils 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; - static const constexpr unsigned char isx[256] = - { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - }; - inline bool isspace(char c) { return lut[(uint8_t)c] & 8; @@ -159,6 +134,7 @@ namespace misc_utils while (fi != buf_end && ((lut[(uint8_t)*fi] & 32)) == 0) ++fi; val.assign(it, fi); + val.reserve(std::distance(star_end_string, buf_end)); it = fi; for(;it != buf_end;it++) { @@ -186,42 +162,6 @@ namespace misc_utils val.push_back('\\');break; case '/': //Slash character val.push_back('/');break; - case 'u': //Unicode code point - if (buf_end - it < 4) - { - ASSERT_MES_AND_THROW("Invalid Unicode escape sequence"); - } - else - { - uint32_t dst = 0; - for (int i = 0; i < 4; ++i) - { - const unsigned char tmp = isx[(unsigned char)*++it]; - CHECK_AND_ASSERT_THROW_MES(tmp != 0xff, "Bad Unicode encoding"); - dst = dst << 4 | tmp; - } - // encode as UTF-8 - if (dst <= 0x7f) - { - val.push_back(dst); - } - else if (dst <= 0x7ff) - { - val.push_back(0xc0 | (dst >> 6)); - val.push_back(0x80 | (dst & 0x3f)); - } - else if (dst <= 0xffff) - { - val.push_back(0xe0 | (dst >> 12)); - val.push_back(0x80 | ((dst >> 6) & 0x3f)); - val.push_back(0x80 | (dst & 0x3f)); - } - else - { - ASSERT_MES_AND_THROW("Unicode code point is out or range"); - } - } - break; default: val.push_back(*it); LOG_PRINT_L0("Unknown escape sequence :\"\\" << *it << "\""); diff --git a/src/contrib/epee/include/storages/portable_storage.h b/src/contrib/epee/include/storages/portable_storage.h index 4b759a2..d0e40d6 100644 --- a/src/contrib/epee/include/storages/portable_storage.h +++ b/src/contrib/epee/include/storages/portable_storage.h @@ -28,8 +28,6 @@ #pragma once -#include - #include "misc_language.h" #include "portable_storage_base.h" #include "portable_storage_to_bin.h" @@ -61,7 +59,7 @@ namespace epee bool get_value(const std::string& value_name, t_value& val, hsection hparent_section); bool get_value(const std::string& value_name, storage_entry& val, hsection hparent_section); template - bool set_value(const std::string& value_name, t_value&& target, hsection hparent_section); + bool set_value(const std::string& value_name, const t_value& target, hsection hparent_section); //serial access for arrays of values -------------------------------------- //values @@ -70,9 +68,9 @@ namespace epee template bool get_next_value(harray hval_array, t_value& target); template - harray insert_first_value(const std::string& value_name, t_value&& target, hsection hparent_section); + harray insert_first_value(const std::string& value_name, const t_value& target, hsection hparent_section); template - bool insert_next_value(harray hval_array, t_value&& target); + bool insert_next_value(harray hval_array, const t_value& target); //sections harray get_first_section(const std::string& pSectionName, hsection& h_child_section, hsection hparent_section); bool get_next_section(harray hSecArray, hsection& h_child_section); @@ -96,7 +94,7 @@ namespace epee hsection get_root_section() {return &m_root;} storage_entry* find_storage_entry(const std::string& pentry_name, hsection psection); template - storage_entry* insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, entry_type&& entry); + storage_entry* insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, const entry_type& entry); hsection insert_new_section(const std::string& pentry_name, hsection psection); @@ -243,22 +241,21 @@ namespace epee } //--------------------------------------------------------------------------------------------------------------- template - bool portable_storage::set_value(const std::string& value_name, t_value&& v, hsection hparent_section) + bool portable_storage::set_value(const std::string& value_name, const t_value& v, hsection hparent_section) { - using t_real_value = typename std::decay::type; - BOOST_MPL_ASSERT(( boost::mpl::contains::type, t_real_value> )); + BOOST_MPL_ASSERT(( boost::mpl::contains::type, t_value> )); TRY_ENTRY(); if(!hparent_section) hparent_section = &m_root; storage_entry* pentry = find_storage_entry(value_name, hparent_section); if(!pentry) { - pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, std::forward(v)); + pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, v); if(!pentry) return false; return true; } - *pentry = std::forward(v); + *pentry = storage_entry(v); return true; CATCH_ENTRY("portable_storage::template<>set_value", false); } @@ -277,12 +274,11 @@ namespace epee } //--------------------------------------------------------------------------------------------------------------- template - storage_entry* portable_storage::insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, entry_type&& entry) + storage_entry* portable_storage::insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, const entry_type& entry) { - static_assert(std::is_rvalue_reference(), "unexpected copy of value"); TRY_ENTRY(); CHECK_AND_ASSERT(psection, nullptr); - auto ins_res = psection->m_entries.emplace(pentry_name, std::forward(entry)); + auto ins_res = psection->m_entries.insert(std::pair(pentry_name, entry)); return &ins_res.first->second; CATCH_ENTRY("portable_storage::insert_new_entry_get_storage_entry", nullptr); } @@ -366,45 +362,41 @@ namespace epee } //--------------------------------------------------------------------------------------------------------------- template - harray portable_storage::insert_first_value(const std::string& value_name, t_value&& target, hsection hparent_section) + harray portable_storage::insert_first_value(const std::string& value_name, const t_value& target, hsection hparent_section) { - using t_real_value = typename std::decay::type; - static_assert(std::is_rvalue_reference(), "unexpected copy of value"); TRY_ENTRY(); if(!hparent_section) hparent_section = &m_root; storage_entry* pentry = find_storage_entry(value_name, hparent_section); if(!pentry) { - pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, array_entry(array_entry_t())); + pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, array_entry(array_entry_t())); if(!pentry) return nullptr; } if(pentry->type() != typeid(array_entry)) - *pentry = storage_entry(array_entry(array_entry_t())); + *pentry = storage_entry(array_entry(array_entry_t())); array_entry& arr = boost::get(*pentry); - if(arr.type() != typeid(array_entry_t)) - arr = array_entry(array_entry_t()); + if(arr.type() != typeid(array_entry_t)) + arr = array_entry(array_entry_t()); - array_entry_t& arr_typed = boost::get >(arr); - arr_typed.insert_first_val(std::forward(target)); + array_entry_t& arr_typed = boost::get >(arr); + arr_typed.insert_first_val(target); return &arr; CATCH_ENTRY("portable_storage::insert_first_value", nullptr); } //--------------------------------------------------------------------------------------------------------------- template - bool portable_storage::insert_next_value(harray hval_array, t_value&& target) + bool portable_storage::insert_next_value(harray hval_array, const t_value& target) { - using t_real_value = typename std::decay::type; - static_assert(std::is_rvalue_reference(), "unexpected copy of value"); TRY_ENTRY(); CHECK_AND_ASSERT(hval_array, false); - CHECK_AND_ASSERT_MES(hval_array->type() == typeid(array_entry_t), - false, "unexpected type in insert_next_value: " << typeid(array_entry_t).name()); + CHECK_AND_ASSERT_MES(hval_array->type() == typeid(array_entry_t), + false, "unexpected type in insert_next_value: " << typeid(array_entry_t).name()); - array_entry_t& arr_typed = boost::get >(*hval_array); - arr_typed.insert_next_value(std::forward(target)); + array_entry_t& arr_typed = boost::get >(*hval_array); + arr_typed.insert_next_value(target); return true; CATCH_ENTRY("portable_storage::insert_next_value", false); } diff --git a/src/contrib/epee/include/storages/portable_storage_base.h b/src/contrib/epee/include/storages/portable_storage_base.h index 1676f41..ca7c81d 100644 --- a/src/contrib/epee/include/storages/portable_storage_base.h +++ b/src/contrib/epee/include/storages/portable_storage_base.h @@ -84,13 +84,6 @@ namespace epee array_entry_t():m_it(m_array.end()){} array_entry_t(const array_entry_t& other):m_array(other.m_array), m_it(m_array.end()){} - array_entry_t& operator=(const array_entry_t& other) - { - m_array = other.m_array; - m_it = m_array.end(); - return *this; - } - const t_entry_type* get_first_val() const { m_it = m_array.begin(); @@ -118,16 +111,16 @@ namespace epee return (t_entry_type*)&(*(m_it++));//fuckoff } - t_entry_type& insert_first_val(t_entry_type&& v) + t_entry_type& insert_first_val(const t_entry_type& v) { m_array.clear(); m_it = m_array.end(); - return insert_next_value(std::move(v)); + return insert_next_value(v); } - t_entry_type& insert_next_value(t_entry_type&& v) + t_entry_type& insert_next_value(const t_entry_type& v) { - m_array.push_back(std::move(v)); + m_array.push_back(v); return m_array.back(); } diff --git a/src/contrib/epee/include/storages/portable_storage_from_bin.h b/src/contrib/epee/include/storages/portable_storage_from_bin.h index b39dc7c..e0a32b3 100644 --- a/src/contrib/epee/include/storages/portable_storage_from_bin.h +++ b/src/contrib/epee/include/storages/portable_storage_from_bin.h @@ -30,7 +30,6 @@ #include "misc_language.h" #include "portable_storage_base.h" -#include "portable_storage_bin_utils.h" #ifdef EPEE_PORTABLE_STORAGE_RECURSION_LIMIT #define EPEE_PORTABLE_STORAGE_RECURSION_LIMIT_INTERNAL EPEE_PORTABLE_STORAGE_RECURSION_LIMIT @@ -118,7 +117,6 @@ namespace epee RECURSION_LIMITATION(); static_assert(std::is_pod::value, "POD type expected"); read(&pod_val, sizeof(pod_val)); - pod_val = CONVERT_POD(pod_val); } template @@ -142,8 +140,8 @@ namespace epee sa.reserve(size); //TODO: add some optimization here later while(size--) - sa.m_array.push_back(read()); - return storage_entry(array_entry(std::move(sa))); + sa.m_array.push_back(read()); + return storage_entry(array_entry(sa)); } inline @@ -213,7 +211,7 @@ namespace epee { RECURSION_LIMITATION(); section s;//use extra variable due to vs bug, line "storage_entry se(section()); " can't be compiled in visual studio - storage_entry se(std::move(s)); + storage_entry se(s); section& section_entry = boost::get
(se); read(section_entry); return se; @@ -268,7 +266,7 @@ namespace epee //read section name string std::string sec_name; read_sec_name(sec_name); - sec.m_entries.emplace(std::move(sec_name), load_storage_entry()); + sec.m_entries.insert(std::make_pair(sec_name, load_storage_entry())); } } inline diff --git a/src/contrib/epee/include/storages/portable_storage_from_json.h b/src/contrib/epee/include/storages/portable_storage_from_json.h index 2b2dc7f..3e30525 100644 --- a/src/contrib/epee/include/storages/portable_storage_from_json.h +++ b/src/contrib/epee/include/storages/portable_storage_from_json.h @@ -128,20 +128,20 @@ namespace epee errno = 0; int64_t nval = strtoll(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - stg.set_value(name, int64_t(nval), current_section); + stg.set_value(name, nval, current_section); }else { errno = 0; uint64_t nval = strtoull(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - stg.set_value(name, uint64_t(nval), current_section); + stg.set_value(name, nval, current_section); } }else { errno = 0; double nval = strtod(val.data(), NULL); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - stg.set_value(name, double(nval), current_section); + stg.set_value(name, nval, current_section); } state = match_state_wonder_after_value; }else if(isalpha(*it) ) @@ -219,13 +219,13 @@ namespace epee errno = 0; int64_t nval = strtoll(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - h_array = stg.insert_first_value(name, int64_t(nval), current_section); + h_array = stg.insert_first_value(name, nval, current_section); }else { errno = 0; uint64_t nval = strtoull(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - h_array = stg.insert_first_value(name, uint64_t(nval), current_section); + h_array = stg.insert_first_value(name, nval, current_section); } CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry"); }else @@ -233,7 +233,7 @@ namespace epee errno = 0; double nval = strtod(val.data(), NULL); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - h_array = stg.insert_first_value(name, double(nval), current_section); + h_array = stg.insert_first_value(name, nval, current_section); CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry"); } @@ -310,20 +310,20 @@ namespace epee errno = 0; int64_t nval = strtoll(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - insert_res = stg.insert_next_value(h_array, int64_t(nval)); + insert_res = stg.insert_next_value(h_array, nval); }else { errno = 0; uint64_t nval = strtoull(val.data(), NULL, 10); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - insert_res = stg.insert_next_value(h_array, uint64_t(nval)); + insert_res = stg.insert_next_value(h_array, nval); } }else { errno = 0; double nval = strtod(val.data(), NULL); if (errno) throw std::runtime_error("Invalid number: " + std::string(val)); - insert_res = stg.insert_next_value(h_array, double(nval)); + insert_res = stg.insert_next_value(h_array, nval); } CHECK_AND_ASSERT_THROW_MES(insert_res, "Failed to insert next value"); state = match_state_array_after_value; diff --git a/src/contrib/epee/include/storages/portable_storage_to_bin.h b/src/contrib/epee/include/storages/portable_storage_to_bin.h index 137497e..9501bbc 100644 --- a/src/contrib/epee/include/storages/portable_storage_to_bin.h +++ b/src/contrib/epee/include/storages/portable_storage_to_bin.h @@ -31,7 +31,6 @@ #include "pragma_comp_defs.h" #include "misc_language.h" #include "portable_storage_base.h" -#include "portable_storage_bin_utils.h" namespace epee { @@ -41,9 +40,8 @@ namespace epee template size_t pack_varint_t(t_stream& strm, uint8_t type_or, size_t& pv) { - pack_value v = pv << 2; + pack_value v = (*((pack_value*)&pv)) << 2; v |= type_or; - v = CONVERT_POD(v); strm.write((const char*)&v, sizeof(pack_value)); return sizeof(pack_value); } @@ -95,11 +93,8 @@ namespace epee uint8_t type = contained_type|SERIALIZE_FLAG_ARRAY; m_strm.write((const char*)&type, 1); pack_varint(m_strm, arr_pod.m_array.size()); - for(t_pod_type x: arr_pod.m_array) - { - x = CONVERT_POD(x); + for(const t_pod_type& x: arr_pod.m_array) m_strm.write((const char*)&x, sizeof(t_pod_type)); - } return true; } @@ -152,8 +147,7 @@ namespace epee bool pack_pod_type(uint8_t type, const pod_type& v) { m_strm.write((const char*)&type, 1); - pod_type v0 = CONVERT_POD(v); - m_strm.write((const char*)&v0, sizeof(pod_type)); + m_strm.write((const char*)&v, sizeof(pod_type)); return true; } //section, array_entry