From 4729cffbaeb64b6a29e5f0a3ba67e5eb73cd57cf Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 28 Jan 2020 00:47:44 +0100 Subject: [PATCH] changed plain wallet iface to avoid pointers --- src/wallet/plain_wallet_api.cpp | 64 ++++++++++++++++++++++------ src/wallet/plain_wallet_api.h | 12 +++--- src/wallet/plain_wallet_api_impl.cpp | 8 ++-- src/wallet/plain_wallet_api_impl.h | 8 ++-- 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 5e0c32a7..65dcd585 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -6,49 +6,85 @@ #include "plain_wallet_api.h" #include "plain_wallet_api_impl.h" +//TODO: global objects, need refactoring. Just temporary solution +std::map ginstances; +epee::critical_section ginstances_lock; +std::atomic gcounter = 1; + +#define GENERAL_INTERNAL_ERRROR_INSTANCE "GENERAL_INTERNAL_ERROR: WALLET INSTNACE NOT FOUND" + +#define GET_INSTANCE(var_name, instance_handle) plain_wallet_api_impl* var_name = nullptr;\ + CRITICAL_REGION_BEGIN(ginstances_lock);\ + auto it = ginstances.find(instance_handle);\ + if (it == ginstances.end())\ + {\ + LOG_ERROR("Internall error: attempt to delete wallet with wrong instance id: " << instance_handle);\ + return GENERAL_INTERNAL_ERRROR_INSTANCE;\ + }\ + var_name = it->second;\ + CRITICAL_REGION_END(); + + namespace plain_wallet { - hwallet create_instance(const std::string ip, const std::string port) + hwallet create_instance(const std::string& ip, const std::string& port) { - return new plain_wallet_api_impl(ip, port); + plain_wallet_api_impl* ptr = new plain_wallet_api_impl(ip, port); + hwallet new_h = gcounter++; + CRITICAL_REGION_BEGIN(ginstances_lock); + ginstances[new_h] = ptr; + CRITICAL_REGION_END(); + return new_h; } + void destroy_instance(hwallet h) { - delete ((plain_wallet_api_impl*)h); + plain_wallet_api_impl* instance_ptr = nullptr; + CRITICAL_REGION_BEGIN(ginstances_lock); + auto it = ginstances.find(h); + if (it == ginstances.end()) + { + LOG_ERROR("Internall error: attempt to delete wallet with wrong instance id: " << h); + } + instance_ptr = it->second; + ginstances.erase(instance_ptr); + CRITICAL_REGION_END(); + delete instance_ptr; } - std::string open(hwallet h, const std::string& path, const std::string password) + std::string open(hwallet h, const std::string& path, const std::string& password) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->open(path, password); } - std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string password) + std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string& password) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->restore(seed, path, password); } - std::string generate(hwallet h, const std::string& path, const std::string password) + std::string generate(hwallet h, const std::string& path, const std::string& password) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->generate(path, password); } - void start_sync_thread(hwallet h) + std::string start_sync_thread(hwallet h) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); pimpl->start_sync_thread(); + return ""; } std::string get_sync_status(hwallet h) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->get_sync_status(); } std::string sync(hwallet h) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->sync(); } std::string invoke(hwallet h, const std::string& params) { - plain_wallet_api_impl* pimpl = (plain_wallet_api_impl*)h; + GET_INSTANCE(pimpl, h); return pimpl->invoke(params); } } \ No newline at end of file diff --git a/src/wallet/plain_wallet_api.h b/src/wallet/plain_wallet_api.h index 6e503bb3..836b0f2d 100644 --- a/src/wallet/plain_wallet_api.h +++ b/src/wallet/plain_wallet_api.h @@ -9,15 +9,15 @@ namespace plain_wallet { - typedef void* hwallet; - hwallet create_instance(const std::string ip, const std::string port); + typedef int64_t hwallet; + hwallet create_instance(const std::string& ip, const std::string& port); void destroy_instance(hwallet h); - std::string open(hwallet h, const std::string& path, const std::string password); - std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string password); - std::string generate(hwallet h, const std::string& path, const std::string password); + std::string open(hwallet h, const std::string& path, const std::string& password); + std::string restore(hwallet h, const std::string& seed, const std::string& path, const std::string& password); + std::string generate(hwallet h, const std::string& path, const std::string& password); - void start_sync_thread(hwallet h); + std::string start_sync_thread(hwallet h); std::string get_sync_status(hwallet h); std::string sync(hwallet h); std::string invoke(hwallet h, const std::string& params); diff --git a/src/wallet/plain_wallet_api_impl.cpp b/src/wallet/plain_wallet_api_impl.cpp index c4b10ddf..15614968 100644 --- a/src/wallet/plain_wallet_api_impl.cpp +++ b/src/wallet/plain_wallet_api_impl.cpp @@ -9,7 +9,7 @@ namespace plain_wallet { typedef epee::json_rpc::response error_response; - plain_wallet_api_impl::plain_wallet_api_impl(const std::string ip, const std::string port): + plain_wallet_api_impl::plain_wallet_api_impl(const std::string& ip, const std::string& port): m_stop(false), m_sync_finished(false) { @@ -24,7 +24,7 @@ namespace plain_wallet m_sync_thread.join(); } - std::string plain_wallet_api_impl::open(const std::string& path, const std::string password) + std::string plain_wallet_api_impl::open(const std::string& path, const std::string& password) { error_response err_result = AUTO_VAL_INIT(err_result); try @@ -50,7 +50,7 @@ namespace plain_wallet return epee::serialization::store_t_to_json(ok_response); } - std::string plain_wallet_api_impl::restore(const std::string& seed, const std::string& path, const std::string password) + std::string plain_wallet_api_impl::restore(const std::string& seed, const std::string& path, const std::string& password) { error_response err_result = AUTO_VAL_INIT(err_result); try @@ -74,7 +74,7 @@ namespace plain_wallet return epee::serialization::store_t_to_json(ok_response); } - std::string plain_wallet_api_impl::generate(const std::string& path, const std::string password) + std::string plain_wallet_api_impl::generate(const std::string& path, const std::string& password) { error_response err_result = AUTO_VAL_INIT(err_result); try diff --git a/src/wallet/plain_wallet_api_impl.h b/src/wallet/plain_wallet_api_impl.h index dba03a77..2aede346 100644 --- a/src/wallet/plain_wallet_api_impl.h +++ b/src/wallet/plain_wallet_api_impl.h @@ -15,11 +15,11 @@ namespace plain_wallet class plain_wallet_api_impl { public: - plain_wallet_api_impl(const std::string ip, const std::string port); + plain_wallet_api_impl(const std::string& ip, const std::string& port); ~plain_wallet_api_impl(); - std::string open(const std::string& path, const std::string password); - std::string restore(const std::string& seed, const std::string& path, const std::string password); - std::string generate(const std::string& path, const std::string password); + std::string open(const std::string& path, const std::string& password); + std::string restore(const std::string& seed, const std::string& path, const std::string& password); + std::string generate(const std::string& path, const std::string& password); std::string start_sync_thread(); std::string cancel_sync_thread();