1
0
Fork 0
forked from lthn/blockchain

implemented basic code for injection of debug events into wallet multistep opperations

This commit is contained in:
cryptozoidberg 2023-08-24 17:56:59 +02:00
parent 035097b5af
commit 06db8961e7
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
9 changed files with 189 additions and 14 deletions

View file

@ -34,6 +34,7 @@
#include <algorithm>
#include <functional>
#include <boost/thread.hpp>
#include <boost/any.hpp>
#include "include_base_utils.h"
#include "auto_val_init.h"
@ -372,6 +373,11 @@ namespace misc_utils
virtual void do_call(){};
};
template<typename param_t>
struct call_basic_param
{
virtual void do_call(param_t& p) {};
};
template<typename t_callback>
struct call_specific: public call_basic
@ -386,12 +392,34 @@ namespace misc_utils
t_callback m_cb;
};
template<typename param_t, typename t_callback>
struct call_specific_param : public call_basic_param<param_t>
{
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<typename t_callback>
auto build_abstract_callback(t_callback cb) -> std::shared_ptr<call_basic>
{
return std::shared_ptr<call_basic>(new call_specific<t_callback>(cb));
}
template<typename param_t, typename t_callback>
auto build_abstract_callback_param(t_callback cb) -> std::shared_ptr<call_basic_param<param_t>>
{
return std::shared_ptr<call_basic_param<param_t>>(new call_specific_param<param_t, t_callback>(cb));
}
template<class callback_type>
@ -427,6 +455,55 @@ namespace misc_utils
return res.first;
}
class events_dispatcher
{
public:
template<typename param_t>
struct callback_entry
{
std::shared_ptr<epee::misc_utils::call_basic_param<param_t> > m_cb;
};
std::map<std::type_index, boost::any> m_callbacks;
template<typename param_t, typename callback_t>
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<param_t> cb_entry = { epee::misc_utils::build_abstract_callback_param<param_t>(cb) };
m_callbacks[ti] = cb_entry;
}
template<typename param_t>
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<typename param_t >* pcallback_entry = boost::any_cast<callback_entry<typename param_t >>(&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

View file

@ -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

View file

@ -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 */)

View file

@ -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 ------------------------------------------------

View file

@ -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
{
};

View file

@ -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>([&](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<test_event_entry>& events) con
bool multiassets_basic_test::c1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
{
//test_test();
bool r = false;
std::shared_ptr<tools::wallet2> miner_wlt = init_playtime_test_wallet(events, c, MINER_ACC_IDX);
std::shared_ptr<tools::debug_wallet2> miner_wlt = init_playtime_test_wallet_t<debug_wallet2>(events, c, MINER_ACC_IDX);
miner_wlt->get_account().set_createtime(0);
account_base alice_acc;
alice_acc.generate();
std::shared_ptr<tools::wallet2> alice_wlt = init_playtime_test_wallet(events, c, alice_acc);
std::shared_ptr<tools::debug_wallet2> alice_wlt = init_playtime_test_wallet_t<debug_wallet2>(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;
}

View file

@ -7,7 +7,6 @@
#include "chaingen.h"
#include "wallet_tests_basic.h"
struct multiassets_basic_test : public wallet_test
{
static uint64_t ts_starter;

View file

@ -86,18 +86,25 @@ bool wallet_test::check_balance(currency::core& c, size_t ev_index, const std::v
}
}
template<typename wallet_t>
std::shared_ptr<wallet_t> wallet_test::init_playtime_test_wallet_t(const std::vector<test_event_entry>& 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<block>(events[0]));
std::shared_ptr<wallet_t> 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<tools::wallet2> wallet_test::init_playtime_test_wallet(const std::vector<test_event_entry>& 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<block>(events[0]));
std::shared_ptr<tools::wallet2> 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<tools::wallet2>(events, c, acc);
}
std::shared_ptr<tools::wallet2> wallet_test::init_playtime_test_wallet(const std::vector<test_event_entry>& events, currency::core& c, size_t account_index) const

View file

@ -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;
}
};