From 47d18e5deb3a992d1c8b2b06839fe82660780a4c Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 14 Oct 2022 19:18:24 +0200 Subject: [PATCH] wallet2::prepare_and_sign_pos_block() refactored, vector added --- src/wallet/wallet2.cpp | 47 +++++++++------------- src/wallet/wallet2.h | 3 +- tests/core_tests/chain_switch_pow_pos.cpp | 4 +- tests/core_tests/chaingen.cpp | 49 ++++++++--------------- tests/core_tests/chaingen.h | 11 ++++- tests/core_tests/wallet_tests_basic.cpp | 4 +- 6 files changed, 50 insertions(+), 68 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 468c15e5..75e8368a 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3647,12 +3647,15 @@ bool wallet2::is_in_hardfork_zone(uint64_t hardfork_index) const return m_core_runtime_config.is_hardfork_active_for_height(hardfork_index, get_blockchain_current_size()); } //---------------------------------------------------------------------------------------------------- -bool wallet2::prepare_and_sign_pos_block(currency::block& b, const pos_entry& pe) const +bool wallet2::prepare_and_sign_pos_block(const mining_context& cxt, currency::block& b, const pos_entry& pe) const { + bool r = false; WLT_CHECK_AND_ASSERT_MES(pe.wallet_index < m_transfers.size(), false, "invalid pe.wallet_index: " << pe.wallet_index); - const transaction& source_tx = m_transfers[pe.wallet_index].m_ptx_wallet_info->m_tx; - - if (!is_in_hardfork_zone(ZANO_HARDFORK_04_ZARCANUM)) + const transfer_details& td = m_transfers[pe.wallet_index]; + const transaction& source_tx = td.m_ptx_wallet_info->m_tx; + WLT_CHECK_AND_ASSERT_MES(pe.tx_out_index < source_tx.vout.size(), false, "invalid pe.tx_out_index: " << pe.tx_out_index); + const currency::tx_out_v& stake_out_v = source_tx.vout[pe.tx_out_index]; + if (!cxt.zarcanum) { // old PoS with non-hidden amounts WLT_CHECK_AND_ASSERT_MES(b.miner_tx.vin[0].type() == typeid(currency::txin_gen), false, "Wrong input 0 type in transaction: " << b.miner_tx.vin[0].type().name()); @@ -3687,7 +3690,6 @@ bool wallet2::prepare_and_sign_pos_block(currency::block& b, const pos_entry& pe // get stake output pub key (stealth address) for ring signature generation std::vector keys_ptrs; TRY_ENTRY() - const currency::tx_out_v& stake_out_v = source_tx.vout[pe.tx_out_index]; keys_ptrs.push_back(&boost::get(boost::get(stake_out_v).target).key); CATCH_ENTRY_CUSTOM("wallet2::prepare_and_sign_pos_block", { LOG_PRINT_RED_L0("unable to get output's pub key because of the exception"); }, false); @@ -3884,6 +3886,7 @@ bool wallet2::build_minted_block(const mining_context& cxt, const currency::acco tmpl_req.wallet_address = get_account_address_as_str(miner_address); tmpl_req.stakeholder_address = get_account_address_as_str(m_account.get_public_address()); tmpl_req.pos_block = true; + tmpl_req.extra_text = m_miner_text_info; tmpl_req.pe = AUTO_VAL_INIT(tmpl_req.pe); tmpl_req.pe.amount = td.amount(); @@ -3895,18 +3898,6 @@ bool wallet2::build_minted_block(const mining_context& cxt, const currency::acco tmpl_req.pe.tx_out_index = td.m_internal_output_index; tmpl_req.pe.wallet_index = cxt.index; - //pe.g_index = tmpl_req.pos_g_index = td.m_global_output_index; - //pe.amount = tmpl_req.pos_amount = td.amount();// pe.amount; - //pe.keyimage = td.m_key_image; - //pe.block_timestamp = td.m_ptx_wallet_info->m_block_timestamp; - //pe.stake_unlock_time = tmpl_req.stake_unlock_time = cxt.stake_unlock_time; - //pe.tx_id = tmpl_req.tx_id = td.tx_hash(); - //pe.tx_out_index = tmpl_req.tx_out_index = td.m_internal_output_index; - //pe.wallet_index = cxt.index; - - //tmpl_req.pos_index = pe.index; // gindex <--- this should be removed as soon as pos_entry::index is replaced with tx_id and tx_out_index - // TODO: also fill out tx_id and tx_out_index for mining tx creation - tmpl_req.extra_text = m_miner_text_info; //generate packing tx transaction pack_tx = AUTO_VAL_INIT(pack_tx); if (generate_packing_transaction_if_needed(pack_tx, 0)) @@ -3936,20 +3927,18 @@ bool wallet2::build_minted_block(const mining_context& cxt, const currency::acco set_block_datetime(current_timestamp, b); WLT_LOG_MAGENTA("Applying actual timestamp: " << current_timestamp, LOG_LEVEL_0); - const currency::tx_out_v& stake_out_v = td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index]; - if (cxt.zarcanum && td.is_zc()) - { - // Zarcanum - WLT_CHECK_AND_ASSERT_MES(stake_out_v.type() == typeid(tx_out_zarcanum), false, "unexpected stake output type: " << stake_out_v.type().name() << ", expected: zarcanum"); - - return false; - } - else - { + //const currency::tx_out_v& stake_out_v = td.m_ptx_wallet_info->m_tx.vout[td.m_internal_output_index]; + //if (cxt.zarcanum && td.is_zc()) + //{ + // // Zarcanum + // return false; + //} + //else + //{ // old fashioned non-hidden amount PoS scheme - res = prepare_and_sign_pos_block(b, tmpl_req.pe); + res = prepare_and_sign_pos_block(cxt, b, tmpl_req.pe); WLT_CHECK_AND_ASSERT_MES(res, false, "Failed to prepare_and_sign_pos_block"); - } + //} crypto::hash block_hash = get_block_hash(b); WLT_LOG_GREEN("Block " << print16(block_hash) << " has been constructed, sending to core...", LOG_LEVEL_0); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 52ff03ee..16796983 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -429,6 +429,7 @@ namespace tools crypto::scalar_t last_pow_block_id_hashed; // Zarcanum notation: f' crypto::scalar_t secret_q; // Zarcanum notation: q boost::multiprecision::uint256_t z_l_div_z_D; // Zarcanum notation: z * floor( l / (z * D) ) (max possible value (assuming z=2^64) : z * 2^252 / (z * 1) ~= 2^252) + crypto::hash kernel_hash; // Zarcanum notation: h currency::wide_difficulty_type basic_diff; currency::stake_kernel sk; @@ -839,7 +840,7 @@ namespace tools //next functions in public area only becausce of test_generator //TODO: Need refactoring - remove it back to private zone void set_genesis(const crypto::hash& genesis_hash); - bool prepare_and_sign_pos_block(currency::block& b, const currency::pos_entry& pe) const; + bool prepare_and_sign_pos_block(const mining_context& cxt, currency::block& b, const currency::pos_entry& pe) const; void process_new_blockchain_entry(const currency::block& b, const currency::block_direct_data_entry& bche, const crypto::hash& bl_id, diff --git a/tests/core_tests/chain_switch_pow_pos.cpp b/tests/core_tests/chain_switch_pow_pos.cpp index 73512246..21fb28ea 100644 --- a/tests/core_tests/chain_switch_pow_pos.cpp +++ b/tests/core_tests/chain_switch_pow_pos.cpp @@ -221,7 +221,7 @@ bool gen_chain_switch_pow_pos::check_balance_1(currency::core& c, size_t ev_inde test_generator::wallets_vector w; bool r = generator.build_wallets(get_block_hash(plk_2), m_accounts, w); - CHECK_AND_ASSERT_MES(r && w.size() == 3 && w[0] != 0 && w[1] != 0 && w[2] != 0, false, "failed to build wallets"); + CHECK_AND_ASSERT_MES(r && w.size() == 3 && w[0].wallet != 0 && w[1].wallet != 0 && w[2].wallet != 0, false, "failed to build wallets"); /* uint64_t mined_amount = m_early_blocks_reward * 12 + TX_POOL_MINIMUM_FEE / 2 + m_enormous_fee / 2; @@ -229,7 +229,7 @@ bool gen_chain_switch_pow_pos::check_balance_1(currency::core& c, size_t ev_inde return false; */ - if (!check_balance_via_wallet(*w[1], "alice", MK_TEST_COINS(1), 0, 0, 0, 0)) + if (!check_balance_via_wallet(*w[1].wallet, "alice", MK_TEST_COINS(1), 0, 0, 0, 0)) return false; return true; diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 897d9a4b..11c2dbce 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -330,7 +330,7 @@ bool test_generator::construct_block(currency::block& blk, else { //need to build pos block - r = sign_block(blk, pe, *wallets[won_walled_index], blocks, oi); + r = sign_block(blk, pe, *wallets[won_walled_index].wallet, wallets[won_walled_index].mining_context, blocks, oi); CHECK_AND_ASSERT_MES(r, false, "Failed to find_kernel_and_sign()"); } @@ -347,32 +347,15 @@ bool test_generator::construct_block(currency::block& blk, return true; } -bool test_generator::sign_block(currency::block& b, - pos_entry& pe, - tools::wallet2& w, +bool test_generator::sign_block(currency::block& b, + pos_entry& pe, + tools::wallet2& w, + const tools::wallet2::mining_context& mining_context, const std::vector& blocks, const outputs_index& oi) { - /*uint64_t h = 0; - uint64_t out_i = 0; - const transaction * pts = nullptr; - crypto::public_key source_tx_pub_key = null_pkey; - crypto::public_key out_key = null_pkey; - - bool r = get_output_details_by_global_index(blocks, - oi, - pe.amount, - pe.g_index, - h, - pts, - out_i, - source_tx_pub_key, - out_key); - CHECK_AND_ASSERT_THROW_MES(r, "Failed to get_output_details_by_global_index()");*/ - - bool r = w.prepare_and_sign_pos_block(b, pe); - CHECK_AND_ASSERT_THROW_MES(r,"Failed to prepare_and_sign_pos_block()"); - + bool r = w.prepare_and_sign_pos_block(mining_context, b, pe); + CHECK_AND_ASSERT_MES(r, false, "prepare_and_sign_pos_block failed"); return true; } @@ -438,10 +421,11 @@ bool test_generator::build_wallets(const blockchain_vector& blockchain, wallets.clear(); for (auto a : accs) { - wallets.push_back(std::shared_ptr(new tools::wallet2())); - wallets.back()->assign_account(a); - wallets.back()->get_account().set_createtime(0); - wallets.back()->set_core_proxy(tmp_proxy); + wallets.push_back(gen_wallet_info()); + wallets.back().wallet = std::shared_ptr(new tools::wallet2()); + wallets.back().wallet->assign_account(a); + wallets.back().wallet->get_account().set_createtime(0); + wallets.back().wallet->set_core_proxy(tmp_proxy); currency::core_runtime_config pc = cc; pc.min_coinstake_age = TESTS_POS_CONFIG_MIN_COINSTAKE_AGE; @@ -449,7 +433,7 @@ bool test_generator::build_wallets(const blockchain_vector& blockchain, pc.hard_forks = m_hardforks; pc.get_core_time = test_core_time::get_time; - wallets.back()->set_core_runtime_config(pc); + wallets.back().wallet->set_core_runtime_config(pc); } for (auto& w : wallets) @@ -474,7 +458,7 @@ bool test_generator::build_wallets(const blockchain_vector& blockchain, bdde.txs_ptr.push_back(tx_ptr); } - w->process_new_blockchain_entry(b->b, bdde, currency::get_block_hash(b->b), height); + w.wallet->process_new_blockchain_entry(b->b, bdde, currency::get_block_hash(b->b), height); } } return true; @@ -539,12 +523,13 @@ bool test_generator::find_kernel(const std::list& accs, //lets try to find block for (size_t wallet_index = 0, size = wallets.size(); wallet_index < size; ++wallet_index) { - std::shared_ptr w = wallets[wallet_index]; + std::shared_ptr w = wallets[wallet_index].wallet; + wallets[wallet_index].mining_context = AUTO_VAL_INIT_T(tools::wallet2::mining_context); + tools::wallet2::mining_context& context = wallets[wallet_index].mining_context; //set m_last_pow_block_h to big value, to let wallet to use any available outputs, including the those which is not behind last pow block if (m_ignore_last_pow_in_wallets) w->m_last_pow_block_h = CURRENCY_MAX_BLOCK_NUMBER; - tools::wallet2::mining_context context = AUTO_VAL_INIT(context); w->fill_mining_context(context); std::atomic stop(false); diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index 24026886..9a9ea641 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -372,7 +372,13 @@ public: typedef std::unordered_map > tx_global_indexes; typedef std::vector blockchain_vector; - typedef std::vector > wallets_vector; + + struct gen_wallet_info + { + tools::wallet2::mining_context mining_context; + std::shared_ptr wallet; + }; + typedef std::vector wallets_vector; enum block_fields @@ -430,7 +436,8 @@ public: bool sign_block(currency::block& b, currency::pos_entry& pe, - tools::wallet2& w, + tools::wallet2& w, + const tools::wallet2::mining_context& mining_context, const blockchain_vector& blocks, const outputs_index& oi); diff --git a/tests/core_tests/wallet_tests_basic.cpp b/tests/core_tests/wallet_tests_basic.cpp index a3d40912..6d3c9292 100644 --- a/tests/core_tests/wallet_tests_basic.cpp +++ b/tests/core_tests/wallet_tests_basic.cpp @@ -47,9 +47,9 @@ bool wallet_test::check_balance_via_build_wallets(currency::core& c, size_t ev_i std::list accounts(1, acc); test_generator::wallets_vector w; r = generator.build_wallets(get_block_hash(*top_block), accounts, w, c.get_blockchain_storage().get_core_runtime_config()); - CHECK_AND_ASSERT_MES(r && w.size() == 1 && w[0] != 0, false, "check_balance: failed to build wallets"); + CHECK_AND_ASSERT_MES(r && w.size() == 1 && w[0].wallet != 0, false, "check_balance: failed to build wallets"); - if (!check_balance_via_wallet(*w[0], epee::string_tools::num_to_string_fast(pcb.account_index).c_str(), pcb.total_balance, pcb.mined_balance, pcb.unlocked_balance, pcb.awaiting_in, pcb.awaiting_out)) + if (!check_balance_via_wallet(*w[0].wallet, epee::string_tools::num_to_string_fast(pcb.account_index).c_str(), pcb.total_balance, pcb.mined_balance, pcb.unlocked_balance, pcb.awaiting_in, pcb.awaiting_out)) return false; return true;