From d0971413ca8b49d9308f631d1096d6f547b51260 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Sat, 2 Nov 2024 13:05:45 +0400 Subject: [PATCH] added postponed wallet loop launch in plain wallet --- src/wallet/core_default_rpc_proxy.cpp | 6 +- src/wallet/plain_wallet_api.cpp | 110 +++++++++++++++++++++--- src/wallet/plain_wallet_api_defs.h | 15 ++++ src/wallet/wallet_public_structs_defs.h | 92 +------------------- src/wallet/wallets_manager.cpp | 13 ++- src/wallet/wallets_manager.h | 1 + tests/performance_tests/main.cpp | 18 +++- 7 files changed, 147 insertions(+), 108 deletions(-) diff --git a/src/wallet/core_default_rpc_proxy.cpp b/src/wallet/core_default_rpc_proxy.cpp index 4bd94188..38907e37 100644 --- a/src/wallet/core_default_rpc_proxy.cpp +++ b/src/wallet/core_default_rpc_proxy.cpp @@ -20,7 +20,11 @@ namespace tools { bool default_http_core_proxy::set_connection_addr(const std::string& url) { - m_daemon_address = url; + if (m_daemon_address != url) + { + m_daemon_address = url; + m_http_client.disconnect(); + } return true; } //------------------------------------------------------------------------------------------------------------------------------ diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 3a9a1552..bb322481 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -56,6 +56,9 @@ void static_destroy_handler() LOG_PRINT_L0("[DESTROY CALLBACK HANDLER FINISHED]: "); } + + + namespace plain_wallet { struct plain_wallet_instance @@ -63,7 +66,9 @@ namespace plain_wallet plain_wallet_instance() :initialized(false), gjobs_counter(1) {} wallets_manager gwm; - std::atomic initialized; + std::atomic initialized = false; + std::atomic postponed_run_wallet = false; + std::atomic postponed_main_worked_started = false; std::atomic gjobs_counter; std::map gjobs; @@ -73,7 +78,6 @@ namespace plain_wallet std::shared_ptr ginstance_ptr; - typedef epee::json_rpc::response error_response; @@ -182,8 +186,11 @@ namespace plain_wallet } - std::string init(const std::string& ip, const std::string& port, const std::string& working_dir, int log_level) + std::string init(const std::string& ip_, const std::string& port_, const std::string& working_dir, int log_level) { + std::string ip = ip_; + std::string port = port_; + auto local_ptr = std::atomic_load(&ginstance_ptr); if (local_ptr) { @@ -197,6 +204,14 @@ namespace plain_wallet std::shared_ptr ptr(new plain_wallet_instance()); + if (ip.empty()) + { + ip = "0.0.0.0"; + port = "0"; + ptr->postponed_run_wallet = true; + } + + set_bundle_working_dir(working_dir); initialize_logs(log_level); @@ -217,10 +232,14 @@ namespace plain_wallet ptr->gwm.set_use_deffered_global_outputs(true); - if(!ptr->gwm.start()) + if (!ptr->postponed_run_wallet && !ptr->postponed_main_worked_started) { - LOG_ERROR("Failed to start wallets_manager"); - return GENERAL_INTERNAL_ERRROR_INIT; + if (!ptr->gwm.start()) + { + LOG_ERROR("Failed to start wallets_manager"); + return GENERAL_INTERNAL_ERRROR_INIT; + } + ptr->postponed_main_worked_started = true; } LOG_PRINT_L0("[INIT PLAIN_WALLET_INSTANCE] Ver:" << PROJECT_VERSION_LONG << "(" << BUILD_TYPE << ")"); @@ -261,11 +280,12 @@ namespace plain_wallet std::string init(const std::string& address, const std::string& working_dir, int log_level) { epee::net_utils::http::url_content url_data = AUTO_VAL_INIT(url_data); - if(!epee::net_utils::parse_url(address, url_data)) + if(!address.empty() && !epee::net_utils::parse_url(address, url_data)) { LOG_ERROR("Failed to parse address"); return API_RETURN_CODE_BAD_ARG; } + return init(url_data.host, std::to_string(url_data.port), working_dir, log_level); } @@ -438,7 +458,10 @@ namespace plain_wallet { ok_response.result.recovered = true; } - inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + if (!inst_ptr->postponed_run_wallet) + { + inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + } return epee::serialization::store_t_to_json(ok_response); } @@ -460,7 +483,10 @@ namespace plain_wallet { ok_response.result.recovered = true; } - inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + if (!inst_ptr->postponed_run_wallet) + { + inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + } return epee::serialization::store_t_to_json(ok_response); } error_response err_result = AUTO_VAL_INIT(err_result); @@ -481,7 +507,10 @@ namespace plain_wallet { ok_response.result.recovered = true; } - inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + if (!inst_ptr->postponed_run_wallet) + { + inst_ptr->gwm.run_wallet(ok_response.result.wallet_id); + } return epee::serialization::store_t_to_json(ok_response); } error_response err_result = AUTO_VAL_INIT(err_result); @@ -550,6 +579,52 @@ namespace plain_wallet return std::string("{ \"job_id\": ") + std::to_string(job_id) + "}"; } + std::string handle_reset_connection_url(const std::string& url) + { + GET_INSTANCE_PTR(inst_ptr); + inst_ptr->gwm.set_remote_node_url(url); + + view::api_response ar = AUTO_VAL_INIT(ar); + ar.error_code = API_RETURN_CODE_OK; + return epee::serialization::store_t_to_json(ar); + } + + std::string handle_run_wallet(uint64_t instance_id) + { + GET_INSTANCE_PTR(inst_ptr); + + + //postponed worker loop launch + if (!inst_ptr->postponed_main_worked_started) + { + if (!inst_ptr->gwm.start()) + { + LOG_ERROR("Failed to start wallets_manager"); + return API_RETURN_CODE_INTERNAL_ERROR; + } + inst_ptr->postponed_main_worked_started = true; + } + + view::api_response ar = AUTO_VAL_INIT(ar); + ar.error_code = inst_ptr->gwm.run_wallet(instance_id); + return epee::serialization::store_t_to_json(ar); + } + + std::string handle_configure(const std::string& settings_json) + { + GET_INSTANCE_PTR(inst_ptr); + configure_object conf = AUTO_VAL_INIT(conf); + configure_response conf_resp = AUTO_VAL_INIT(conf_resp); + bool res = epee::serialization::load_t_from_json(conf, settings_json); + if (!res) + { + conf_resp.status = API_RETURN_CODE_BAD_ARG; + return epee::serialization::store_t_to_json(conf_resp); + } + inst_ptr->postponed_run_wallet = conf.postponed_run_wallet; + conf_resp.status = API_RETURN_CODE_OK; + return epee::serialization::store_t_to_json(conf_resp); + } std::string sync_call(const std::string& method_name, uint64_t instance_id, const std::string& params) { @@ -612,6 +687,18 @@ namespace plain_wallet { res = get_wallet_status(instance_id); } + else if (method_name == "configure") + { + res = handle_configure(params); + } + else if (method_name == "reset_connection_url") + { + res = handle_reset_connection_url(params); + } + else if (method_name == "run_wallet") + { + res = handle_run_wallet(instance_id); + } else { view::api_response ar = AUTO_VAL_INIT(ar); @@ -621,9 +708,6 @@ namespace plain_wallet return res; } - - - std::string try_pull_result(uint64_t job_id) { auto inst_ptr = std::atomic_load(&ginstance_ptr); diff --git a/src/wallet/plain_wallet_api_defs.h b/src/wallet/plain_wallet_api_defs.h index 9273d524..613227d0 100644 --- a/src/wallet/plain_wallet_api_defs.h +++ b/src/wallet/plain_wallet_api_defs.h @@ -47,4 +47,19 @@ namespace plain_wallet END_KV_SERIALIZE_MAP() }; + struct configure_object + { + bool postponed_run_wallet = false; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(postponed_run_wallet) + END_KV_SERIALIZE_MAP() + }; + struct configure_response + { + std::string status; + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + } // namespace tools diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index e81aaa87..74db48f9 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -1012,92 +1012,6 @@ namespace wallet_public END_KV_SERIALIZE_MAP() }; - struct COMMAND_RPC_MAKETELEPOD - { - struct request - { - uint64_t amount; - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(amount) - END_KV_SERIALIZE_MAP() - }; - - struct response - { - std::string status; //"OK", "INSUFFICIENT_COINS", "INTERNAL_ERROR" - telepod tpd; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(status) - KV_SERIALIZE(tpd) - END_KV_SERIALIZE_MAP() - }; - }; - - - struct COMMAND_RPC_TELEPODSTATUS - { - struct request - { - telepod tpd; - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tpd) - END_KV_SERIALIZE_MAP() - }; - - struct response - { - std::string status; //"OK", "UNCONFIRMED", "BAD", "SPENT", "INTERNAL_ERROR" - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(status) - END_KV_SERIALIZE_MAP() - }; - }; - - struct COMMAND_RPC_CLONETELEPOD - { - struct request - { - telepod tpd; - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tpd) - END_KV_SERIALIZE_MAP() - }; - - struct response - { - std::string status;//"OK", "UNCONFIRMED", "BAD", "SPENT", "INTERNAL_ERROR:" - telepod tpd; - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(status) - KV_SERIALIZE(tpd) - END_KV_SERIALIZE_MAP() - }; - }; - - struct COMMAND_RPC_WITHDRAWTELEPOD - { - struct request - { - telepod tpd; - std::string addr; - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tpd) - KV_SERIALIZE(addr) - END_KV_SERIALIZE_MAP() - }; - - struct response - { - std::string status; //"OK", "UNCONFIRMED", "BAD", "SPENT", "INTERNAL_ERROR", "BAD_ADDRESS" - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(status) - END_KV_SERIALIZE_MAP() - }; - }; - - struct create_proposal_param { // uint64_t wallet_id; @@ -2131,8 +2045,8 @@ namespace wallet_public { currency::blobdata finalized_tx; currency::blobdata unsigned_tx; - crypto::eth_signature eth_sig; - crypto::hash expected_tx_id; + crypto::eth_signature eth_sig; //TODO: add value initialization here + crypto::hash expected_tx_id = currency::null_hash; bool unlock_transfers_on_fail = false; BEGIN_KV_SERIALIZE_MAP() @@ -2147,7 +2061,7 @@ namespace wallet_public struct response { std::string status; - bool transfers_were_unlocked; + bool transfers_were_unlocked = false; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(status) DOC_DSCR("Status of the call") DOC_EXMP("OK") DOC_END diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 3a08b448..426aff27 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -395,7 +395,13 @@ bool wallets_manager::start() CATCH_ENTRY_L0("main", false); } - +std::string wallets_manager::set_remote_node_url(const std::string& url) +{ + if (m_rpc_proxy) + m_rpc_proxy->set_connection_addr(url); + + return API_RETURN_CODE_OK; +} bool wallets_manager::stop() { @@ -1921,7 +1927,10 @@ std::string wallets_manager::stop_pos_mining(uint64_t wallet_id) std::string wallets_manager::run_wallet(uint64_t wallet_id) { GET_WALLET_OPT_BY_ID(wallet_id, wo); - wo.miner_thread = std::thread(boost::bind(&wallets_manager::wallet_vs_options::worker_func, &wo)); + if (!wo.major_stop && !wo.miner_thread.joinable()) + { + wo.miner_thread = std::thread(boost::bind(&wallets_manager::wallet_vs_options::worker_func, &wo)); + } return API_RETURN_CODE_OK; } diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 01c62062..6fa14328 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -147,6 +147,7 @@ public: std::string get_tx_pool_info(currency::COMMAND_RPC_GET_POOL_INFO::response& res); std::string export_wallet_history(const view::export_wallet_info& ewi); std::string setup_wallet_rpc(const std::string& jwt_secret); + std::string set_remote_node_url(const std::string& url); #ifndef MOBILE_WALLET_BUILD currency::core_rpc_server& get_rpc_server() { return m_rpc_server; } diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index 1055bfca..9b285b68 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -24,9 +24,10 @@ #include "free_space_check.h" #include "htlc_hash_tests.h" #include "threads_pool_tests.h" -#include "wallet/plain_wallet_api.h" +#include "wallet/plain_wallet_api.h" #include "wallet/view_iface.h" - +#include "wallet/plain_wallet_api_defs.h" + PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4244) #include "jwt-cpp/jwt.h" @@ -34,9 +35,15 @@ POP_VS_WARNINGS void test_plain_wallet() { - std::string res = plain_wallet::init("195.201.107.230", "33340", "C:\\Users\\roky\\home\\", 0); + //std::string res = plain_wallet::init("195.201.107.230", "33340", "C:\\Users\\roky\\home\\", 0); + std::string res = plain_wallet::init("", "", "C:\\Users\\roky\\home\\", 0); //std::string res = plain_wallet::init("127.0.0.1", "12111", "C:\\Users\\roky\\home22\\", 0); + plain_wallet::configure_object conf = AUTO_VAL_INIT(conf); + //plain_wallet::configure_response conf_resp = AUTO_VAL_INIT(conf_resp); + conf.postponed_run_wallet = true; + std::string r = plain_wallet::sync_call("configure", 0, epee::serialization::store_t_to_json(conf)); + std::string res___ = plain_wallet::get_wallet_files(); @@ -46,6 +53,10 @@ void test_plain_wallet() //res = plain_wallet::restore("", // "test_restored_2.zan", "111", ""); + epee::misc_utils::sleep_no_w(2000); + + res = plain_wallet::sync_call("reset_connection_url", 0, "195.201.107.230:33336"); + r = plain_wallet::sync_call("run_wallet", instance_id, ""); while(true) { @@ -53,6 +64,7 @@ void test_plain_wallet() res = plain_wallet::sync_call("get_wallet_status", instance_id, ""); view::wallet_sync_status_info wsi = AUTO_VAL_INIT(wsi); epee::serialization::load_t_from_json(wsi, res); + LOG_PRINT_L0("Progress: " << wsi.progress); if (wsi.wallet_state == 2) break; }