diff --git a/src/currency_core/blockchain_storage.h b/src/currency_core/blockchain_storage.h index cca1aecd..075bc789 100644 --- a/src/currency_core/blockchain_storage.h +++ b/src/currency_core/blockchain_storage.h @@ -933,7 +933,7 @@ namespace currency if (this->get_current_blockchain_size() - max_related_block_height < CURRENCY_HF4_MANDATORY_MIN_COINAGE) { - LOG_ERROR("Coinage rule broken(mainblock): h = " << this->get_current_blockchain_size() << ", max_related_block_height=" << max_related_block_height << ", tx: " << get_transaction_hash(validated_tx)); + LOG_ERROR("Coinage rule is broken (mainblock): current blockchain size = " << this->get_current_blockchain_size() << ", max_related_block_height = " << max_related_block_height << ", tx: " << get_transaction_hash(validated_tx)); return false; } } diff --git a/src/currency_core/tx_pool.cpp b/src/currency_core/tx_pool.cpp index 0c93fd65..b99ba859 100644 --- a/src/currency_core/tx_pool.cpp +++ b/src/currency_core/tx_pool.cpp @@ -725,15 +725,26 @@ namespace currency return true; } //--------------------------------------------------------------------------------- + std::string tx_memory_pool::get_blacklisted_txs_string() const + { + std::stringstream ss; + m_db_black_tx_list.enumerate_items([&](uint64_t i, const crypto::hash& td_id, const bool& /*dummy */ ) + { + ss << td_id << ENDL; + return true; + }); + return ss.str(); + } + //--------------------------------------------------------------------------------- bool tx_memory_pool::add_transaction_to_black_list(const transaction& tx) { // atm: // 1) the only side effect of a tx being blacklisted is the one is just ignored by fill_block_template(), but it still can be added to blockchain/pool // 2) it's permanent - LOG_PRINT_YELLOW("TX ADDED TO POOL'S BLACKLIST: " << get_transaction_hash(tx), LOG_LEVEL_0); m_db.begin_transaction(); m_db_black_tx_list.set(get_transaction_hash(tx), true); m_db.commit_transaction(); + LOG_PRINT_YELLOW("TX ADDED TO POOL'S BLACKLIST: " << get_transaction_hash(tx) << ", full black list: " << ENDL << get_blacklisted_txs_string(), LOG_LEVEL_0); return true; } //--------------------------------------------------------------------------------- @@ -910,8 +921,12 @@ namespace currency { //not the best implementation at this time, sorry :( - if (is_tx_blacklisted(get_transaction_hash(txd.tx))) + if (is_tx_blacklisted(id)) + { + LOG_PRINT_L2("[is_transaction_ready_to_go]Tx " << id << " skipped as it blacklisted"); return false; + } + //check is ring_signature already checked ? if(txd.max_used_block_id == null_hash) @@ -926,7 +941,8 @@ namespace currency txd.last_failed_id = m_blockchain.get_block_id_by_height(txd.last_failed_height); return false; } - }else + } + else { if(txd.max_used_block_height >= m_blockchain.get_current_blockchain_size()) return false; @@ -944,6 +960,10 @@ namespace currency } } } + + if (txd.tx.version > TRANSACTION_VERSION_PRE_HF4 && m_blockchain.get_current_blockchain_size() < txd.max_used_block_height + CURRENCY_HF4_MANDATORY_MIN_COINAGE) // coinage rule since HF4, s.a. scan_outputkeys_for_indexes() + return false; + //if we here, transaction seems valid, but, anyway, check for key_images collisions with blockchain, just to be sure if (m_blockchain.have_tx_keyimges_as_spent(txd.tx)) { @@ -1195,13 +1215,14 @@ namespace currency if (i < best_position) { bl.tx_hashes.push_back(tx.first); + LOG_PRINT_L2("[fill_block_template]: Added tx to block: " << tx.first); } - else if (have_attachment_service_in_container(tx.second->tx.attachment, BC_OFFERS_SERVICE_ID, BC_OFFERS_SERVICE_INSTRUCTION_DEL)) + /*else if (have_attachment_service_in_container(tx.second->tx.attachment, BC_OFFERS_SERVICE_ID, BC_OFFERS_SERVICE_INSTRUCTION_DEL)) { // BC_OFFERS_SERVICE_INSTRUCTION_DEL transactions has zero fee, so include them here regardless of reward effectiveness bl.tx_hashes.push_back(tx.first); total_size += tx.second->blob_size; - } + }*/ } } // add explicit transactions diff --git a/src/currency_core/tx_pool.h b/src/currency_core/tx_pool.h index c97a498a..e573d2b5 100644 --- a/src/currency_core/tx_pool.h +++ b/src/currency_core/tx_pool.h @@ -155,6 +155,7 @@ namespace currency bool insert_alias_info(const transaction& tx); bool remove_alias_info(const transaction& tx); bool check_tx_fee(const transaction &tx, uint64_t amount_fee); + std::string get_blacklisted_txs_string() const; bool is_valid_contract_finalization_tx(const transaction &tx)const; void store_db_solo_options_values(); diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index ca2a613e..3cf5d3fd 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1227,6 +1227,7 @@ int main(int argc, char* argv[]) the heights >= 10. */ GENERATE_AND_PLAY_HF(tx_pool_semantic_validation, "3"); GENERATE_AND_PLAY(input_refers_to_incompatible_by_type_output); + GENERATE_AND_PLAY_HF(tx_pool_validation_and_chain_switch, "3-*"); // Double spend GENERATE_AND_PLAY(gen_double_spend_in_tx); diff --git a/tests/core_tests/tx_validation.cpp b/tests/core_tests/tx_validation.cpp index 654113a0..9d171eb4 100644 --- a/tests/core_tests/tx_validation.cpp +++ b/tests/core_tests/tx_validation.cpp @@ -2602,3 +2602,121 @@ bool input_refers_to_incompatible_by_type_output::assert_htlc_input_refers_zarca return true; } + +//------------------------------------------------------------------ + +tx_pool_validation_and_chain_switch::tx_pool_validation_and_chain_switch() +{ + REGISTER_CALLBACK_METHOD(tx_pool_validation_and_chain_switch, c1); +} + +bool tx_pool_validation_and_chain_switch::generate(std::vector& events) const +{ + // Test idea: + + 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(); + account_base& alice_acc = m_accounts[ALICE_ACC_IDX]; alice_acc.generate(); + MAKE_GENESIS_BLOCK(events, blk_0, miner_acc, ts); + DO_CALLBACK(events, "configure_core"); + + std::list miner_stake_sources( {miner_acc} ); + + REWIND_BLOCKS_N_WITH_TIME(events, blk_0r, blk_0, miner_acc, CURRENCY_MINED_MONEY_UNLOCK_WINDOW * 2); + MAKE_TX(events, tx_0, miner_acc, alice_acc, MK_TEST_COINS(100), blk_0r); + MAKE_NEXT_BLOCK_TX1(events, blk_1, blk_0r, miner_acc, tx_0); + + // 0 ... 10 21 22 23 24 25 26 27 28 29 30 31 <- height + // (0 )- (0r)- (1 )- <- chain A + // tx_0 + + MAKE_NEXT_POS_BLOCK(events, blk_2, blk_1, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_3, blk_2, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_4, blk_3, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_5, blk_4, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_6, blk_5, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_7, blk_6, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_8, blk_7, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_9, blk_8, miner_acc, miner_stake_sources); + MAKE_NEXT_POS_BLOCK(events, blk_10, blk_9, miner_acc, miner_stake_sources); + + // now Alice should be able to spend her coins + MAKE_TX(events, tx_a, alice_acc, miner_acc, MK_TEST_COINS(100) - TESTS_DEFAULT_FEE, blk_10); + DO_CALLBACK_PARAMS(events, "check_tx_pool_count", static_cast(1)); + + MAKE_NEXT_POS_BLOCK_TX1(events, blk_11, blk_10, miner_acc, miner_stake_sources, tx_a); + DO_CALLBACK_PARAMS(events, "check_tx_pool_count", static_cast(0)); + + // construct chain B as PoW-PoS-PoW-PoS, it should win + MAKE_NEXT_BLOCK(events, blk_3a, blk_3, miner_acc); + MAKE_NEXT_POS_BLOCK(events, blk_4a, blk_3a, miner_acc, miner_stake_sources); + MAKE_NEXT_BLOCK(events, blk_5a, blk_4a, miner_acc); + MAKE_NEXT_POS_BLOCK(events, blk_6a, blk_5a, miner_acc, miner_stake_sources); + + // make sure it won + DO_CALLBACK_PARAMS(events, "check_top_block", params_top_block(blk_6a)); + + // now tx_a should have been put back to the tx pool + DO_CALLBACK_PARAMS(events, "check_tx_pool_count", static_cast(1)); + + // the next block should be rejected, because of max_related_block_height=21 and the current height is 28 + //DO_CALLBACK(events, "mark_invalid_block"); + //MAKE_NEXT_BLOCK_TX1(events, blk_7a, blk_6a, miner_acc, tx_a); + + // and if we clear tx pool ... + //DO_CALLBACK(events, "clear_tx_pool"); + //DO_CALLBACK_PARAMS(events, "check_tx_pool_count", static_cast(0)); + // and re-add the transaction on this height, it sould be added + //ADD_CUSTOM_EVENT(events, tx_a); + + // however, we need to make sure that tx pool won't be use tx_a in a block template until the height is good enough + DO_CALLBACK(events, "c1"); + + return true; +} + +bool tx_pool_validation_and_chain_switch::c1(currency::core& c, size_t ev_index, const std::vector& events) +{ + std::shared_ptr miner_wlt = init_playtime_test_wallet(events, c, MINER_ACC_IDX); + std::shared_ptr alice_wlt = init_playtime_test_wallet(events, c, ALICE_ACC_IDX); + + bool r = false; + alice_wlt->refresh(); + + // make sure tx_a is still in the pool + CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count()); + // and Alice's unconfirmed balance is nonzero + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", MK_TEST_COINS(100)), false, ""); + + // mine a block, make sure it is possible and the tx is still in the pool afterwards + r = mine_next_pow_block_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c); + CHECK_AND_ASSERT_MES(r, false, "mine_next_pow_blocks_in_playtime failed"); + CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count()); + + // make sure tx_a is still in the pool + CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count()); + // and Alice's unconfirmed balance is nonzero + alice_wlt->refresh(); + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", MK_TEST_COINS(100)), false, ""); + + // mine more + r = mine_next_pow_blocks_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c, 2); + + // make sure tx_a is still in the pool + CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == 1, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count()); + // and Alice's unconfirmed balance is nonzero + alice_wlt->refresh(); + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", MK_TEST_COINS(100)), false, ""); + + // the next blocktemplate should include it + r = mine_next_pow_block_in_playtime(m_accounts[MINER_ACC_IDX].get_public_address(), c); + // make sure it did + CHECK_AND_ASSERT_MES(c.get_pool_transactions_count() == 0, false, "Unexpected number of txs in the pool: " << c.get_pool_transactions_count()); + // and Alice's unconfirmed balance is zero + alice_wlt->refresh(); + CHECK_AND_ASSERT_MES(check_balance_via_wallet(*alice_wlt, "Alice", MK_TEST_COINS(0)), false, ""); + + return true; +} + diff --git a/tests/core_tests/tx_validation.h b/tests/core_tests/tx_validation.h index 6e3ae56e..3428d1eb 100644 --- a/tests/core_tests/tx_validation.h +++ b/tests/core_tests/tx_validation.h @@ -6,6 +6,7 @@ #pragma once #include "chaingen.h" +#include "wallet_tests_basic.h" struct gen_tx_big_version : public test_chain_unit_enchanced { @@ -175,3 +176,10 @@ struct input_refers_to_incompatible_by_type_output : public test_chain_unit_ench bool assert_zc_input_refers_bare_output_is_wrong(const currency::core& c, const size_t ev_index, const std::vector& events) const; bool assert_htlc_input_refers_zarcanum_output_is_wrong(const currency::core& c, const size_t ev_index, const std::vector& events) const; }; + +struct tx_pool_validation_and_chain_switch : public wallet_test +{ + tx_pool_validation_and_chain_switch(); + bool generate(std::vector& events) const; + bool c1(currency::core& c, size_t ev_index, const std::vector& events); +};