From cfe9837ba8646f2eeb7225d0a621f79a606fdbda Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Mon, 27 Jan 2020 20:57:12 +0100 Subject: [PATCH] tested plain wallet interface, and fixed errors --- src/wallet/plain_wallet_api.cpp | 4 +- src/wallet/plain_wallet_api.h | 2 +- src/wallet/plain_wallet_api_impl.cpp | 5 +- tests/functional_tests/main.cpp | 2 +- tests/functional_tests/plain_wallet_tests.cpp | 83 ++++++++++++++++++- 5 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 33adf229..5e0c32a7 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -8,9 +8,9 @@ namespace plain_wallet { - hwallet create_instance(const std::string port, const std::string ip) + hwallet create_instance(const std::string ip, const std::string port) { - return new plain_wallet_api_impl(port, ip); + return new plain_wallet_api_impl(ip, port); } void destroy_instance(hwallet h) { diff --git a/src/wallet/plain_wallet_api.h b/src/wallet/plain_wallet_api.h index 11002e13..6e503bb3 100644 --- a/src/wallet/plain_wallet_api.h +++ b/src/wallet/plain_wallet_api.h @@ -10,7 +10,7 @@ namespace plain_wallet { typedef void* hwallet; - hwallet create_instance(const std::string port, const std::string ip); + 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); diff --git a/src/wallet/plain_wallet_api_impl.cpp b/src/wallet/plain_wallet_api_impl.cpp index 5476efc9..c4b10ddf 100644 --- a/src/wallet/plain_wallet_api_impl.cpp +++ b/src/wallet/plain_wallet_api_impl.cpp @@ -9,7 +9,9 @@ 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) + 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); @@ -159,6 +161,7 @@ namespace plain_wallet 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; diff --git a/tests/functional_tests/main.cpp b/tests/functional_tests/main.cpp index b9da8288..a7870627 100644 --- a/tests/functional_tests/main.cpp +++ b/tests/functional_tests/main.cpp @@ -191,7 +191,7 @@ int main(int argc, char* argv[]) do_deadlock_test_main(); return 1; } - else if (command_line::has_arg(vm, arg_deadlock_guard)) + else if (command_line::has_arg(vm, arg_test_plain_wallet)) { run_plain_wallet_api_test(); return 1; diff --git a/tests/functional_tests/plain_wallet_tests.cpp b/tests/functional_tests/plain_wallet_tests.cpp index 20892e7b..c843c3fe 100644 --- a/tests/functional_tests/plain_wallet_tests.cpp +++ b/tests/functional_tests/plain_wallet_tests.cpp @@ -19,11 +19,11 @@ using namespace epee; void run_plain_wallet_api_test() { LOG_PRINT_L0("Creating instance..."); - plain_wallet::hwallet hw = plain_wallet::create_instance("11211", "127.0.0.1."); + plain_wallet::hwallet hw = plain_wallet::create_instance("127.0.0.1", "11211"); LOG_PRINT_L0("Creating instance..." << std::hex << hw); LOG_PRINT_L0("Generating wallet..."); - std::string rsp = plain_wallet::generate(hw, std::string("E:\\tmp\\zano_testwallet_") + std::to_string(epee::misc_utils::get_tick_count()) + ".zan", ""); + std::string rsp = plain_wallet::open(hw, std::string("E:\\tmp\\zano_testwallet_74565030.zan"), ""); LOG_PRINT_L0("RESPONSE:" << ENDL << rsp); epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); epee::serialization::load_t_from_json(ok_response, rsp); @@ -53,7 +53,7 @@ void run_plain_wallet_api_test() std::string res = plain_wallet::invoke(hw, req_str); epee::serialization::load_t_from_json(gbres, res); - LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], str_response: " + LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], str_response: " << ENDL << res); } @@ -82,5 +82,80 @@ void run_plain_wallet_api_test() epee::serialization::load_t_from_json(gbres, res); LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], str_response: " - << ENDL << res); } + << ENDL << res); + } + + plain_wallet::destroy_instance(hw); + + + return; + //------- + { + LOG_PRINT_L0("Creating instance..."); + plain_wallet::hwallet hw = plain_wallet::create_instance("127.0.0.1", "11211"); + LOG_PRINT_L0("Creating instance..." << std::hex << hw); + + LOG_PRINT_L0("Generating wallet..."); + std::string rsp = plain_wallet::generate(hw, std::string("E:\\tmp\\zano_testwallet_") + std::to_string(epee::misc_utils::get_tick_count()) + ".zan", ""); + LOG_PRINT_L0("RESPONSE:" << ENDL << rsp); + epee::json_rpc::response ok_response = AUTO_VAL_INIT(ok_response); + epee::serialization::load_t_from_json(ok_response, rsp); + + plain_wallet::start_sync_thread(hw); + LOG_PRINT_L0("Started sync thread."); + + while (true) + { + std::string prog = plain_wallet::get_sync_status(hw); + plain_wallet::sync_status_response ssr = AUTO_VAL_INIT(ssr); + epee::serialization::load_t_from_json(ssr, prog); + LOG_PRINT_L0("Progress: " << ssr.progress << "Finished: " << ssr.finished); + if (ssr.finished) + break; + epee::misc_utils::sleep_no_w(1000); + } + LOG_PRINT_L0("Sync finished OK"); + + { + //request get wallet info: + epee::json_rpc::request gbreq = AUTO_VAL_INIT(gbreq); + gbreq.method = "get_wallet_info"; + epee::json_rpc::response gbres = AUTO_VAL_INIT(gbres); + std::string req_str = epee::serialization::store_t_to_json(gbreq); + + std::string res = plain_wallet::invoke(hw, req_str); + epee::serialization::load_t_from_json(gbres, res); + + LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], str_response: " + << ENDL << res); + } + + { + //request balance + epee::json_rpc::request gbreq = AUTO_VAL_INIT(gbreq); + gbreq.method = "getbalance"; + epee::json_rpc::response gbres = AUTO_VAL_INIT(gbres); + std::string req_str = epee::serialization::store_t_to_json(gbreq); + + std::string res = plain_wallet::invoke(hw, req_str); + epee::serialization::load_t_from_json(gbres, res); + + LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], balance: " + << gbres.result.balance << ", unlocked_balance: " << gbres.result.unlocked_balance); + } + + { + //request balance + epee::json_rpc::request gbreq = AUTO_VAL_INIT(gbreq); + gbreq.method = "store"; + epee::json_rpc::response gbres = AUTO_VAL_INIT(gbres); + std::string req_str = epee::serialization::store_t_to_json(gbreq); + + std::string res = plain_wallet::invoke(hw, req_str); + epee::serialization::load_t_from_json(gbres, res); + + LOG_PRINT_L0("Balance request returned: code [" << gbres.error.code << "], str_response: " + << ENDL << res); + } + } } \ No newline at end of file