From 09612a42a60407045864c22d34645956be7aa991 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Mon, 6 Apr 2020 22:17:18 +0200 Subject: [PATCH] implemented connectivity status api, got rid if unused implementation --- src/wallet/plain_wallet_api.cpp | 6 +- src/wallet/plain_wallet_api.h | 1 + src/wallet/plain_wallet_api_impl.cpp | 176 --------------------------- src/wallet/plain_wallet_api_impl.h | 40 ------ src/wallet/view_iface.h | 13 ++ src/wallet/wallets_manager.cpp | 9 ++ src/wallet/wallets_manager.h | 1 + 7 files changed, 29 insertions(+), 217 deletions(-) delete mode 100644 src/wallet/plain_wallet_api_impl.cpp delete mode 100644 src/wallet/plain_wallet_api_impl.h diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 071970ba..a2f8762a 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -4,7 +4,7 @@ #include "plain_wallet_api.h" -#include "plain_wallet_api_impl.h" +#include "plain_wallet_api_defs.h" #include "currency_core/currency_config.h" #include "version.h" #include "string_tools.h" @@ -213,6 +213,10 @@ namespace plain_wallet return epee::serialization::store_t_to_json(ok_response); } + std::string get_connectivity_status() + { + return gwm.get_connectivity_status(); + } std::string get_version() { diff --git a/src/wallet/plain_wallet_api.h b/src/wallet/plain_wallet_api.h index 0e964697..e60180a2 100644 --- a/src/wallet/plain_wallet_api.h +++ b/src/wallet/plain_wallet_api.h @@ -20,6 +20,7 @@ namespace plain_wallet std::string generate_random_key(uint64_t lenght); std::string get_logs_buffer(); std::string truncate_log(); + std::string get_connectivity_status(); 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); diff --git a/src/wallet/plain_wallet_api_impl.cpp b/src/wallet/plain_wallet_api_impl.cpp deleted file mode 100644 index 249dca78..00000000 --- a/src/wallet/plain_wallet_api_impl.cpp +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright (c) 2014-2018 Zano Project -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#include "plain_wallet_api_impl.h" -#include "wallet/wallet_helpers.h" - -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): - m_stop(false), - m_sync_finished(false) - { - m_wallet.reset(new tools::wallet2()); - m_wallet->init(ip + ":" + port); - m_rpc_wrapper.reset(new tools::wallet_rpc_server(*m_wallet)); - } - - plain_wallet_api_impl::~plain_wallet_api_impl() - { - if (m_sync_thread.joinable()) - m_sync_thread.join(); - } - - 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 - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - m_wallet->load(epee::string_encoding::utf8_to_wstring(path), password); - epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); - m_wallet->get_recent_transfers_history(ok_response.result.recent_history.history, 0, 20, ok_response.result.recent_history.total_history_items); - m_wallet->get_unconfirmed_transfers(ok_response.result.recent_history.history); - tools::get_wallet_info(*m_wallet, ok_response.result.wi); - return epee::serialization::store_t_to_json(ok_response); - } - catch (const tools::error::wallet_load_notice_wallet_restored& e) - { - LOG_ERROR("Wallet initialize was with problems, but still worked : " << e.what()); - err_result.error.code = API_RETURN_CODE_FILE_RESTORED; - return epee::serialization::store_t_to_json(err_result); - } - catch (const std::exception& e) - { - LOG_ERROR("Wallet initialize failed: " << e.what()); - err_result.error.code = API_RETURN_CODE_FAIL; - return epee::serialization::store_t_to_json(err_result); - } - } - - 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 - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - m_wallet->restore(epee::string_encoding::utf8_to_wstring(path), password, seed); - epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); - tools::get_wallet_info(*m_wallet, ok_response.result.wi); - return epee::serialization::store_t_to_json(ok_response); - - } - catch (const tools::error::wallet_load_notice_wallet_restored& e) - { - LOG_ERROR("Wallet initialize was with problems, but still worked : " << e.what()); - err_result.error.code = API_RETURN_CODE_FILE_RESTORED; - return epee::serialization::store_t_to_json(err_result); - } - catch (const std::exception& e) - { - LOG_ERROR("Wallet initialize failed: " << e.what()); - err_result.error.code = API_RETURN_CODE_FAIL; - return epee::serialization::store_t_to_json(err_result); - } - } - - 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 - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - m_wallet->generate(epee::string_encoding::utf8_to_wstring(path), password); - epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); - tools::get_wallet_info(*m_wallet, ok_response.result.wi); - return epee::serialization::store_t_to_json(ok_response); - } - catch (const tools::error::wallet_load_notice_wallet_restored& e) - { - LOG_ERROR("Wallet initialize was with problems, but still worked : " << e.what()); - err_result.error.code = API_RETURN_CODE_FILE_RESTORED; - return epee::serialization::store_t_to_json(err_result); - } - catch (const std::exception& e) - { - LOG_ERROR("Wallet initialize failed: " << e.what()); - err_result.error.code = API_RETURN_CODE_FAIL; - return epee::serialization::store_t_to_json(err_result); - } - } - - std::string plain_wallet_api_impl::start_sync_thread() - { - m_sync_thread = std::thread([&]() - { - - try - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - m_wallet->refresh(m_stop); - } - catch (const std::exception& e) - { - LOG_ERROR("Wallet refresh failed: " << e.what()); - return; - } - m_sync_finished = true; - }); - basic_status_response bsr = AUTO_VAL_INIT(bsr); - bsr.status = API_RETURN_CODE_OK; - return epee::serialization::store_t_to_json(bsr); - } - - std::string plain_wallet_api_impl::cancel_sync_thread() - { - m_stop = true; - if (m_sync_thread.joinable()) - m_sync_thread.join(); - basic_status_response bsr = AUTO_VAL_INIT(bsr); - bsr.status = API_RETURN_CODE_OK; - return epee::serialization::store_t_to_json(bsr); - - } - - std::string plain_wallet_api_impl::get_sync_status() - { - sync_status_response ssr = AUTO_VAL_INIT(ssr); - ssr.finished = m_sync_finished; - ssr.progress = m_wallet->get_sync_progress(); - return epee::serialization::store_t_to_json(ssr); - } - - std::string plain_wallet_api_impl::sync() - { - basic_status_response bsr = AUTO_VAL_INIT(bsr); - try - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - m_wallet->refresh(m_stop); - bsr.status = API_RETURN_CODE_OK; - return epee::serialization::store_t_to_json(bsr); - } - catch (const std::exception& e) - { - LOG_ERROR("Wallet refresh failed: " << e.what()); - bsr.status = API_RETURN_CODE_FAIL; - return epee::serialization::store_t_to_json(bsr); - } - } - std::string plain_wallet_api_impl::invoke(const std::string& params) - { - CRITICAL_REGION_LOCAL(m_wallet_lock); - epee::net_utils::http::http_request_info query_info = AUTO_VAL_INIT(query_info); - epee::net_utils::http::http_response_info response_info = AUTO_VAL_INIT(response_info); - epee::net_utils::connection_context_base stub_conn_context = AUTO_VAL_INIT(stub_conn_context); - std::string reference_stub; - bool call_found = false; - query_info.m_URI = "/json_rpc"; - query_info.m_body = params; - m_rpc_wrapper->handle_http_request_map(query_info, response_info, stub_conn_context, call_found, reference_stub); - return response_info.m_body; - } -} \ No newline at end of file diff --git a/src/wallet/plain_wallet_api_impl.h b/src/wallet/plain_wallet_api_impl.h deleted file mode 100644 index 310c37a5..00000000 --- a/src/wallet/plain_wallet_api_impl.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2014-2020 Zano Project -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - - -#pragma once - -#include -#include "wallet2.h" -#include "wallet_rpc_server.h" -#include "plain_wallet_api_defs.h" - -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(); - 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(); - std::string get_sync_status(); - - std::string sync(); - std::string invoke(const std::string& params); - private: - bool get_wallet_info(view::wallet_info& wi); - std::thread m_sync_thread; - epee::critical_section m_wallet_lock; - std::atomic m_stop; - std::atomic m_sync_finished; - std::shared_ptr m_wallet; - std::shared_ptr m_rpc_wrapper; - }; - -} \ No newline at end of file diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index 5d6f4aea..6ce27063 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -448,6 +448,19 @@ public: END_KV_SERIALIZE_MAP() }; + struct general_connectivity_info + { + bool is_online; + bool last_daemon_is_disconnected; + uint64_t last_proxy_communicate_timestamp; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(is_online) + KV_SERIALIZE(last_daemon_is_disconnected) + KV_SERIALIZE(last_proxy_communicate_timestamp) + END_KV_SERIALIZE_MAP() + }; + struct header_entry { diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index a65a14d6..b2da3256 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -1238,6 +1238,15 @@ bool wallets_manager::get_is_remote_daemon_connected() return true; } +std::string wallets_manager::get_connectivity_status() +{ + view::general_connectivity_info gci = AUTO_VAL_INIT(gci); + gci.is_online = get_is_remote_daemon_connected(); + gci.last_daemon_is_disconnected = m_last_daemon_is_disconnected; + gci.last_proxy_communicate_timestamp = m_rpc_proxy->get_last_success_interract_time(); + return epee::serialization::store_t_to_json(wsi); +} + std::string wallets_manager::get_wallet_status(uint64_t wallet_id) { GET_WALLET_OPT_BY_ID(wallet_id, wo); diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 168553cc..34d55b00 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -106,6 +106,7 @@ public: std::string request_cancel_contract(size_t wallet_id, const crypto::hash& contract_id, uint64_t fee, uint64_t expiration_period); std::string accept_cancel_contract(size_t wallet_id, const crypto::hash& contract_id); + std::string get_connectivity_status(); std::string get_wallet_info(wallet_vs_options& w, view::wallet_info& wi); std::string close_wallet(size_t wallet_id); std::string push_offer(size_t wallet_id, const bc_services::offer_details_ex& od, currency::transaction& res_tx);