From 06db8961e7c7a1480d9a04c20a83d7bcb52a29d4 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Thu, 24 Aug 2023 17:56:59 +0200 Subject: [PATCH] implemented basic code for injection of debug events into wallet multistep opperations --- contrib/epee/include/misc_language.h | 77 ++++++++++++++++++++ src/currency_core/currency_format_utils.h | 2 +- src/wallet/wallet2.cpp | 2 + src/wallet/wallet2.h | 2 + src/wallet/wallet_debug_events_definitions.h | 15 ++++ tests/core_tests/multiassets_test.cpp | 68 ++++++++++++++++- tests/core_tests/multiassets_test.h | 1 - tests/core_tests/wallet_tests_basic.cpp | 27 ++++--- tests/core_tests/wallet_tests_basic.h | 9 +++ 9 files changed, 189 insertions(+), 14 deletions(-) create mode 100644 src/wallet/wallet_debug_events_definitions.h diff --git a/contrib/epee/include/misc_language.h b/contrib/epee/include/misc_language.h index 1389ba03..7d370a72 100644 --- a/contrib/epee/include/misc_language.h +++ b/contrib/epee/include/misc_language.h @@ -34,6 +34,7 @@ #include #include #include +#include #include "include_base_utils.h" #include "auto_val_init.h" @@ -372,6 +373,11 @@ namespace misc_utils virtual void do_call(){}; }; + template + struct call_basic_param + { + virtual void do_call(param_t& p) {}; + }; template struct call_specific: public call_basic @@ -386,12 +392,34 @@ namespace misc_utils t_callback m_cb; }; + template + struct call_specific_param : public call_basic_param + { + call_specific_param(t_callback cb) :m_cb(cb) + {} + virtual void do_call(param_t& p) + { + m_cb(p); + } + private: + t_callback m_cb; + }; + + + template auto build_abstract_callback(t_callback cb) -> std::shared_ptr { return std::shared_ptr(new call_specific(cb)); } + + template + auto build_abstract_callback_param(t_callback cb) -> std::shared_ptr> + { + return std::shared_ptr>(new call_specific_param(cb)); + } + template @@ -427,6 +455,55 @@ namespace misc_utils return res.first; } + + class events_dispatcher + { + + public: + + template + struct callback_entry + { + std::shared_ptr > m_cb; + }; + + std::map m_callbacks; + + template + void SUBSCIRBE_DEBUG_EVENT(callback_t cb) + { + std::type_index ti = typeid(param_t); + auto it = m_callbacks.find(ti); + if (it != m_callbacks.end()) + { + throw std::runtime_error("Handler for this type already registered"); + } + + callback_entry cb_entry = { epee::misc_utils::build_abstract_callback_param(cb) }; + + m_callbacks[ti] = cb_entry; + } + + template + void RAISE_DEBUG_EVENT(param_t& p) + { + std::type_index ti = typeid(param_t); + auto it = m_callbacks.find(ti); + if (it != m_callbacks.end()) + { + callback_entry* pcallback_entry = boost::any_cast>(&it->second); + if (!pcallback_entry) + { + throw std::runtime_error("Unexpected error: registered tipe holding something else in boost::eny"); + } + pcallback_entry->m_cb->do_call(p); + } + } + + }; + + + } // namespace misc_utils } // namespace epee diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 355c85a9..f21819aa 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -159,7 +159,7 @@ namespace currency uint64_t tx_version; uint64_t mode_separate_fee = 0; crypto::secret_key asset_control_key = currency::null_skey; - + epee::misc_utils::events_dispatcher* pevents_dispatcher; tx_generation_context gen_context{}; // solely for consolidated txs diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 68a58b8b..ce84d528 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -42,6 +42,7 @@ using namespace epee; #include "common/variant_helper.h" #include "currency_core/crypto_config.h" #include "crypto/zarcanum.h" +#include "wallet_debug_events_definitions.h" using namespace currency; @@ -7009,6 +7010,7 @@ void wallet2::transfer(construct_tx_param& ctp, print_tx_sent_message(result.tx, std::string() + "(transfer)", ctp.fee); } + //---------------------------------------------------------------------------------------------------- void wallet2::sweep_below(size_t fake_outs_count, const currency::account_public_address& destination_addr, uint64_t threshold_amount, const currency::payment_id_t& payment_id, uint64_t fee, size_t& outs_total, uint64_t& amount_total, size_t& outs_swept, uint64_t& amount_swept, currency::transaction* p_result_tx /* = nullptr */, std::string* p_filename_or_unsigned_tx_blob_str /* = nullptr */) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index dfcc2f85..4c691ecd 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -1042,6 +1042,8 @@ namespace tools void operator()(const asset_update_event& e); void operator()(const asset_unown_event& e); +protected: + epee::misc_utils::events_dispatcher m_debug_events_dispatcher; private: // -------- t_transport_state_notifier ------------------------------------------------ diff --git a/src/wallet/wallet_debug_events_definitions.h b/src/wallet/wallet_debug_events_definitions.h new file mode 100644 index 00000000..c8c8f3b7 --- /dev/null +++ b/src/wallet/wallet_debug_events_definitions.h @@ -0,0 +1,15 @@ +// Copyright (c) 2014-2023 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + + + +//Wallet Debug Events +struct wde_construct_tx_a1 +{ + +}; + + diff --git a/tests/core_tests/multiassets_test.cpp b/tests/core_tests/multiassets_test.cpp index 2f468d1b..316d954b 100644 --- a/tests/core_tests/multiassets_test.cpp +++ b/tests/core_tests/multiassets_test.cpp @@ -11,6 +11,65 @@ using namespace currency; + + + + + +/* + + +struct debug_context_event_1 +{ + int& i; + std::string& s; +}; + + + +//#define RAISE_DEBUG_EVENT dw.handle_type + + +void test_test() +{ + epee::misc_utils::events_dispatcher ed; + + //-------------------------------------------------------------------------------- + //-------------------------------------------------------------------------------- + //-------------------------------------------------------------------------------- + //thus code will be called in the tests + ed.SUBSCIRBE_DEBUG_EVENT([&](debug_context_event_1& d) + { + //here some operations + LOG_PRINT_L0("lala: " << d.i << d.s); + // + d.i = 10; + d.s = "33333"; + + }); + + + //-------------------------------------------------------------------------------- + //-------------------------------------------------------------------------------- + //-------------------------------------------------------------------------------- + //this code will be in the wallet and helper functions + + int i = 22; + std::string sss = "11111"; + + ed.RAISE_DEBUG_EVENT(debug_context_event_1{i, sss }); + + + LOG_PRINT_L0("lala: " << i << sss); +} +*/ + + + + + + + //------------------------------------------------------------------------------ #define AMOUNT_TO_TRANSFER_MULTIASSETS_BASIC (TESTS_DEFAULT_FEE) @@ -47,12 +106,14 @@ bool multiassets_basic_test::generate(std::vector& events) con bool multiassets_basic_test::c1(currency::core& c, size_t ev_index, const std::vector& events) { + //test_test(); + bool r = false; - std::shared_ptr miner_wlt = init_playtime_test_wallet(events, c, MINER_ACC_IDX); + std::shared_ptr miner_wlt = init_playtime_test_wallet_t(events, c, MINER_ACC_IDX); miner_wlt->get_account().set_createtime(0); account_base alice_acc; alice_acc.generate(); - std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, alice_acc); + std::shared_ptr alice_wlt = init_playtime_test_wallet_t(events, c, alice_acc); alice_wlt->get_account().set_createtime(0); miner_wlt->refresh(); @@ -179,6 +240,7 @@ bool multiassets_basic_test::c1(currency::core& c, size_t ev_index, const std::v CHECK_AND_ASSERT_MES(asset_info3.current_supply == asset_info2.current_supply + destinations[1].amount + destinations[0].amount, false, "Failed to find needed asset in result balances"); + miner_wlt->burn_asset(asset_id, last_miner_balance, tx); r = mine_next_pow_blocks_in_playtime(miner_wlt->get_account().get_public_address(), c, 1); @@ -190,6 +252,8 @@ bool multiassets_basic_test::c1(currency::core& c, size_t ev_index, const std::v CHECK_AND_ASSERT_MES(r, false, "Failed to get_asset_info"); CHECK_AND_ASSERT_MES(asset_info4.current_supply == asset_info3.current_supply - last_miner_balance, false, "Failed to find needed asset in result balances"); + + return true; } diff --git a/tests/core_tests/multiassets_test.h b/tests/core_tests/multiassets_test.h index 1f8ecbd0..515f6efd 100644 --- a/tests/core_tests/multiassets_test.h +++ b/tests/core_tests/multiassets_test.h @@ -7,7 +7,6 @@ #include "chaingen.h" #include "wallet_tests_basic.h" - struct multiassets_basic_test : public wallet_test { static uint64_t ts_starter; diff --git a/tests/core_tests/wallet_tests_basic.cpp b/tests/core_tests/wallet_tests_basic.cpp index c4cca86d..f09809d6 100644 --- a/tests/core_tests/wallet_tests_basic.cpp +++ b/tests/core_tests/wallet_tests_basic.cpp @@ -86,18 +86,25 @@ bool wallet_test::check_balance(currency::core& c, size_t ev_index, const std::v } } + + template + std::shared_ptr wallet_test::init_playtime_test_wallet_t(const std::vector& events, currency::core& c, const account_base& acc) const + { + CHECK_AND_ASSERT_THROW_MES(events.size() > 0 && events[0].type() == typeid(currency::block), "Invalid events queue, can't find genesis block at the beginning"); + crypto::hash genesis_hash = get_block_hash(boost::get(events[0])); + + std::shared_ptr w(new wallet_t); + w->set_core_runtime_config(c.get_blockchain_storage().get_core_runtime_config()); + w->assign_account(acc); + w->set_genesis(genesis_hash); + w->set_core_proxy(m_core_proxy); + w->set_disable_tor_relay(true); + return w; + } + std::shared_ptr wallet_test::init_playtime_test_wallet(const std::vector& events, currency::core& c, const account_base& acc) const { - CHECK_AND_ASSERT_THROW_MES(events.size() > 0 && events[0].type() == typeid(currency::block), "Invalid events queue, can't find genesis block at the beginning"); - crypto::hash genesis_hash = get_block_hash(boost::get(events[0])); - - std::shared_ptr w(new tools::wallet2); - w->set_core_runtime_config(c.get_blockchain_storage().get_core_runtime_config()); - w->assign_account(acc); - w->set_genesis(genesis_hash); - w->set_core_proxy(m_core_proxy); - w->set_disable_tor_relay(true); - return w; + return init_playtime_test_wallet_t(events, c, acc); } std::shared_ptr wallet_test::init_playtime_test_wallet(const std::vector& events, currency::core& c, size_t account_index) const diff --git a/tests/core_tests/wallet_tests_basic.h b/tests/core_tests/wallet_tests_basic.h index 0329e0a2..2123fa5a 100644 --- a/tests/core_tests/wallet_tests_basic.h +++ b/tests/core_tests/wallet_tests_basic.h @@ -118,3 +118,12 @@ struct wlt_lambda_on_transfer2_wrapper : public tools::i_wallet2_callback bool m_result; Func m_callback; }; + +class debug_wallet2: public tools::wallet2 +{ +public: + epee::misc_utils::events_dispatcher& get_debug_events_dispatcher() + { + return this->m_debug_events_dispatcher; + } +}; \ No newline at end of file