1
0
Fork 0
forked from lthn/blockchain

coretests: pos_mining_with_decoys test added

This commit is contained in:
sowle 2023-10-31 21:22:15 +01:00
parent 760ff033cb
commit 299124877e
4 changed files with 131 additions and 2 deletions

View file

@ -2569,14 +2569,14 @@ bool blockchain_storage::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDO
}
if (result_outs.outs.size() < req.outs_count)
{
LOG_PRINT_RED_L0("Not enough inputs for amount " << print_money_brief(amount) << ", needed " << req.outs_count << ", added " << result_outs.outs.size() << " good outs from " << up_index_limit << " unlocked of " << outs_container_size << " total");
LOG_PRINT_YELLOW("Not enough inputs for amount " << print_money_brief(amount) << ", needed " << req.outs_count << ", added " << result_outs.outs.size() << " good outs from " << up_index_limit << " unlocked of " << outs_container_size << " total", LOG_LEVEL_0);
}
}else
{
size_t added = 0;
for (size_t i = 0; i != up_index_limit; i++)
added += add_out_to_get_random_outs(result_outs, amount, i, req.outs_count, req.use_forced_mix_outs) ? 1 : 0;
LOG_PRINT_RED_L0("Not enough inputs for amount " << print_money_brief(amount) << ", needed " << req.outs_count << ", added " << added << " good outs from " << up_index_limit << " unlocked of " << outs_container_size << " total - respond with all good outs");
LOG_PRINT_YELLOW("Not enough inputs for amount " << print_money_brief(amount) << ", needed " << req.outs_count << ", added " << added << " good outs from " << up_index_limit << " unlocked of " << outs_container_size << " total - respond with all good outs", LOG_LEVEL_0);
}
}
return true;

View file

@ -893,6 +893,7 @@ int main(int argc, char* argv[])
GENERATE_AND_PLAY(pos_wallet_big_block_test);
//GENERATE_AND_PLAY(block_template_against_txs_size); // Long test! by demand only
GENERATE_AND_PLAY(pos_altblocks_validation);
GENERATE_AND_PLAY(pos_mining_with_decoys);
// alternative blocks and generic chain-switching tests
GENERATE_AND_PLAY(gen_chain_switch_pow_pos);

View file

