From 7414e2bac11cbf9163ca16dc1b1510b4fd9a0d64 Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Tue, 13 Oct 2020 15:15:07 +0000 Subject: Change epee binary output from std::stringstream to byte_stream --- src/cryptonote_protocol/cryptonote_protocol_handler.h | 9 +++++---- src/cryptonote_protocol/cryptonote_protocol_handler.inl | 14 +++++++------- src/cryptonote_protocol/levin_notify.cpp | 17 +++++++++-------- src/wallet/wallet2.cpp | 9 ++++----- 4 files changed, 25 insertions(+), 24 deletions(-) (limited to 'src') 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 #include +#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* 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(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 - bool t_cryptonote_protocol_handler::get_payload_sync_data(blobdata& data) + bool t_cryptonote_protocol_handler::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(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(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 #include +#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&& txs, const bool pad, const bool fluff) + epee::byte_slice make_tx_payload(std::vector&& 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&& 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(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(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::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::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(account_data.data()), account_data.size()); json.AddMember("key_data", value, json.GetAllocator()); if (!seed_language.empty()) { @@ -3989,13 +3989,12 @@ boost::optional wallet2::get_keys_file_data(const epee: rapidjson::StringBuffer buffer; rapidjson::Writer 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::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; } -- cgit v1.2.3