forked from lthn/blockchain
1) on_test_generator_created() 2) zarcanum_pos_block_math test added 3) init_output_indices() adapted -- WIP
This commit is contained in:
parent
4db7ac1668
commit
5f9aea7a22
6 changed files with 169 additions and 74 deletions
|
|
@ -309,7 +309,7 @@ namespace currency
|
|||
crypto::hash get_multisig_out_id(const transaction& tx, size_t n);
|
||||
bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::key_derivation& derivation, size_t output_index);
|
||||
bool is_out_to_acc(const account_keys& acc, const txout_multisig& out_multisig, const crypto::key_derivation& derivation, size_t output_index);
|
||||
bool is_out_to_acc(const account_keys& acc, const tx_out_zarcanum& zo, const crypto::key_derivation& derivation, size_t output_index, uint64_t& decoded_amount);
|
||||
bool is_out_to_acc(const account_keys& acc, const tx_out_zarcanum& zo, const crypto::key_derivation& derivation, size_t output_index, uint64_t& decoded_amount, crypto::scalar_t& blinding_mask);
|
||||
bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector<wallet_out_info>& outs, uint64_t& money_transfered, crypto::key_derivation& derivation);
|
||||
bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector<wallet_out_info>& outs, uint64_t& money_transfered, crypto::key_derivation& derivation, std::list<htlc_info>& htlc_info_list);
|
||||
bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<wallet_out_info>& outs, uint64_t& money_transfered, crypto::key_derivation& derivation);
|
||||
|
|
|
|||
|
|
@ -927,42 +927,44 @@ bool test_generator::construct_pow_block_with_alias_info_in_coinbase(const accou
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
struct output_index {
|
||||
const currency::txout_target_v out;
|
||||
uint64_t amount;
|
||||
size_t blk_height; // block height
|
||||
size_t tx_no; // index of transaction in block
|
||||
size_t out_no; // index of out in transaction
|
||||
size_t idx;
|
||||
bool spent;
|
||||
const currency::block *p_blk;
|
||||
const currency::transaction *p_tx;
|
||||
struct output_index
|
||||
{
|
||||
const currency::txout_target_v out;
|
||||
uint64_t amount;
|
||||
size_t blk_height; // block height
|
||||
size_t tx_no; // index of transaction in block
|
||||
size_t out_no; // index of out in transaction
|
||||
size_t idx;
|
||||
bool spent;
|
||||
const currency::block *p_blk;
|
||||
const currency::transaction *p_tx;
|
||||
|
||||
output_index(const currency::txout_target_v &_out, uint64_t _a, size_t _h, size_t tno, size_t ono, const currency::block *_pb, const currency::transaction *_pt)
|
||||
: out(_out), amount(_a), blk_height(_h), tx_no(tno), out_no(ono), idx(0), spent(false), p_blk(_pb), p_tx(_pt) { }
|
||||
output_index(const currency::txout_target_v &_out, uint64_t _a, size_t _h, size_t tno, size_t ono, const currency::block *_pb, const currency::transaction *_pt)
|
||||
: out(_out), amount(_a), blk_height(_h), tx_no(tno), out_no(ono), idx(0), spent(false), p_blk(_pb), p_tx(_pt)
|
||||
{}
|
||||
|
||||
output_index(const output_index &other)
|
||||
: out(other.out), amount(other.amount), blk_height(other.blk_height), tx_no(other.tx_no), out_no(other.out_no), idx(other.idx), spent(other.spent), p_blk(other.p_blk), p_tx(other.p_tx) { }
|
||||
output_index(const output_index &other)
|
||||
: out(other.out), amount(other.amount), blk_height(other.blk_height), tx_no(other.tx_no), out_no(other.out_no), idx(other.idx), spent(other.spent), p_blk(other.p_blk), p_tx(other.p_tx)
|
||||
{}
|
||||
|
||||
const std::string toString() const {
|
||||
std::stringstream ss;
|
||||
const std::string to_string() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "output_index{blk_height=" << blk_height
|
||||
<< " tx_no=" << tx_no
|
||||
<< " out_no=" << out_no
|
||||
<< " amount=" << amount
|
||||
<< " idx=" << idx
|
||||
<< " spent=" << spent
|
||||
<< "}";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
ss << "output_index{blk_height=" << blk_height
|
||||
<< " tx_no=" << tx_no
|
||||
<< " out_no=" << out_no
|
||||
<< " amount=" << amount
|
||||
<< " idx=" << idx
|
||||
<< " spent=" << spent
|
||||
<< "}";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
output_index& operator=(const output_index& other)
|
||||
{
|
||||
new(this) output_index(other);
|
||||
return *this;
|
||||
}
|
||||
output_index& operator=(const output_index& other)
|
||||
{
|
||||
new(this) output_index(other);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::map<uint64_t, std::vector<size_t> > map_output_t; // amount -> [N -> global out index]
|
||||
|
|
@ -985,9 +987,11 @@ namespace
|
|||
|
||||
bool init_output_indices(map_output_idx_t& outs, map_output_t& outs_mine, const std::vector<currency::block>& blockchain, const map_hash2tx_t& mtx, const currency::account_keys& acc_keys)
|
||||
{
|
||||
bool r = false;
|
||||
|
||||
for (const block& blk : blockchain)
|
||||
{
|
||||
|
||||
uint64_t height = get_block_height(blk);
|
||||
std::vector<const transaction*> vtx;
|
||||
vtx.push_back(&blk.miner_tx);
|
||||
|
||||
|
|
@ -1007,18 +1011,35 @@ bool init_output_indices(map_output_idx_t& outs, map_output_t& outs_mine, const
|
|||
|
||||
for (size_t j = 0; j < tx.vout.size(); ++j)
|
||||
{
|
||||
const tx_out_bare &out = boost::get<tx_out_bare>(tx.vout[j]);
|
||||
output_index oi(out.target, out.amount, boost::get<txin_gen>(*blk.miner_tx.vin.begin()).height, i, j, &blk, vtx[i]);
|
||||
VARIANT_SWITCH_BEGIN(tx.vout[j])
|
||||
VARIANT_CASE_CONST(tx_out_bare, out)
|
||||
if (out.target.type() == typeid(txout_to_key))
|
||||
{
|
||||
std::vector<output_index>& outs_vec = outs[out.amount];
|
||||
size_t out_global_idx = outs_vec.size();
|
||||
output_index oi(out.target, out.amount, height, i, j, &blk, vtx[i]);
|
||||
oi.idx = out_global_idx;
|
||||
outs_vec.emplace_back(std::move(oi));
|
||||
// Is out to me?
|
||||
if (is_out_to_acc(acc_keys, boost::get<txout_to_key>(out.target), derivation, j))
|
||||
outs_mine[out.amount].push_back(out_global_idx);
|
||||
}
|
||||
VARIANT_CASE_CONST(tx_out_zarcanum, out)
|
||||
std::vector<output_index>& outs_vec = outs[0]; // amount = 0 for ZC outs
|
||||
size_t out_global_idx = outs_vec.size();
|
||||
|
||||
if (out.target.type() == typeid(txout_to_key))
|
||||
{
|
||||
outs[out.amount].push_back(oi);
|
||||
size_t tx_global_idx = outs[out.amount].size() - 1;
|
||||
outs[out.amount][tx_global_idx].idx = tx_global_idx;
|
||||
// Is out to me?
|
||||
if (is_out_to_acc(acc_keys, boost::get<txout_to_key>(out.target), derivation, j))
|
||||
outs_mine[out.amount].push_back(tx_global_idx);
|
||||
}
|
||||
output_index oi(currency::txout_target_v(), 0 /* amount */, height, i, j, &blk, vtx[i]);
|
||||
oi.idx = out_global_idx;
|
||||
outs_vec.emplace_back(std::move(oi));
|
||||
|
||||
uint64_t decoded_amount = 0;
|
||||
crypto::scalar_t blinding_mask{};
|
||||
if (is_out_to_acc(acc_keys, out, derivation, j, decoded_amount, blinding_mask))
|
||||
{
|
||||
outs_vec.back().amount = decoded_amount;
|
||||
outs_mine[decoded_amount /* TODO @#@# use 0 here?? */].push_back(out_global_idx);
|
||||
}
|
||||
VARIANT_SWITCH_END()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1202,7 +1223,7 @@ bool fill_tx_sources(std::vector<currency::tx_source_entry>& sources, const std:
|
|||
// Iterate in reverse is more efficiency
|
||||
uint64_t sources_amount = 0;
|
||||
bool sources_found = false;
|
||||
BOOST_REVERSE_FOREACH(const map_output_t::value_type o, outs_mine)
|
||||
for(const map_output_t::value_type o : outs_mine)
|
||||
{
|
||||
for (size_t i = 0; i < o.second.size() && !sources_found; ++i)
|
||||
{
|
||||
|
|
@ -1219,7 +1240,7 @@ bool fill_tx_sources(std::vector<currency::tx_source_entry>& sources, const std:
|
|||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
//interpret as time
|
||||
}
|
||||
}
|
||||
|
|
@ -1507,33 +1528,36 @@ bool construct_tx_with_many_outputs(const currency::hard_forks_descriptor& hf, s
|
|||
return construct_tx(keys_from, sources, destinations, empty_attachment, tx, tx_version, 0);
|
||||
}
|
||||
|
||||
uint64_t get_balance(const currency::account_keys& addr, const std::vector<currency::block>& blockchain, const map_hash2tx_t& mtx, bool dbg_log) {
|
||||
uint64_t res = 0;
|
||||
std::map<uint64_t, std::vector<output_index> > outs;
|
||||
std::map<uint64_t, std::vector<size_t> > outs_mine;
|
||||
uint64_t get_balance(const currency::account_keys& addr, const std::vector<currency::block>& blockchain, const map_hash2tx_t& mtx, bool dbg_log)
|
||||
{
|
||||
uint64_t res = 0;
|
||||
std::map<uint64_t, std::vector<output_index> > outs;
|
||||
std::map<uint64_t, std::vector<size_t> > outs_mine;
|
||||
|
||||
map_hash2tx_t confirmed_txs;
|
||||
get_confirmed_txs(blockchain, mtx, confirmed_txs);
|
||||
map_hash2tx_t confirmed_txs;
|
||||
get_confirmed_txs(blockchain, mtx, confirmed_txs);
|
||||
|
||||
if (!init_output_indices(outs, outs_mine, blockchain, confirmed_txs, addr))
|
||||
return false;
|
||||
if (!init_output_indices(outs, outs_mine, blockchain, confirmed_txs, addr))
|
||||
return false;
|
||||
|
||||
if (!init_spent_output_indices(outs, outs_mine, blockchain, confirmed_txs, addr))
|
||||
return false;
|
||||
if (!init_spent_output_indices(outs, outs_mine, blockchain, confirmed_txs, addr))
|
||||
return false;
|
||||
|
||||
BOOST_FOREACH (const map_output_t::value_type &o, outs_mine) {
|
||||
for (size_t i = 0; i < o.second.size(); ++i) {
|
||||
if (outs[o.first][o.second[i]].spent)
|
||||
continue;
|
||||
for (const map_output_t::value_type &o : outs_mine)
|
||||
{
|
||||
for (size_t i = 0; i < o.second.size(); ++i)
|
||||
{
|
||||
if (outs[o.first][o.second[i]].spent)
|
||||
continue;
|
||||
|
||||
output_index& oiv = outs[o.first][o.second[i]];
|
||||
res += oiv.amount;
|
||||
if (dbg_log)
|
||||
LOG_PRINT_L0(oiv.toString());
|
||||
}
|
||||
output_index& oiv = outs[o.first][o.second[i]];
|
||||
res += oiv.amount;
|
||||
if (dbg_log)
|
||||
LOG_PRINT_L0(oiv.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint64_t get_balance(const currency::account_base& addr, const std::vector<currency::block>& blockchain, const map_hash2tx_t& mtx, bool dbg_log)
|
||||
|
|
@ -2034,6 +2058,11 @@ bool test_chain_unit_base::verify(const std::string& cb_name, currency::core& c,
|
|||
return cb_it->second(c, ev_index, events);
|
||||
}
|
||||
|
||||
void test_chain_unit_base::on_test_generator_created(test_generator& gen) const
|
||||
{
|
||||
gen.set_hardforks(m_hardforks);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
test_chain_unit_enchanced::test_chain_unit_enchanced()
|
||||
|
|
|
|||
|
|
@ -223,6 +223,8 @@ struct test_gentime_settings
|
|||
uint64_t dust_threshold;
|
||||
};
|
||||
|
||||
class test_generator;
|
||||
|
||||
class test_chain_unit_base
|
||||
{
|
||||
public:
|
||||
|
|
@ -236,6 +238,8 @@ public:
|
|||
void set_core_proxy(std::shared_ptr<tools::i_core_proxy>) { /* do nothing */ }
|
||||
uint64_t get_tx_version_from_events(const std::vector<test_event_entry> &events) const;
|
||||
|
||||
void on_test_generator_created(test_generator& generator) const; // tests can override this for special initialization
|
||||
|
||||
private:
|
||||
callbacks_map m_callbacks;
|
||||
|
||||
|
|
@ -1005,6 +1009,7 @@ void append_vector_by_another_vector(U& dst, const V& src)
|
|||
#define MAKE_GENESIS_BLOCK(VEC_EVENTS, BLK_NAME, MINER_ACC, TS) \
|
||||
PRINT_EVENT_N_TEXT(VEC_EVENTS, "MAKE_GENESIS_BLOCK(" << #BLK_NAME << ")"); \
|
||||
test_generator generator; \
|
||||
on_test_generator_created(generator); \
|
||||
currency::block BLK_NAME = AUTO_VAL_INIT(BLK_NAME); \
|
||||
generator.construct_genesis_block(BLK_NAME, MINER_ACC, TS); \
|
||||
VEC_EVENTS.push_back(BLK_NAME)
|
||||
|
|
|
|||
|
|
@ -231,9 +231,8 @@ void pos_block_builder::step5_sign_zarcanum(const crypto::public_key& stake_tx_p
|
|||
crypto::derive_secret_key(pos_coin_derivation, stake_tx_out_index, stakeholder_account.get_keys().spend_secret_key, secret_x); // x = s + Hs(v * 8 * R, i)
|
||||
|
||||
std::vector<crypto::CLSAG_GGXG_input_ref_t> ring;
|
||||
crypto::scalar_t stake_blinding_mask;
|
||||
crypto::scalar_t blinding_masks_sum;
|
||||
uint64_t secret_index = 0;
|
||||
crypto::scalar_t blinding_masks_sum;
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,6 @@ bool zarcanum_basic_test::generate(std::vector<test_event_entry>& events) const
|
|||
|
||||
MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, ts);
|
||||
DO_CALLBACK(events, "configure_core"); // default configure_core callback will initialize core runtime config with m_hardforks
|
||||
set_hard_fork_heights_to_generator(generator);
|
||||
//TODO: Need to make sure REWIND_BLOCKS_N and other coretests codebase are capable of following hardfork4 rules
|
||||
//in this test hardfork4 moment moved to runtime section
|
||||
REWIND_BLOCKS_N(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 3);
|
||||
|
|
@ -226,4 +225,63 @@ bool zarcanum_test_n_inputs_validation::generate(std::vector<test_event_entry>&
|
|||
REWIND_BLOCKS_N(events, blk_16, blk_14, miner_account, 2);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
zarcanum_pos_block_math::zarcanum_pos_block_math()
|
||||
{
|
||||
m_hardforks.set_hardfork_height(ZANO_HARDFORK_04_ZARCANUM, 1);
|
||||
|
||||
REGISTER_CALLBACK_METHOD(zarcanum_pos_block_math, c1);
|
||||
}
|
||||
|
||||
bool zarcanum_pos_block_math::generate(std::vector<test_event_entry>& events) const
|
||||
{
|
||||
// Test idea:
|
||||
|
||||
bool r = false;
|
||||
|
||||
uint64_t ts = test_core_time::get_time();
|
||||
m_accounts.resize(TOTAL_ACCS_COUNT);
|
||||
account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); miner_acc.set_createtime(ts);
|
||||
account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); alice_acc.set_createtime(ts);
|
||||
account_base& bob_acc = m_accounts[BOB_ACC_IDX]; bob_acc.generate(); bob_acc.set_createtime(ts);
|
||||
|
||||
MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, test_core_time::get_time());
|
||||
DO_CALLBACK(events, "configure_core"); // necessary to set m_hardforks
|
||||
REWIND_BLOCKS_N_WITH_TIME(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 3);
|
||||
|
||||
//
|
||||
// before hardfork 1
|
||||
//
|
||||
|
||||
std::vector<tx_source_entry> sources;
|
||||
std::vector<tx_destination_entry> destinations;
|
||||
CHECK_AND_ASSERT_MES(fill_tx_sources_and_destinations(events, blk_0r, miner_acc, alice_acc, MK_TEST_COINS(1), TESTS_DEFAULT_FEE, 0, sources, destinations), false, "");
|
||||
|
||||
std::vector<extra_v> extra;
|
||||
transaction tx_0 = AUTO_VAL_INIT(tx_0);
|
||||
crypto::secret_key tx_sec_key;
|
||||
r = construct_tx(miner_acc.get_keys(), sources, destinations, extra, empty_attachment, tx_0, get_tx_version_from_events(events), tx_sec_key, 0 /* unlock time 1 is zero and thus will not be set */);
|
||||
CHECK_AND_ASSERT_MES(r, false, "construct_tx failed");
|
||||
//DO_CALLBACK(events, "mark_invalid_tx");
|
||||
events.push_back(tx_0);
|
||||
|
||||
MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_0);
|
||||
|
||||
REWIND_BLOCKS_N_WITH_TIME(events, blk_1r, blk_1, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
|
||||
|
||||
DO_CALLBACK(events, "c1");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool zarcanum_pos_block_math::c1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
|
||||
{
|
||||
std::shared_ptr<tools::wallet2> alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX);
|
||||
|
||||
CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", MK_TEST_COINS(1), 0, MK_TEST_COINS(1), 0, 0), false, "");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
// Copyright (c) 2014-2018 Zano Project
|
||||
// Copyright (c) 2014-2022 Zano Project
|
||||
// Copyright (c) 2014-2018 The Louisdor Project
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#pragma once
|
||||
#include "chaingen.h"
|
||||
#include "wallet_tests_basic.h"
|
||||
|
|
@ -21,4 +20,9 @@ struct zarcanum_test_n_inputs_validation : public wallet_test
|
|||
bool generate(std::vector<test_event_entry>& events) const;
|
||||
};
|
||||
|
||||
|
||||
struct zarcanum_pos_block_math : public wallet_test
|
||||
{
|
||||
zarcanum_pos_block_math();
|
||||
bool generate(std::vector<test_event_entry>& events) const;
|
||||
bool zarcanum_pos_block_math::c1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue