Merge pull request #7661

08e4497 Improve cryptonote (block and tx) binary read performance (Lee Clagett)
This commit is contained in:
luigi1111 2021-06-10 12:00:54 -05:00
commit 7499837a6f
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
29 changed files with 229 additions and 230 deletions

View file

@ -31,7 +31,6 @@
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
#include <memory> #include <memory>
#include <string>
#include <type_traits> #include <type_traits>
namespace epee namespace epee
@ -167,10 +166,11 @@ namespace epee
} }
//! make a span from a std::string //! make a span from a std::string
template<typename T> template<typename T, typename U>
span<const T> strspan(const std::string &s) noexcept span<const T> strspan(const U&s) noexcept
{ {
static_assert(std::is_same<T, char>() || std::is_same<T, unsigned char>() || std::is_same<T, int8_t>() || std::is_same<T, uint8_t>(), "Unexpected type"); static_assert(std::is_same<typename U::value_type, char>(), "unexpected source type");
static_assert(std::is_same<T, char>() || std::is_same<T, unsigned char>() || std::is_same<T, int8_t>() || std::is_same<T, uint8_t>(), "Unexpected destination type");
return {reinterpret_cast<const T*>(s.data()), s.size()}; return {reinterpret_cast<const T*>(s.data()), s.size()};
} }
} }

View file

@ -388,9 +388,7 @@ static bool for_all_transactions(const std::string &filename, uint64_t &start_id
cryptonote::transaction_prefix tx; cryptonote::transaction_prefix tx;
blobdata bd; blobdata bd;
bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size); bd.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size);
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(bd)};
ss << bd;
binary_archive<false> ba(ss);
bool r = do_serialize(ba, tx); bool r = do_serialize(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");

View file

@ -152,10 +152,6 @@ namespace cryptonote
}; };
template<typename T> static inline unsigned int getpos(T &ar) { return 0; }
template<> inline unsigned int getpos(binary_archive<true> &ar) { return ar.stream().tellp(); }
template<> inline unsigned int getpos(binary_archive<false> &ar) { return ar.stream().tellg(); }
class transaction_prefix class transaction_prefix
{ {
@ -236,17 +232,17 @@ namespace cryptonote
set_blob_size_valid(false); set_blob_size_valid(false);
} }
const unsigned int start_pos = getpos(ar); const auto start_pos = ar.getpos();
FIELDS(*static_cast<transaction_prefix *>(this)) FIELDS(*static_cast<transaction_prefix *>(this))
if (std::is_same<Archive<W>, binary_archive<W>>()) if (std::is_same<Archive<W>, binary_archive<W>>())
prefix_size = getpos(ar) - start_pos; prefix_size = ar.getpos() - start_pos;
if (version == 1) if (version == 1)
{ {
if (std::is_same<Archive<W>, binary_archive<W>>()) if (std::is_same<Archive<W>, binary_archive<W>>())
unprunable_size = getpos(ar) - start_pos; unprunable_size = ar.getpos() - start_pos;
ar.tag("signatures"); ar.tag("signatures");
ar.begin_array(); ar.begin_array();
@ -284,11 +280,11 @@ namespace cryptonote
{ {
ar.begin_object(); ar.begin_object();
bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size()); bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
if (!r || !ar.stream().good()) return false; if (!r || !ar.good()) return false;
ar.end_object(); ar.end_object();
if (std::is_same<Archive<W>, binary_archive<W>>()) if (std::is_same<Archive<W>, binary_archive<W>>())
unprunable_size = getpos(ar) - start_pos; unprunable_size = ar.getpos() - start_pos;
if (!pruned && rct_signatures.type != rct::RCTTypeNull) if (!pruned && rct_signatures.type != rct::RCTTypeNull)
{ {
@ -296,7 +292,7 @@ namespace cryptonote
ar.begin_object(); ar.begin_object();
r = rct_signatures.p.serialize_rctsig_prunable(ar, rct_signatures.type, vin.size(), vout.size(), r = rct_signatures.p.serialize_rctsig_prunable(ar, rct_signatures.type, vin.size(), vout.size(),
vin.size() > 0 && vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(vin[0]).key_offsets.size() - 1 : 0); vin.size() > 0 && vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(vin[0]).key_offsets.size() - 1 : 0);
if (!r || !ar.stream().good()) return false; if (!r || !ar.good()) return false;
ar.end_object(); ar.end_object();
} }
} }
@ -320,13 +316,13 @@ namespace cryptonote
{ {
ar.begin_object(); ar.begin_object();
bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size()); bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
if (!r || !ar.stream().good()) return false; if (!r || !ar.good()) return false;
ar.end_object(); ar.end_object();
} }
} }
if (!typename Archive<W>::is_saving()) if (!typename Archive<W>::is_saving())
pruned = true; pruned = true;
return ar.stream().good(); return ar.good();
} }
private: private:

View file

