From f5ca243caf2333004235de63e3ab25dbdb0f1fdd Mon Sep 17 00:00:00 2001 From: Aiwe Date: Thu, 10 Dec 2020 18:55:34 +0200 Subject: [PATCH] p2p: use vector instead of list for peer lists --- src/P2p/NetNode.cpp | 12 ++++++------ src/P2p/NetNode.h | 6 +++--- src/P2p/P2pProtocolDefinitions.h | 8 ++++---- src/P2p/PeerListManager.cpp | 6 +++--- src/P2p/PeerListManager.h | 6 +++--- src/Rpc/RpcServer.cpp | 4 ++-- tests/UnitTests/TestPeerlist.cpp | 6 +++--- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/P2p/NetNode.cpp b/src/P2p/NetNode.cpp index 659590a16..b8614feff 100755 --- a/src/P2p/NetNode.cpp +++ b/src/P2p/NetNode.cpp @@ -132,7 +132,7 @@ const command_line::arg_descriptor > arg_p2p_add_exclus const command_line::arg_descriptor > arg_p2p_seed_node = {"seed-node", "Connect to a node to retrieve peer addresses, and disconnect"}; const command_line::arg_descriptor arg_p2p_hide_my_port = {"hide-my-port", "Do not announce yourself as peerlist candidate", false, true}; -std::string print_peerlist_to_string(const std::list& pl) { +std::string print_peerlist_to_string(const std::vector& pl) { time_t now_time = 0; time(&now_time); std::stringstream ss; @@ -1164,7 +1164,7 @@ std::string print_banlist_to_string(std::map list) { } //----------------------------------------------------------------------------------- - bool NodeServer::fix_time_delta(std::list& local_peerlist, time_t local_time, int64_t& delta) + bool NodeServer::fix_time_delta(std::vector& local_peerlist, time_t local_time, int64_t& delta) { //fix time delta time_t now = 0; @@ -1184,7 +1184,7 @@ std::string print_banlist_to_string(std::map list) { } //----------------------------------------------------------------------------------- - bool NodeServer::handle_remote_peerlist(const std::list& peerlist, time_t local_time, const CryptoNoteConnectionContext& context) + bool NodeServer::handle_remote_peerlist(const std::vector& peerlist, time_t local_time, const CryptoNoteConnectionContext& context) { if (peerlist.size() > P2P_MAX_PEERS_IN_HANDSHAKE) { @@ -1193,7 +1193,7 @@ std::string print_banlist_to_string(std::map list) { } int64_t delta = 0; - std::list peerlist_ = peerlist; + std::vector peerlist_ = peerlist; if(!fix_time_delta(peerlist_, local_time, delta)) return false; //logger(Logging::TRACE) << context << "REMOTE PEERLIST: TIME_DELTA: " << delta << ", remote peerlist size=" << peerlist_.size(); @@ -1462,8 +1462,8 @@ std::string print_banlist_to_string(std::map list) { bool NodeServer::log_peerlist() { std::list pl_anchor; - std::list pl_wite; - std::list pl_gray; + std::vector pl_wite; + std::vector pl_gray; m_peerlist.get_peerlist_full(pl_anchor, pl_gray, pl_wite); logger(INFO) << ENDL << "Peerlist anchor:" << ENDL << print_peerlist_to_string(pl_anchor) << ENDL << "Peerlist white:" << ENDL << print_peerlist_to_string(pl_wite) << ENDL diff --git a/src/P2p/NetNode.h b/src/P2p/NetNode.h index 73a362275..2eeb0068e 100755 --- a/src/P2p/NetNode.h +++ b/src/P2p/NetNode.h @@ -200,10 +200,10 @@ namespace CryptoNote bool handleConfig(const NetNodeConfig& config); bool append_net_address(std::vector& nodes, const std::string& addr); bool idle_worker(); - bool handle_remote_peerlist(const std::list& peerlist, time_t local_time, const CryptoNoteConnectionContext& context); + bool handle_remote_peerlist(const std::vector& peerlist, time_t local_time, const CryptoNoteConnectionContext& context); bool get_local_node_data(basic_node_data& node_data); - bool fix_time_delta(std::list& local_peerlist, time_t local_time, int64_t& delta); + bool fix_time_delta(std::vector& local_peerlist, time_t local_time, int64_t& delta); bool connections_maker(); bool make_new_connection_from_peerlist(bool use_white_list); @@ -286,7 +286,7 @@ namespace CryptoNote std::vector m_priority_peers; std::vector m_exclusive_peers; std::vector m_seed_nodes; - std::list m_command_line_peers; + std::vector m_command_line_peers; uint64_t m_peer_livetime; boost::uuids::uuid m_network_id; std::map m_blocked_hosts; diff --git a/src/P2p/P2pProtocolDefinitions.h b/src/P2p/P2pProtocolDefinitions.h index 3111127f2..760e4b0e6 100755 --- a/src/P2p/P2pProtocolDefinitions.h +++ b/src/P2p/P2pProtocolDefinitions.h @@ -109,7 +109,7 @@ namespace CryptoNote { basic_node_data node_data; CORE_SYNC_DATA payload_data; - std::list local_peerlist; + std::vector local_peerlist; void serialize(ISerializer& s) { KV_MEMBER(node_data) @@ -141,7 +141,7 @@ namespace CryptoNote { uint64_t local_time; CORE_SYNC_DATA payload_data; - std::list local_peerlist; + std::vector local_peerlist; void serialize(ISerializer& s) { KV_MEMBER(local_time) @@ -260,8 +260,8 @@ namespace CryptoNote struct response { std::list local_peerlist_anchor; - std::list local_peerlist_white; - std::list local_peerlist_gray; + std::vector local_peerlist_white; + std::vector local_peerlist_gray; std::list connections_list; PeerIdType my_id; uint64_t local_time; diff --git a/src/P2p/PeerListManager.cpp b/src/P2p/PeerListManager.cpp index 0c6639b67..49ab754c6 100755 --- a/src/P2p/PeerListManager.cpp +++ b/src/P2p/PeerListManager.cpp @@ -122,7 +122,7 @@ void PeerlistManager::trim_gray_peerlist() { } //-------------------------------------------------------------------------------------------------- -bool PeerlistManager::merge_peerlist(const std::list& outer_bs) +bool PeerlistManager::merge_peerlist(const std::vector& outer_bs) { for(const PeerlistEntry& be : outer_bs) { append_with_peer_gray(be); @@ -161,7 +161,7 @@ bool PeerlistManager::is_ip_allowed(uint32_t ip) const } //-------------------------------------------------------------------------------------------------- -bool PeerlistManager::get_peerlist_head(std::list& bs_head, uint32_t depth) const +bool PeerlistManager::get_peerlist_head(std::vector& bs_head, uint32_t depth) const { const peers_indexed::index::type& by_time_index = m_peers_white.get(); uint32_t cnt = 0; @@ -180,7 +180,7 @@ bool PeerlistManager::get_peerlist_head(std::list& bs_head, uint3 } //-------------------------------------------------------------------------------------------------- -bool PeerlistManager::get_peerlist_full(std::list& pl_anchor, std::list& pl_gray, std::list& pl_white) const +bool PeerlistManager::get_peerlist_full(std::list& pl_anchor, std::vector& pl_gray, std::vector& pl_white) const { const anchor_peers_indexed::index::type& by_time_index_an = m_peers_anchor.get(); const peers_indexed::index::type& by_time_index_gr = m_peers_gray.get(); diff --git a/src/P2p/PeerListManager.h b/src/P2p/PeerListManager.h index ec0b04a27..fc8ec9e6a 100755 --- a/src/P2p/PeerListManager.h +++ b/src/P2p/PeerListManager.h @@ -79,9 +79,9 @@ class PeerlistManager { bool init(bool allow_local_ip); size_t get_white_peers_count() const { return m_peers_white.size(); } size_t get_gray_peers_count() const { return m_peers_gray.size(); } - bool merge_peerlist(const std::list& outer_bs); - bool get_peerlist_head(std::list& bs_head, uint32_t depth = CryptoNote::P2P_DEFAULT_PEERS_IN_HANDSHAKE) const; - bool get_peerlist_full(std::list& pl_anchor, std::list& pl_gray, std::list& pl_white) const; + bool merge_peerlist(const std::vector& outer_bs); + bool get_peerlist_head(std::vector& bs_head, uint32_t depth = CryptoNote::P2P_DEFAULT_PEERS_IN_HANDSHAKE) const; + bool get_peerlist_full(std::list& pl_anchor, std::vector& pl_gray, std::vector& pl_white) const; bool get_white_peer_by_index(PeerlistEntry& p, size_t i) const; bool get_gray_peer_by_index(PeerlistEntry& p, size_t i) const; bool append_with_peer_anchor(const AnchorPeerlistEntry& pr); diff --git a/src/Rpc/RpcServer.cpp b/src/Rpc/RpcServer.cpp index 901bb4724..13c6de243 100755 --- a/src/Rpc/RpcServer.cpp +++ b/src/Rpc/RpcServer.cpp @@ -1261,8 +1261,8 @@ bool RpcServer::onGetPeerList(const COMMAND_RPC_GET_PEER_LIST::request& req, COM } std::list pl_anchor; - std::list pl_wite; - std::list pl_gray; + std::vector pl_wite; + std::vector pl_gray; m_p2p.getPeerlistManager().get_peerlist_full(pl_anchor, pl_gray, pl_wite); for (const auto& pe : pl_anchor) { std::stringstream ss; diff --git a/tests/UnitTests/TestPeerlist.cpp b/tests/UnitTests/TestPeerlist.cpp index db39d48b4..943f8d0ad 100755 --- a/tests/UnitTests/TestPeerlist.cpp +++ b/tests/UnitTests/TestPeerlist.cpp @@ -34,7 +34,7 @@ TEST(peer_list, peer_list_general) #define ADD_GRAY_NODE(ip_, port_, id_, last_seen_) { PeerlistEntry ple; ple.last_seen=last_seen_;ple.adr.ip = ip_; ple.adr.port = port_; ple.id = id_;plm.append_with_peer_gray(ple);} #define ADD_WHITE_NODE(ip_, port_, id_, last_seen_) { PeerlistEntry ple;ple.last_seen=last_seen_; ple.adr.ip = ip_; ple.adr.port = port_; ple.id = id_;plm.append_with_peer_white(ple);} -#define PRINT_HEAD(step) {std::list bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;} +#define PRINT_HEAD(step) {std::vector bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;} ADD_GRAY_NODE(MAKE_IP(123,43,12,1), 8080, 121241, 34345); ADD_GRAY_NODE(MAKE_IP(123,43,12,2), 8080, 121241, 34345); @@ -50,7 +50,7 @@ TEST(peer_list, peer_list_general) size_t gray_list_size = plm.get_gray_peers_count(); ASSERT_EQ(gray_list_size, 1); - std::list bs_head; + std::vector bs_head; bool r = plm.get_peerlist_head(bs_head, 100); std::cout << bs_head.size() << std::endl; ASSERT_TRUE(r); @@ -70,7 +70,7 @@ TEST(peer_list, merge_peer_lists) //ADD_NODE_TO_PL("\2", \3, 0x\1, (1353346618 -(\4*60*60*24+\5*60*60+\6*60+\7 )));\n PeerlistManager plm; plm.init(false); - std::list outer_bs; + std::vector outer_bs; }