danicoin/tests/CoreTests/Chaingen.h

632 lines
28 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-03-03 22:07:58 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <boost/program_options.hpp>
#include <boost/serialization/variant.hpp>
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/CoreConfig.h"
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
#include "Common/CommandLine.h"
2015-07-15 12:23:00 +00:00
#include "Common/ConsoleTools.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/Account.h"
#include "CryptoNoteCore/Core.h"
#include "CryptoNoteCore/TransactionExtra.h"
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
#include "../TestGenerator/TestGenerator.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/CryptoNoteTools.h"
#include "BoostSerializationHelper.h"
#include "AccountBoostSerialization.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerGroup.h>
#include <Logging/ConsoleLogger.h>
2014-03-03 22:07:58 +00:00
namespace concolor
{
2015-07-15 12:23:00 +00:00
using namespace Common::Console;
2014-03-03 22:07:58 +00:00
inline std::basic_ostream<char, std::char_traits<char> >& bright_white(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::BrightWhite);
2014-03-03 22:07:58 +00:00
return ostr;
}
inline std::basic_ostream<char, std::char_traits<char> >& red(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::BrightRed);
2014-03-03 22:07:58 +00:00
return ostr;
}
inline std::basic_ostream<char, std::char_traits<char> >& green(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::BrightGreen);
2014-03-03 22:07:58 +00:00
return ostr;
}
inline std::basic_ostream<char, std::char_traits<char> >& magenta(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::BrightMagenta);
2014-03-03 22:07:58 +00:00
return ostr;
}
inline std::basic_ostream<char, std::char_traits<char> >& yellow(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::BrightYellow);
2014-03-03 22:07:58 +00:00
return ostr;
}
inline std::basic_ostream<char, std::char_traits<char> >& normal(std::basic_ostream<char, std::char_traits<char> >& ostr)
{
2015-07-15 12:23:00 +00:00
setTextColor(Color::Default);
2014-03-03 22:07:58 +00:00
return ostr;
}
}
2015-07-15 12:23:00 +00:00
#define LOG_ERROR(msg) std::cout << concolor::red << msg << concolor::normal << std::endl
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message) do{if(!(expr)) {std::cout << concolor::red << message << concolor::normal << std::endl; return fail_ret_val;};}while(0)
#define CHECK_AND_NO_ASSERT_MES(expr, fail_ret_val, message) do{if(!(expr)) {std::cout << concolor::red << message << concolor::normal << std::endl; return fail_ret_val;};}while(0)
2014-03-03 22:07:58 +00:00
struct callback_entry
{
std::string callback_name;
2015-07-30 15:22:07 +00:00
//BEGIN_SERIALIZE_OBJECT()
// FIELD(callback_name)
//END_SERIALIZE()
2014-03-03 22:07:58 +00:00
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/)
{
ar & callback_name;
}
};
template<typename T>
struct serialized_object
{
serialized_object() { }
2015-07-30 15:22:07 +00:00
serialized_object(const CryptoNote::BinaryArray& a_data)
2014-03-03 22:07:58 +00:00
: data(a_data)
{
}
2015-07-30 15:22:07 +00:00
CryptoNote::BinaryArray data;
//BEGIN_SERIALIZE_OBJECT()
// FIELD(data)
// END_SERIALIZE()
2014-03-03 22:07:58 +00:00
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/)
{
ar & data;
}
};
2015-05-27 12:08:46 +00:00
typedef serialized_object<CryptoNote::Block> serialized_block;
typedef serialized_object<CryptoNote::Transaction> serialized_transaction;
2014-03-03 22:07:58 +00:00
struct event_visitor_settings
{
int valid_mask;
bool txs_keeped_by_block;
enum settings
{
set_txs_keeped_by_block = 1 << 0
};
event_visitor_settings(int a_valid_mask = 0, bool a_txs_keeped_by_block = false)
: valid_mask(a_valid_mask)
, txs_keeped_by_block(a_txs_keeped_by_block)
{
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*version*/)
{
ar & valid_mask;
ar & txs_keeped_by_block;
}
};
2015-07-30 15:22:07 +00:00
//VARIANT_TAG(binary_archive, callback_entry, 0xcb);
//VARIANT_TAG(binary_archive, CryptoNote::account_base, 0xcc);
//VARIANT_TAG(binary_archive, serialized_block, 0xcd);
//VARIANT_TAG(binary_archive, serialized_transaction, 0xce);
//VARIANT_TAG(binary_archive, event_visitor_settings, 0xcf);
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
typedef boost::variant<CryptoNote::Block, CryptoNote::Transaction, CryptoNote::AccountBase, callback_entry, serialized_block, serialized_transaction, event_visitor_settings> test_event_entry;
typedef std::unordered_map<Crypto::Hash, const CryptoNote::Transaction*> map_hash2tx_t;
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
class test_chain_unit_base: boost::noncopyable
2014-03-03 22:07:58 +00:00
{
public:
2014-08-13 10:51:37 +00:00
test_chain_unit_base() :
2015-05-27 12:08:46 +00:00
m_currency(CryptoNote::CurrencyBuilder(m_logger).currency()) {
2014-08-13 10:51:37 +00:00
}
2015-05-27 12:08:46 +00:00
typedef std::function<bool (CryptoNote::core& c, size_t ev_index, const std::vector<test_event_entry> &events)> verify_callback;
2014-03-03 22:07:58 +00:00
typedef std::map<std::string, verify_callback> callbacks_map;
2015-05-27 12:08:46 +00:00
const CryptoNote::Currency& currency() const;
2014-03-03 22:07:58 +00:00
void register_callback(const std::string& cb_name, verify_callback cb);
2015-05-27 12:08:46 +00:00
bool verify(const std::string& cb_name, CryptoNote::core& c, size_t ev_index, const std::vector<test_event_entry> &events);
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
protected:
2015-05-27 12:08:46 +00:00
mutable Logging::ConsoleLogger m_logger;
CryptoNote::Currency m_currency;
2014-03-03 22:07:58 +00:00
private:
2014-08-13 10:51:37 +00:00
callbacks_map m_callbacks;
2014-03-03 22:07:58 +00:00
};
2015-05-27 12:08:46 +00:00
bool construct_tx_to_key(Logging::ILogger& logger, const std::vector<test_event_entry>& events, CryptoNote::Transaction& tx,
2015-07-30 15:22:07 +00:00
const CryptoNote::Block& blk_head, const CryptoNote::AccountBase& from, const CryptoNote::AccountBase& to,
2014-03-03 22:07:58 +00:00
uint64_t amount, uint64_t fee, size_t nmix);
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction construct_tx_with_fee(Logging::ILogger& logger, std::vector<test_event_entry>& events, const CryptoNote::Block& blk_head,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& acc_from, const CryptoNote::AccountBase& acc_to,
2014-03-03 22:07:58 +00:00
uint64_t amount, uint64_t fee);
2015-05-27 12:08:46 +00:00
void get_confirmed_txs(const std::vector<CryptoNote::Block>& blockchain, const map_hash2tx_t& mtx, map_hash2tx_t& confirmed_txs);
2015-07-30 15:22:07 +00:00
bool find_block_chain(const std::vector<test_event_entry>& events, std::vector<CryptoNote::Block>& blockchain, map_hash2tx_t& mtx, const Crypto::Hash& head);
2015-05-27 12:08:46 +00:00
void fill_tx_sources_and_destinations(const std::vector<test_event_entry>& events, const CryptoNote::Block& blk_head,
2015-07-30 15:22:07 +00:00
const CryptoNote::AccountBase& from, const CryptoNote::AccountBase& to,
2014-03-03 22:07:58 +00:00
uint64_t amount, uint64_t fee, size_t nmix,
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::TransactionSourceEntry>& sources,
std::vector<CryptoNote::TransactionDestinationEntry>& destinations);
uint64_t get_balance(const CryptoNote::AccountBase& addr, const std::vector<CryptoNote::Block>& blockchain, const map_hash2tx_t& mtx);
2014-03-03 22:07:58 +00:00
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
auto do_check_tx_verification_context(const CryptoNote::tx_verification_context& tvc, bool tx_added, size_t event_index, const CryptoNote::Transaction& tx, t_test_class& validator, int)
2014-03-03 22:07:58 +00:00
-> decltype(validator.check_tx_verification_context(tvc, tx_added, event_index, tx))
{
return validator.check_tx_verification_context(tvc, tx_added, event_index, tx);
}
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
bool do_check_tx_verification_context(const CryptoNote::tx_verification_context& tvc, bool tx_added, size_t /*event_index*/, const CryptoNote::Transaction& /*tx*/, t_test_class&, long)
2014-03-03 22:07:58 +00:00
{
// Default block verification context check
if (tvc.m_verifivation_failed)
throw std::runtime_error("Transaction verification failed");
return true;
}
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
bool check_tx_verification_context(const CryptoNote::tx_verification_context& tvc, bool tx_added, size_t event_index, const CryptoNote::Transaction& tx, t_test_class& validator)
2014-03-03 22:07:58 +00:00
{
// SFINAE in action
return do_check_tx_verification_context(tvc, tx_added, event_index, tx, validator, 0);
}
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
auto do_check_block_verification_context(const CryptoNote::block_verification_context& bvc, size_t event_index, const CryptoNote::Block& blk, t_test_class& validator, int)
2014-03-03 22:07:58 +00:00
-> decltype(validator.check_block_verification_context(bvc, event_index, blk))
{
return validator.check_block_verification_context(bvc, event_index, blk);
}
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
bool do_check_block_verification_context(const CryptoNote::block_verification_context& bvc, size_t /*event_index*/, const CryptoNote::Block& /*blk*/, t_test_class&, long)
2014-03-03 22:07:58 +00:00
{
// Default block verification context check
if (bvc.m_verifivation_failed)
throw std::runtime_error("Block verification failed");
return true;
}
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
bool check_block_verification_context(const CryptoNote::block_verification_context& bvc, size_t event_index, const CryptoNote::Block& blk, t_test_class& validator)
2014-03-03 22:07:58 +00:00
{
// SFINAE in action
return do_check_block_verification_context(bvc, event_index, blk, validator, 0);
}
/************************************************************************/
/* */
/************************************************************************/
template<class t_test_class>
struct push_core_event_visitor: public boost::static_visitor<bool>
{
private:
2015-05-27 12:08:46 +00:00
CryptoNote::core& m_c;
2014-03-03 22:07:58 +00:00
const std::vector<test_event_entry>& m_events;
t_test_class& m_validator;
size_t m_ev_index;
bool m_txs_keeped_by_block;
public:
2015-05-27 12:08:46 +00:00
push_core_event_visitor(CryptoNote::core& c, const std::vector<test_event_entry>& events, t_test_class& validator)
2014-03-03 22:07:58 +00:00
: m_c(c)
, m_events(events)
, m_validator(validator)
, m_ev_index(0)
, m_txs_keeped_by_block(false)
{
}
void event_index(size_t ev_index)
{
m_ev_index = ev_index;
}
bool operator()(const event_visitor_settings& settings)
{
log_event("event_visitor_settings");
if (settings.valid_mask & event_visitor_settings::set_txs_keeped_by_block)
{
m_txs_keeped_by_block = settings.txs_keeped_by_block;
}
return true;
}
2015-05-27 12:08:46 +00:00
bool operator()(const CryptoNote::Transaction& tx) const
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
log_event("CryptoNote::Transaction");
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
CryptoNote::tx_verification_context tvc = boost::value_initialized<decltype(tvc)>();
2014-03-03 22:07:58 +00:00
size_t pool_size = m_c.get_pool_transactions_count();
2015-07-30 15:22:07 +00:00
m_c.handle_incoming_tx(toBinaryArray(tx), tvc, m_txs_keeped_by_block);
2014-03-03 22:07:58 +00:00
bool tx_added = pool_size + 1 == m_c.get_pool_transactions_count();
bool r = check_tx_verification_context(tvc, tx_added, m_ev_index, tx, m_validator);
CHECK_AND_NO_ASSERT_MES(r, false, "tx verification context check failed");
return true;
}
2015-05-27 12:08:46 +00:00
bool operator()(const CryptoNote::Block& b) const
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
log_event("CryptoNote::Block");
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
CryptoNote::block_verification_context bvc = boost::value_initialized<decltype(bvc)>();
2015-07-30 15:22:07 +00:00
m_c.handle_incoming_block_blob(toBinaryArray(b), bvc, false, false);
2014-03-03 22:07:58 +00:00
bool r = check_block_verification_context(bvc, m_ev_index, b, m_validator);
CHECK_AND_NO_ASSERT_MES(r, false, "block verification context check failed");
return r;
}
bool operator()(const callback_entry& cb) const
{
log_event(std::string("callback_entry ") + cb.callback_name);
return m_validator.verify(cb.callback_name, m_c, m_ev_index, m_events);
}
2015-07-30 15:22:07 +00:00
bool operator()(const CryptoNote::AccountBase& ab) const
2014-03-03 22:07:58 +00:00
{
2015-05-27 12:08:46 +00:00
log_event("CryptoNote::account_base");
2014-03-03 22:07:58 +00:00
return true;
}
bool operator()(const serialized_block& sr_block) const
{
log_event("serialized_block");
2015-05-27 12:08:46 +00:00
CryptoNote::block_verification_context bvc = boost::value_initialized<decltype(bvc)>();
2014-08-13 10:51:37 +00:00
m_c.handle_incoming_block_blob(sr_block.data, bvc, false, false);
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
CryptoNote::Block blk;
2015-07-30 15:22:07 +00:00
if (!CryptoNote::fromBinaryArray(blk, sr_block.data)) {
2015-05-27 12:08:46 +00:00
blk = CryptoNote::Block();
2014-03-03 22:07:58 +00:00
}
2015-07-30 15:22:07 +00:00
2014-03-03 22:07:58 +00:00
bool r = check_block_verification_context(bvc, m_ev_index, blk, m_validator);
CHECK_AND_NO_ASSERT_MES(r, false, "block verification context check failed");
return true;
}
bool operator()(const serialized_transaction& sr_tx) const
{
log_event("serialized_transaction");
2015-05-27 12:08:46 +00:00
CryptoNote::tx_verification_context tvc = boost::value_initialized<decltype(tvc)>();;
2014-03-03 22:07:58 +00:00
size_t pool_size = m_c.get_pool_transactions_count();
m_c.handle_incoming_tx(sr_tx.data, tvc, m_txs_keeped_by_block);
bool tx_added = pool_size + 1 == m_c.get_pool_transactions_count();
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction tx;
2015-07-30 15:22:07 +00:00
if (!CryptoNote::fromBinaryArray(tx, sr_tx.data)) {
2015-05-27 12:08:46 +00:00
tx = CryptoNote::Transaction();
2014-03-03 22:07:58 +00:00
}
bool r = check_tx_verification_context(tvc, tx_added, m_ev_index, tx, m_validator);
CHECK_AND_NO_ASSERT_MES(r, false, "transaction verification context check failed");
return true;
}
private:
void log_event(const std::string& event_type) const
{
std::cout << concolor::yellow << "=== EVENT # " << m_ev_index << ": " << event_type << concolor::normal << std::endl;
}
};
//--------------------------------------------------------------------------
template<class t_test_class>
2015-05-27 12:08:46 +00:00
inline bool replay_events_through_core(CryptoNote::core& cr, const std::vector<test_event_entry>& events, t_test_class& validator)
2014-03-03 22:07:58 +00:00
{
2015-07-15 12:23:00 +00:00
try {
CHECK_AND_ASSERT_MES(typeid(CryptoNote::Block) == events[0].type(), false, "First event must be genesis block creation");
cr.set_genesis_block(boost::get<CryptoNote::Block>(events[0]));
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
bool r = true;
push_core_event_visitor<t_test_class> visitor(cr, events, validator);
for (size_t i = 1; i < events.size() && r; ++i)
{
visitor.event_index(i);
r = boost::apply_visitor(visitor, events[i]);
}
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
return r;
} catch (std::exception& e) {
std::cout << "replay_events_through_core: " << e.what();
return false;
2014-03-03 22:07:58 +00:00
}
}
//--------------------------------------------------------------------------
template<class t_test_class>
2014-08-13 10:51:37 +00:00
inline bool do_replay_events(std::vector<test_event_entry>& events, t_test_class& validator)
2014-03-03 22:07:58 +00:00
{
boost::program_options::options_description desc("Allowed options");
2015-05-27 12:08:46 +00:00
CryptoNote::CoreConfig::initOptions(desc);
2014-03-03 22:07:58 +00:00
command_line::add_arg(desc, command_line::arg_data_dir);
boost::program_options::variables_map vm;
bool r = command_line::handle_error_helper(desc, [&]()
{
boost::program_options::store(boost::program_options::basic_parsed_options<char>(&desc), vm);
boost::program_options::notify(vm);
return true;
});
if (!r)
return false;
2015-05-27 12:08:46 +00:00
Logging::ConsoleLogger logger;
CryptoNote::CoreConfig coreConfig;
coreConfig.init(vm);
2015-05-27 12:08:46 +00:00
CryptoNote::MinerConfig emptyMinerConfig;
CryptoNote::cryptonote_protocol_stub pr; //TODO: stub only for this kind of test, make real validation of relayed objects
CryptoNote::core c(validator.currency(), &pr, logger);
if (!c.init(coreConfig, emptyMinerConfig, false))
2014-03-03 22:07:58 +00:00
{
std::cout << concolor::magenta << "Failed to init core" << concolor::normal << std::endl;
return false;
}
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
return replay_events_through_core<t_test_class>(c, events, validator);
}
//--------------------------------------------------------------------------
template<class t_test_class>
inline bool do_replay_file(const std::string& filename)
{
std::vector<test_event_entry> events;
2015-07-30 15:22:07 +00:00
if (!Tools::unserialize_obj_from_file(events, filename))
2014-03-03 22:07:58 +00:00
{
std::cout << concolor::magenta << "Failed to deserialize data from file: " << filename << concolor::normal << std::endl;
return false;
}
2014-08-13 10:51:37 +00:00
t_test_class validator;
return do_replay_events<t_test_class>(events, validator);
2014-03-03 22:07:58 +00:00
}
//--------------------------------------------------------------------------
#define GENERATE_ACCOUNT(account) \
2015-07-30 15:22:07 +00:00
CryptoNote::AccountBase account; \
2014-03-03 22:07:58 +00:00
account.generate();
#define MAKE_ACCOUNT(VEC_EVENTS, account) \
2015-07-30 15:22:07 +00:00
CryptoNote::AccountBase account; \
2014-03-03 22:07:58 +00:00
account.generate(); \
VEC_EVENTS.push_back(account);
#define DO_CALLBACK(VEC_EVENTS, CB_NAME) \
{ \
callback_entry CALLBACK_ENTRY; \
CALLBACK_ENTRY.callback_name = CB_NAME; \
VEC_EVENTS.push_back(CALLBACK_ENTRY); \
}
#define REGISTER_CALLBACK(CB_NAME, CLBACK) \
2014-08-13 10:51:37 +00:00
register_callback(CB_NAME, std::bind(&CLBACK, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
2014-03-03 22:07:58 +00:00
#define REGISTER_CALLBACK_METHOD(CLASS, METHOD) \
2014-08-13 10:51:37 +00:00
register_callback(#METHOD, std::bind(&CLASS::METHOD, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
2014-03-03 22:07:58 +00:00
#define MAKE_GENESIS_BLOCK(VEC_EVENTS, BLK_NAME, MINER_ACC, TS) \
2014-08-13 10:51:37 +00:00
test_generator generator(this->m_currency); \
2015-05-27 12:08:46 +00:00
CryptoNote::Block BLK_NAME; \
2014-08-13 10:51:37 +00:00
generator.constructBlock(BLK_NAME, MINER_ACC, TS); \
2014-03-03 22:07:58 +00:00
VEC_EVENTS.push_back(BLK_NAME);
#define MAKE_NEXT_BLOCK(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC) \
2015-05-27 12:08:46 +00:00
CryptoNote::Block BLK_NAME; \
2014-08-13 10:51:37 +00:00
generator.constructBlock(BLK_NAME, PREV_BLOCK, MINER_ACC); \
2014-03-03 22:07:58 +00:00
VEC_EVENTS.push_back(BLK_NAME);
#define MAKE_NEXT_BLOCK_TX1(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TX1) \
2015-05-27 12:08:46 +00:00
CryptoNote::Block BLK_NAME; \
2014-03-03 22:07:58 +00:00
{ \
2015-05-27 12:08:46 +00:00
std::list<CryptoNote::Transaction> tx_list; \
2014-03-03 22:07:58 +00:00
tx_list.push_back(TX1); \
2014-08-13 10:51:37 +00:00
generator.constructBlock(BLK_NAME, PREV_BLOCK, MINER_ACC, tx_list); \
2014-03-03 22:07:58 +00:00
} \
VEC_EVENTS.push_back(BLK_NAME);
#define MAKE_NEXT_BLOCK_TX_LIST(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TXLIST) \
2015-05-27 12:08:46 +00:00
CryptoNote::Block BLK_NAME; \
2014-08-13 10:51:37 +00:00
generator.constructBlock(BLK_NAME, PREV_BLOCK, MINER_ACC, TXLIST); \
2014-03-03 22:07:58 +00:00
VEC_EVENTS.push_back(BLK_NAME);
#define REWIND_BLOCKS_N(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, COUNT) \
2015-05-27 12:08:46 +00:00
CryptoNote::Block BLK_NAME; \
2014-03-03 22:07:58 +00:00
{ \
2015-05-27 12:08:46 +00:00
CryptoNote::Block blk_last = PREV_BLOCK; \
2014-03-03 22:07:58 +00:00
for (size_t i = 0; i < COUNT; ++i) \
{ \
MAKE_NEXT_BLOCK(VEC_EVENTS, blk, blk_last, MINER_ACC); \
blk_last = blk; \
} \
BLK_NAME = blk_last; \
}
2014-08-13 10:51:37 +00:00
#define REWIND_BLOCKS(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC) \
REWIND_BLOCKS_N(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, this->m_currency.minedMoneyUnlockWindow())
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
#define MAKE_TX_MIX(VEC_EVENTS, TX_NAME, FROM, TO, AMOUNT, NMIX, HEAD) \
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction TX_NAME; \
construct_tx_to_key(this->m_logger, VEC_EVENTS, TX_NAME, HEAD, FROM, TO, AMOUNT, this->m_currency.minimumFee(), NMIX); \
2014-03-03 22:07:58 +00:00
VEC_EVENTS.push_back(TX_NAME);
#define MAKE_TX(VEC_EVENTS, TX_NAME, FROM, TO, AMOUNT, HEAD) MAKE_TX_MIX(VEC_EVENTS, TX_NAME, FROM, TO, AMOUNT, 0, HEAD)
2014-08-13 10:51:37 +00:00
#define MAKE_TX_MIX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, NMIX, HEAD) \
{ \
2015-05-27 12:08:46 +00:00
CryptoNote::Transaction t; \
construct_tx_to_key(this->m_logger, VEC_EVENTS, t, HEAD, FROM, TO, AMOUNT, this->m_currency.minimumFee(), NMIX); \
2014-08-13 10:51:37 +00:00
SET_NAME.push_back(t); \
VEC_EVENTS.push_back(t); \
2014-03-03 22:07:58 +00:00
}
#define MAKE_TX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD) MAKE_TX_MIX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, 0, HEAD)
#define MAKE_TX_LIST_START(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD) \
2015-05-27 12:08:46 +00:00
std::list<CryptoNote::Transaction> SET_NAME; \
2014-03-03 22:07:58 +00:00
MAKE_TX_LIST(VEC_EVENTS, SET_NAME, FROM, TO, AMOUNT, HEAD);
2014-08-13 10:51:37 +00:00
#define MAKE_MINER_TX_AND_KEY_MANUALLY(TX, BLK, KEY) \
Transaction TX; \
if (!constructMinerTxManually(this->m_currency, get_block_height(BLK) + 1, generator.getAlreadyGeneratedCoins(BLK), \
2015-07-30 15:22:07 +00:00
miner_account.getAccountKeys().address, TX, 0, KEY)) \
2014-03-03 22:07:58 +00:00
return false;
#define MAKE_MINER_TX_MANUALLY(TX, BLK) MAKE_MINER_TX_AND_KEY_MANUALLY(TX, BLK, 0)
#define SET_EVENT_VISITOR_SETT(VEC_EVENTS, SETT, VAL) VEC_EVENTS.push_back(event_visitor_settings(SETT, VAL));
#define GENERATE(filename, genclass) \
{ \
std::vector<test_event_entry> events; \
genclass g; \
g.generate(events); \
2015-07-30 15:22:07 +00:00
if (!Tools::serialize_obj_to_file(events, filename)) \
2014-03-03 22:07:58 +00:00
{ \
std::cout << concolor::magenta << "Failed to serialize data to file: " << filename << concolor::normal << std::endl; \
throw std::runtime_error("Failed to serialize data to file"); \
} \
}
#define PLAY(filename, genclass) \
if(!do_replay_file<genclass>(filename)) \
{ \
std::cout << concolor::magenta << "Failed to pass test : " << #genclass << concolor::normal << std::endl; \
return 1; \
}
#define GENERATE_AND_PLAY(genclass) \
{ \
std::vector<test_event_entry> events; \
++tests_count; \
bool generated = false; \
try \
{ \
genclass g; \
generated = g.generate(events);; \
} \
catch (const std::exception& ex) \
{ \
2015-07-15 12:23:00 +00:00
std::cout << #genclass << " generation failed: what=" << ex.what(); \
2014-03-03 22:07:58 +00:00
} \
catch (...) \
{ \
2015-07-15 12:23:00 +00:00
std::cout << #genclass << " generation failed: generic exception"; \
2014-03-03 22:07:58 +00:00
} \
2014-08-13 10:51:37 +00:00
genclass validator; \
if (generated && do_replay_events< genclass >(events, validator)) \
2014-03-03 22:07:58 +00:00
{ \
std::cout << concolor::green << "#TEST# Succeeded " << #genclass << concolor::normal << '\n'; \
} \
else \
{ \
std::cout << concolor::magenta << "#TEST# Failed " << #genclass << concolor::normal << '\n'; \
failed_tests.push_back(#genclass); \
} \
std::cout << std::endl; \
}
2014-08-13 10:51:37 +00:00
template <typename GenClassT>
bool GenerateAndPlay(const char* testname, GenClassT&& g) {
std::vector<test_event_entry> events;
bool generated = false;
try {
generated = g.generate(events);
} catch (const std::exception& ex) {
2015-07-15 12:23:00 +00:00
std::cout << testname << " generation failed: what=" << ex.what();
2014-08-13 10:51:37 +00:00
} catch (...) {
2015-07-15 12:23:00 +00:00
std::cout << testname << " generation failed: generic exception";
2014-08-13 10:51:37 +00:00
}
bool succeeded = generated && do_replay_events<GenClassT>(events, g);
if (succeeded) {
std::cout << concolor::green << "#TEST# Succeeded " << testname << concolor::normal << '\n';
} else {
std::cout << concolor::magenta << "#TEST# Failed " << testname << concolor::normal << '\n';
}
std::cout << std::endl;
return succeeded;
}
#define GENERATE_AND_PLAY_EX(genclass) { ++tests_count; if (!GenerateAndPlay(#genclass, genclass)) failed_tests.push_back(#genclass); }
2014-03-03 22:07:58 +00:00
#define CALL_TEST(test_name, function) \
{ \
if(!function()) \
{ \
std::cout << concolor::magenta << "#TEST# Failed " << test_name << concolor::normal << std::endl; \
return 1; \
} \
else \
{ \
std::cout << concolor::green << "#TEST# Succeeded " << test_name << concolor::normal << std::endl; \
} \
}
template<uint64_t N>
struct Pow10 {
static const uint64_t value = 10 * Pow10<N - 1>::value;
};
template<>
struct Pow10<0> {
static const uint64_t value = 1;
};
2015-05-27 12:08:46 +00:00
const uint64_t COIN = Pow10<CryptoNote::parameters::CRYPTONOTE_DISPLAY_DECIMAL_POINT>::value;
2014-03-03 22:07:58 +00:00
#define QUOTEME(x) #x
#define DEFINE_TESTS_ERROR_CONTEXT(text) const char* perr_context = text;
#define CHECK_TEST_CONDITION(cond) CHECK_AND_ASSERT_MES(cond, false, "[" << perr_context << "] failed: \"" << QUOTEME(cond) << "\"")
#define CHECK_EQ(v1, v2) CHECK_AND_ASSERT_MES(v1 == v2, false, "[" << perr_context << "] failed: \"" << QUOTEME(v1) << " == " << QUOTEME(v2) << "\", " << v1 << " != " << v2)
#define CHECK_NOT_EQ(v1, v2) CHECK_AND_ASSERT_MES(!(v1 == v2), false, "[" << perr_context << "] failed: \"" << QUOTEME(v1) << " != " << QUOTEME(v2) << "\", " << v1 << " == " << v2)
#define MK_COINS(amount) (UINT64_C(amount) * COIN)