merge updates from latest wasm branch, mostly serializing using

rapidjson instead of boost::property_tree
This commit is contained in:
woodser
2019-11-29 16:21:34 -05:00
parent 8a80775344
commit 2aef5a65f4
8 changed files with 1145 additions and 387 deletions
+144 -38
View File
@@ -54,6 +54,7 @@
#include "rpc/core_rpc_server_commands_defs.h"
#include "storages/portable_storage_template_helper.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "string_tools.h"
using namespace std;
using namespace cryptonote;
@@ -148,6 +149,7 @@ shared_ptr<monero_output_query> node_to_output_query(const boost::property_tree:
return m_output_query;
}
// TODO: finish deserialization implementation, uses tests
void node_to_tx(const boost::property_tree::ptree& node, shared_ptr<monero_tx> tx) {
// initialize tx from node
@@ -156,7 +158,7 @@ void node_to_tx(const boost::property_tree::ptree& node, shared_ptr<monero_tx> t
if (key == string("id")) tx->m_id = it->second.data();
else if (key == string("version")) throw runtime_error("version deserializationn not implemented");
else if (key == string("isMinerTx")) tx->m_is_miner_tx = it->second.get_value<bool>();
else if (key == string("paymentId")) throw runtime_error("paymentId deserializationn not implemented");
else if (key == string("paymentId")) tx->m_payment_id = it->second.data(); // TODO: this threw runtime_error, how is "Can get transactions by payment ids" test passing in Java?
else if (key == string("fee")) tx->m_fee = it->second.get_value<uint64_t>();
else if (key == string("mixin")) throw runtime_error("mixin deserialization not implemented");
else if (key == string("doNotRelay")) tx->m_do_not_relay = it->second.get_value<bool>();
@@ -252,7 +254,7 @@ shared_ptr<monero_destination> node_to_destination(const boost::property_tree::p
return destination;
}
// ------------------------- PUBLIC STATIC UTILITIES --------------------------
// -------------------------- BINARY SERIALIZATION ----------------------------
void monero_utils::json_to_binary(const std::string &json, std::string &bin) {
epee::serialization::portable_storage ps;
@@ -345,6 +347,93 @@ monero_tx_set monero_utils::deserialize_tx_set(const string& tx_set_str) {
return tx_set;
}
// ------------------------------- RAPIDJSON ----------------------------------
string monero_utils::serialize(const rapidjson::Document& doc) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}
void monero_utils::addJsonMember(string key, uint8_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field) {
rapidjson::Value field_key(key.c_str(), key.size(), allocator);
field.SetInt(val);
root.AddMember(field_key, field, allocator);
}
void monero_utils::addJsonMember(string key, uint32_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field) {
rapidjson::Value field_key(key.c_str(), key.size(), allocator);
field.SetUint64(val);
root.AddMember(field_key, field, allocator);
}
void monero_utils::addJsonMember(string key, uint64_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field) {
rapidjson::Value field_key(key.c_str(), key.size(), allocator);
field.SetUint64(val);
root.AddMember(field_key, field, allocator);
}
void monero_utils::addJsonMember(string key, string val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field) {
rapidjson::Value field_key(key.c_str(), key.size(), allocator);
field.SetString(val.c_str(), val.size(), allocator);
root.AddMember(field_key, field, allocator);
}
void monero_utils::addJsonMember(string key, bool val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root) {
rapidjson::Value field_key(key.c_str(), key.size(), allocator);
if (val) {
rapidjson::Value field_val(rapidjson::kTrueType);
root.AddMember(field_key, field_val, allocator);
} else {
rapidjson::Value field_val(rapidjson::kFalseType);
root.AddMember(field_key, field_val, allocator);
}
}
rapidjson::Value monero_utils::to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<string>& strs) {
rapidjson::Value value_arr(rapidjson::kArrayType);
rapidjson::Value value_str(rapidjson::kStringType);
for (const string& str : strs) {
value_str.SetString(str.c_str(), str.size(), allocator);
value_arr.PushBack(value_str, allocator);
}
return value_arr;
}
rapidjson::Value monero_utils::to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint8_t>& nums) {
rapidjson::Value value_arr(rapidjson::kArrayType);
rapidjson::Value value_num(rapidjson::kNumberType);
for (const auto& num : nums) {
value_num.SetInt(num);
value_arr.PushBack(value_num, allocator);
}
return value_arr;
}
// TODO: remove these redundant implementations for different sizes?
rapidjson::Value monero_utils::to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint32_t>& nums) {
rapidjson::Value value_arr(rapidjson::kArrayType);
rapidjson::Value value_num(rapidjson::kNumberType);
for (const auto& num : nums) {
value_num.SetUint64(num);
value_arr.PushBack(value_num, allocator);
}
return value_arr;
}
rapidjson::Value monero_utils::to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint64_t>& nums) {
rapidjson::Value value_arr(rapidjson::kArrayType);
rapidjson::Value value_num(rapidjson::kNumberType);
for (const auto& num : nums) {
value_num.SetUint64(num);
value_arr.PushBack(value_num, allocator);
}
return value_arr;
}
// -------------------------- MODEL DESERIALIZATION ---------------------------
shared_ptr<monero_tx_query> monero_utils::deserialize_tx_query(const string& tx_query_str) {
// deserialize tx query string to property rooted at block
@@ -473,6 +562,10 @@ vector<shared_ptr<monero_key_image>> monero_utils::deserialize_key_images(const
return key_images;
}
// ------------------------ PROPERTY TREES ---------------------------
// TODO: fully switch to rapidjson
string monero_utils::serialize(const boost::property_tree::ptree& node) {
std::stringstream ss;
boost::property_tree::write_json(ss, node, false);
@@ -480,47 +573,60 @@ string monero_utils::serialize(const boost::property_tree::ptree& node) {
return str.substr(0, str.size() - 1); // strip newline
}
boost::property_tree::ptree monero_utils::to_property_tree(const vector<string>& strs) {
boost::property_tree::ptree strsNode;
for (const auto& str : strs) {
boost::property_tree::ptree strNode;
strNode.put("", str);
strsNode.push_back(std::make_pair("", strNode));
// TODO: switch deserializes in this utils class to use this deserialize util
void monero_utils::deserialize(const string& json, boost::property_tree::ptree& root) {
std::istringstream iss = json.empty() ? std::istringstream() : std::istringstream(json);
try {
boost::property_tree::read_json(iss, root);
} catch (std::exception const& e) {
throw runtime_error("Invalid JSON");
}
return strsNode;
}
boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint8_t>& nums) {
boost::property_tree::ptree numsNode;
for (const auto& num : nums) {
boost::property_tree::ptree numNode;
numNode.put("", num);
numsNode.push_back(std::make_pair("", numNode));
}
return numsNode;
}
//boost::property_tree::ptree monero_utils::to_property_tree(const vector<string>& strs) {
// boost::property_tree::ptree strsNode;
// for (const auto& str : strs) {
// boost::property_tree::ptree strNode;
// strNode.put("", str);
// strsNode.push_back(std::make_pair("", strNode));
// }
// return strsNode;
//}
//
//boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint8_t>& nums) {
// boost::property_tree::ptree numsNode;
// for (const auto& num : nums) {
// boost::property_tree::ptree numNode;
// numNode.put("", num);
// numsNode.push_back(std::make_pair("", numNode));
// }
// return numsNode;
//}
//
//// TODO: remove these redundant implementations for different sizes?
//boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint32_t>& nums) {
// boost::property_tree::ptree numsNode;
// for (const auto& num : nums) {
// boost::property_tree::ptree numNode;
// numNode.put("", num);
// numsNode.push_back(std::make_pair("", numNode));
// }
// return numsNode;
//}
//
//boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint64_t>& nums) {
// boost::property_tree::ptree numsNode;
// for (const auto& num : nums) {
// boost::property_tree::ptree numNode;
// numNode.put("", num);
// numsNode.push_back(std::make_pair("", numNode));
// }
// return numsNode;
//}
// TODO: remove these redundant implementations for different sizes?
boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint32_t>& nums) {
boost::property_tree::ptree numsNode;
for (const auto& num : nums) {
boost::property_tree::ptree numNode;
numNode.put("", num);
numsNode.push_back(std::make_pair("", numNode));
}
return numsNode;
}
boost::property_tree::ptree monero_utils::to_property_tree(const vector<uint64_t>& nums) {
boost::property_tree::ptree numsNode;
for (const auto& num : nums) {
boost::property_tree::ptree numNode;
numNode.put("", num);
numsNode.push_back(std::make_pair("", numNode));
}
return numsNode;
}
// ----------------------------------------------------------------------------
// TODO: is this unused?
shared_ptr<monero_block> monero_utils::cn_block_to_block(const cryptonote::block& cn_block) {
cryptonote::block temp = cn_block;
cout << cryptonote::obj_to_json_str(temp) << endl;
+74 -34
View File
@@ -53,38 +53,103 @@
#ifndef monero_utils_h
#define monero_utils_h
#include "wallet/monero_wallet.h"
#include "wallet/monero_wallet_model.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include "serialization/keyvalue_serialization.h" // TODO: consolidate with other binary deps?
#include "storages/portable_storage.h"
/**
* Collection of utilities for working with Monero's binary portable storage format.
* Collection of utilities for the Monero library.
*/
namespace monero_utils
{
using namespace std;
using namespace cryptonote;
void dummy_method(const string& str); // TODO: remove this
// ------------------------ BINARY SERIALIZATION ----------------------------
void json_to_binary(const std::string &json, std::string &bin);
void binary_to_json(const std::string &bin, std::string &json);
void binary_blocks_to_json(const std::string &bin, std::string &json);
monero_tx_set deserialize_tx_set(const string& tx_set_str);
// ------------------------------ RAPIDJSON ---------------------------------
string serialize(const rapidjson::Document& doc);
void addJsonMember(string key, uint8_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
void addJsonMember(string key, uint32_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
void addJsonMember(string key, uint64_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
void addJsonMember(string key, string val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
void addJsonMember(string key, bool val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root);
// TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
template <class T> rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<shared_ptr<T>>& vals) {
rapidjson::Value value_arr(rapidjson::kArrayType);
for (const auto& val : vals) {
value_arr.PushBack(val->to_json_val(allocator), allocator);
}
return value_arr;
}
// TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
template <class T> rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<T>& vals) {
rapidjson::Value value_arr(rapidjson::kArrayType);
for (const auto& val : vals) {
value_arr.PushBack(val.to_json_val(allocator), allocator);
}
return value_arr;
}
rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<string>& strs);
rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint8_t>& nums);
rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint32_t>& nums);
rapidjson::Value to_json_val(rapidjson::Document::AllocatorType& allocator, const vector<uint64_t>& nums);
// ------------------------ MODEL DESERIALIZATION ---------------------------
// TODO: move deserialization to static utilities on class
shared_ptr<monero_tx_query> deserialize_tx_query(const string& txQueryStr);
shared_ptr<monero_transfer_query> deserialize_transfer_query(const string& transfer_query_str);
shared_ptr<monero_output_query> deserialize_output_query(const string& output_query_str);
shared_ptr<monero_send_request> deserialize_send_request(const string& send_request_str);
vector<shared_ptr<monero_key_image>> deserialize_key_images(const string& key_images_json);
monero_tx_set deserialize_tx_set(const string& tx_set_str);
// ------------------------ PROPERTY TREES ---------------------------
// TODO: fully switch from property trees to rapidjson
string serialize(const boost::property_tree::ptree& node);
void deserialize(const string& json, boost::property_tree::ptree& root);
//
// // TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
// template <class T> boost::property_tree::ptree to_property_tree(const vector<shared_ptr<T>>& types) {
// boost::property_tree::ptree typeNodes;
// for (const auto& type : types) {
// typeNodes.push_back(std::make_pair("", type->to_property_tree()));
// }
// return typeNodes;
// }
//
// // TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
// template <class T> boost::property_tree::ptree to_property_tree(const vector<T>& types) {
// boost::property_tree::ptree typeNodes;
// for (const auto& type : types) {
// typeNodes.push_back(std::make_pair("", type.to_property_tree()));
// }
// return typeNodes;
// }
//
// boost::property_tree::ptree to_property_tree(const vector<string>& strs);
// boost::property_tree::ptree to_property_tree(const vector<uint8_t>& nums);
// boost::property_tree::ptree to_property_tree(const vector<uint32_t>& nums);
// boost::property_tree::ptree to_property_tree(const vector<uint64_t>& nums);
// --------------------------------------------------------------------------
/**
* Convert a Monero Core cryptonote::block to a block in this library's native model.
@@ -116,30 +181,5 @@ namespace monero_utils
CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base");
return ss.str();
}
// ----------------------------- PROPERTY TREE ------------------------------
// TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
template <class T> boost::property_tree::ptree to_property_tree(const vector<shared_ptr<T>>& types) {
boost::property_tree::ptree typeNodes;
for (const auto& type : types) {
typeNodes.push_back(std::make_pair("", type->to_property_tree()));
}
return typeNodes;
}
// TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
template <class T> boost::property_tree::ptree to_property_tree(const vector<T>& types) {
boost::property_tree::ptree typeNodes;
for (const auto& type : types) {
typeNodes.push_back(std::make_pair("", type.to_property_tree()));
}
return typeNodes;
}
boost::property_tree::ptree to_property_tree(const vector<string>& strs);
boost::property_tree::ptree to_property_tree(const vector<uint8_t>& nums);
boost::property_tree::ptree to_property_tree(const vector<uint32_t>& nums);
boost::property_tree::ptree to_property_tree(const vector<uint64_t>& nums);
}
#endif /* monero_utils_h */