From cdd435f0422215fa1b7f758573f3a2db7b40c2f0 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 14 Jun 2024 15:08:56 +0200 Subject: [PATCH] added update alias api + coretest --- src/rpc/core_rpc_server.h | 2 +- tests/core_tests/chaingen_main.cpp | 1 + tests/core_tests/wallet_rpc_tests.cpp | 192 +++++++++++++++++++++----- tests/core_tests/wallet_rpc_tests.h | 8 ++ 4 files changed, 167 insertions(+), 36 deletions(-) diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index ad4786f2..02e7b478 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -39,7 +39,7 @@ namespace currency void set_rpc_chain_handler(epee::net_utils::http::i_chain_handler* prpc_chain_handler) { m_prpc_chain_handler = prpc_chain_handler; } bool on_get_blocks_direct(const COMMAND_RPC_GET_BLOCKS_DIRECT::request& req, COMMAND_RPC_GET_BLOCKS_DIRECT::response& res, connection_context& cntx); - + void set_ignore_connectivity_status(bool ignore) { m_ignore_status = ignore;} bool on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res, connection_context& cntx); bool on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res, connection_context& cntx); diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 8f69aebc..010c2230 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1100,6 +1100,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(wallet_rpc_integrated_address); GENERATE_AND_PLAY(wallet_rpc_integrated_address_transfer); GENERATE_AND_PLAY(wallet_rpc_transfer); + GENERATE_AND_PLAY(wallet_rpc_alias_tests); GENERATE_AND_PLAY_HF(wallet_rpc_exchange_suite, "3,4"); GENERATE_AND_PLAY(wallet_chain_switch_with_spending_the_same_ki); GENERATE_AND_PLAY(wallet_sending_to_integrated_address); diff --git a/tests/core_tests/wallet_rpc_tests.cpp b/tests/core_tests/wallet_rpc_tests.cpp index e704855f..3fa4b2d7 100644 --- a/tests/core_tests/wallet_rpc_tests.cpp +++ b/tests/core_tests/wallet_rpc_tests.cpp @@ -6,12 +6,76 @@ #include "chaingen.h" #include "wallet_rpc_tests.h" #include "wallet_test_core_proxy.h" +#include "currency_core/currency_core.h" +#include "currency_core/bc_offers_service.h" +#include "rpc/core_rpc_server.h" +#include "currency_protocol/currency_protocol_handler.h" + + #include "../../src/wallet/wallet_rpc_server.h" #include "offers_helper.h" #include "random_helper.h" using namespace currency; +template +struct transport +{ + server_t& m_rpc_srv; + transport(server_t& rpc_srv) :m_rpc_srv(rpc_srv) + {} + epee::net_utils::http::http_response_info m_response; + + bool is_connected() { return true; } + template + bool connect(t_a ta, t_b tb, t_c tc) { return true; } + + template + bool invoke(const std::string uri, const std::string method_, const std::string& body, const epee::net_utils::http::http_response_info** ppresponse_info, const dummy_t& d) + { + epee::net_utils::http::http_request_info query_info; + query_info.m_URI = uri; + query_info.m_body = body; + tools::wallet_rpc_server::connection_context ctx; + bool r = m_rpc_srv.handle_http_request(query_info, m_response, ctx); + if (ppresponse_info) + *ppresponse_info = &m_response; + return r; + } +}; + + + +template +bool invoke_text_json_for_rpc(t_rpc_server& srv, const std::string& method_name, const request_t& req, response_t& resp) +{ + transport tr(srv); + + bool r = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, resp, tr); + return r; +} + +template +bool invoke_text_json_for_wallet(std::shared_ptr wlt, const std::string& method_name, const request_t& req, response_t& resp) +{ + tools::wallet_rpc_server wlt_rpc_wrapper(wlt); + return invoke_text_json_for_rpc(wlt_rpc_wrapper, method_name, req, resp); +} + +template +bool invoke_text_json_for_core(currency::core& c, const std::string& method_name, const request_t& req, response_t& resp) +{ + currency::t_currency_protocol_handler m_cprotocol(c, nullptr); + nodetool::node_server > p2p(m_cprotocol); + bc_services::bc_offers_service of(nullptr); + + currency::core_rpc_server core_rpc_wrapper(c, p2p, of); + core_rpc_wrapper.set_ignore_connectivity_status(true); + return invoke_text_json_for_rpc(core_rpc_wrapper, method_name, req, resp); +} + + + wallet_rpc_integrated_address::wallet_rpc_integrated_address() { //REGISTER_CALLBACK_METHOD(wallet_rpc_integrated_address, c1); @@ -189,7 +253,6 @@ bool wallet_rpc_integrated_address_transfer::c1(currency::core& c, size_t ev_ind } //------------------------------------------------------------------------------ - wallet_rpc_transfer::wallet_rpc_transfer() { REGISTER_CALLBACK_METHOD(wallet_rpc_transfer, configure_core); @@ -300,7 +363,100 @@ bool wallet_rpc_transfer::c1(currency::core& c, size_t ev_index, const std::vect return true; } +//------------------------------------------------------------------------------ +wallet_rpc_alias_tests::wallet_rpc_alias_tests() +{ + REGISTER_CALLBACK_METHOD(wallet_rpc_alias_tests, configure_core); + REGISTER_CALLBACK_METHOD(wallet_rpc_alias_tests, c1); + m_hardforks.set_hardfork_height(1, 1); + m_hardforks.set_hardfork_height(2, 1); + m_hardforks.set_hardfork_height(3, 1); + m_hardforks.set_hardfork_height(4, 1); +} + +bool wallet_rpc_alias_tests::generate(std::vector& events) const +{ + m_accounts.resize(TOTAL_ACCS_COUNT); + account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); + account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); + account_base& bob_acc = m_accounts[BOB_ACC_IDX]; bob_acc.generate(); + + MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, test_core_time::get_time()); + DO_CALLBACK(events, "configure_core"); // default callback will initialize core runtime config with m_hardforks + set_hard_fork_heights_to_generator(generator); + REWIND_BLOCKS_N(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 6); + + DO_CALLBACK(events, "c1"); + + return true; +} + +bool wallet_rpc_alias_tests::c1(currency::core& c, size_t ev_index, const std::vector& events) +{ + bool r = false; + std::shared_ptr miner_wlt = init_playtime_test_wallet(events, c, MINER_ACC_IDX); + std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX); + + miner_wlt->refresh(); + + { + tools::wallet_public::COMMAND_RPC_REGISTER_ALIAS::request req = AUTO_VAL_INIT(req); + tools::wallet_public::COMMAND_RPC_REGISTER_ALIAS::response rsp = AUTO_VAL_INIT(rsp); +#define ALIAS_FOR_TEST "monero" + + req.al.alias = ALIAS_FOR_TEST; + req.al.details.address = miner_wlt->get_account().get_public_address_str(); + req.al.details.comment = "XMR"; + r = invoke_text_json_for_wallet(miner_wlt, "register_alias", req, rsp); + CHECK_AND_ASSERT_MES(r, false, "failed to invoke_text_json_for_wallet"); + } + r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, 3); + CHECK_AND_ASSERT_MES(r, false, "failed to mine_next_pow_blocks_in_playtime"); + + { + currency::COMMAND_RPC_GET_ALIAS_DETAILS::request req = AUTO_VAL_INIT(req); + currency::COMMAND_RPC_GET_ALIAS_DETAILS::response rsp = AUTO_VAL_INIT(rsp); + + req.alias = ALIAS_FOR_TEST; + r = invoke_text_json_for_core(c, "get_alias_details", req, rsp); + CHECK_AND_ASSERT_MES(r, false, "failed to invoke_text_json_for_wallet"); + CHECK_AND_ASSERT_MES(rsp.status == API_RETURN_CODE_OK, false, "failed to invoke_text_json_for_wallet"); + + CHECK_AND_ASSERT_MES(rsp.alias_details.address == miner_wlt->get_account().get_public_address_str(), false, "failed to invoke_text_json_for_wallet"); + } + + miner_wlt->refresh(); + + { + tools::wallet_public::COMMAND_RPC_UPDATE_ALIAS::request req = AUTO_VAL_INIT(req); + tools::wallet_public::COMMAND_RPC_UPDATE_ALIAS::response rsp = AUTO_VAL_INIT(rsp); + + req.al.alias = ALIAS_FOR_TEST; + req.al.details.address = alice_wlt->get_account().get_public_address_str(); + req.al.details.comment = "XMR of Alice"; + r = invoke_text_json_for_wallet(miner_wlt, "update_alias", req, rsp); + CHECK_AND_ASSERT_MES(r, false, "failed to invoke_text_json_for_wallet"); + } + + r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, 3); + CHECK_AND_ASSERT_MES(r, false, "failed to mine_next_pow_blocks_in_playtime"); + + { + currency::COMMAND_RPC_GET_ALIAS_DETAILS::request req = AUTO_VAL_INIT(req); + currency::COMMAND_RPC_GET_ALIAS_DETAILS::response rsp = AUTO_VAL_INIT(rsp); + + req.alias = ALIAS_FOR_TEST; + r = invoke_text_json_for_core(c, "get_alias_details", req, rsp); + CHECK_AND_ASSERT_MES(r, false, "failed to invoke_text_json_for_wallet"); + CHECK_AND_ASSERT_MES(rsp.status == API_RETURN_CODE_OK, false, "failed to invoke_text_json_for_wallet"); + + CHECK_AND_ASSERT_MES(rsp.alias_details.address == alice_wlt->get_account().get_public_address_str(), false, "failed to invoke_text_json_for_wallet"); + } + + + return true; +} //------------------------------------------------------------------------------ wallet_rpc_exchange_suite::wallet_rpc_exchange_suite() @@ -322,40 +478,6 @@ bool wallet_rpc_exchange_suite::generate(std::vector& events) } -struct transport -{ - tools::wallet_rpc_server& m_rpc_srv; - transport(tools::wallet_rpc_server& rpc_srv):m_rpc_srv(rpc_srv) - {} - epee::net_utils::http::http_response_info m_response; - - bool is_connected() { return true; } - template - bool connect(t_a ta, t_b tb, t_c tc) { return true; } - - template - bool invoke(const std::string uri, const std::string method_, const std::string& body, const epee::net_utils::http::http_response_info** ppresponse_info, const dummy_t& d) - { - epee::net_utils::http::http_request_info query_info; - query_info.m_URI = uri; - query_info.m_body = body; - tools::wallet_rpc_server::connection_context ctx; - bool r = m_rpc_srv.handle_http_request(query_info, m_response, ctx); - if (ppresponse_info) - *ppresponse_info = &m_response; - return r; - } -}; - -template -bool invoke_text_json_for_rpc(tools::wallet_rpc_server& srv, const std::string& method_name, const request_t& req, response_t& resp) -{ - transport tr(srv); - - bool r = epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, resp, tr); - return r; -} - #include "wallet_rpc_tests_legacy_defs.h" diff --git a/tests/core_tests/wallet_rpc_tests.h b/tests/core_tests/wallet_rpc_tests.h index aa6e9b5e..0d939778 100644 --- a/tests/core_tests/wallet_rpc_tests.h +++ b/tests/core_tests/wallet_rpc_tests.h @@ -29,6 +29,14 @@ struct wallet_rpc_transfer : public wallet_test bool c1(currency::core& c, size_t ev_index, const std::vector& events); }; +struct wallet_rpc_alias_tests : public wallet_test +{ + wallet_rpc_alias_tests(); + bool generate(std::vector& events) const; + bool c1(currency::core& c, size_t ev_index, const std::vector& events); +}; + + /* Tests to make sure api for exchanges didn't change after HF4(Zarcanum) testing api: get_recent_txs_and_info, make_integrated_address,