From 0f78b06e8c578819f831a513490278a5f70b01af Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Thu, 7 Nov 2019 05:45:06 +0000 Subject: Various improvements to the ZMQ JSON-RPC handling: - Finding handling function in ZMQ JSON-RPC now uses binary search - Temporary `std::vector`s in JSON output now use `epee::span` to prevent allocations. - Binary -> hex in JSON output no longer allocates temporary buffer - C++ structs -> JSON skips intermediate DOM creation, and instead write directly to an output stream. --- tests/unit_tests/json_serialization.cpp | 47 +++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 20 deletions(-) (limited to 'tests/unit_tests/json_serialization.cpp') diff --git a/tests/unit_tests/json_serialization.cpp b/tests/unit_tests/json_serialization.cpp index 53d9f84c7..6f98d854d 100644 --- a/tests/unit_tests/json_serialization.cpp +++ b/tests/unit_tests/json_serialization.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include "crypto/hash.h" @@ -80,6 +82,27 @@ namespace return tx; } + + template + T test_json(const T& value) + { + rapidjson::StringBuffer buffer; + { + rapidjson::Writer dest{buffer}; + cryptonote::json::toJsonValue(dest, value); + } + + rapidjson::Document doc; + doc.Parse(buffer.GetString()); + if (doc.HasParseError() || !doc.IsObject()) + { + throw cryptonote::json::PARSE_FAIL(); + } + + T out{}; + cryptonote::json::fromJsonValue(doc, out); + return out; + } } // anonymous TEST(JsonSerialization, MinerTransaction) @@ -91,11 +114,7 @@ TEST(JsonSerialization, MinerTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, miner_tx, doc); - - cryptonote::transaction miner_tx_copy; - cryptonote::json::fromJsonValue(doc, miner_tx_copy); + cryptonote::transaction miner_tx_copy = test_json(miner_tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx_copy, tx_copy_hash)); @@ -126,11 +145,7 @@ TEST(JsonSerialization, RegularTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); @@ -161,11 +176,7 @@ TEST(JsonSerialization, RingctTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); @@ -196,11 +207,7 @@ TEST(JsonSerialization, BulletproofTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); -- cgit v1.2.3