diff options
Diffstat (limited to '')
-rw-r--r-- | src/cryptonote_core/blockchain_storage.cpp | 5 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain_storage.h | 2 | ||||
-rw-r--r-- | src/cryptonote_core/connection_context.h | 45 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_basic_impl.cpp | 14 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_core.cpp | 4 | ||||
-rw-r--r-- | src/cryptonote_core/cryptonote_format_utils.h | 1 | ||||
-rw-r--r-- | src/cryptonote_protocol/cryptonote_protocol_handler.h | 11 | ||||
-rw-r--r-- | src/cryptonote_protocol/cryptonote_protocol_handler.inl | 59 |
8 files changed, 83 insertions, 58 deletions
diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp index 1ec186652..3eb7f86c0 100644 --- a/src/cryptonote_core/blockchain_storage.cpp +++ b/src/cryptonote_core/blockchain_storage.cpp @@ -104,6 +104,9 @@ bool blockchain_storage::init(const std::string& config_folder) //------------------------------------------------------------------ bool blockchain_storage::store_blockchain() { + m_is_blockchain_storing = true; + misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler([&](){m_is_blockchain_storing=false;}); + LOG_PRINT_L0("Storing blockchain..."); if (!tools::create_directories_if_necessary(m_config_folder)) { @@ -1029,7 +1032,7 @@ void blockchain_storage::print_blockchain(uint64_t start_index, uint64_t end_ind for(size_t i = start_index; i != m_blocks.size() && i != end_index; i++) { - ss << "height " << i << ", timastamp " << m_blocks[i].bl.timestamp << ", cumul_dif " << m_blocks[i].cumulative_difficulty << ", cumul_size " << m_blocks[i].block_cumulative_size + ss << "height " << i << ", timestamp " << m_blocks[i].bl.timestamp << ", cumul_dif " << m_blocks[i].cumulative_difficulty << ", cumul_size " << m_blocks[i].block_cumulative_size << "\nid\t\t" << get_block_hash(m_blocks[i].bl) << "\ndifficulty\t\t" << block_difficulty(i) << ", nonce " << m_blocks[i].bl.nonce << ", tx_count " << m_blocks[i].bl.tx_hashes.size() << ENDL; } diff --git a/src/cryptonote_core/blockchain_storage.h b/src/cryptonote_core/blockchain_storage.h index 4ff5e83ff..c263f7503 100644 --- a/src/cryptonote_core/blockchain_storage.h +++ b/src/cryptonote_core/blockchain_storage.h @@ -110,6 +110,7 @@ namespace cryptonote bool check_tx_inputs(const transaction& tx, uint64_t* pmax_used_block_height = NULL); bool check_tx_inputs(const transaction& tx, uint64_t& pmax_used_block_height, crypto::hash& max_used_block_id); uint64_t get_current_comulative_blocksize_limit(); + bool is_storing_blockchain(){return m_is_blockchain_storing;} template<class t_ids_container, class t_blocks_container, class t_missed_container> bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) @@ -188,6 +189,7 @@ namespace cryptonote std::string m_config_folder; checkpoints m_checkpoints; std::atomic<bool> m_is_in_checkpoint_zone; + std::atomic<bool> m_is_blockchain_storing; diff --git a/src/cryptonote_core/connection_context.h b/src/cryptonote_core/connection_context.h index bf13449bc..53cac992d 100644 --- a/src/cryptonote_core/connection_context.h +++ b/src/cryptonote_core/connection_context.h @@ -6,32 +6,10 @@ #include <unordered_set> #include <atomic> #include "net/net_utils_base.h" - +#include "copyable_atomic.h" namespace cryptonote { - class my_atomic: public std::atomic<uint32_t> - { - public: - my_atomic() - {}; - my_atomic(const my_atomic& a):std::atomic<uint32_t>(a.load()) - {} - my_atomic& operator= (const my_atomic& a) - { - store(a.load()); - return *this; - } - uint32_t operator++() - { - return std::atomic<uint32_t>::operator++(); - } - uint32_t operator++(int fake) - { - return std::atomic<uint32_t>::operator++(fake); - } - }; - struct cryptonote_connection_context: public epee::net_utils::connection_context_base { @@ -40,6 +18,7 @@ namespace cryptonote { state_befor_handshake = 0, //default state state_synchronizing, + state_idle, state_normal }; @@ -48,7 +27,25 @@ namespace cryptonote std::unordered_set<crypto::hash> m_requested_objects; uint64_t m_remote_blockchain_height; uint64_t m_last_response_height; - my_atomic m_callback_request_count; //in debug purpose: problem with double callback rise + epee::copyable_atomic m_callback_request_count; //in debug purpose: problem with double callback rise //size_t m_score; TODO: add score calculations }; + + inline std::string get_protocol_state_string(cryptonote_connection_context::state s) + { + switch (s) + { + case cryptonote_connection_context::state_befor_handshake: + return "state_befor_handshake"; + case cryptonote_connection_context::state_synchronizing: + return "state_synchronizing"; + case cryptonote_connection_context::state_idle: + return "state_idle"; + case cryptonote_connection_context::state_normal: + return "state_normal"; + default: + return "unknown"; + } + } + } diff --git a/src/cryptonote_core/cryptonote_basic_impl.cpp b/src/cryptonote_core/cryptonote_basic_impl.cpp index b320a3463..194b89052 100644 --- a/src/cryptonote_core/cryptonote_basic_impl.cpp +++ b/src/cryptonote_core/cryptonote_basic_impl.cpp @@ -109,25 +109,25 @@ namespace cryptonote { uint64_t prefix; if (!tools::base58::decode_addr(str, prefix, data)) { - LOG_PRINT_L0("Invalid address format"); + LOG_PRINT_L1("Invalid address format"); return false; } if (CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX != prefix) { - LOG_PRINT_L0("Wrong address prefix: " << prefix << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX); + LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX); return false; } if (!::serialization::parse_binary(data, adr)) { - LOG_PRINT_L0("Account public address keys can't be parsed"); + LOG_PRINT_L1("Account public address keys can't be parsed"); return false; } if (!crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key)) { - LOG_PRINT_L0("Failed to validate address keys"); + LOG_PRINT_L1("Failed to validate address keys"); return false; } } @@ -140,7 +140,7 @@ namespace cryptonote { if(buff.size()!=sizeof(public_address_outer_blob)) { - LOG_PRINT_L0("Wrong public address size: " << buff.size() << ", expected size: " << sizeof(public_address_outer_blob)); + LOG_PRINT_L1("Wrong public address size: " << buff.size() << ", expected size: " << sizeof(public_address_outer_blob)); return false; } @@ -149,13 +149,13 @@ namespace cryptonote { if(blob.m_ver > CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER) { - LOG_PRINT_L0("Unknown version of public address: " << blob.m_ver << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER); + LOG_PRINT_L1("Unknown version of public address: " << blob.m_ver << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER); return false; } if(blob.check_sum != get_account_address_checksum(blob)) { - LOG_PRINT_L0("Wrong public address checksum"); + LOG_PRINT_L1("Wrong public address checksum"); return false; } diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index d5ab8d65a..a09f25d31 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -230,9 +230,9 @@ namespace cryptonote return false; } - boost::uint64_t amount_in = 0; + uint64_t amount_in = 0; get_inputs_money_amount(tx, amount_in); - boost::uint64_t amount_out = get_outs_money_amount(tx); + uint64_t amount_out = get_outs_money_amount(tx); if(amount_in <= amount_out) { diff --git a/src/cryptonote_core/cryptonote_format_utils.h b/src/cryptonote_core/cryptonote_format_utils.h index 1c50832b6..1bc180f8f 100644 --- a/src/cryptonote_core/cryptonote_format_utils.h +++ b/src/cryptonote_core/cryptonote_format_utils.h @@ -139,7 +139,6 @@ namespace cryptonote { if (0 == amount) { - chunk_handler(0); return; } diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index f599cf40f..178ec2eb1 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -22,7 +22,7 @@ namespace cryptonote template<class t_core> class t_cryptonote_protocol_handler: public i_cryptonote_protocol - { + { public: typedef cryptonote_connection_context connection_context; typedef core_stat_info stat_info; @@ -51,8 +51,8 @@ namespace cryptonote bool get_stat_info(core_stat_info& stat_inf); bool on_callback(cryptonote_connection_context& context); t_core& get_core(){return m_core;} - - + bool is_synchronized(){return m_synchronized;} + void log_connections(); private: //----------------- commands handlers ---------------------------------------------- int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context); @@ -60,7 +60,6 @@ namespace cryptonote int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context); int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, cryptonote_connection_context& context); int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context); -// int handle_request_chain_entry(int command, NOTIFY_REQUEST_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context); int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context); @@ -77,9 +76,7 @@ namespace cryptonote nodetool::p2p_endpoint_stub<connection_context> m_p2p_stub; nodetool::i_p2p_endpoint<connection_context>* m_p2p; std::atomic<uint32_t> m_syncronized_connections_count; - //std::atomic<uint32_t> m_syncronizing_connections_count; - std::atomic<bool> m_welcome_showed; - + std::atomic<bool> m_synchronized; template<class t_parametr> bool post_notify(typename t_parametr::request& arg, cryptonote_connection_context& context) diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 9c9668071..287461caa 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -13,7 +13,7 @@ namespace cryptonote t_cryptonote_protocol_handler<t_core>::t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout):m_core(rcore), m_p2p(p_net_layout), m_syncronized_connections_count(0), - m_welcome_showed(false) + m_synchronized(false) { if(!m_p2p) @@ -68,6 +68,30 @@ namespace cryptonote } //------------------------------------------------------------------------------------------------------------------------ template<class t_core> + void t_cryptonote_protocol_handler<t_core>::log_connections() + { + std::stringstream ss; + + ss << std::setw(25) << std::left << "Remote Host" + << std::setw(20) << "Peer id" + << std::setw(25) << "Recv/Sent (inactive,sec)" + << std::setw(25) << "State" + << std::setw(20) << "Livetime(seconds)" << ENDL; + + m_p2p->for_each_connection([&](const connection_context& cntxt, nodetool::peerid_type peer_id) + { + ss << std::setw(25) << std::left << std::string(cntxt.m_is_income ? " [INC]":"[OUT]") + + string_tools::get_ip_string_from_int32(cntxt.m_remote_ip) + ":" + std::to_string(cntxt.m_remote_port) + << std::setw(20) << std::hex << peer_id + << std::setw(25) << std::to_string(cntxt.m_recv_cnt)+ "(" + std::to_string(time(NULL) - cntxt.m_last_recv) + ")" + "/" + std::to_string(cntxt.m_send_cnt) + "(" + std::to_string(time(NULL) - cntxt.m_last_send) + ")" + << std::setw(25) << get_protocol_state_string(cntxt.m_state) + << std::setw(20) << std::to_string(time(NULL) - cntxt.m_started) << ENDL; + return true; + }); + LOG_PRINT_L0("Connections: " << ENDL << ss.str()); + } + //------------------------------------------------------------------------------------------------------------------------ + template<class t_core> bool t_cryptonote_protocol_handler<t_core>::process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital) { if(context.m_state == cryptonote_connection_context::state_befor_handshake && !is_inital) @@ -84,7 +108,7 @@ namespace cryptonote return true; } - LOG_PRINT_CCONTEXT_BLUE("Sync data returned unknown top block " << "["<< m_core.get_current_blockchain_height() << "->" << hshd.current_height << "] " << hshd.top_id << ", set SYNCHRONIZATION mode", LOG_LEVEL_0); + LOG_PRINT_CCONTEXT_BLUE("Sync data returned unknown top block " << "["<< m_core.get_current_blockchain_height() << "->" << hshd.current_height << "] " << hshd.top_id << ", set SYNCHRONIZATION mode", (is_inital ? LOG_LEVEL_0:LOG_LEVEL_1)); context.m_state = cryptonote_connection_context::state_synchronizing; context.m_remote_blockchain_height = hshd.current_height; //let the socket to send response to handshake, but request callback, to let send request data after response @@ -93,14 +117,6 @@ namespace cryptonote m_p2p->request_callback(context); return true; } - //------------------------------------------------------------------------------------------------------------------------ - /* template<class t_core> - bool t_cryptonote_protocol_handler<t_core>::process_handshake_data(const blobdata& data, cryptonote_connection_context& context) - { - CORE_SYNC_DATA hsd = boost::value_initialized<CORE_SYNC_DATA>(); - StorageNamed::load_struct_from_storage_buff(hsd, data); - return process_handshake_data(hsd, context); - }*/ //------------------------------------------------------------------------------------------------------------------------ template<class t_core> bool t_cryptonote_protocol_handler<t_core>::get_payload_sync_data(CORE_SYNC_DATA& hshd) @@ -228,8 +244,10 @@ namespace cryptonote context.m_remote_blockchain_height = arg.current_blockchain_height; + size_t count = 0; BOOST_FOREACH(const block_complete_entry& block_entry, arg.blocks) { + ++count; block b; if(!parse_and_validate_block_from_blob(block_entry.block, b)) { @@ -237,6 +255,18 @@ namespace cryptonote << string_tools::buff_to_hex_nodelimer(block_entry.block) << "\r\n dropping connection"); m_p2p->drop_connection(context); return 1; + } + //to avoid concurrency in core between connections, suspend connections which delivered block later then first one + if(count == 2) + { + if(m_core.have_block(get_block_hash(b))) + { + context.m_state = cryptonote_connection_context::state_idle; + context.m_needed_objects.clear(); + context.m_requested_objects.clear(); + LOG_PRINT_CCONTEXT_L1("Connection set to idle state."); + return 1; + } } auto req_it = context.m_requested_objects.find(get_block_hash(b)); @@ -380,10 +410,7 @@ namespace cryptonote context.m_state = cryptonote_connection_context::state_normal; LOG_PRINT_CCONTEXT_GREEN(" SYNCHRONIZED OK", LOG_LEVEL_0); - if( true/*get_synchronizing_connections_count() == 0 && !m_welcome_showed*/) - { - on_connection_synchronized(); - } + on_connection_synchronized(); } return true; } @@ -392,7 +419,7 @@ namespace cryptonote bool t_cryptonote_protocol_handler<t_core>::on_connection_synchronized() { bool val_expected = false; - if(m_welcome_showed.compare_exchange_strong(val_expected, true)) + if(m_synchronized.compare_exchange_strong(val_expected, true)) { LOG_PRINT_L0(ENDL << "**********************************************************************" << ENDL << "You are now synchronized with the network. You may now start simplewallet." << ENDL @@ -411,7 +438,7 @@ namespace cryptonote size_t t_cryptonote_protocol_handler<t_core>::get_synchronizing_connections_count() { size_t count = 0; - m_p2p->for_each_connection([&](cryptonote_connection_context& context)->bool{ + m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id)->bool{ if(context.m_state == cryptonote_connection_context::state_synchronizing) ++count; return true; |