From fcb06f7a820f6a1efc6982a2f4b08571c4c28cf9 Mon Sep 17 00:00:00 2001 From: xiphon Date: Wed, 19 Feb 2020 15:17:43 +0000 Subject: [PATCH] cryptonote_core: skip block notify on blockchain switching rollback --- src/cryptonote_core/blockchain.cpp | 24 ++++++++++++++++-------- src/cryptonote_core/blockchain.h | 6 ++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 78893fdf2..f8d6a5653 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -928,7 +928,7 @@ bool Blockchain::rollback_blockchain_switching(std::list& original_chain, for (auto& bl : original_chain) { block_verification_context bvc = {}; - bool r = handle_block_to_main_chain(bl, bvc); + bool r = handle_block_to_main_chain(bl, bvc, false); CHECK_AND_ASSERT_MES(r && bvc.m_added_to_main_chain, false, "PANIC! failed to add (again) block while chain switching during the rollback!"); } @@ -979,7 +979,7 @@ bool Blockchain::switch_to_alternative_blockchain(std::list block_verification_context bvc = {}; // add block to main chain - bool r = handle_block_to_main_chain(bei.bl, bvc); + bool r = handle_block_to_main_chain(bei.bl, bvc, false); // if adding block to main chain failed, rollback to previous state and // return false @@ -1044,6 +1044,11 @@ bool Blockchain::switch_to_alternative_blockchain(std::list reorg_notify->notify("%s", std::to_string(split_height).c_str(), "%h", std::to_string(m_db->height()).c_str(), "%n", std::to_string(m_db->height() - split_height).c_str(), "%d", std::to_string(discarded_blocks).c_str(), NULL); + std::shared_ptr block_notify = m_block_notify; + if (block_notify) + for (const auto &bei: alt_chain) + block_notify->notify("%s", epee::string_tools::pod_to_hex(get_block_hash(bei.bl)).c_str(), NULL); + MGINFO_GREEN("REORGANIZE SUCCESS! on height: " << split_height << ", new blockchain size: " << m_db->height()); return true; } @@ -2567,11 +2572,11 @@ bool Blockchain::have_block(const crypto::hash& id) const return false; } //------------------------------------------------------------------ -bool Blockchain::handle_block_to_main_chain(const block& bl, block_verification_context& bvc) +bool Blockchain::handle_block_to_main_chain(const block& bl, block_verification_context& bvc, bool notify/* = true*/) { LOG_PRINT_L3("Blockchain::" << __func__); crypto::hash id = get_block_hash(bl); - return handle_block_to_main_chain(bl, id, bvc); + return handle_block_to_main_chain(bl, id, bvc, notify); } //------------------------------------------------------------------ size_t Blockchain::get_total_transactions() const @@ -3680,7 +3685,7 @@ bool Blockchain::flush_txes_from_pool(const std::vector &txids) // Needs to validate the block and acquire each transaction from the // transaction mem_pool, then pass the block and transactions to // m_db->add_block() -bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc) +bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc, bool notify/* = true*/) { LOG_PRINT_L3("Blockchain::" << __func__); @@ -4060,9 +4065,12 @@ leave: get_difficulty_for_next_block(); // just to cache it invalidate_block_template_cache(); - std::shared_ptr block_notify = m_block_notify; - if (block_notify) - block_notify->notify("%s", epee::string_tools::pod_to_hex(id).c_str(), NULL); + if (notify) + { + std::shared_ptr block_notify = m_block_notify; + if (block_notify) + block_notify->notify("%s", epee::string_tools::pod_to_hex(id).c_str(), NULL); + } return true; } diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 0aecdcb57..cf1cf33c4 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -1207,10 +1207,11 @@ namespace cryptonote * * @param bl the block to be added * @param bvc metadata concerning the block's validity + * @param notify if set to true, sends new block notification on success * * @return true if the block was added successfully, otherwise false */ - bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc); + bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc, bool notify = true); /** * @brief validate and add a new block to the end of the blockchain @@ -1222,10 +1223,11 @@ namespace cryptonote * @param bl the block to be added * @param id the hash of the block * @param bvc metadata concerning the block's validity + * @param notify if set to true, sends new block notification on success * * @return true if the block was added successfully, otherwise false */ - bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc); + bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc, bool notify = true); /** * @brief validate and add a new block to an alternate blockchain