diff --git a/src/currency_core/currency_core.cpp b/src/currency_core/currency_core.cpp index 0e4e66c4..24473ff0 100644 --- a/src/currency_core/currency_core.cpp +++ b/src/currency_core/currency_core.cpp @@ -32,10 +32,12 @@ namespace currency m_blockchain_storage(m_mempool), m_miner(this, m_blockchain_storage), m_miner_address(boost::value_initialized()), - m_starter_message_showed(false) + m_starter_message_showed(false), + m_stop_handler(nullptr) { set_currency_protocol(pprotocol); } + //----------------------------------------------------------------------------------- void core::set_currency_protocol(i_currency_protocol* pprotocol) { if(pprotocol) @@ -46,6 +48,11 @@ namespace currency m_mempool.set_protocol(m_pprotocol); } //----------------------------------------------------------------------------------- + void core::set_stop_handler(i_stop_handler *handler) + { + m_stop_handler = handler; + } + //----------------------------------------------------------------------------------- bool core::set_checkpoints(checkpoints&& chk_pts) { return m_blockchain_storage.set_checkpoints(std::move(chk_pts)); diff --git a/src/currency_core/currency_core.h b/src/currency_core/currency_core.h index 158c6302..b27b07fb 100644 --- a/src/currency_core/currency_core.h +++ b/src/currency_core/currency_core.h @@ -81,6 +81,8 @@ namespace currency size_t get_alternative_blocks_count(); void set_currency_protocol(i_currency_protocol* pprotocol); + void set_stop_handler(i_stop_handler *handler); + i_stop_handler* get_stop_handler() const { return m_stop_handler; } bool set_checkpoints(checkpoints&& chk_pts); bool get_pool_transactions(std::list& txs); @@ -138,6 +140,7 @@ namespace currency blockchain_storage m_blockchain_storage; tx_memory_pool m_mempool; i_currency_protocol* m_pprotocol; + i_stop_handler* m_stop_handler; critical_section m_incoming_tx_lock; miner m_miner; account_public_address m_miner_address; diff --git a/src/currency_protocol/currency_protocol_handler.inl b/src/currency_protocol/currency_protocol_handler.inl index 15155716..990cf163 100644 --- a/src/currency_protocol/currency_protocol_handler.inl +++ b/src/currency_protocol/currency_protocol_handler.inl @@ -127,7 +127,19 @@ namespace currency // for outgoing connections -- check time difference if (!context.m_is_income) - add_time_delta_and_check_time_sync(context.m_time_delta); + { + if (!add_time_delta_and_check_time_sync(context.m_time_delta)) + { + // serious time sync problem detected + std::shared_ptr ish(m_core.get_stop_handler()); + if (ish != nullptr) + { + // this is a daemon -- stop immediately + ish->stop_handling(); + return true; + } + } + } if(context.m_state == currency_connection_context::state_synchronizing) return true; @@ -679,8 +691,7 @@ namespace currency { std::list local_que; { - std::unique_lock lk(m_relay_que_lock); - //m_relay_que_cv.wait(lk); + CRITICAL_REGION_LOCAL(m_relay_que_lock); local_que.swap(m_relay_que); } if (local_que.size()) @@ -788,7 +799,7 @@ namespace currency template bool t_currency_protocol_handler::add_time_delta_and_check_time_sync(int64_t time_delta) { - std::unique_lock lk(m_time_deltas_lock); + CRITICAL_REGION_LOCAL(m_time_deltas_lock); m_time_deltas.push_back(time_delta); while (m_time_deltas.size() > TIME_SYNC_DELTA_RING_BUFFER_SIZE) @@ -834,7 +845,7 @@ namespace currency template bool t_currency_protocol_handler::get_last_time_sync_difference(int64_t& last_median2local_time_difference, int64_t& last_ntp2local_time_difference) { - std::unique_lock lk(m_time_deltas_lock); + CRITICAL_REGION_LOCAL(m_time_deltas_lock); last_median2local_time_difference = m_last_median2local_time_difference; last_ntp2local_time_difference = m_last_ntp2local_time_difference; @@ -896,7 +907,7 @@ namespace currency { #ifdef ASYNC_RELAY_MODE { - std::unique_lock lk(m_relay_que_lock); + CRITICAL_REGION_LOCAL(m_relay_que_lock); m_relay_que.push_back(AUTO_VAL_INIT(relay_que_entry())); m_relay_que.back().first = arg; m_relay_que.back().second = exclude_context; diff --git a/src/currency_protocol/currency_protocol_handler_common.h b/src/currency_protocol/currency_protocol_handler_common.h index ccbd88b2..3a71f4dd 100644 --- a/src/currency_protocol/currency_protocol_handler_common.h +++ b/src/currency_protocol/currency_protocol_handler_common.h @@ -36,4 +36,14 @@ namespace currency } }; + + /************************************************************************/ + /* */ + /************************************************************************/ + struct i_stop_handler + { + virtual void stop_handling() = 0; + }; + + } diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index b90a4c3a..a9ddf296 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -167,6 +167,7 @@ int main(int argc, char* argv[]) cprotocol.set_p2p_endpoint(&p2psrv); ccore.set_currency_protocol(&cprotocol); daemon_cmmands_handler dch(p2psrv, rpc_server); + ccore.set_stop_handler(&dch); //ccore.get_blockchain_storage().get_attachment_services_manager().add_service(&offers_service); std::shared_ptr stratum_server_ptr; if (stratum_enabled) diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index 6770d0f1..7a69a085 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -20,7 +20,7 @@ PUSH_WARNINGS DISABLE_VS_WARNINGS(4100) -class daemon_cmmands_handler +class daemon_cmmands_handler : public currency::i_stop_handler { typedef nodetool::node_server > srv_type; srv_type& m_srv; @@ -75,7 +75,8 @@ public: return true; } - void stop_handling() + // interface currency::i_stop_handler + virtual void stop_handling() override { m_cmd_binder.stop_handling(); } diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index d28de566..d78b0a00 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -116,7 +116,11 @@ namespace currency //conditional values if (req.flags&COMMAND_RPC_GET_INFO_FLAG_NET_TIME_DELTA_MEDIAN) - res.net_time_delta_median = m_p2p.get_payload_object().get_net_time_delta_median(); + { + int64_t last_median2local_time_diff, last_ntp2local_time_diff; + if (!m_p2p.get_payload_object().get_last_time_sync_difference(last_median2local_time_diff, last_ntp2local_time_diff)) + res.net_time_delta_median = 1; + } if (req.flags&COMMAND_RPC_GET_INFO_FLAG_CURRENT_NETWORK_HASHRATE_50) res.current_network_hashrate_50 = m_core.get_blockchain_storage().get_current_hashrate(50); if (req.flags&COMMAND_RPC_GET_INFO_FLAG_CURRENT_NETWORK_HASHRATE_350)