From 2c181de6a34a632f3f8777889ac727da5c255264 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 4 Apr 2024 23:41:35 +0200 Subject: [PATCH] fixed serialization for hex-encoded array of pod items --- .../include/serialization/keyvalue_helpers.h | 34 +++++++++++++++++++ .../serialization/keyvalue_serialization.h | 5 +++ src/currency_core/currency_basic.h | 2 +- tests/performance_tests/main.cpp | 10 ++++-- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/contrib/epee/include/serialization/keyvalue_helpers.h b/contrib/epee/include/serialization/keyvalue_helpers.h index 09555a9e..9b719103 100644 --- a/contrib/epee/include/serialization/keyvalue_helpers.h +++ b/contrib/epee/include/serialization/keyvalue_helpers.h @@ -97,6 +97,40 @@ namespace epee return epee::string_encoding::base64_decode(a); } + + //basic helpers for pod-to-hex serialization + template + std::string transform_t_pod_array_to_hex_str_array(const t_pod_container_type& a) + { + std::string res; + for (const auto& item : a) + { + res += epee::string_tools::pod_to_hex(a) + ", "; + } + if (a.size()) + { + res.erase(res.size() - 2); + } + + return res; + } + template + t_pod_container_type transform_hex_str_array_to_t_pod_array(const std::string& a) + { + std::vector pod_items; + boost::split(pod_items, a, boost::is_any_of(", ][\"")); + + t_pod_container_type res; + for (const auto& item : pod_items) + { + res.resize(res.size() + 1); + t_pod_container_type::value_type& pod_val = res.back(); + + if (!epee::string_tools::hex_to_pod(item, pod_val)) + throw std::runtime_error(std::string("Unable to transform \"") + item + "\" to pod type " + typeid(t_pod_container_type::value_type).name()); + } + return res; + } //------------------------------------------------------------------------------------------------------------------- #pragma pack(push, 1) template diff --git a/contrib/epee/include/serialization/keyvalue_serialization.h b/contrib/epee/include/serialization/keyvalue_serialization.h index f60b92d1..317944ca 100644 --- a/contrib/epee/include/serialization/keyvalue_serialization.h +++ b/contrib/epee/include/serialization/keyvalue_serialization.h @@ -131,6 +131,10 @@ public: \ static_assert(std::is_pod::value, "t_type must be a POD type."); \ KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) +#define KV_SERIALIZE_CONTAINER_POD_AS_HEX_N(varialble, val_name) \ + KV_SERIALIZE_CUSTOM_N(varialble, std::string, epee::transform_t_pod_array_to_hex_str_array, epee::transform_hex_str_array_to_t_pod_array, val_name) + + #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, val_name) \ epee::serialization::selector::serialize_stl_container_pod_val_as_blob(this_ref.varialble, stg, hparent_section, val_name); @@ -145,6 +149,7 @@ public: \ #define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, #varialble) #define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_pod compile time check #define KV_SERIALIZE_CONTAINER_POD_AS_BLOB(varialble) KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, #varialble) +#define KV_SERIALIZE_CONTAINER_POD_AS_HEX(varialble) KV_SERIALIZE_CONTAINER_POD_AS_HEX_N(varialble, #varialble) #define KV_SERIALIZE_CUSTOM(varialble, stored_type, from_v_to_stored, from_stored_to_v) KV_SERIALIZE_CUSTOM_N(varialble, stored_type, from_v_to_stored, from_stored_to_v, #varialble) #define KV_SERIALIZE_POD_AS_HEX_STRING(varialble) KV_SERIALIZE_POD_AS_HEX_STRING_N(varialble, #varialble) #define KV_SERIALIZE_BLOB_AS_HEX_STRING(varialble) KV_SERIALIZE_BLOB_AS_HEX_STRING_N(varialble, #varialble) diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index b5aa7fab..f3ba4b31 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -592,7 +592,7 @@ namespace currency KV_SERIALIZE(service_id) KV_SERIALIZE(instruction) KV_SERIALIZE_BLOB_AS_HEX_STRING(body) - KV_SERIALIZE_CONTAINER_POD_AS_BLOB(security) + KV_SERIALIZE_CONTAINER_POD_AS_HEX(security) KV_SERIALIZE(flags) END_KV_SERIALIZE_MAP() }; diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index d1a507b1..d93220b5 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -38,9 +38,9 @@ void test_plain_wallet() std::string res = plain_wallet::init("127.0.0.1", "12111", "C:\\Users\\roky\\home\\", 0); uint64_t instance_id = 0; - res = plain_wallet::open("test_restored.zan", "111"); - //res = plain_wallet::restore("heart level clear fate sorrow childhood sent fate ceiling party third steel came ask mix neither message already almost vast date glide tumble color okay space", - // "test_restored.zan", "111", ""); + res = plain_wallet::open("test_restored_2.zan", "111"); + //res = plain_wallet::restore("", + // "test_restored_2.zan", "111", ""); while(true) @@ -60,6 +60,10 @@ void test_plain_wallet() invoke_body = "{\"method\":\"get_recent_txs_and_info\",\"params\":{\"offset\":0,\"count\":30,\"update_provision_info\":true}}"; std::string res2 = plain_wallet::sync_call("invoke", instance_id, invoke_body); + invoke_body = "{\"method\":\"get_recent_txs_and_info2\",\"params\":{\"offset\":0,\"count\":30,\"update_provision_info\":true}}"; + res2 = plain_wallet::sync_call("invoke", instance_id, invoke_body); + + invoke_body = "{\"method\":\"getbalance\",\"params\":{}}"; std::string res3 = plain_wallet::sync_call("invoke", instance_id, invoke_body);