From b7ec081460de80a75539c0649c9fac1f382801b9 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Wed, 3 Jun 2020 21:29:43 +0200 Subject: [PATCH] implemente native lib state access api(mostly for paindroid) --- src/wallet/plain_wallet_api.cpp | 11 ++++-- src/wallet/plain_wallet_api.h | 1 + src/wallet/view_iface.h | 5 ++- src/wallet/wallet2.cpp | 14 +++++++- src/wallet/wallet2.h | 12 ++++--- src/wallet/wallet_rpc_server.cpp | 2 +- src/wallet/wallets_manager.cpp | 59 ++++++++++++++++++++++++++++---- src/wallet/wallets_manager.h | 4 ++- 8 files changed, 92 insertions(+), 16 deletions(-) diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 9a3fa7e7..316019f1 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -162,14 +162,13 @@ namespace plain_wallet LOG_PRINT_L0("[QUICK_STOP_NO_SAVE] return " << r); //let's prepare wallet manager for quick shutdown local_ptr.reset(); - } } std::string reset() { GET_INSTANCE_PTR(inst_ptr); - inst_ptr->gwm.quick_stop_no_save(); + inst_ptr->gwm.quick_clear_wallets_no_save(); epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); ok_response.result.return_code = API_RETURN_CODE_OK; return epee::serialization::store_t_to_json(ok_response); @@ -398,6 +397,14 @@ namespace plain_wallet return epee::serialization::store_t_to_json(err_result); } + std::string get_opened_wallets() + { + GET_INSTANCE_PTR(inst_ptr); + epee::json_rpc::response, epee::json_rpc::dummy_error> ok_response = AUTO_VAL_INIT(ok_response); + bool r = inst_ptr->gwm.get_opened_wallets(ok_response.result); + return epee::serialization::store_t_to_json(ok_response); + } + std::string close_wallet(hwallet h) { GET_INSTANCE_PTR(inst_ptr); diff --git a/src/wallet/plain_wallet_api.h b/src/wallet/plain_wallet_api.h index 3a18804d..a746e8d0 100644 --- a/src/wallet/plain_wallet_api.h +++ b/src/wallet/plain_wallet_api.h @@ -27,6 +27,7 @@ namespace plain_wallet 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 get_opened_wallets(); std::string get_wallet_status(hwallet h); std::string close_wallet(hwallet h); diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index 835bade9..6ec0ec2b 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -439,6 +439,8 @@ public: bool recovered; uint64_t wallet_local_bc_size; uint64_t wallet_file_size; + std::string name; + std::string pass; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(wallet_id) @@ -447,7 +449,8 @@ public: KV_SERIALIZE(seed) KV_SERIALIZE(recovered) KV_SERIALIZE(wallet_local_bc_size) - KV_SERIALIZE(wallet_file_size) + KV_SERIALIZE(name) + KV_SERIALIZE(pass) END_KV_SERIALIZE_MAP() }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a211092c..9b3909b6 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2009,6 +2009,7 @@ bool wallet2::reset_all() m_height_of_start_sync = 0; m_last_sync_percent = 0; m_last_pow_block_h = 0; + m_current_wallet_file_size = 0; return true; } //---------------------------------------------------------------------------------------------------- @@ -2269,7 +2270,10 @@ void wallet2::load(const std::wstring& wallet_, const std::string& password) { reset_history(); WLT_LOG_L0("Unable to load history data from wallet file, wallet will be resynced!"); - } + } + + boost::system::error_code ec = AUTO_VAL_INIT(ec); + m_current_wallet_file_size = boost::filesystem::file_size(wallet_, ec); THROW_IF_TRUE_WALLET_EX(need_to_resync, error::wallet_load_notice_wallet_restored, epee::string_encoding::convert_to_ansii(m_wallet_file)); } @@ -2353,6 +2357,9 @@ void wallet2::store(const std::wstring& path_to_save, const std::string& passwor bool path_to_save_exists = boost::filesystem::is_regular_file(path_to_save); bool tmp_file_path_exists = boost::filesystem::is_regular_file(tmp_file_path); bool tmp_old_file_path_exists = boost::filesystem::is_regular_file(tmp_old_file_path); + + boost::system::error_code ec = AUTO_VAL_INIT(ec); + m_current_wallet_file_size = boost::filesystem::file_size(path_to_save, ec); if (path_to_save_exists && !tmp_file_path_exists && !tmp_old_file_path_exists) { WLT_LOG_L0("Wallet was successfully stored to " << ascii_path_to_save); @@ -2364,6 +2371,11 @@ void wallet2::store(const std::wstring& path_to_save, const std::string& passwor } } //---------------------------------------------------------------------------------------------------- +uint64_t wallet2::get_wallet_file_size()const +{ + return m_current_wallet_file_size; +} +//---------------------------------------------------------------------------------------------------- void wallet2::store_watch_only(const std::wstring& path_to_save, const std::string& password) const { WLT_THROW_IF_FALSE_WALLET_INT_ERR_EX(path_to_save != m_wallet_file, "trying to save watch-only wallet to the same wallet file!"); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 8a4a7b0f..c3e893d1 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -324,7 +324,8 @@ namespace tools m_watch_only(false), m_last_pow_block_h(0), m_minimum_height(WALLET_MINIMUM_HEIGHT_UNSET_CONST), - m_pos_mint_packing_size(WALLET_DEFAULT_POS_MINT_PACKING_SIZE) + m_pos_mint_packing_size(WALLET_DEFAULT_POS_MINT_PACKING_SIZE), + m_current_wallet_file_size(0) {}; public: wallet2() : m_stop(false), @@ -339,7 +340,8 @@ namespace tools m_watch_only(false), m_last_pow_block_h(0), m_minimum_height(WALLET_MINIMUM_HEIGHT_UNSET_CONST), - m_pos_mint_packing_size(WALLET_DEFAULT_POS_MINT_PACKING_SIZE) + m_pos_mint_packing_size(WALLET_DEFAULT_POS_MINT_PACKING_SIZE), + m_current_wallet_file_size(0) { m_core_runtime_config = currency::get_default_core_runtime_config(); }; @@ -495,7 +497,8 @@ namespace tools void store(const std::wstring& path, const std::string& password); void store_watch_only(const std::wstring& path, const std::string& password) const; bool store_keys(std::string& buff, const std::string& password, bool store_as_watch_only = false); - std::wstring get_wallet_path(){ return m_wallet_file; } + std::wstring get_wallet_path()const { return m_wallet_file; } + std::string get_wallet_password()const { return m_password; } currency::account_base& get_account() { return m_account; } const currency::account_base& get_account() const { return m_account; } @@ -817,7 +820,7 @@ namespace tools static uint64_t get_max_unlock_time_from_receive_indices(const currency::transaction& tx, const money_transfer2_details& td); bool get_utxo_distribution(std::map& distribution); uint64_t get_sync_progress(); - + uint64_t get_wallet_file_size()const; private: @@ -991,6 +994,7 @@ private: uint64_t m_fake_outputs_count; std::string m_miner_text_info; + mutable uint64_t m_current_wallet_file_size; //this needed to access wallets state in coretests, for creating abnormal blocks and tranmsactions friend class test_generator; diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 5b3fff59..6118141b 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -346,7 +346,7 @@ namespace tools WALLET_RPC_BEGIN_TRY_ENTRY(); m_wallet.store(); boost::system::error_code ec = AUTO_VAL_INIT(ec); - res.wallet_file_size = boost::filesystem::file_size(m_wallet.get_wallet_path(), ec); + res.wallet_file_size = m_wallet.get_wallet_file_size(); WALLET_RPC_CATCH_TRY_ENTRY(); return true; } diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 3dc30737..ab02b533 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -62,7 +62,7 @@ wallets_manager::wallets_manager():m_pview(&m_view_stub), m_remote_node_mode(false), m_is_pos_allowed(false), m_qt_logs_enbaled(false), - dont_save_wallet_at_stop(false) + m_dont_save_wallet_at_stop(false) { #ifndef MOBILE_WALLET_BUILD m_offers_service.set_disabled(true); @@ -304,8 +304,38 @@ bool wallets_manager::stop() bool wallets_manager::quick_stop_no_save() //stop without storing wallets { - dont_save_wallet_at_stop = true; - return stop(); + m_dont_save_wallet_at_stop = true; + bool r = stop(); + EXCLUSIVE_CRITICAL_REGION_BEGIN(m_wallets_lock); + m_wallets.clear(); + m_wallet_log_prefixes.clear(); + m_stop_singal_sent = false; + EXCLUSIVE_CRITICAL_REGION_END(); + return r; +} + +bool wallets_manager::quick_clear_wallets_no_save() //stop without storing wallets +{ + SHARED_CRITICAL_REGION_BEGIN(m_wallets_lock); + LOG_PRINT_L0("Wallets[" << m_wallets.size() << "] stopping..."); + for (auto& w : m_wallets) + { + w.second.stop(false); + } + LOG_PRINT_L0("Wallets[" << m_wallets.size() << "] waiting..."); + for (auto& w : m_wallets) + { + w.second.stop(true); + } + SHARED_CRITICAL_REGION_END(); + + EXCLUSIVE_CRITICAL_REGION_BEGIN(m_wallets_lock); + LOG_PRINT_L0("Wallets[" << m_wallets.size() << "] closing..."); + m_wallets.clear(); + m_wallet_log_prefixes.clear(); + m_stop_singal_sent = false; + EXCLUSIVE_CRITICAL_REGION_END(); + return true; } std::string wallets_manager::get_config_folder() @@ -532,7 +562,7 @@ void wallets_manager::main_worker(const po::variables_map& m_vm) try { wo.second.stop(); - if(!dont_save_wallet_at_stop) + if(!m_dont_save_wallet_at_stop) wo.second.w->get()->store(); } catch (const std::exception& e) @@ -838,16 +868,33 @@ std::string wallets_manager::open_wallet(const std::wstring& path, const std::st } } EXCLUSIVE_CRITICAL_REGION_LOCAL(m_wallets_lock); - boost::system::error_code ec = AUTO_VAL_INIT(ec); - owr.wallet_file_size = boost::filesystem::file_size(path, ec); wallet_vs_options& wo = m_wallets[owr.wallet_id]; **wo.w = w; + owr.wallet_file_size = w->get_wallet_file_size(); get_wallet_info(wo, owr.wi); init_wallet_entry(wo, owr.wallet_id); return return_code; } +bool wallets_manager::get_opened_wallets(std::list& result) +{ + SHARED_CRITICAL_REGION_LOCAL(m_wallets_lock); + for (auto& w : m_wallets) + { + result.push_back(view::open_wallet_response()); + view::open_wallet_response& owr = result.back(); + owr.wallet_id = w.first; + owr.wallet_file_size = w.second.w.unlocked_get()->get_wallet_file_size(); + owr.wallet_local_bc_size = w.second.w->get()->get_blockchain_current_size(); + std::string path = epee::string_encoding::convert_to_ansii(w.second.w.unlocked_get()->get_wallet_path()); + owr.name = boost::filesystem::path(path).filename().string(); + owr.pass = w.second.w.unlocked_get()->get_wallet_password(); + get_wallet_info(w.second, owr.wi); + } + return true; +} + std::string wallets_manager::get_recent_transfers(size_t wallet_id, uint64_t offset, uint64_t count, view::transfers_array& tr_hist) { GET_WALLET_BY_ID(wallet_id, w); diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index e992109b..af037621 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -91,7 +91,9 @@ public: bool start(); bool stop(); bool quick_stop_no_save(); //stop without storing wallets + bool quick_clear_wallets_no_save(); bool send_stop_signal(); + bool get_opened_wallets(std::list& result); std::string open_wallet(const std::wstring& path, const std::string& password, uint64_t txs_to_return, view::open_wallet_response& owr); std::string generate_wallet(const std::wstring& path, const std::string& password, view::open_wallet_response& owr); std::string restore_wallet(const std::wstring& path, const std::string& password, const std::string& restore_key, bool auditable_watch_only, view::open_wallet_response& owr); @@ -189,7 +191,7 @@ private: std::atomic m_last_daemon_is_disconnected; // std::atomic m_last_wallet_synch_height; std::atomic m_wallet_id_counter; - std::atomic dont_save_wallet_at_stop; + std::atomic m_dont_save_wallet_at_stop; std::string m_data_dir; view::gui_options m_ui_opt;