@ -211,9 +211,7 @@ namespace cryptonote
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_tx_from_blob(const blobdata_ref& tx_blob, transaction& tx) bool parse_and_validate_tx_from_blob(const blobdata_ref& tx_blob, transaction& tx)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
ss << tx_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize(ba, tx); bool r = ::serialization::serialize(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data"); CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data");
@ -224,9 +222,7 @@ namespace cryptonote
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_tx_base_from_blob(const blobdata_ref& tx_blob, transaction& tx) bool parse_and_validate_tx_base_from_blob(const blobdata_ref& tx_blob, transaction& tx)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
ss << tx_blob;
binary_archive<false> ba(ss);
bool r = tx.serialize_base(ba); bool r = tx.serialize_base(ba);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, true), false, "Failed to expand transaction data"); CHECK_AND_ASSERT_MES(expand_transaction_1(tx, true), false, "Failed to expand transaction data");
@ -236,9 +232,7 @@ namespace cryptonote
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_tx_prefix_from_blob(const blobdata_ref& tx_blob, transaction_prefix& tx) bool parse_and_validate_tx_prefix_from_blob(const blobdata_ref& tx_blob, transaction_prefix& tx)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
ss << tx_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize_noeof(ba, tx); bool r = ::serialization::serialize_noeof(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction prefix from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction prefix from blob");
return true; return true;
@ -246,9 +240,7 @@ namespace cryptonote
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_tx_from_blob(const blobdata_ref& tx_blob, transaction& tx, crypto::hash& tx_hash) bool parse_and_validate_tx_from_blob(const blobdata_ref& tx_blob, transaction& tx, crypto::hash& tx_hash)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(tx_blob)};
ss << tx_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize(ba, tx); bool r = ::serialization::serialize(ba, tx);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse transaction from blob");
CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data"); CHECK_AND_ASSERT_MES(expand_transaction_1(tx, false), false, "Failed to expand transaction data");
@ -532,22 +524,15 @@ namespace cryptonote
if(tx_extra.empty()) if(tx_extra.empty())
return true; return true;
std::string extra_str(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()); binary_archive<false> ar{epee::to_span(tx_extra)};
std::istringstream iss(extra_str);
binary_archive<false> ar(iss);
bool eof = false; do
while (!eof)
{ {
tx_extra_field field; tx_extra_field field;
bool r = ::do_serialize(ar, field); bool r = ::do_serialize(ar, field);
CHECK_AND_NO_ASSERT_MES_L1(r, false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()))); CHECK_AND_NO_ASSERT_MES_L1(r, false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size())));
tx_extra_fields.push_back(field); tx_extra_fields.push_back(field);
} while (!ar.eof());
std::ios_base::iostate state = iss.rdstate();
eof = (EOF == iss.peek());
iss.clear(state);
}
CHECK_AND_NO_ASSERT_MES_L1(::serialization::check_stream_state(ar), false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()))); CHECK_AND_NO_ASSERT_MES_L1(::serialization::check_stream_state(ar), false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size())));
return true; return true;
@ -578,13 +563,10 @@ namespace cryptonote
return true; return true;
} }
std::string extra_str(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()); binary_archive<false> ar{epee::to_span(tx_extra)};
std::istringstream iss(extra_str);
binary_archive<false> ar(iss);
bool eof = false;
size_t processed = 0; size_t processed = 0;
while (!eof) do
{ {
tx_extra_field field; tx_extra_field field;
bool r = ::do_serialize(ar, field); bool r = ::do_serialize(ar, field);
@ -596,12 +578,8 @@ namespace cryptonote
break; break;
} }
tx_extra_fields.push_back(field); tx_extra_fields.push_back(field);
processed = iss.tellg(); processed = ar.getpos();
} while (!ar.eof());
std::ios_base::iostate state = iss.rdstate();
eof = (EOF == iss.peek());
iss.clear(state);
}
if (!::serialization::check_stream_state(ar)) if (!::serialization::check_stream_state(ar))
{ {
MWARNING("failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()))); MWARNING("failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size())));
@ -752,24 +730,18 @@ namespace cryptonote
if (tx_extra.empty()) if (tx_extra.empty())
return true; return true;
std::string extra_str(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()); std::string extra_str(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size());
std::istringstream iss(extra_str); binary_archive<false> ar{epee::strspan<std::uint8_t>(extra_str)};
binary_archive<false> ar(iss);
std::ostringstream oss; std::ostringstream oss;
binary_archive<true> newar(oss); binary_archive<true> newar(oss);
bool eof = false; do
while (!eof)
{ {
tx_extra_field field; tx_extra_field field;
bool r = ::do_serialize(ar, field); bool r = ::do_serialize(ar, field);
CHECK_AND_NO_ASSERT_MES_L1(r, false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()))); CHECK_AND_NO_ASSERT_MES_L1(r, false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size())));
if (field.type() != type) if (field.type() != type)
::do_serialize(newar, field); ::do_serialize(newar, field);
} while (!ar.eof());
std::ios_base::iostate state = iss.rdstate();
eof = (EOF == iss.peek());
iss.clear(state);
}
CHECK_AND_NO_ASSERT_MES_L1(::serialization::check_stream_state(ar), false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size()))); CHECK_AND_NO_ASSERT_MES_L1(::serialization::check_stream_state(ar), false, "failed to deserialize extra field. extra = " << string_tools::buff_to_hex_nodelimer(std::string(reinterpret_cast<const char*>(tx_extra.data()), tx_extra.size())));
tx_extra.clear(); tx_extra.clear();
std::string s = oss.str(); std::string s = oss.str();
@ -1357,9 +1329,7 @@ namespace cryptonote
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_block_from_blob(const blobdata_ref& b_blob, block& b, crypto::hash *block_hash) bool parse_and_validate_block_from_blob(const blobdata_ref& b_blob, block& b, crypto::hash *block_hash)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(b_blob)};
ss << b_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize(ba, b); bool r = ::serialization::serialize(ba, b);
CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob");
b.invalidate_hashes(); b.invalidate_hashes();

View file

@ -148,9 +148,7 @@ namespace cryptonote
template<class t_object> template<class t_object>
bool t_serializable_object_from_blob(t_object& to, const blobdata& b_blob) bool t_serializable_object_from_blob(t_object& to, const blobdata& b_blob)
{ {
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(b_blob)};
ss << b_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize(ba, to); bool r = ::serialization::serialize(ba, to);
return r; return r;
} }

View file

@ -57,11 +57,7 @@ namespace cryptonote
// size - 1 - because of variant tag // size - 1 - because of variant tag
for (size = 1; size <= TX_EXTRA_PADDING_MAX_COUNT; ++size) for (size = 1; size <= TX_EXTRA_PADDING_MAX_COUNT; ++size)
{ {
std::ios_base::iostate state = ar.stream().rdstate(); if (ar.eof())
bool eof = EOF == ar.stream().peek();
ar.stream().clear(state);
if (eof)
break; break;
uint8_t zero; uint8_t zero;
@ -139,8 +135,7 @@ namespace cryptonote
if(!::do_serialize(ar, field)) if(!::do_serialize(ar, field))
return false; return false;
std::istringstream iss(field); binary_archive<false> iar{epee::strspan<std::uint8_t>(field)};
binary_archive<false> iar(iss);
serialize_helper helper(*this); serialize_helper helper(*this);
return ::serialization::serialize(iar, helper); return ::serialization::serialize(iar, helper);
} }

View file

