From 3f7d6fb57d1bfb5ee806d3facc1261efe3ad14a0 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 4 Dec 2016 12:27:45 +0000 Subject: Fix delayed exit when syncing --- src/cryptonote_core/blockchain.cpp | 21 ++++++++++++++++++++- src/cryptonote_core/blockchain.h | 5 +++++ src/cryptonote_core/cryptonote_core.cpp | 2 +- .../cryptonote_protocol_handler.h | 2 ++ .../cryptonote_protocol_handler.inl | 22 ++++++++++++++++++++-- src/p2p/net_node.inl | 1 + 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index c2ccf3db0..9b9bbb4d4 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -118,7 +118,7 @@ static const uint64_t testnet_hard_fork_version_1_till = 624633; //------------------------------------------------------------------ Blockchain::Blockchain(tx_memory_pool& tx_pool) : m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false), - m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0) + m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_cancel(false) { LOG_PRINT_L3("Blockchain::" << __func__); } @@ -3436,6 +3436,8 @@ void Blockchain::block_longhash_worker(const uint64_t height, const std::vector< // the height of the block passed to it for (const auto & block : blocks) { + if (m_cancel) + return; crypto::hash id = get_block_hash(block); crypto::hash pow = get_block_longhash(block, height); map.emplace(id, pow); @@ -3611,6 +3613,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list> Blockchain:: get_ou return m_db->get_output_histogram(amounts, unlocked, recent_cutoff); } +void Blockchain::cancel() +{ + m_cancel = true; +} + #if defined(PER_BLOCK_CHECKPOINT) void Blockchain::load_compiled_in_block_hashes() { diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index f9ae9d8aa..9afc22657 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -843,6 +843,9 @@ namespace cryptonote */ void block_longhash_worker(const uint64_t height, const std::vector &blocks, std::unordered_map &map) const; + + void cancel(); + private: // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage @@ -912,6 +915,8 @@ namespace cryptonote bool m_testnet; + std::atomic m_cancel; + /** * @brief collects the keys for all outputs being "spent" as an input * diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 84a41cfbf..4da4533f8 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -123,7 +123,7 @@ namespace cryptonote //----------------------------------------------------------------------------------- void core::stop() { - graceful_exit(); + m_blockchain_storage.cancel(); } //----------------------------------------------------------------------------------- void core::init_options(boost::program_options::options_description& desc) diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index ab5d8230d..6bf630834 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -109,6 +109,7 @@ namespace cryptonote bool is_synchronized(){return m_synchronized;} void log_connections(); std::list get_connections(); + void stop(); private: //----------------- commands handlers ---------------------------------------------- int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context); @@ -135,6 +136,7 @@ namespace cryptonote std::atomic m_syncronized_connections_count; std::atomic m_synchronized; bool m_one_request = true; + std::atomic m_stopping; // static std::ofstream m_logreq; boost::mutex m_buffer_mutex; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 65377f990..03509906a 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -60,7 +60,8 @@ namespace cryptonote t_cryptonote_protocol_handler::t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint* p_net_layout):m_core(rcore), m_p2p(p_net_layout), m_syncronized_connections_count(0), - m_synchronized(false) + m_synchronized(false), + m_stopping(false) { if(!m_p2p) @@ -793,6 +794,11 @@ namespace cryptonote size_t count = 0; BOOST_FOREACH(const block_complete_entry& block_entry, arg.blocks) { + if (m_stopping) + { + return 1; + } + ++count; block b; if(!parse_and_validate_block_from_blob(block_entry.block, b)) @@ -857,6 +863,12 @@ namespace cryptonote m_core.prepare_handle_incoming_blocks(arg.blocks); BOOST_FOREACH(const block_complete_entry& block_entry, arg.blocks) { + if (m_stopping) + { + m_core.cleanup_handle_incoming_blocks(); + return 1; + } + // process transactions TIME_MEASURE_START(transactions_process_time); BOOST_FOREACH(auto& tx_blob, block_entry.txs) @@ -1131,5 +1143,11 @@ namespace cryptonote (*logreq) << "log used" << std::endl; return *logreq; } - + //------------------------------------------------------------------------------------------------------------------------ + template + void t_cryptonote_protocol_handler::stop() + { + m_stopping = true; + m_core.stop(); + } } // namespace diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index b5bfc2979..38ff78949 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -654,6 +654,7 @@ namespace nodetool template bool node_server::send_stop_signal() { + m_payload_handler.stop(); m_net_server.send_stop_signal(); LOG_PRINT_L0("[node] Stop signal sent"); return true; -- cgit v1.2.3