aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Blair <snipa@jagtech.io>2020-08-16 12:43:38 -0700
committerAlexander Blair <snipa@jagtech.io>2020-08-16 12:43:38 -0700
commita08df6eb1beaf442ef6d9b27c1f47497b74d3471 (patch)
treeef0f0ac25c2932f26cdd0a389a4ad44ec2a97826
parentMerge pull request #6601 (diff)
parentChange ZMQ-JSON txextra to hex and remove unnecessary base fields (diff)
downloadmonero-a08df6eb1beaf442ef6d9b27c1f47497b74d3471.tar.xz
Merge pull request #6603
4e2377995 Change ZMQ-JSON txextra to hex and remove unnecessary base fields (Lee Clagett)
-rw-r--r--src/rpc/daemon_rpc_version.h2
-rw-r--r--src/rpc/message.cpp4
-rw-r--r--src/serialization/json_object.cpp20
-rw-r--r--src/serialization/json_object.h11
-rw-r--r--tests/unit_tests/json_serialization.cpp17
5 files changed, 48 insertions, 6 deletions
diff --git a/src/rpc/daemon_rpc_version.h b/src/rpc/daemon_rpc_version.h
index d178a82bc..2955d5449 100644
--- a/src/rpc/daemon_rpc_version.h
+++ b/src/rpc/daemon_rpc_version.h
@@ -35,7 +35,7 @@ namespace rpc
{
static const uint32_t DAEMON_RPC_VERSION_ZMQ_MINOR = 0;
-static const uint32_t DAEMON_RPC_VERSION_ZMQ_MAJOR = 1;
+static const uint32_t DAEMON_RPC_VERSION_ZMQ_MAJOR = 2;
static const uint32_t DAEMON_RPC_VERSION_ZMQ = DAEMON_RPC_VERSION_ZMQ_MINOR + (DAEMON_RPC_VERSION_ZMQ_MAJOR << 16);
diff --git a/src/rpc/message.cpp b/src/rpc/message.cpp
index 3aefbd76e..f6c6b887d 100644
--- a/src/rpc/message.cpp
+++ b/src/rpc/message.cpp
@@ -65,8 +65,6 @@ const rapidjson::Value& get_method_field(const rapidjson::Value& src)
void Message::toJson(rapidjson::Writer<epee::byte_stream>& dest) const
{
dest.StartObject();
- INSERT_INTO_JSON_OBJECT(dest, status, status);
- INSERT_INTO_JSON_OBJECT(dest, error_details, error_details);
INSERT_INTO_JSON_OBJECT(dest, rpc_version, DAEMON_RPC_VERSION_ZMQ);
doToJson(dest);
dest.EndObject();
@@ -74,8 +72,6 @@ void Message::toJson(rapidjson::Writer<epee::byte_stream>& dest) const
void Message::fromJson(const rapidjson::Value& val)
{
- GET_FROM_JSON_OBJECT(val, status, status);
- GET_FROM_JSON_OBJECT(val, error_details, error_details);
GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version);
}
diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp
index 7cb9c2ed4..7c48cf6c3 100644
--- a/src/serialization/json_object.cpp
+++ b/src/serialization/json_object.cpp
@@ -148,6 +148,26 @@ void fromJsonValue(const rapidjson::Value& val, std::string& str)
str = val.GetString();
}
+void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>& src)
+{
+ const std::string hex = epee::to_hex::string(epee::to_span(src));
+ dest.String(hex.data(), hex.size());
+}
+
+void fromJsonValue(const rapidjson::Value& val, std::vector<std::uint8_t>& dest)
+{
+ if (!val.IsString())
+ {
+ throw WRONG_TYPE("string");
+ }
+
+ dest.resize(val.GetStringLength() / 2);
+ if ((val.GetStringLength() % 2) != 0 || !epee::from_hex::to_buffer(epee::to_mut_span(dest), {val.GetString(), val.GetStringLength()}))
+ {
+ throw BAD_INPUT();
+ }
+}
+
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i)
{
dest.Bool(i);
diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h
index b5d0f1508..de14c8911 100644
--- a/src/serialization/json_object.h
+++ b/src/serialization/json_object.h
@@ -154,6 +154,9 @@ inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::s
}
void fromJsonValue(const rapidjson::Value& val, std::string& str);
+void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>&);
+void fromJsonValue(const rapidjson::Value& src, std::vector<std::uint8_t>& i);
+
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i);
void fromJsonValue(const rapidjson::Value& val, bool& b);
@@ -357,6 +360,10 @@ inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type from
template <typename Vec>
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec)
{
+ using value_type = typename Vec::value_type;
+ static_assert(!std::is_same<value_type, char>::value, "encoding an array of chars is faster as hex");
+ static_assert(!std::is_same<value_type, unsigned char>::value, "encoding an array of unsigned char is faster as hex");
+
dest.StartArray();
for (const auto& t : vec)
toJsonValue(dest, t);
@@ -379,6 +386,10 @@ namespace traits
template <typename Vec>
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec)
{
+ using value_type = typename Vec::value_type;
+ static_assert(!std::is_same<value_type, char>::value, "encoding a vector of chars is faster as hex");
+ static_assert(!std::is_same<value_type, unsigned char>::value, "encoding a vector of unsigned char is faster as hex");
+
if (!val.IsArray())
{
throw WRONG_TYPE("json array");
diff --git a/tests/unit_tests/json_serialization.cpp b/tests/unit_tests/json_serialization.cpp
index 1db923f7b..f76199e57 100644
--- a/tests/unit_tests/json_serialization.cpp
+++ b/tests/unit_tests/json_serialization.cpp
@@ -97,7 +97,7 @@ namespace
rapidjson::Document doc;
doc.Parse(reinterpret_cast<const char*>(buffer.data()), buffer.size());
- if (doc.HasParseError() || !doc.IsObject())
+ if (doc.HasParseError())
{
throw cryptonote::json::PARSE_FAIL();
}
@@ -108,6 +108,21 @@ namespace
}
} // anonymous
+TEST(JsonSerialization, VectorBytes)
+{
+ EXPECT_EQ(std::vector<std::uint8_t>{}, test_json(std::vector<std::uint8_t>{}));
+ EXPECT_EQ(std::vector<std::uint8_t>{0x00}, test_json(std::vector<std::uint8_t>{0x00}));
+}
+
+TEST(JsonSerialization, InvalidVectorBytes)
+{
+ rapidjson::Document doc;
+ doc.SetString("1");
+
+ std::vector<std::uint8_t> out;
+ EXPECT_THROW(cryptonote::json::fromJsonValue(doc, out), cryptonote::json::BAD_INPUT);
+}
+
TEST(JsonSerialization, MinerTransaction)
{
cryptonote::account_base acct;