From 2382e9717bb5122c773dd7841484fea95d85128a Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 5 May 2020 17:47:09 +0200 Subject: [PATCH] Implemented debug mode for remote node --- .../currency_protocol_handler.h | 3 ++ .../currency_protocol_handler.inl | 30 +++++++++++++++++++ src/daemon/daemon_commands_handler.h | 21 ++++++++++++- src/p2p/net_node.h | 2 +- src/p2p/net_node.inl | 1 + 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/currency_protocol/currency_protocol_handler.h b/src/currency_protocol/currency_protocol_handler.h index 5b603ba7..eaf06bda 100644 --- a/src/currency_protocol/currency_protocol_handler.h +++ b/src/currency_protocol/currency_protocol_handler.h @@ -70,6 +70,8 @@ namespace currency int64_t get_net_time_delta_median(); bool add_time_delta_and_check_time_sync(int64_t delta); bool get_last_time_sync_difference(int64_t& last_median2local_time_difference, int64_t& last_ntp2local_time_difference); // returns true if differences in allowed bounds + //----------------------------------------------------------------------------------- + void set_to_debug_mode(uint32_t ip); private: //----------------- commands handlers ---------------------------------------------- @@ -114,6 +116,7 @@ namespace currency std::mutex m_time_deltas_lock; int64_t m_last_median2local_time_difference; int64_t m_last_ntp2local_time_difference; + uint32_t m_debug_ip_address; template bool post_notify(typename t_parametr::request& arg, currency_connection_context& context) diff --git a/src/currency_protocol/currency_protocol_handler.inl b/src/currency_protocol/currency_protocol_handler.inl index 2598d837..166cdb80 100644 --- a/src/currency_protocol/currency_protocol_handler.inl +++ b/src/currency_protocol/currency_protocol_handler.inl @@ -22,6 +22,7 @@ namespace currency , m_want_stop(false) , m_last_median2local_time_difference(0) , m_last_ntp2local_time_difference(0) + , m_debug_ip_address(0) { if(!m_p2p) m_p2p = &m_p2p_stub; @@ -241,6 +242,10 @@ namespace currency template int t_currency_protocol_handler::handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + if(context.m_state != currency_connection_context::state_normal) return 1; @@ -363,6 +368,10 @@ namespace currency template int t_currency_protocol_handler::handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + if(context.m_state != currency_connection_context::state_normal) return 1; uint64_t inital_tx_count = arg.txs.size(); @@ -400,6 +409,10 @@ namespace currency template int t_currency_protocol_handler::handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + LOG_PRINT_L2("[HANDLE]NOTIFY_REQUEST_GET_OBJECTS: arg.blocks.size() = " << arg.blocks.size() << ", arg.txs.size()="<< arg.txs.size()); LOG_PRINT_L3("[HANDLE]NOTIFY_REQUEST_GET_OBJECTS: " << ENDL << currency::print_kv_structure(arg)); @@ -445,6 +458,10 @@ namespace currency template int t_currency_protocol_handler::handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + LOG_PRINT_L2("[HANDLE]NOTIFY_RESPONSE_GET_OBJECTS: arg.blocks.size()=" << arg.blocks.size() << ", arg.missed_ids.size()=" << arg.missed_ids.size() << ", arg.txs.size()=" << arg.txs.size()); LOG_PRINT_L3("[HANDLE]NOTIFY_RESPONSE_GET_OBJECTS: " << ENDL << currency::print_kv_structure(arg)); if(context.m_last_response_height > arg.current_blockchain_height) @@ -627,6 +644,10 @@ namespace currency template int t_currency_protocol_handler::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + LOG_PRINT_L2("[HANDLE]NOTIFY_REQUEST_CHAIN: block_ids.size()=" << arg.block_ids.size()); LOG_PRINT_L3("[HANDLE]NOTIFY_REQUEST_CHAIN: " << print_kv_structure(arg)); NOTIFY_RESPONSE_CHAIN_ENTRY::request r; @@ -851,10 +872,19 @@ namespace currency return !(std::abs(m_last_median2local_time_difference) > TIME_SYNC_DELTA_TO_LOCAL_MAX_DIFFERENCE && std::abs(m_last_ntp2local_time_difference) > TIME_SYNC_NTP_TO_LOCAL_MAX_DIFFERENCE); } + template + void t_currency_protocol_handler::set_to_debug_mode(uint32_t ip) + { + m_debug_ip_address = ip; + } //------------------------------------------------------------------------------------------------------------------------ template int t_currency_protocol_handler::handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, currency_connection_context& context) { + //do not process requests if it comes from node wich is debugged + if (m_debug_ip_address != 0 && context.m_remote_ip == m_debug_ip_address) + return 1; + LOG_PRINT_L2("[HANDLE]NOTIFY_RESPONSE_CHAIN_ENTRY: m_block_ids.size()=" << arg.m_block_ids.size() << ", m_start_height=" << arg.start_height << ", m_total_height=" << arg.total_height); LOG_PRINT_L3("[HANDLE]NOTIFY_RESPONSE_CHAIN_ENTRY: " << ENDL << currency::print_kv_structure(arg)); diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index 532e35d1..32a5f404 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -16,6 +16,7 @@ #include "warnings.h" #include "currency_core/bc_offers_service.h" #include "serialization/binary_utils.h" +#include "simplewallet/password_container.h" PUSH_VS_WARNINGS DISABLE_VS_WARNINGS(4100) @@ -69,7 +70,7 @@ public: m_cmd_binder.set_handler("print_tx_from_hex_blob", boost::bind(&daemon_commands_handler::print_tx_from_hex_blob, this, _1), "Unserialize transaction from hex binary data to json-like representation"); m_cmd_binder.set_handler("print_tx_outputs_usage", boost::bind(&daemon_commands_handler::print_tx_outputs_usage, this, _1), "Analyse if tx outputs for involved in subsequent transactions"); m_cmd_binder.set_handler("print_difficulties_of_last_n_blocks", boost::bind(&daemon_commands_handler::print_difficulties_of_last_n_blocks, this, _1), "Print difficulties of last n blocks"); - + m_cmd_binder.set_handler("debug_remore_node_mode", boost::bind(&daemon_commands_handler::debug_remore_node_mode, this, _1), " - If node got connected put node into 'debug mode' i.e. no sync process of other communication except ping responses, maintenance secrete key will be requested"); #ifdef _DEBUG m_cmd_binder.set_handler("debug_set_time_adj", boost::bind(&daemon_commands_handler::debug_set_time_adj, this, _1), "DEBUG: set core time adjustment"); #endif @@ -734,6 +735,24 @@ private: return true; } //-------------------------------------------------------------------------------- + bool debug_remore_node_mode(const std::vector& args) + { + if (args.empty()) + { + std::cout << "expected: ip address" << std::endl; + return true; + } + uint32_t ip = AUTO_VAL_INIT(ip); + if (!string_tools::get_ip_int32_from_string(ip, args.front())) + { + std::cout << "expected: ip address" << std::endl; + return true; + } + m_srv.get_payload_object().set_to_debug_mode(ip); + + return true; + } + //-------------------------------------------------------------------------------- bool print_pool(const std::vector& args) { LOG_PRINT_L0("Pool state: " << ENDL << m_srv.get_payload_object().get_core().print_pool(false)); diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index c57fa524..301b4cda 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -82,7 +82,6 @@ namespace nodetool m_use_only_priority_peers(false), m_peer_livetime{}, m_debug_requests_enabled(false) - {} static void init_options(boost::program_options::options_description& desc); @@ -248,6 +247,7 @@ namespace nodetool bool m_debug_requests_enabled; uint64_t m_startup_time; + //critical_section m_connections_lock; //connections_indexed_container m_connections; diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 7ea6c905..5042548d 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -1442,6 +1442,7 @@ namespace nodetool std::string s = ss.str(); return s; } + //----------------------------------------------------------------------------------- template void node_server::on_connection_new(p2p_connection_context& context)