diff options
author | Lee Clagett <code@leeclagett.com> | 2020-10-13 15:15:07 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2020-10-10 15:28:40 +0000 |
commit | 7414e2bac11cbf9163ca16dc1b1510b4fd9a0d64 (patch) | |
tree | 1cd7aa7394630a8be218534112ba88a5b618d96a | |
parent | Merge pull request #7072 (diff) | |
download | monero-7414e2bac11cbf9163ca16dc1b1510b4fd9a0d64.tar.xz |
Change epee binary output from std::stringstream to byte_stream
Diffstat (limited to '')
23 files changed, 126 insertions, 106 deletions
diff --git a/contrib/epee/include/net/abstract_http_client.h b/contrib/epee/include/net/abstract_http_client.h index 1f8bbc605..5270824e1 100644 --- a/contrib/epee/include/net/abstract_http_client.h +++ b/contrib/epee/include/net/abstract_http_client.h @@ -70,7 +70,7 @@ namespace http virtual bool connect(std::chrono::milliseconds timeout) = 0; virtual bool disconnect() = 0; virtual bool is_connected(bool *ssl = NULL) = 0; - virtual bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0; + virtual bool invoke(const boost::string_ref uri, const boost::string_ref method, const boost::string_ref body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0; virtual bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string& body = std::string(), const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0; virtual bool invoke_post(const boost::string_ref uri, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0; virtual uint64_t get_bytes_sent() const = 0; diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h index 9645e896b..3725ef079 100644 --- a/contrib/epee/include/net/http_client.h +++ b/contrib/epee/include/net/http_client.h @@ -233,7 +233,7 @@ namespace net_utils } //--------------------------------------------------------------------------- - inline bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) override + inline bool invoke(const boost::string_ref uri, const boost::string_ref method, const boost::string_ref body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) override { CRITICAL_REGION_LOCAL(m_lock); if(!is_connected()) diff --git a/contrib/epee/include/net/http_server_handlers_map2.h b/contrib/epee/include/net/http_server_handlers_map2.h index ac22cd7a9..1665fdac7 100644 --- a/contrib/epee/include/net/http_server_handlers_map2.h +++ b/contrib/epee/include/net/http_server_handlers_map2.h @@ -118,8 +118,10 @@ return true; \ } \ uint64_t ticks2 = misc_utils::get_tick_count(); \ - epee::serialization::store_t_to_binary(static_cast<command_type::response&>(resp), response_info.m_body); \ + epee::byte_slice buffer; \ + epee::serialization::store_t_to_binary(static_cast<command_type::response&>(resp), buffer, 64 * 1024); \ uint64_t ticks3 = epee::misc_utils::get_tick_count(); \ + response_info.m_body.assign(reinterpret_cast<const char*>(buffer.data()), buffer.size()); \ response_info.m_mime_tipe = " application/octet-stream"; \ response_info.m_header_info.m_content_type = " application/octet-stream"; \ MDEBUG( s_pattern << "() processed with " << ticks1-ticks << "/"<< ticks2-ticks1 << "/" << ticks3-ticks2 << "ms"); \ diff --git a/contrib/epee/include/net/levin_base.h b/contrib/epee/include/net/levin_base.h index f9b6f9a81..ad561c5b6 100644 --- a/contrib/epee/include/net/levin_base.h +++ b/contrib/epee/include/net/levin_base.h @@ -31,7 +31,6 @@ #include <cstdint> -#include "byte_slice.h" #include "net_utils_base.h" #include "span.h" @@ -39,6 +38,7 @@ namespace epee { +class byte_slice; namespace levin { #pragma pack(push) @@ -86,7 +86,7 @@ namespace levin template<class t_connection_context = net_utils::connection_context_base> struct levin_commands_handler { - virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, t_connection_context& context)=0; + virtual int invoke(int command, const epee::span<const uint8_t> in_buff, byte_slice& buff_out, t_connection_context& context)=0; virtual int notify(int command, const epee::span<const uint8_t> in_buff, t_connection_context& context)=0; virtual void callback(t_connection_context& context){}; diff --git a/contrib/epee/include/net/levin_protocol_handler_async.h b/contrib/epee/include/net/levin_protocol_handler_async.h index 1341a4ae6..8cb2be3e1 100644 --- a/contrib/epee/include/net/levin_protocol_handler_async.h +++ b/contrib/epee/include/net/levin_protocol_handler_async.h @@ -514,16 +514,15 @@ public: { if(m_current_head.m_have_to_return_data) { - std::string return_buff; + byte_slice return_buff; const uint32_t return_code = m_config.m_pcommands_handler->invoke( m_current_head.m_command, buff_to_invoke, return_buff, m_connection_context ); bucket_head2 head = make_header(m_current_head.m_command, return_buff.size(), LEVIN_PACKET_RESPONSE, false); head.m_return_code = SWAP32LE(return_code); - return_buff.insert(0, reinterpret_cast<const char*>(&head), sizeof(head)); - if(!m_pservice_endpoint->do_send(byte_slice{std::move(return_buff)})) + if(!m_pservice_endpoint->do_send(byte_slice{{epee::as_byte_span(head), epee::to_span(return_buff)}})) return false; MDEBUG(m_connection_context << "LEVIN_PACKET_SENT. [len=" << head.m_cb diff --git a/contrib/epee/include/net/net_helper.h b/contrib/epee/include/net/net_helper.h index 9446e3588..508c79d76 100644 --- a/contrib/epee/include/net/net_helper.h +++ b/contrib/epee/include/net/net_helper.h @@ -44,6 +44,7 @@ #include <boost/lambda/lambda.hpp> #include <boost/interprocess/detail/atomic.hpp> #include <boost/system/error_code.hpp> +#include <boost/utility/string_ref.hpp> #include <functional> #include "net/net_utils_base.h" #include "net/net_ssl.h" @@ -280,7 +281,7 @@ namespace net_utils inline - bool send(const std::string& buff, std::chrono::milliseconds timeout) + bool send(const boost::string_ref buff, std::chrono::milliseconds timeout) { try @@ -298,7 +299,7 @@ namespace net_utils // object is used as a callback and will update the ec variable when the // operation completes. The blocking_udp_client.cpp example shows how you // can use boost::bind rather than boost::lambda. - async_write(buff.c_str(), buff.size(), ec); + async_write(buff.data(), buff.size(), ec); // Block until the asynchronous operation has completed. while (ec == boost::asio::error::would_block) diff --git a/contrib/epee/include/storages/http_abstract_invoke.h b/contrib/epee/include/storages/http_abstract_invoke.h index a8bc945a8..c4cb91130 100644 --- a/contrib/epee/include/storages/http_abstract_invoke.h +++ b/contrib/epee/include/storages/http_abstract_invoke.h @@ -29,6 +29,7 @@ #include <boost/utility/string_ref.hpp> #include <chrono> #include <string> +#include "byte_slice.h" #include "portable_storage_template_helper.h" #include "net/http_base.h" #include "net/http_server_handlers_map2.h" @@ -74,12 +75,12 @@ namespace epee template<class t_request, class t_response, class t_transport> 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") { - std::string req_param; - if(!serialization::store_t_to_binary(out_struct, req_param)) + byte_slice req_param; + if(!serialization::store_t_to_binary(out_struct, req_param, 16 * 1024)) return false; const http::http_response_info* pri = NULL; - if(!transport.invoke(uri, method, req_param, timeout, std::addressof(pri))) + if(!transport.invoke(uri, method, boost::string_ref{reinterpret_cast<const char*>(req_param.data()), req_param.size()}, timeout, std::addressof(pri))) { LOG_PRINT_L1("Failed to invoke http request to " << uri); return false; diff --git a/contrib/epee/include/storages/levin_abstract_invoke2.h b/contrib/epee/include/storages/levin_abstract_invoke2.h index cf1262486..95f0bb410 100644 --- a/contrib/epee/include/storages/levin_abstract_invoke2.h +++ b/contrib/epee/include/storages/levin_abstract_invoke2.h @@ -27,8 +27,10 @@ #pragma once #include "portable_storage_template_helper.h" +#include <boost/utility/string_ref.hpp> #include <boost/utility/value_init.hpp> #include <functional> +#include "byte_slice.h" #include "span.h" #include "net/levin_base.h" @@ -110,11 +112,12 @@ namespace epee 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); + byte_slice buff_to_send; + std::string buff_to_recv; + stg.store_to_binary(buff_to_send, 16 * 1024); 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); + int res = transport.invoke(command, boost::string_ref{reinterpret_cast<const char*>(buff_to_send.data()), buff_to_send.size()}, buff_to_recv, conn_id); if( res <=0 ) { LOG_PRINT_L1("Failed to invoke command " << command << " return code " << res); @@ -137,10 +140,10 @@ namespace epee const boost::uuids::uuid &conn_id = context.m_connection_id; typename serialization::portable_storage stg; const_cast<t_arg&>(out_struct).store(stg);//TODO: add true const support to searilzation - std::string buff_to_send; - stg.store_to_binary(buff_to_send); + byte_slice buff_to_send; + stg.store_to_binary(buff_to_send, 16 * 1024); on_levin_traffic(context, true, true, false, buff_to_send.size(), command); - int res = transport.invoke_async(command, epee::strspan<uint8_t>(buff_to_send), conn_id, [cb, command](int code, const epee::span<const uint8_t> buff, typename t_transport::connection_context& context)->bool + int res = transport.invoke_async(command, epee::to_span(buff_to_send), conn_id, [cb, command](int code, const epee::span<const uint8_t> buff, typename t_transport::connection_context& context)->bool { t_result result_struct = AUTO_VAL_INIT(result_struct); if( code <=0 ) @@ -184,11 +187,11 @@ namespace epee const boost::uuids::uuid &conn_id = context.m_connection_id; serialization::portable_storage stg; out_struct.store(stg); - std::string buff_to_send; + byte_slice 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<uint8_t>(buff_to_send), conn_id); + int res = transport.notify(command, epee::to_span(buff_to_send), conn_id); if(res <=0 ) { MERROR("Failed to notify command " << command << " return code " << res); @@ -199,7 +202,7 @@ namespace epee //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- template<class t_owner, class t_in_type, class t_out_type, class t_context, class callback_t> - int buff_to_t_adapter(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, callback_t cb, t_context& context ) + int buff_to_t_adapter(int command, const epee::span<const uint8_t> in_buff, byte_slice& buff_out, callback_t cb, t_context& context ) { serialization::portable_storage strg; if(!strg.load_from_binary(in_buff)) @@ -222,7 +225,7 @@ namespace epee serialization::portable_storage strg_out; static_cast<t_out_type&>(out_struct).store(strg_out); - if(!strg_out.store_to_binary(buff_out)) + if(!strg_out.store_to_binary(buff_out, 32 * 1024)) { LOG_ERROR("Failed to store_to_binary in command" << command); return -1; @@ -254,7 +257,7 @@ namespace epee } #define CHAIN_LEVIN_INVOKE_MAP2(context_type) \ - int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, context_type& context) \ + int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, context_type& context) \ { \ bool handled = false; \ return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ @@ -263,13 +266,13 @@ namespace epee #define CHAIN_LEVIN_NOTIFY_MAP2(context_type) \ int notify(int command, const epee::span<const uint8_t> in_buff, context_type& context) \ { \ - bool handled = false; std::string fake_str;\ + bool handled = false; epee::byte_slice fake_str; \ return handle_invoke_map(true, command, in_buff, fake_str, context, handled); \ } #define CHAIN_LEVIN_INVOKE_MAP() \ - int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, epee::net_utils::connection_context_base& context) \ + int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, epee::net_utils::connection_context_base& context) \ { \ bool handled = false; \ return handle_invoke_map(false, command, in_buff, buff_out, context, handled); \ @@ -289,7 +292,7 @@ namespace epee } #define BEGIN_INVOKE_MAP2(owner_type) \ - template <class t_context> int handle_invoke_map(bool is_notify, int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, t_context& context, bool& handled) \ + template <class t_context> int handle_invoke_map(bool is_notify, int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, t_context& context, bool& handled) \ { \ try { \ typedef owner_type internal_owner_type_name; diff --git a/contrib/epee/include/storages/portable_storage.h b/contrib/epee/include/storages/portable_storage.h index 4b759a24f..589e6ad63 100644 --- a/contrib/epee/include/storages/portable_storage.h +++ b/contrib/epee/include/storages/portable_storage.h @@ -32,7 +32,6 @@ #include "misc_language.h" #include "portable_storage_base.h" -#include "portable_storage_to_bin.h" #include "portable_storage_from_bin.h" #include "portable_storage_to_json.h" #include "portable_storage_from_json.h" @@ -42,6 +41,7 @@ namespace epee { + class byte_slice; namespace serialization { /************************************************************************/ @@ -83,7 +83,7 @@ namespace epee bool delete_entry(const std::string& pentry_name, hsection hparent_section = nullptr); //------------------------------------------------------------------------------- - bool store_to_binary(binarybuffer& target); + bool store_to_binary(byte_slice& target, std::size_t initial_buffer_size = 8192); bool load_from_binary(const epee::span<const uint8_t> target); bool load_from_binary(const std::string& target) { return load_from_binary(epee::strspan<uint8_t>(target)); } template<class trace_policy> @@ -133,22 +133,6 @@ namespace epee { return false;//TODO: don't think i ever again will use xml - ambiguous and "overtagged" format } - - inline - bool portable_storage::store_to_binary(binarybuffer& target) - { - TRY_ENTRY(); - std::stringstream ss; - storage_block_header sbh = AUTO_VAL_INIT(sbh); - sbh.m_signature_a = SWAP32LE(PORTABLE_STORAGE_SIGNATUREA); - sbh.m_signature_b = SWAP32LE(PORTABLE_STORAGE_SIGNATUREB); - sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER; - ss.write((const char*)&sbh, sizeof(storage_block_header)); - pack_entry_to_buff(ss, m_root); - target = ss.str(); - return true; - CATCH_ENTRY("portable_storage::store_to_binary", false) - } inline bool portable_storage::load_from_binary(const epee::span<const uint8_t> source) { diff --git a/contrib/epee/include/storages/portable_storage_template_helper.h b/contrib/epee/include/storages/portable_storage_template_helper.h index 13c870d44..39f900c8d 100644 --- a/contrib/epee/include/storages/portable_storage_template_helper.h +++ b/contrib/epee/include/storages/portable_storage_template_helper.h @@ -28,6 +28,7 @@ #include <string> +#include "byte_slice.h" #include "parserse_base_utils.h" #include "portable_storage.h" #include "file_io_utils.h" @@ -111,18 +112,18 @@ namespace epee } //----------------------------------------------------------------------------------------------------------- template<class t_struct> - bool store_t_to_binary(t_struct& str_in, std::string& binary_buff, size_t indent = 0) + bool store_t_to_binary(t_struct& str_in, byte_slice& binary_buff, size_t initial_buffer_size = 8192) { portable_storage ps; str_in.store(ps); - return ps.store_to_binary(binary_buff); + return ps.store_to_binary(binary_buff, initial_buffer_size); } //----------------------------------------------------------------------------------------------------------- template<class t_struct> - std::string store_t_to_binary(t_struct& str_in, size_t indent = 0) + byte_slice store_t_to_binary(t_struct& str_in, size_t initial_buffer_size = 8192) { - std::string binary_buff; - store_t_to_binary(str_in, binary_buff, indent); + byte_slice binary_buff; + store_t_to_binary(str_in, binary_buff, initial_buffer_size); return binary_buff; } } diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 8adf69162..5e101a86a 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -29,7 +29,7 @@ add_library(epee STATIC byte_slice.cpp byte_stream.cpp hex.cpp abstract_http_client.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp levin_base.cpp memwipe.c connection_basic.cpp network_throttle.cpp network_throttle-detail.cpp mlocker.cpp buffer.cpp net_ssl.cpp - int-util.cpp) + int-util.cpp portable_storage.cpp) if (USE_READLINE AND (GNU_READLINE_FOUND OR (DEPENDS AND NOT MINGW))) add_library(epee_readline STATIC readline_buffer.cpp) diff --git a/contrib/epee/src/portable_storage.cpp b/contrib/epee/src/portable_storage.cpp new file mode 100644 index 000000000..4534deff3 --- /dev/null +++ b/contrib/epee/src/portable_storage.cpp @@ -0,0 +1,29 @@ + +#include "byte_slice.h" +#include "byte_stream.h" +#include "misc_log_ex.h" +#include "span.h" +#include "storages/portable_storage.h" +#include "storages/portable_storage_to_bin.h" + +namespace epee +{ +namespace serialization +{ + bool portable_storage::store_to_binary(byte_slice& target, const std::size_t initial_buffer_size) + { + TRY_ENTRY(); + byte_stream ss; + ss.reserve(initial_buffer_size); + storage_block_header sbh{}; + sbh.m_signature_a = SWAP32LE(PORTABLE_STORAGE_SIGNATUREA); + sbh.m_signature_b = SWAP32LE(PORTABLE_STORAGE_SIGNATUREB); + sbh.m_ver = PORTABLE_STORAGE_FORMAT_VER; + ss.write(epee::as_byte_span(sbh)); + pack_entry_to_buff(ss, m_root); + target = epee::byte_slice{std::move(ss)}; + return true; + CATCH_ENTRY("portable_storage::store_to_binary", false) + } +} +} diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index ee3a67198..7093f7826 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -37,6 +37,7 @@ #include <boost/program_options/variables_map.hpp> #include <string> +#include "byte_slice.h" #include "math_helper.h" #include "storages/levin_abstract_invoke2.h" #include "warnings.h" @@ -100,7 +101,7 @@ namespace cryptonote void set_p2p_endpoint(nodetool::i_p2p_endpoint<connection_context>* p2p); //bool process_handshake_data(const blobdata& data, cryptonote_connection_context& context); bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital); - bool get_payload_sync_data(blobdata& data); + bool get_payload_sync_data(epee::byte_slice& data); bool get_payload_sync_data(CORE_SYNC_DATA& hshd); bool on_callback(cryptonote_connection_context& context); t_core& get_core(){return m_core;} @@ -191,10 +192,10 @@ namespace cryptonote bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context) { LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->"); - std::string blob; - epee::serialization::store_t_to_binary(arg, blob); + epee::byte_slice blob; + epee::serialization::store_t_to_binary(arg, blob, 256 * 1024); // optimize for block responses //handler_response_blocks_now(blob.size()); // XXX - return m_p2p->invoke_notify_to_peer(t_parameter::ID, epee::strspan<uint8_t>(blob), context); + return m_p2p->invoke_notify_to_peer(t_parameter::ID, epee::to_span(blob), context); } }; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 25b14d903..e16c2acff 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -425,7 +425,7 @@ namespace cryptonote } //------------------------------------------------------------------------------------------------------------------------ template<class t_core> - bool t_cryptonote_protocol_handler<t_core>::get_payload_sync_data(blobdata& data) + bool t_cryptonote_protocol_handler<t_core>::get_payload_sync_data(epee::byte_slice& data) { CORE_SYNC_DATA hsd = {}; get_payload_sync_data(hsd); @@ -2586,15 +2586,15 @@ skip: // send fluffy ones first, we want to encourage people to run that if (!fluffyConnections.empty()) { - std::string fluffyBlob; - epee::serialization::store_t_to_binary(fluffy_arg, fluffyBlob); - m_p2p->relay_notify_to_list(NOTIFY_NEW_FLUFFY_BLOCK::ID, epee::strspan<uint8_t>(fluffyBlob), std::move(fluffyConnections)); + epee::byte_slice fluffyBlob; + epee::serialization::store_t_to_binary(fluffy_arg, fluffyBlob, 32 * 1024); + m_p2p->relay_notify_to_list(NOTIFY_NEW_FLUFFY_BLOCK::ID, epee::to_span(fluffyBlob), std::move(fluffyConnections)); } if (!fullConnections.empty()) { - std::string fullBlob; - epee::serialization::store_t_to_binary(arg, fullBlob); - m_p2p->relay_notify_to_list(NOTIFY_NEW_BLOCK::ID, epee::strspan<uint8_t>(fullBlob), std::move(fullConnections)); + epee::byte_slice fullBlob; + epee::serialization::store_t_to_binary(arg, fullBlob, 128 * 1024); + m_p2p->relay_notify_to_list(NOTIFY_NEW_BLOCK::ID, epee::to_span(fullBlob), std::move(fullConnections)); } return true; diff --git a/src/cryptonote_protocol/levin_notify.cpp b/src/cryptonote_protocol/levin_notify.cpp index 2d04dffb6..318bcf4e1 100644 --- a/src/cryptonote_protocol/levin_notify.cpp +++ b/src/cryptonote_protocol/levin_notify.cpp @@ -36,6 +36,7 @@ #include <stdexcept> #include <utility> +#include "byte_slice.h" #include "common/expect.h" #include "common/varint.h" #include "cryptonote_config.h" @@ -52,7 +53,7 @@ namespace { - int get_command_from_message(const cryptonote::blobdata &msg) + int get_command_from_message(const epee::byte_slice &msg) { return msg.size() >= sizeof(epee::levin::bucket_head2) ? SWAP32LE(((epee::levin::bucket_head2*)msg.data())->m_command) : 0; } @@ -166,7 +167,7 @@ namespace levin return get_out_connections(p2p, get_blockchain_height(p2p, core)); } - std::string make_tx_payload(std::vector<blobdata>&& txs, const bool pad, const bool fluff) + epee::byte_slice make_tx_payload(std::vector<blobdata>&& txs, const bool pad, const bool fluff) { NOTIFY_NEW_TRANSACTIONS::request request{}; request.txs = std::move(txs); @@ -188,7 +189,7 @@ namespace levin padding -= overhead; request._ = std::string(padding, ' '); - std::string arg_buff; + epee::byte_slice arg_buff; epee::serialization::store_t_to_binary(request, arg_buff); // we probably lowballed the payload size a bit, so added a but too much. Fix this now. @@ -200,7 +201,7 @@ namespace levin // if the size of _ moved enough, we might lose byte in size encoding, we don't care } - std::string fullBlob; + epee::byte_slice fullBlob; if (!epee::serialization::store_t_to_binary(request, fullBlob)) throw std::runtime_error{"Failed to serialize to epee binary format"}; @@ -209,12 +210,12 @@ namespace levin bool make_payload_send_txs(connections& p2p, std::vector<blobdata>&& txs, const boost::uuids::uuid& destination, const bool pad, const bool fluff) { - const cryptonote::blobdata blob = make_tx_payload(std::move(txs), pad, fluff); + const epee::byte_slice blob = make_tx_payload(std::move(txs), pad, fluff); p2p.for_connection(destination, [&blob](detail::p2p_context& context) { on_levin_traffic(context, true, true, false, blob.size(), get_command_from_message(blob)); return true; }); - return p2p.notify(NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(blob), destination); + return p2p.notify(NOTIFY_NEW_TRANSACTIONS::ID, epee::to_span(blob), destination); } /* The current design uses `asio::strand`s. The documentation isn't as clear @@ -825,9 +826,9 @@ namespace levin // Padding is not useful when using noise mode. Send as stem so receiver // forwards in Dandelion++ mode. - const std::string payload = make_tx_payload(std::move(txs), false, false); + const epee::byte_slice payload = make_tx_payload(std::move(txs), false, false); epee::byte_slice message = epee::levin::make_fragmented_notify( - zone_->noise, NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload) + zone_->noise, NOTIFY_NEW_TRANSACTIONS::ID, epee::to_span(payload) ); if (CRYPTONOTE_MAX_FRAGMENTS * zone_->noise.size() < message.size()) { diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 920e0413c..91b3c0535 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3788,7 +3788,7 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable //---------------------------------------------------------------------------------------------------- boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee::wipeable_string& password, bool watch_only) { - std::string account_data; + epee::byte_slice account_data; std::string multisig_signers; std::string multisig_derivations; cryptonote::account_base account = m_account; @@ -3815,7 +3815,7 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee: rapidjson::Document json; json.SetObject(); rapidjson::Value value(rapidjson::kStringType); - value.SetString(account_data.c_str(), account_data.length()); + value.SetString(reinterpret_cast<const char*>(account_data.data()), account_data.size()); json.AddMember("key_data", value, json.GetAllocator()); if (!seed_language.empty()) { @@ -3989,13 +3989,12 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee: rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); json.Accept(writer); - account_data = buffer.GetString(); // Encrypt the entire JSON object. std::string cipher; - cipher.resize(account_data.size()); + cipher.resize(buffer.GetSize()); keys_file_data.get().iv = crypto::rand<crypto::chacha_iv>(); - crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.get().iv, &cipher[0]); + crypto::chacha20(buffer.GetString(), buffer.GetSize(), key, keys_file_data.get().iv, &cipher[0]); keys_file_data.get().account_data = cipher; return keys_file_data; } diff --git a/tests/fuzz/http-client.cpp b/tests/fuzz/http-client.cpp index 4b12b36bb..65df24a3f 100644 --- a/tests/fuzz/http-client.cpp +++ b/tests/fuzz/http-client.cpp @@ -26,6 +26,7 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include <boost/utility/string_ref.hpp> #include "include_base_utils.h" #include "file_io_utils.h" #include "net/http_client.h" @@ -38,7 +39,7 @@ public: bool connect(const std::string& addr, int port, std::chrono::milliseconds timeout, bool ssl = false, const std::string& bind_ip = "0.0.0.0") { return true; } bool connect(const std::string& addr, const std::string& port, std::chrono::milliseconds timeout, bool ssl = false, const std::string& bind_ip = "0.0.0.0") { return true; } bool disconnect() { return true; } - bool send(const std::string& buff, std::chrono::milliseconds timeout) { return true; } + bool send(const boost::string_ref buff, std::chrono::milliseconds timeout) { return true; } bool send(const void* data, size_t sz) { return true; } bool is_connected() { return true; } bool recv(std::string& buff, std::chrono::milliseconds timeout) diff --git a/tests/fuzz/levin.cpp b/tests/fuzz/levin.cpp index 012d05f36..b090c350b 100644 --- a/tests/fuzz/levin.cpp +++ b/tests/fuzz/levin.cpp @@ -65,13 +65,13 @@ namespace { } - virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context) + virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, test_levin_connection_context& context) { m_invoke_counter.inc(); boost::unique_lock<boost::mutex> lock(m_mutex); m_last_command = command; m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size()); - buff_out = m_invoke_out_buf; + buff_out = m_invoke_out_buf.clone(); return m_return_code; } @@ -111,8 +111,7 @@ namespace int return_code() const { return m_return_code; } void return_code(int v) { m_return_code = v; } - const std::string& invoke_out_buf() const { return m_invoke_out_buf; } - void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; } + void invoke_out_buf(std::string v) { m_invoke_out_buf = epee::byte_slice{std::move(v)}; } int last_command() const { return m_last_command; } const std::string& last_in_buf() const { return m_last_in_buf; } @@ -127,7 +126,7 @@ namespace boost::mutex m_mutex; int m_return_code; - std::string m_invoke_out_buf; + epee::byte_slice m_invoke_out_buf; int m_last_command; std::string m_last_in_buf; diff --git a/tests/net_load_tests/net_load_tests.h b/tests/net_load_tests/net_load_tests.h index 882d42c02..e7e0ee247 100644 --- a/tests/net_load_tests/net_load_tests.h +++ b/tests/net_load_tests/net_load_tests.h @@ -64,7 +64,7 @@ namespace net_load_tests { } - virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_connection_context& context) + virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, test_connection_context& context) { //m_invoke_counter.inc(); //std::unique_lock<std::mutex> lock(m_mutex); diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp index 314fcf9f2..ab6791324 100644 --- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp +++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp @@ -56,13 +56,13 @@ namespace { } - virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context) + virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, test_levin_connection_context& context) { m_invoke_counter.inc(); boost::unique_lock<boost::mutex> lock(m_mutex); m_last_command = command; m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size()); - buff_out = m_invoke_out_buf; + buff_out = m_invoke_out_buf.clone(); return m_return_code; } @@ -102,8 +102,7 @@ namespace int return_code() const { return m_return_code; } void return_code(int v) { m_return_code = v; } - const std::string& invoke_out_buf() const { return m_invoke_out_buf; } - void invoke_out_buf(const std::string& v) { m_invoke_out_buf = v; } + void invoke_out_buf(std::string v) { m_invoke_out_buf = epee::byte_slice{std::move(v)}; } int last_command() const { return m_last_command; } const std::string& last_in_buf() const { return m_last_in_buf; } @@ -118,7 +117,7 @@ namespace boost::mutex m_mutex; int m_return_code; - std::string m_invoke_out_buf; + epee::byte_slice m_invoke_out_buf; int m_last_command; std::string m_last_in_buf; diff --git a/tests/unit_tests/levin.cpp b/tests/unit_tests/levin.cpp index 22638942d..2ad149afe 100644 --- a/tests/unit_tests/levin.cpp +++ b/tests/unit_tests/levin.cpp @@ -238,9 +238,9 @@ namespace return {connection, std::move(request)}; } - virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, cryptonote::levin::detail::p2p_context& context) override final + virtual int invoke(int command, const epee::span<const uint8_t> in_buff, epee::byte_slice& buff_out, cryptonote::levin::detail::p2p_context& context) override final { - buff_out.clear(); + buff_out = nullptr; invoked_.push_back( {context.m_connection_id, command, std::string{reinterpret_cast<const char*>(in_buff.data()), in_buff.size()}} ); diff --git a/tests/unit_tests/net.cpp b/tests/unit_tests/net.cpp index f5aef4796..dffda5e4e 100644 --- a/tests/unit_tests/net.cpp +++ b/tests/unit_tests/net.cpp @@ -245,7 +245,7 @@ namespace TEST(tor_address, epee_serializev_v2) { - std::string buffer{}; + epee::byte_slice buffer{}; { test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v2_onion, 10))}; EXPECT_FALSE(command.tor.is_unknown()); @@ -266,7 +266,7 @@ TEST(tor_address, epee_serializev_v2) EXPECT_EQ(0u, command.tor.port()); epee::serialization::portable_storage stg{}; - EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); EXPECT_TRUE(command.load(stg)); } EXPECT_FALSE(command.tor.is_unknown()); @@ -277,7 +277,7 @@ TEST(tor_address, epee_serializev_v2) // make sure that exceeding max buffer doesn't destroy tor_address::_load { epee::serialization::portable_storage stg{}; - stg.load_from_binary(buffer); + stg.load_from_binary(epee::to_span(buffer)); std::string host{}; ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); @@ -296,7 +296,7 @@ TEST(tor_address, epee_serializev_v2) TEST(tor_address, epee_serializev_v3) { - std::string buffer{}; + epee::byte_slice buffer{}; { test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v3_onion, 10))}; EXPECT_FALSE(command.tor.is_unknown()); @@ -317,7 +317,7 @@ TEST(tor_address, epee_serializev_v3) EXPECT_EQ(0u, command.tor.port()); epee::serialization::portable_storage stg{}; - EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); EXPECT_TRUE(command.load(stg)); } EXPECT_FALSE(command.tor.is_unknown()); @@ -328,7 +328,7 @@ TEST(tor_address, epee_serializev_v3) // make sure that exceeding max buffer doesn't destroy tor_address::_load { epee::serialization::portable_storage stg{}; - stg.load_from_binary(buffer); + stg.load_from_binary(epee::to_span(buffer)); std::string host{}; ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); @@ -347,7 +347,7 @@ TEST(tor_address, epee_serializev_v3) TEST(tor_address, epee_serialize_unknown) { - std::string buffer{}; + epee::byte_slice buffer{}; { test_command_tor command{net::tor_address::unknown()}; EXPECT_TRUE(command.tor.is_unknown()); @@ -368,7 +368,7 @@ TEST(tor_address, epee_serialize_unknown) EXPECT_EQ(0u, command.tor.port()); epee::serialization::portable_storage stg{}; - EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); EXPECT_TRUE(command.load(stg)); } EXPECT_TRUE(command.tor.is_unknown()); @@ -379,7 +379,7 @@ TEST(tor_address, epee_serialize_unknown) // make sure that exceeding max buffer doesn't destroy tor_address::_load { epee::serialization::portable_storage stg{}; - stg.load_from_binary(buffer); + stg.load_from_binary(epee::to_span(buffer)); std::string host{}; ASSERT_TRUE(stg.get_value("host", host, stg.open_section("tor", nullptr, false))); @@ -700,7 +700,7 @@ namespace TEST(i2p_address, epee_serializev_b32) { - std::string buffer{}; + epee::byte_slice buffer{}; { test_command_i2p command{MONERO_UNWRAP(net::i2p_address::make(b32_i2p, 10))}; EXPECT_FALSE(command.i2p.is_unknown()); @@ -721,7 +721,7 @@ TEST(i2p_address, epee_serializev_b32) EXPECT_EQ(0u, command.i2p.port()); epee::serialization::portable_storage stg{}; - EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); EXPECT_TRUE(command.load(stg)); } EXPECT_FALSE(command.i2p.is_unknown()); @@ -732,7 +732,7 @@ TEST(i2p_address, epee_serializev_b32) // make sure that exceeding max buffer doesn't destroy i2p_address::_load { epee::serialization::portable_storage stg{}; - stg.load_from_binary(buffer); + stg.load_from_binary(epee::to_span(buffer)); std::string host{}; ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); @@ -751,7 +751,7 @@ TEST(i2p_address, epee_serializev_b32) TEST(i2p_address, epee_serialize_unknown) { - std::string buffer{}; + epee::byte_slice buffer{}; { test_command_i2p command{net::i2p_address::unknown()}; EXPECT_TRUE(command.i2p.is_unknown()); @@ -772,7 +772,7 @@ TEST(i2p_address, epee_serialize_unknown) EXPECT_EQ(0u, command.i2p.port()); epee::serialization::portable_storage stg{}; - EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(stg.load_from_binary(epee::to_span(buffer))); EXPECT_TRUE(command.load(stg)); } EXPECT_TRUE(command.i2p.is_unknown()); @@ -783,7 +783,7 @@ TEST(i2p_address, epee_serialize_unknown) // make sure that exceeding max buffer doesn't destroy i2p_address::_load { epee::serialization::portable_storage stg{}; - stg.load_from_binary(buffer); + stg.load_from_binary(epee::to_span(buffer)); std::string host{}; ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); diff --git a/tests/unit_tests/test_protocol_pack.cpp b/tests/unit_tests/test_protocol_pack.cpp index 312f18dfe..1a4fd30f8 100644 --- a/tests/unit_tests/test_protocol_pack.cpp +++ b/tests/unit_tests/test_protocol_pack.cpp @@ -36,7 +36,7 @@ TEST(protocol_pack, protocol_pack_command) { - std::string buff; + epee::byte_slice buff; cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request r; r.start_height = 1; r.total_height = 3; @@ -47,7 +47,7 @@ TEST(protocol_pack, protocol_pack_command) ASSERT_TRUE(res); cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request r2; - res = epee::serialization::load_t_from_binary(r2, buff); + res = epee::serialization::load_t_from_binary(r2, epee::to_span(buff)); ASSERT_TRUE(res); ASSERT_TRUE(r.m_block_ids.size() == i); ASSERT_TRUE(r.start_height == 1); |