From 3e2a39db2cd4629f82bf8a579f09bd5707c5e87f Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 18 Mar 2025 18:51:27 +0400 Subject: [PATCH] added proxy to daemon --- src/common/error_codes.h | 1 + src/wallet/core_default_rpc_proxy.cpp | 3 ++- src/wallet/plain_wallet_api.cpp | 36 +++++++++++++++++++++++++++ tests/performance_tests/main.cpp | 22 ++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/common/error_codes.h b/src/common/error_codes.h index 1e3dc2ef..910e1e42 100644 --- a/src/common/error_codes.h +++ b/src/common/error_codes.h @@ -21,6 +21,7 @@ #define API_RETURN_CODE_BAD_ARG_WRONG_AMOUNT "BAD_ARG_WRONG_AMOUNT" #define API_RETURN_CODE_BAD_ARG_UNKNOWN_DECIMAL_POINT "BAD_ARG_UNKNOWN_DECIMAL_POINT" #define API_RETURN_CODE_BAD_ARG_WRONG_PAYMENT_ID "BAD_ARG_WRONG_PAYMENT_ID" +#define API_RETURN_CODE_BAD_ARG_INVALID_JSON "BAD_ARG_INVALID_JSON" #define API_RETURN_CODE_WRONG_PASSWORD "WRONG_PASSWORD" #define API_RETURN_CODE_WALLET_WRONG_ID "WALLET_WRONG_ID" #define API_RETURN_CODE_WALLET_WATCH_ONLY_NOT_SUPPORTED "WALLET_WATCH_ONLY_NOT_SUPPORTED" diff --git a/src/wallet/core_default_rpc_proxy.cpp b/src/wallet/core_default_rpc_proxy.cpp index 5e82efa4..5e67beb4 100644 --- a/src/wallet/core_default_rpc_proxy.cpp +++ b/src/wallet/core_default_rpc_proxy.cpp @@ -63,7 +63,8 @@ namespace tools #endif const epee::net_utils::http::http_response_info* response = nullptr; - bool res = m_http_client.invoke(uri, "POST", body, &response); + bool res = epee::net_utils::http::invoke_request(m_daemon_address + uri, m_http_client, m_connection_timeout, &response, "POST", body); + //bool res = m_http_client.invoke(uri, "POST", body, &response); if (response) { response_body = response->m_body; diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index d8c8a07e..67bba659 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -73,6 +73,7 @@ namespace plain_wallet std::atomic gjobs_counter; std::map gjobs; epee::critical_section gjobs_lock; + tools::default_http_core_proxy m_common_daemon_proxy; }; std::shared_ptr ginstance_ptr; @@ -249,6 +250,8 @@ namespace plain_wallet ptr->postponed_main_worked_started = true; } + ptr->m_common_daemon_proxy.set_connection_addr(ip + ":" + port); + LOG_PRINT_L0("[INIT PLAIN_WALLET_INSTANCE] Ver:" << PROJECT_VERSION_LONG << "(" << BUILD_TYPE << ")" << ENDL << "Working dir: " << working_dir << ENDL << "URL: " << ip << ", port: " << port); @@ -598,6 +601,35 @@ namespace plain_wallet return sanitized_store_to_json(ar); } + std::string handle_proxy_to_daemon(const std::string& data) + { + GET_INSTANCE_PTR(inst_ptr); + tools::wallet_public::COMMAND_PROXY_TO_DAEMON::request req; + tools::wallet_public::COMMAND_PROXY_TO_DAEMON::response res; + + if (!epee::serialization::load_t_from_json(req, data)) + { + view::api_response ar = AUTO_VAL_INIT(ar); + ar.error_code = API_RETURN_CODE_BAD_ARG_INVALID_JSON; + return sanitized_store_to_json(ar); + } + + std::string buff = epee::string_encoding::base64_decode(req.base64_body); + int response_code = 0; + std::string response_body; + if (!inst_ptr->m_common_daemon_proxy.call_COMMAND_RPC_INVOKE(req.uri, buff, response_code, response_body)) + { + view::api_response ar = AUTO_VAL_INIT(ar); + ar.error_code = API_RETURN_CODE_FAIL; + return sanitized_store_to_json(ar); + } + res.base64_body = epee::string_encoding::base64_encode(response_body); + res.response_code = response_code; + return sanitized_store_to_json(res); + } + + + std::string handle_run_wallet(uint64_t instance_id) { GET_INSTANCE_PTR(inst_ptr); @@ -708,6 +740,10 @@ namespace plain_wallet { res = handle_run_wallet(instance_id); } + else if (method_name == "proxy_to_daemon") + { + res = handle_proxy_to_daemon(params); + } else { view::api_response ar = AUTO_VAL_INIT(ar); diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index 3c621038..8521a399 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -46,6 +46,21 @@ void test_plain_wallet() conf.postponed_run_wallet = true; std::string r = plain_wallet::sync_call("configure", 0, epee::serialization::store_t_to_json(conf)); + + + //------------------------------------------------------------------- +//test proxy_to_wallet + std::string daemon_body = "{\"method\":\"getinfo\",\"params\":{}}"; + std::string daemon_body_base64 = epee::string_encoding::base64_encode(daemon_body); + tools::wallet_public::COMMAND_PROXY_TO_DAEMON::request req_to_daemon; + req_to_daemon.uri = "/json_rpc"; + req_to_daemon.base64_body = daemon_body_base64; + std::string rsp_ = plain_wallet::sync_call("proxy_to_daemon", 0, epee::serialization::store_t_to_json(req_to_daemon)); + //------------------------------------------------------------------- + + + + std::string seed; if (!epee::file_io_utils::load_file_to_string("C:\\Users\\roky\\home\\temp\\wallets\\seed.txt", seed)) return; @@ -65,6 +80,11 @@ void test_plain_wallet() r = plain_wallet::sync_call("run_wallet", instance_id, ""); + + + + + while(true) { epee::misc_utils::sleep_no_w(2000); @@ -75,6 +95,8 @@ void test_plain_wallet() if (wsi.wallet_state == 2) break; } + + std::string invoke_body = "{\"method\":\"store\",\"params\":{}}"; std::string res1 = plain_wallet::sync_call("invoke", instance_id, invoke_body);