diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 0a8c2bd2..fdbb1f48 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -2327,6 +2327,53 @@ bool shuffle_source_entries(std::vector& sources) return true; } +bool replace_coinbase_in_genesis_block(const std::vector& destinations, test_generator& generator, std::vector& events, currency::block& genesis_block) +{ + bool r = false; + generator.remove_block_info(genesis_block); + events.pop_back(); + + // remember premine amount + uint64_t premine_amount = get_outs_money_amount(genesis_block.miner_tx); + + // replace tx key + keypair tx_key = keypair::generate(); + for(auto& el : genesis_block.miner_tx.extra) + { + if (el.type() == typeid(crypto::public_key)) + { + boost::get(el) = tx_key.pub; + break; + } + } + uint64_t total_amount = 0; + + // replace outputs + genesis_block.miner_tx.vout.clear(); + + for(size_t output_index = 0; output_index < destinations.size() + 1; ++output_index) + { + uint64_t amount = output_index < destinations.size() ? destinations[output_index].amount : premine_amount - total_amount; + const account_public_address& addr = output_index < destinations.size() ? destinations[output_index].addr.back() : destinations.back().addr.back(); + + crypto::key_derivation derivation{}; + bool r = crypto::generate_key_derivation(addr.view_public_key, tx_key.sec, derivation); + CHECK_AND_ASSERT_MES(r, false, "generate_key_derivation failed"); + + txout_to_key target{}; + r = crypto::derive_public_key(derivation, output_index, addr.spend_public_key, target.key); + CHECK_AND_ASSERT_MES(r, false, "derive_public_key failed"); + genesis_block.miner_tx.vout.emplace_back(tx_out_bare{amount, target}); + total_amount += amount; + CHECK_AND_ASSERT_MES(total_amount <= premine_amount, false, "total amount is greater than premine amount"); + } + + events.push_back(genesis_block); + std::vector block_sizes; + generator.add_block(genesis_block, 0, block_sizes, 0, 0, std::list{}, null_hash); + return true; +} + //------------------------------------------------------------------------------ test_chain_unit_base::test_chain_unit_base() diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index 61e0d4aa..2af9bcfe 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -745,6 +745,9 @@ bool check_mixin_value_for_each_input(size_t mixin, const crypto::hash& tx_id, c bool shuffle_source_entry(currency::tx_source_entry& se); bool shuffle_source_entries(std::vector& sources); +// one output will be created for each destination entry and one additional output to add up to old coinbase total amount +bool replace_coinbase_in_genesis_block(const std::vector& destinations, test_generator& generator, std::vector& events, currency::block& genesis_block); + //-------------------------------------------------------------------------- template auto do_check_tx_verification_context(const currency::tx_verification_context& tvc, bool tx_added, size_t event_index, const currency::transaction& tx, t_test_class& validator, int)