@ -66,9 +66,7 @@ namespace protocol{
template<typename T> template<typename T>
bool cn_deserialize(const void * buff, size_t len, T & dst){ bool cn_deserialize(const void * buff, size_t len, T & dst){
std::stringstream ss; binary_archive<false> ba{{reinterpret_cast<const std::uint8_t*>(buff), len}};
ss.write(static_cast<const char *>(buff), len); //ss << tx_blob;
binary_archive<false> ba(ss);
bool r = ::serialization::serialize(ba, dst); bool r = ::serialization::serialize(ba, dst);
return r; return r;
} }

View file

@ -284,7 +284,7 @@ namespace rct {
{ {
FIELD(type) FIELD(type)
if (type == RCTTypeNull) if (type == RCTTypeNull)
return ar.stream().good(); return ar.good();
if (type != RCTTypeFull && type != RCTTypeSimple && type != RCTTypeBulletproof && type != RCTTypeBulletproof2 && type != RCTTypeCLSAG) if (type != RCTTypeFull && type != RCTTypeSimple && type != RCTTypeBulletproof && type != RCTTypeBulletproof2 && type != RCTTypeCLSAG)
return false; return false;
VARINT_FIELD(txnFee) VARINT_FIELD(txnFee)
@ -344,7 +344,7 @@ namespace rct {
ar.delimit_array(); ar.delimit_array();
} }
ar.end_array(); ar.end_array();
return ar.stream().good(); return ar.good();
} }
BEGIN_SERIALIZE_OBJECT() BEGIN_SERIALIZE_OBJECT()
@ -375,7 +375,7 @@ namespace rct {
if (mixin >= 0xffffffff) if (mixin >= 0xffffffff)
return false; return false;
if (type == RCTTypeNull) if (type == RCTTypeNull)
return ar.stream().good(); return ar.good();
if (type != RCTTypeFull && type != RCTTypeSimple && type != RCTTypeBulletproof && type != RCTTypeBulletproof2 && type != RCTTypeCLSAG) if (type != RCTTypeFull && type != RCTTypeSimple && type != RCTTypeBulletproof && type != RCTTypeBulletproof2 && type != RCTTypeCLSAG)
return false; return false;
if (type == RCTTypeBulletproof || type == RCTTypeBulletproof2 || type == RCTTypeCLSAG) if (type == RCTTypeBulletproof || type == RCTTypeBulletproof2 || type == RCTTypeCLSAG)
@ -522,7 +522,7 @@ namespace rct {
} }
ar.end_array(); ar.end_array();
} }
return ar.stream().good(); return ar.good();
} }
BEGIN_SERIALIZE_OBJECT() BEGIN_SERIALIZE_OBJECT()

View file

@ -293,12 +293,13 @@ namespace cryptonote
MINFO("loading rpc payments data from " << state_file_path); MINFO("loading rpc payments data from " << state_file_path);
std::ifstream data; std::ifstream data;
data.open(state_file_path, std::ios_base::binary | std::ios_base::in); data.open(state_file_path, std::ios_base::binary | std::ios_base::in);
std::string bytes(std::istream_iterator<char>{data}, std::istream_iterator<char>{});
if (!data.fail()) if (!data.fail())
{ {
bool loaded = false; bool loaded = false;
try try
{ {
binary_archive<false> ar(data); binary_archive<false> ar{epee::strspan<std::uint8_t>(bytes)};
if (::serialization::serialize(ar, *this)) if (::serialization::serialize(ar, *this))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;
@ -306,6 +307,8 @@ namespace cryptonote
catch (...) {} catch (...) {}
if (!loaded) if (!loaded)
{ {
bytes.clear();
bytes.shrink_to_fit();
try try
{ {
boost::archive::portable_binary_iarchive a(data); boost::archive::portable_binary_iarchive a(data);

View file

@ -36,9 +36,11 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <boost/endian/conversion.hpp>
#include <boost/type_traits/make_unsigned.hpp> #include <boost/type_traits/make_unsigned.hpp>
#include "common/varint.h" #include "common/varint.h"
#include "span.h"
#include "warnings.h" #include "warnings.h"
/* I have no clue what these lines means */ /* I have no clue what these lines means */
@ -55,16 +57,15 @@ DISABLE_VS_WARNINGS(4244)
* purpse is to define the functions used for the binary_archive. Its * purpse is to define the functions used for the binary_archive. Its
* a header, basically. I think it was declared simply to save typing... * a header, basically. I think it was declared simply to save typing...
*/ */
template <class Stream, bool IsSaving> template <bool IsSaving>
struct binary_archive_base struct binary_archive_base
{ {
typedef Stream stream_type; typedef binary_archive_base<IsSaving> base_type;
typedef binary_archive_base<Stream, IsSaving> base_type;
typedef boost::mpl::bool_<IsSaving> is_saving; typedef boost::mpl::bool_<IsSaving> is_saving;
typedef uint8_t variant_tag_type; typedef uint8_t variant_tag_type;
explicit binary_archive_base(stream_type &s) : stream_(s) { } explicit binary_archive_base() { }
/* definition of standard API functions */ /* definition of standard API functions */
void tag(const char *) { } void tag(const char *) { }
@ -72,12 +73,6 @@ struct binary_archive_base
void end_object() { } void end_object() { }
void begin_variant() { } void begin_variant() { }
void end_variant() { } void end_variant() { }
/* I just want to leave a comment saying how this line really shows
flaws in the ownership model of many OOP languages, that is all. */
stream_type &stream() { return stream_; }
protected:
stream_type &stream_;
}; };
/* \struct binary_archive /* \struct binary_archive
@ -95,15 +90,18 @@ struct binary_archive;
template <> template <>
struct binary_archive<false> : public binary_archive_base<std::istream, false> struct binary_archive<false> : public binary_archive_base<false>
{ {
explicit binary_archive(epee::span<const std::uint8_t> s)
: base_type(), bytes_(s), begin_(s.begin()), good_(true), varint_bug_backward_compatibility_(false)
{}
explicit binary_archive(stream_type &s) : base_type(s), varint_bug_backward_compatibility_(false) { bool good() const noexcept { return good_; }
stream_type::pos_type pos = stream_.tellg(); void set_fail() noexcept { good_ = false; }
stream_.seekg(0, std::ios_base::end);
eof_pos_ = stream_.tellg(); //! If implementing as `std::istream`, reset stream error state after `peek()` call.
stream_.seekg(pos); bool eof() const noexcept { return bytes_.empty(); }
} std::size_t getpos() const noexcept { return bytes_.begin() - begin_; }
template <class T> template <class T>
void serialize_int(T &v) void serialize_int(T &v)
@ -116,24 +114,24 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false>
* \brief serializes an unsigned integer * \brief serializes an unsigned integer
*/ */
template <class T> template <class T>
void serialize_uint(T &v, size_t width = sizeof(T)) void serialize_uint(T &v)
{ {
T ret = 0; const std::size_t actual = bytes_.remove_prefix(sizeof(T));
unsigned shift = 0; good_ &= (actual == sizeof(T));
for (size_t i = 0; i < width; i++) { if (actual == sizeof(T))
//std::cerr << "tell: " << stream_.tellg() << " value: " << ret << std::endl; {
char c; std::memcpy(std::addressof(v), bytes_.data() - sizeof(T), sizeof(T));
stream_.get(c); boost::endian::little_to_native_inplace(v); // epee isn't templated
T b = (unsigned char)c;
ret += (b << shift); // can this be changed to OR, i think it can.
shift += 8;
} }
v = ret; else
v = 0; // ensures initialization
} }
void serialize_blob(void *buf, size_t len, const char *delimiter="") void serialize_blob(void *buf, size_t len, const char *delimiter="")
{ {
stream_.read((char *)buf, len); const std::size_t actual = bytes_.remove_prefix(len);
good_ &= (len == actual);
std::memcpy(buf, bytes_.data() - actual, actual);
} }
template <class T> template <class T>
@ -145,9 +143,11 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false>
template <class T> template <class T>
void serialize_uvarint(T &v) void serialize_uvarint(T &v)
{ {
typedef std::istreambuf_iterator<char> it; auto current = bytes_.cbegin();
if (tools::read_varint(it(stream_), it(), v) < 0) auto end = bytes_.cend();
stream_.setstate(std::ios_base::failbit); good_ &= (0 <= tools::read_varint(current, end, v));
current = std::min(current, bytes_.cend());
bytes_ = {current, std::size_t(bytes_.cend() - current)};
} }
void begin_array(size_t &s) void begin_array(size_t &s)
@ -166,26 +166,26 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false>
serialize_int(t); serialize_int(t);
} }
size_t remaining_bytes() { size_t remaining_bytes() const noexcept { return good() ? bytes_.size() : 0; }
if (!stream_.good())
return 0;
//std::cerr << "tell: " << stream_.tellg() << std::endl;
assert(stream_.tellg() <= eof_pos_);
return eof_pos_ - stream_.tellg();
}
void enable_varint_bug_backward_compatibility() { varint_bug_backward_compatibility_ = true; } void enable_varint_bug_backward_compatibility() { varint_bug_backward_compatibility_ = true; }
bool varint_bug_backward_compatibility_enabled() const { return varint_bug_backward_compatibility_; } bool varint_bug_backward_compatibility_enabled() const { return varint_bug_backward_compatibility_; }
protected: protected:
std::streamoff eof_pos_; epee::span<const std::uint8_t> bytes_;
std::uint8_t const* const begin_;
bool good_;
bool varint_bug_backward_compatibility_; bool varint_bug_backward_compatibility_;
}; };
template <> template <>
struct binary_archive<true> : public binary_archive_base<std::ostream, true> struct binary_archive<true> : public binary_archive_base<true>
{ {
explicit binary_archive(stream_type &s) : base_type(s) { } typedef std::ostream stream_type;
explicit binary_archive(stream_type &s) : base_type(), stream_(s) { }
bool good() const { return stream_.good(); }
void set_fail() { stream_.setstate(std::ios::failbit); }
std::streampos getpos() const { return stream_.tellp(); }
template <class T> template <class T>
void serialize_int(T v) void serialize_int(T v)
@ -234,6 +234,8 @@ struct binary_archive<true> : public binary_archive_base<std::ostream, true>
} }
bool varint_bug_backward_compatibility_enabled() const { return false; } bool varint_bug_backward_compatibility_enabled() const { return false; }
protected:
stream_type& stream_;
}; };
POP_WARNINGS POP_WARNINGS

View file

@ -39,8 +39,7 @@ namespace serialization {
template <class T> template <class T>
bool parse_binary(const std::string &blob, T &v) bool parse_binary(const std::string &blob, T &v)
{ {
std::istringstream istr(blob); binary_archive<false> iar{epee::strspan<std::uint8_t>(blob)};
binary_archive<false> iar(istr);
return ::serialization::serialize(iar, v); return ::serialization::serialize(iar, v);
} }

View file

@ -67,13 +67,13 @@ bool do_serialize_container(Archive<false> &ar, C &v)
{ {
size_t cnt; size_t cnt;
ar.begin_array(cnt); ar.begin_array(cnt);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
v.clear(); v.clear();
// very basic sanity check // very basic sanity check
if (ar.remaining_bytes() < cnt) { if (ar.remaining_bytes() < cnt) {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
@ -86,7 +86,7 @@ bool do_serialize_container(Archive<false> &ar, C &v)
if (!::serialization::detail::serialize_container_element(ar, e)) if (!::serialization::detail::serialize_container_element(ar, e))
return false; return false;
::serialization::detail::do_add(v, std::move(e)); ::serialization::detail::do_add(v, std::move(e));
if (!ar.stream().good()) if (!ar.good())
return false; return false;
} }
ar.end_array(); ar.end_array();
@ -100,13 +100,13 @@ bool do_serialize_container(Archive<true> &ar, C &v)
ar.begin_array(cnt); ar.begin_array(cnt);
for (auto i = v.begin(); i != v.end(); ++i) for (auto i = v.begin(); i != v.end(); ++i)
{ {
if (!ar.stream().good()) if (!ar.good())
return false; return false;
if (i != v.begin()) if (i != v.begin())
ar.delimit_array(); ar.delimit_array();
if(!::serialization::detail::serialize_container_element(ar, (typename C::value_type&)*i)) if(!::serialization::detail::serialize_container_element(ar, (typename C::value_type&)*i))
return false; return false;
if (!ar.stream().good()) if (!ar.good())
return false; return false;
} }
ar.end_array(); ar.end_array();

View file

@ -47,7 +47,7 @@ bool do_serialize(Archive<false> &ar, std::vector<crypto::signature> &v)
// very basic sanity check // very basic sanity check
if (ar.remaining_bytes() < cnt*sizeof(crypto::signature)) { if (ar.remaining_bytes() < cnt*sizeof(crypto::signature)) {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
@ -55,7 +55,7 @@ bool do_serialize(Archive<false> &ar, std::vector<crypto::signature> &v)
for (size_t i = 0; i < cnt; i++) { for (size_t i = 0; i < cnt; i++) {
v.resize(i+1); v.resize(i+1);
ar.serialize_blob(&(v[i]), sizeof(crypto::signature), ""); ar.serialize_blob(&(v[i]), sizeof(crypto::signature), "");
if (!ar.stream().good()) if (!ar.good())
return false; return false;
} }
return true; return true;
@ -70,7 +70,7 @@ bool do_serialize(Archive<true> &ar, std::vector<crypto::signature> &v)
size_t cnt = v.size(); size_t cnt = v.size();
for (size_t i = 0; i < cnt; i++) { for (size_t i = 0; i < cnt; i++) {
ar.serialize_blob(&(v[i]), sizeof(crypto::signature), ""); ar.serialize_blob(&(v[i]), sizeof(crypto::signature), "");
if (!ar.stream().good()) if (!ar.good())
return false; return false;
} }
ar.end_string(); ar.end_string();

View file

@ -38,6 +38,7 @@ struct debug_archive : public json_archive<W> {
typedef typename json_archive<W>::stream_type stream_type; typedef typename json_archive<W>::stream_type stream_type;
debug_archive(stream_type &s) : json_archive<W>(s) { } debug_archive(stream_type &s) : json_archive<W>(s) { }
stream_type& stream() { return this->stream_; }
}; };
template <class T> template <class T>

View file

@ -38,10 +38,10 @@ inline bool do_serialize(Archive<false>& ar, cryptonote::difficulty_type &diff)
{ {
uint64_t hi, lo; uint64_t hi, lo;
ar.serialize_varint(hi); ar.serialize_varint(hi);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
ar.serialize_varint(lo); ar.serialize_varint(lo);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
diff = hi; diff = hi;
diff <<= 64; diff <<= 64;
@ -52,13 +52,13 @@ inline bool do_serialize(Archive<false>& ar, cryptonote::difficulty_type &diff)
template <template <bool> class Archive> template <template <bool> class Archive>
inline bool do_serialize(Archive<true>& ar, cryptonote::difficulty_type &diff) inline bool do_serialize(Archive<true>& ar, cryptonote::difficulty_type &diff)
{ {
if (!ar.stream().good()) if (!ar.good())
return false; return false;
const uint64_t hi = ((diff >> 64) & 0xffffffffffffffff).convert_to<uint64_t>(); const uint64_t hi = ((diff >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
const uint64_t lo = (diff & 0xffffffffffffffff).convert_to<uint64_t>(); const uint64_t lo = (diff & 0xffffffffffffffff).convert_to<uint64_t>();
ar.serialize_varint(hi); ar.serialize_varint(hi);
ar.serialize_varint(lo); ar.serialize_varint(lo);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
return true; return true;
} }

View file

@ -58,6 +58,10 @@ struct json_archive_base
json_archive_base(stream_type &s, bool indent = false) json_archive_base(stream_type &s, bool indent = false)
: stream_(s), indent_(indent), object_begin(false), depth_(0) { } : stream_(s), indent_(indent), object_begin(false), depth_(0) { }
bool good() const { return stream_.good(); }
void set_fail() { stream_.setstate(std::ios::failbit); }
void clear_fail() { stream_.clear(); }
void tag(const char *tag) { void tag(const char *tag) {
if (!object_begin) if (!object_begin)
stream_ << ", "; stream_ << ", ";
@ -82,7 +86,6 @@ struct json_archive_base
void begin_variant() { begin_object(); } void begin_variant() { begin_object(); }
void end_variant() { end_object(); } void end_variant() { end_object(); }
Stream &stream() { return stream_; }
bool varint_bug_backward_compatibility_enabled() const { return false; } bool varint_bug_backward_compatibility_enabled() const { return false; }
@ -117,6 +120,8 @@ struct json_archive<true> : public json_archive_base<std::ostream, true>
{ {
json_archive(stream_type &s, bool indent = false) : base_type(s, indent), inner_array_size_(0) { } json_archive(stream_type &s, bool indent = false) : base_type(s, indent), inner_array_size_(0) { }
std::streampos getpos() const { return stream_.tellp(); }
template<typename T> template<typename T>
static auto promote_to_printable_integer_type(T v) -> decltype(+v) static auto promote_to_printable_integer_type(T v) -> decltype(+v)
{ {

View file

@ -69,19 +69,19 @@ inline bool do_serialize(Archive<false>& ar, std::pair<F,S>& p)
{ {
size_t cnt; size_t cnt;
ar.begin_array(cnt); ar.begin_array(cnt);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
if (cnt != 2) if (cnt != 2)
return false; return false;
if (!::serialization::detail::serialize_pair_element(ar, p.first)) if (!::serialization::detail::serialize_pair_element(ar, p.first))
return false; return false;
if (!ar.stream().good()) if (!ar.good())
return false; return false;
ar.delimit_array(); ar.delimit_array();
if (!::serialization::detail::serialize_pair_element(ar, p.second)) if (!::serialization::detail::serialize_pair_element(ar, p.second))
return false; return false;
if (!ar.stream().good()) if (!ar.good())
return false; return false;
ar.end_array(); ar.end_array();
@ -92,16 +92,16 @@ template <template <bool> class Archive, class F, class S>
inline bool do_serialize(Archive<true>& ar, std::pair<F,S>& p) inline bool do_serialize(Archive<true>& ar, std::pair<F,S>& p)
{ {
ar.begin_array(2); ar.begin_array(2);
if (!ar.stream().good()) if (!ar.good())
return false; return false;
if(!::serialization::detail::serialize_pair_element(ar, p.first)) if(!::serialization::detail::serialize_pair_element(ar, p.first))
return false; return false;
if (!ar.stream().good()) if (!ar.good())
return false; return false;
ar.delimit_array(); ar.delimit_array();
if(!::serialization::detail::serialize_pair_element(ar, p.second)) if(!::serialization::detail::serialize_pair_element(ar, p.second))
return false; return false;
if (!ar.stream().good()) if (!ar.good())
return false; return false;
ar.end_array(); ar.end_array();
return true; return true;

View file

@ -213,7 +213,7 @@ inline bool do_serialize(Archive &ar, bool &v)
* \brief self-explanatory * \brief self-explanatory
*/ */
#define END_SERIALIZE() \ #define END_SERIALIZE() \
return ar.stream().good(); \ return ar.good(); \
} }
/*! \macro VALUE(f) /*! \macro VALUE(f)
@ -223,7 +223,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag(#f); \ ar.tag(#f); \
bool r = ::do_serialize(ar, f); \ bool r = ::do_serialize(ar, f); \
if (!r || !ar.stream().good()) return false; \ if (!r || !ar.good()) return false; \
} while(0); } while(0);
/*! \macro FIELD_N(t,f) /*! \macro FIELD_N(t,f)
@ -234,7 +234,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag(t); \ ar.tag(t); \
bool r = ::do_serialize(ar, f); \ bool r = ::do_serialize(ar, f); \
if (!r || !ar.stream().good()) return false; \ if (!r || !ar.good()) return false; \
} while(0); } while(0);
/*! \macro FIELD(f) /*! \macro FIELD(f)
@ -245,7 +245,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag(#f); \ ar.tag(#f); \
bool r = ::do_serialize(ar, f); \ bool r = ::do_serialize(ar, f); \
if (!r || !ar.stream().good()) return false; \ if (!r || !ar.good()) return false; \
} while(0); } while(0);
/*! \macro FIELDS(f) /*! \macro FIELDS(f)
@ -255,7 +255,7 @@ inline bool do_serialize(Archive &ar, bool &v)
#define FIELDS(f) \ #define FIELDS(f) \
do { \ do { \
bool r = ::do_serialize(ar, f); \ bool r = ::do_serialize(ar, f); \
if (!r || !ar.stream().good()) return false; \ if (!r || !ar.good()) return false; \
} while(0); } while(0);
/*! \macro VARINT_FIELD(f) /*! \macro VARINT_FIELD(f)
@ -265,7 +265,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag(#f); \ ar.tag(#f); \
ar.serialize_varint(f); \ ar.serialize_varint(f); \
if (!ar.stream().good()) return false; \ if (!ar.good()) return false; \
} while(0); } while(0);
/*! \macro VARINT_FIELD_N(t, f) /*! \macro VARINT_FIELD_N(t, f)
@ -276,7 +276,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag(t); \ ar.tag(t); \
ar.serialize_varint(f); \ ar.serialize_varint(f); \
if (!ar.stream().good()) return false; \ if (!ar.good()) return false; \
} while(0); } while(0);
/*! \macro MAGIC_FIELD(m) /*! \macro MAGIC_FIELD(m)
@ -286,7 +286,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag("magic"); \ ar.tag("magic"); \
ar.serialize_blob((void*)magic.data(), magic.size()); \ ar.serialize_blob((void*)magic.data(), magic.size()); \
if (!ar.stream().good()) return false; \ if (!ar.good()) return false; \
if (magic != m) return false; \ if (magic != m) return false; \
} while(0); } while(0);
@ -297,7 +297,7 @@ inline bool do_serialize(Archive &ar, bool &v)
do { \ do { \
ar.tag("version"); \ ar.tag("version"); \
ar.serialize_varint(version); \ ar.serialize_varint(version); \
if (!ar.stream().good()) return false; \ if (!ar.good()) return false; \
} while(0); } while(0);
@ -339,10 +339,10 @@ namespace serialization {
* *
* \brief self explanatory * \brief self explanatory
*/ */
template<class Stream> template<class Archive>
bool do_check_stream_state(Stream& s, boost::mpl::bool_<true>, bool noeof) bool do_check_stream_state(Archive& ar, boost::mpl::bool_<true>, bool noeof)
{ {
return s.good(); return ar.good();
} }
/*! \fn do_check_stream_state /*! \fn do_check_stream_state
* *
@ -350,15 +350,13 @@ namespace serialization {
* *
* \detailed Also checks to make sure that the stream is not at EOF * \detailed Also checks to make sure that the stream is not at EOF
*/ */
template<class Stream> template<class Archive>
bool do_check_stream_state(Stream& s, boost::mpl::bool_<false>, bool noeof) bool do_check_stream_state(Archive& ar, boost::mpl::bool_<false>, bool noeof)
{ {
bool result = false; bool result = false;
if (s.good()) if (ar.good())
{ {
std::ios_base::iostate state = s.rdstate(); result = noeof || ar.eof();
result = noeof || EOF == s.peek();
s.clear(state);
} }
return result; return result;
} }
@ -371,7 +369,7 @@ namespace serialization {
template<class Archive> template<class Archive>
bool check_stream_state(Archive& ar, bool noeof = false) bool check_stream_state(Archive& ar, bool noeof = false)
{ {
return detail::do_check_stream_state(ar.stream(), typename Archive::is_saving(), noeof); return detail::do_check_stream_state(ar, typename Archive::is_saving(), noeof);
} }
/*! \fn serialize /*! \fn serialize

View file

@ -39,7 +39,7 @@ inline bool do_serialize(Archive<false>& ar, std::string& str)
ar.serialize_varint(size); ar.serialize_varint(size);
if (ar.remaining_bytes() < size) if (ar.remaining_bytes() < size)
{ {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }

View file

@ -74,7 +74,7 @@ struct variant_reader
current_type x; current_type x;
if(!::do_serialize(ar, x)) if(!::do_serialize(ar, x))
{ {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
v = x; v = x;
@ -95,7 +95,7 @@ struct variant_reader<Archive, Variant, TBegin, TBegin>
static inline bool read(Archive &ar, Variant &v, variant_tag_type t) static inline bool read(Archive &ar, Variant &v, variant_tag_type t)
{ {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
}; };
@ -116,7 +116,7 @@ struct serializer<Archive<false>, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
typename boost::mpl::begin<types>::type, typename boost::mpl::begin<types>::type,
typename boost::mpl::end<types>::type>::read(ar, v, t)) typename boost::mpl::end<types>::type>::read(ar, v, t))
{ {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
ar.end_variant(); ar.end_variant();
@ -143,7 +143,7 @@ struct serializer<Archive<true>, boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>>
ar.write_variant_tag(variant_serialization_traits<Archive<true>, T>::get_tag()); ar.write_variant_tag(variant_serialization_traits<Archive<true>, T>::get_tag());
if(!::do_serialize(ar, rv)) if(!::do_serialize(ar, rv))
{ {
ar.stream().setstate(std::ios::failbit); ar.set_fail();
return false; return false;
} }
ar.end_variant(); ar.end_variant();

View file

@ -193,9 +193,7 @@ void message_store::unpack_signer_config(const multisig_wallet_state &state, con
{ {
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(signer_config)};
iss << signer_config;
binary_archive<false> ar(iss);
THROW_WALLET_EXCEPTION_IF(!::serialization::serialize(ar, signers), tools::error::wallet_internal_error, "Failed to serialize signer config"); THROW_WALLET_EXCEPTION_IF(!::serialization::serialize(ar, signers), tools::error::wallet_internal_error, "Failed to serialize signer config");
} }
catch (...) catch (...)
@ -383,9 +381,7 @@ void message_store::process_auto_config_data_message(uint32_t id)
auto_config_data data; auto_config_data data;
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(m.content)};
iss << m.content;
binary_archive<false> ar(iss);
THROW_WALLET_EXCEPTION_IF(!::serialization::serialize(ar, data), tools::error::wallet_internal_error, "Failed to serialize auto config data"); THROW_WALLET_EXCEPTION_IF(!::serialization::serialize(ar, data), tools::error::wallet_internal_error, "Failed to serialize auto config data");
} }
catch (...) catch (...)
@ -790,9 +786,7 @@ void message_store::read_from_file(const multisig_wallet_state &state, const std
file_data read_file_data; file_data read_file_data;
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(buf)};
iss << buf;
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, read_file_data)) if (::serialization::serialize(ar, read_file_data))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;
@ -829,9 +823,7 @@ void message_store::read_from_file(const multisig_wallet_state &state, const std
loaded = false; loaded = false;
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(decrypted_data)};
iss << decrypted_data;
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, *this)) if (::serialization::serialize(ar, *this))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;

View file

@ -5701,17 +5701,13 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(cache_data)};
iss << cache_data;
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, *this)) if (::serialization::serialize(ar, *this))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;
if (!loaded) if (!loaded)
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(cache_data)};
iss << cache_data;
binary_archive<false> ar(iss);
ar.enable_varint_bug_backward_compatibility(); ar.enable_varint_bug_backward_compatibility();
if (::serialization::serialize(ar, *this)) if (::serialization::serialize(ar, *this))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
@ -6786,8 +6782,7 @@ bool wallet2::parse_unsigned_tx_from_str(const std::string &unsigned_tx_st, unsi
catch(const std::exception &e) { LOG_PRINT_L0("Failed to decrypt unsigned tx: " << e.what()); return false; } catch(const std::exception &e) { LOG_PRINT_L0("Failed to decrypt unsigned tx: " << e.what()); return false; }
try try
{ {
std::istringstream iss(s); binary_archive<false> ar{epee::strspan<std::uint8_t>(s)};
binary_archive<false> ar(iss);
if (!::serialization::serialize(ar, exported_txs)) if (!::serialization::serialize(ar, exported_txs))
{ {
LOG_PRINT_L0("Failed to parse data from unsigned tx"); LOG_PRINT_L0("Failed to parse data from unsigned tx");
@ -7101,8 +7096,7 @@ bool wallet2::parse_tx_from_str(const std::string &signed_tx_st, std::vector<too
catch (const std::exception &e) { LOG_PRINT_L0("Failed to decrypt signed transaction: " << e.what()); return false; } catch (const std::exception &e) { LOG_PRINT_L0("Failed to decrypt signed transaction: " << e.what()); return false; }
try try
{ {
std::istringstream iss(s); binary_archive<false> ar{epee::strspan<std::uint8_t>(s)};
binary_archive<false> ar(iss);
if (!::serialization::serialize(ar, signed_txs)) if (!::serialization::serialize(ar, signed_txs))
{ {
LOG_PRINT_L0("Failed to deserialize signed transaction"); LOG_PRINT_L0("Failed to deserialize signed transaction");
@ -7237,8 +7231,7 @@ bool wallet2::parse_multisig_tx_from_str(std::string multisig_tx_st, multisig_tx
bool loaded = false; bool loaded = false;
try try
{ {
std::istringstream iss(multisig_tx_st); binary_archive<false> ar{epee::strspan<std::uint8_t>(multisig_tx_st)};
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, exported_txs)) if (::serialization::serialize(ar, exported_txs))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;
@ -12037,8 +12030,7 @@ bool wallet2::check_reserve_proof(const cryptonote::account_public_address &addr
serializable_unordered_map<crypto::public_key, crypto::signature> subaddr_spendkeys; serializable_unordered_map<crypto::public_key, crypto::signature> subaddr_spendkeys;
try try
{ {
std::istringstream iss(sig_decoded); binary_archive<false> ar{epee::strspan<std::uint8_t>(sig_decoded)};
binary_archive<false> ar(iss);
if (::serialization::serialize_noeof(ar, proofs)) if (::serialization::serialize_noeof(ar, proofs))
if (::serialization::serialize_noeof(ar, subaddr_spendkeys)) if (::serialization::serialize_noeof(ar, subaddr_spendkeys))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
@ -13222,9 +13214,7 @@ size_t wallet2::import_outputs_from_str(const std::string &outputs_st)
std::pair<uint64_t, std::vector<tools::wallet2::transfer_details>> outputs; std::pair<uint64_t, std::vector<tools::wallet2::transfer_details>> outputs;
try try
{ {
std::stringstream iss; binary_archive<false> ar{epee::strspan<std::uint8_t>(body)};
iss << body;
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, outputs)) if (::serialization::serialize(ar, outputs))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;
@ -13476,8 +13466,7 @@ size_t wallet2::import_multisig(std::vector<cryptonote::blobdata> blobs)
bool loaded = false; bool loaded = false;
try try
{ {
std::istringstream iss(body); binary_archive<false> ar{epee::strspan<std::uint8_t>(body)};
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, i)) if (::serialization::serialize(ar, i))
if (::serialization::check_stream_state(ar)) if (::serialization::check_stream_state(ar))
loaded = true; loaded = true;

View file

@ -1639,8 +1639,7 @@ namespace tools
try try
{ {
std::istringstream iss(blob); binary_archive<false> ar{epee::strspan<std::uint8_t>(blob)};
binary_archive<false> ar(iss);
if (::serialization::serialize(ar, ptx)) if (::serialization::serialize(ar, ptx))
loaded = true; loaded = true;
} }

View file

@ -648,11 +648,9 @@ public:
bvc.m_verifivation_failed = true; bvc.m_verifivation_failed = true;
cryptonote::block blk; cryptonote::block blk;
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(sr_block.data)};
ss << sr_block.data;
binary_archive<false> ba(ss);
::serialization::serialize(ba, blk); ::serialization::serialize(ba, blk);
if (!ss.good()) if (!ba.good())
{ {
blk = cryptonote::block(); blk = cryptonote::block();
} }
@ -671,11 +669,9 @@ public:
bool tx_added = pool_size + 1 == m_c.get_pool_transactions_count(); bool tx_added = pool_size + 1 == m_c.get_pool_transactions_count();
cryptonote::transaction tx; cryptonote::transaction tx;
std::stringstream ss; binary_archive<false> ba{epee::strspan<std::uint8_t>(sr_tx.data)};
ss << sr_tx.data;
binary_archive<false> ba(ss);
::serialization::serialize(ba, tx); ::serialization::serialize(ba, tx);
if (!ss.good()) if (!ba.good())
{ {
tx = cryptonote::transaction(); tx = cryptonote::transaction();
} }

View file

@ -37,9 +37,7 @@ BEGIN_INIT_SIMPLE_FUZZER()
END_INIT_SIMPLE_FUZZER() END_INIT_SIMPLE_FUZZER()
BEGIN_SIMPLE_FUZZER() BEGIN_SIMPLE_FUZZER()
std::stringstream ss; binary_archive<false> ba{{buf, len}};
ss << std::string((const char*)buf, len);
binary_archive<false> ba(ss);
rct::Bulletproof proof = AUTO_VAL_INIT(proof); rct::Bulletproof proof = AUTO_VAL_INIT(proof);
::serialization::serialize(ba, proof); ::serialization::serialize(ba, proof);
END_SIMPLE_FUZZER() END_SIMPLE_FUZZER()

View file

@ -50,11 +50,8 @@ BEGIN_INIT_SIMPLE_FUZZER()
END_INIT_SIMPLE_FUZZER() END_INIT_SIMPLE_FUZZER()
BEGIN_SIMPLE_FUZZER() BEGIN_SIMPLE_FUZZER()
std::string s((const char*)buf, len);
std::pair<uint64_t, std::vector<tools::wallet2::transfer_details>> outputs; std::pair<uint64_t, std::vector<tools::wallet2::transfer_details>> outputs;
std::stringstream iss; binary_archive<false> ar{{buf, len}};
iss << s;
binary_archive<false> ar(iss);
::serialization::serialize(ar, outputs); ::serialization::serialize(ar, outputs);
size_t n_outputs = wallet->import_outputs(outputs); size_t n_outputs = wallet->import_outputs(outputs);
std::cout << boost::lexical_cast<std::string>(n_outputs) << " outputs imported" << std::endl; std::cout << boost::lexical_cast<std::string>(n_outputs) << " outputs imported" << std::endl;

View file

@ -50,11 +50,8 @@ BEGIN_INIT_SIMPLE_FUZZER()
END_INIT_SIMPLE_FUZZER() END_INIT_SIMPLE_FUZZER()
BEGIN_SIMPLE_FUZZER() BEGIN_SIMPLE_FUZZER()
std::string s((const char*)buf, len);
tools::wallet2::unsigned_tx_set exported_txs; tools::wallet2::unsigned_tx_set exported_txs;
std::stringstream iss; binary_archive<false> ar{{buf, len}};
iss << s;
binary_archive<false> ar(iss);
::serialization::serialize(ar, exported_txs); ::serialization::serialize(ar, exported_txs);
std::vector<tools::wallet2::pending_tx> ptx; std::vector<tools::wallet2::pending_tx> ptx;
bool success = wallet->sign_tx(exported_txs, "/tmp/cold-transaction-test-signed", ptx); bool success = wallet->sign_tx(exported_txs, "/tmp/cold-transaction-test-signed", ptx);

View file

@ -132,11 +132,10 @@ TEST(Serialization, BinaryArchiveInts) {
ASSERT_EQ(8, oss.str().size()); ASSERT_EQ(8, oss.str().size());
ASSERT_EQ(string("\0\0\0\0\xff\0\0\0", 8), oss.str()); ASSERT_EQ(string("\0\0\0\0\xff\0\0\0", 8), oss.str());
istringstream iss(oss.str()); binary_archive<false> iar{epee::strspan<std::uint8_t>(oss.str())};
binary_archive<false> iar(iss);
iar.serialize_int(x1); iar.serialize_int(x1);
ASSERT_EQ(8, iss.tellg()); ASSERT_EQ(8, iar.getpos());
ASSERT_TRUE(iss.good()); ASSERT_TRUE(iar.good());
ASSERT_EQ(x, x1); ASSERT_EQ(x, x1);
} }
@ -151,10 +150,9 @@ TEST(Serialization, BinaryArchiveVarInts) {
ASSERT_EQ(6, oss.str().size()); ASSERT_EQ(6, oss.str().size());
ASSERT_EQ(string("\x80\x80\x80\x80\xF0\x1F", 6), oss.str()); ASSERT_EQ(string("\x80\x80\x80\x80\xF0\x1F", 6), oss.str());
istringstream iss(oss.str()); binary_archive<false> iar{epee::strspan<std::uint8_t>(oss.str())};
binary_archive<false> iar(iss);
iar.serialize_varint(x1); iar.serialize_varint(x1);
ASSERT_TRUE(iss.good()); ASSERT_TRUE(iar.good());
ASSERT_EQ(x, x1); ASSERT_EQ(x, x1);
} }

View file

@ -287,3 +287,73 @@ TEST(sort_tx_extra, invalid_suffix_partial)
std::vector<uint8_t> expected(&expected_arr[0], &expected_arr[0] + sizeof(expected_arr)); std::vector<uint8_t> expected(&expected_arr[0], &expected_arr[0] + sizeof(expected_arr));
ASSERT_EQ(sorted, expected); ASSERT_EQ(sorted, expected);
} }
TEST(remove_field_from_tx_extra, remove_first)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(2, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
tx_extra_fields.clear();
ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_pub_key)));
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(1, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[0].type());
}
TEST(remove_field_from_tx_extra, remove_last)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(2, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
tx_extra_fields.clear();
ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(1, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
}
TEST(remove_field_from_tx_extra, remove_middle)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 1, 42, 1, 30, 208, 98, 162, 133, 64, 85, 83, 112,
91, 188, 89, 211, 24, 131, 39, 154, 22, 228, 80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
std::vector<cryptonote::tx_extra_field> tx_extra_fields;
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(3, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
ASSERT_EQ(typeid(cryptonote::tx_extra_nonce), tx_extra_fields[1].type());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[2].type());
tx_extra_fields.clear();
ASSERT_TRUE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields));
ASSERT_EQ(2, tx_extra_fields.size());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
ASSERT_EQ(typeid(cryptonote::tx_extra_pub_key), tx_extra_fields[0].type());
}
TEST(remove_field_from_tx_extra, invalid_varint)
{
const uint8_t extra_arr[] = {1, 30, 208, 98, 162, 133, 64, 85, 83, 112, 91, 188, 89, 211, 24, 131, 39, 154, 22, 228,
80, 63, 198, 141, 173, 111, 244, 183, 4, 149, 186, 140, 230, 2, 0x80, 0};
std::vector<uint8_t> extra(&extra_arr[0], &extra_arr[0] + sizeof(extra_arr));
ASSERT_FALSE(cryptonote::remove_field_from_tx_extra(extra, typeid(cryptonote::tx_extra_nonce)));
ASSERT_EQ(sizeof(extra_arr), extra.size());
}