@ -139,3 +139,121 @@ bool gen_pos_basic_tests::check_exchange_1(currency::core& c, size_t ev_index, c
CHECK_EQ(offers.size(), 1);
return true;
}
//------------------------------------------------------------------------------
pos_mining_with_decoys::pos_mining_with_decoys()
{
REGISTER_CALLBACK_METHOD(pos_mining_with_decoys, c1);
}
bool pos_mining_with_decoys::generate(std::vector<test_event_entry>& events) const
{
uint64_t ts = test_core_time::get_time();
m_accounts.resize(TOTAL_ACCS_COUNT);
currency::account_base& miner_acc = m_accounts[MINER_ACC_IDX]; miner_acc.generate(); miner_acc.set_createtime(ts);
currency::account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); alice_acc.set_createtime(ts);
currency::account_base& bob_acc = m_accounts[BOB_ACC_IDX]; bob_acc.generate(); bob_acc.set_createtime(ts);
currency::account_base& carol_acc = m_accounts[CAROL_ACC_IDX]; carol_acc.generate(); carol_acc.set_createtime(ts);
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
REWIND_BLOCKS_N(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW + 1);
bool r = false;
std::vector<tx_source_entry> sources;
r = fill_tx_sources(sources, events, blk_0r, miner_acc.get_keys(), 2 * COIN, 0);
CHECK_AND_ASSERT_MES(r, false, "fill_tx_sources failed");
std::vector<tx_destination_entry> destinations;
destinations.emplace_back(47 * TESTS_DEFAULT_FEE, alice_acc.get_public_address());
destinations.emplace_back(47 * TESTS_DEFAULT_FEE, miner_acc.get_public_address()); // as a decoy for Alice
destinations.emplace_back(5 * TESTS_DEFAULT_FEE, bob_acc.get_public_address());
destinations.emplace_back(COIN, carol_acc.get_public_address());
transaction tx_0{};
r = construct_tx(miner_acc.get_keys(), sources, destinations, empty_attachment, tx_0, 0);
CHECK_AND_ASSERT_MES(r, false, "construct_tx failed");
events.push_back(tx_0);
MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_0);
REWIND_BLOCKS_N(events, blk_1r, blk_1, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW);
DO_CALLBACK(events, "c1");
return true;
}
bool pos_mining_with_decoys::c1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events)
{
bool r = false;
CHECK_AND_ASSERT_MES(!c.get_blockchain_storage().get_core_runtime_config().is_hardfork_active_for_height(4, c.get_top_block_height()), false, "HF4 should not be active");
std::shared_ptr<tools::wallet2> miner_wlt = init_playtime_test_wallet(events, c, m_accounts[MINER_ACC_IDX]);
miner_wlt->refresh();
std::shared_ptr<tools::wallet2> alice_wlt = init_playtime_test_wallet(events, c, m_accounts[ALICE_ACC_IDX]);
alice_wlt->refresh();
CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", 47 * TESTS_DEFAULT_FEE, INVALID_BALANCE_VAL, 47 * TESTS_DEFAULT_FEE), false, "");
std::shared_ptr<tools::wallet2> bob_wlt = init_playtime_test_wallet(events, c, m_accounts[BOB_ACC_IDX]);
bob_wlt->refresh();
CHECK_AND_ASSERT_MES(check_balance_via_wallet(*bob_wlt, "Bob", 5 * TESTS_DEFAULT_FEE, INVALID_BALANCE_VAL, 5 * TESTS_DEFAULT_FEE), false, "");
std::shared_ptr<tools::wallet2> carol_wlt = init_playtime_test_wallet(events, c, m_accounts[CAROL_ACC_IDX]);
carol_wlt->refresh();
CHECK_AND_ASSERT_MES(check_balance_via_wallet(*carol_wlt, "Carol", COIN, INVALID_BALANCE_VAL, COIN), false, "");
// 1. Alice should be able to mine a PoS block with 1 decoys (ring size == 2)
size_t top_block_height = c.get_top_block_height();
r = alice_wlt->try_mint_pos(m_accounts[ALICE_ACC_IDX].get_public_address());
CHECK_AND_ASSERT_MES(r, false, "try_mint_pos failed");
{
block b{};
CHECK_AND_ASSERT_MES(c.get_blockchain_storage().get_top_block(b), false, "");
CHECK_AND_ASSERT_MES(get_block_height(b) == top_block_height + 1, false, "unexpected top block height");
txin_to_key& intk = boost::get<txin_to_key>(b.miner_tx.vin[1]);
CHECK_AND_ASSERT_MES(intk.amount == 47 * TESTS_DEFAULT_FEE, false, "incorrect amount: " << intk.amount);
CHECK_AND_ASSERT_MES(intk.key_offsets.size() == 2, false, "unexpected ring size: " << intk.key_offsets.size());
}
// 2. Bob should only be able to mine a PoS block with zero decoys (ring size == 1)
top_block_height = c.get_top_block_height();
r = bob_wlt->try_mint_pos(m_accounts[BOB_ACC_IDX].get_public_address());
CHECK_AND_ASSERT_MES(r, false, "try_mint_pos failed");
{
block b{};
CHECK_AND_ASSERT_MES(c.get_blockchain_storage().get_top_block(b), false, "");
CHECK_AND_ASSERT_MES(get_block_height(b) == top_block_height + 1, false, "unexpected top block height");
txin_to_key& intk = boost::get<txin_to_key>(b.miner_tx.vin[1]);
CHECK_AND_ASSERT_MES(intk.amount == 5 * TESTS_DEFAULT_FEE, false, "incorrect amount: " << intk.amount);
CHECK_AND_ASSERT_MES(intk.key_offsets.size() == 1, false, "unexpected ring size: " << intk.key_offsets.size());
}
// 3. Carol should only be able to mine a PoS block with CURRENCY_DEFAULT_DECOY_SET_SIZE decoys (ring size == CURRENCY_DEFAULT_DECOY_SET_SIZE + 1)
top_block_height = c.get_top_block_height();
r = carol_wlt->try_mint_pos(m_accounts[CAROL_ACC_IDX].get_public_address());
CHECK_AND_ASSERT_MES(r, false, "try_mint_pos failed");
{
block b{};
CHECK_AND_ASSERT_MES(c.get_blockchain_storage().get_top_block(b), false, "");
CHECK_AND_ASSERT_MES(get_block_height(b) == top_block_height + 1, false, "unexpected top block height");
txin_to_key& intk = boost::get<txin_to_key>(b.miner_tx.vin[1]);
CHECK_AND_ASSERT_MES(intk.amount == COIN, false, "incorrect amount: " << intk.amount);
CHECK_AND_ASSERT_MES(intk.key_offsets.size() == CURRENCY_DEFAULT_DECOY_SET_SIZE + 1, false, "unexpected ring size: " << intk.key_offsets.size());
}
return true;
}

View file

@ -5,6 +5,7 @@
#pragma once
#include "chaingen.h"
#include "wallet_tests_basic.h"
struct gen_pos_basic_tests : public test_chain_unit_base
{
@ -22,3 +23,12 @@ struct gen_pos_basic_tests : public test_chain_unit_base
bool check_exchange_1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
};
struct pos_mining_with_decoys : public wallet_test
{
pos_mining_with_decoys();
bool generate(std::vector<test_event_entry>& events) const;
bool configure_core(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
bool c1(currency::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
};