Skip to content

Commit

Permalink
p2p: use vector instead of list for peer lists
Browse files Browse the repository at this point in the history
  • Loading branch information
aivve committed Dec 10, 2020
1 parent 0971fb6 commit f5ca243
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/P2p/NetNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_add_exclus
const command_line::arg_descriptor<std::vector<std::string> > arg_p2p_seed_node = {"seed-node", "Connect to a node to retrieve peer addresses, and disconnect"};
const command_line::arg_descriptor<bool> 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<PeerlistEntry>& pl) {
std::string print_peerlist_to_string(const std::vector<PeerlistEntry>& pl) {
time_t now_time = 0;
time(&now_time);
std::stringstream ss;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ std::string print_banlist_to_string(std::map<uint32_t, time_t> list) {
}

//-----------------------------------------------------------------------------------
bool NodeServer::fix_time_delta(std::list<PeerlistEntry>& local_peerlist, time_t local_time, int64_t& delta)
bool NodeServer::fix_time_delta(std::vector<PeerlistEntry>& local_peerlist, time_t local_time, int64_t& delta)
{
//fix time delta
time_t now = 0;
Expand All @@ -1184,7 +1184,7 @@ std::string print_banlist_to_string(std::map<uint32_t, time_t> list) {
}

//-----------------------------------------------------------------------------------
bool NodeServer::handle_remote_peerlist(const std::list<PeerlistEntry>& peerlist, time_t local_time, const CryptoNoteConnectionContext& context)
bool NodeServer::handle_remote_peerlist(const std::vector<PeerlistEntry>& peerlist, time_t local_time, const CryptoNoteConnectionContext& context)
{
if (peerlist.size() > P2P_MAX_PEERS_IN_HANDSHAKE)
{
Expand All @@ -1193,7 +1193,7 @@ std::string print_banlist_to_string(std::map<uint32_t, time_t> list) {
}

int64_t delta = 0;
std::list<PeerlistEntry> peerlist_ = peerlist;
std::vector<PeerlistEntry> 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();
Expand Down Expand Up @@ -1462,8 +1462,8 @@ std::string print_banlist_to_string(std::map<uint32_t, time_t> list) {
bool NodeServer::log_peerlist()
{
std::list<AnchorPeerlistEntry> pl_anchor;
std::list<PeerlistEntry> pl_wite;
std::list<PeerlistEntry> pl_gray;
std::vector<PeerlistEntry> pl_wite;
std::vector<PeerlistEntry> 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
Expand Down
6 changes: 3 additions & 3 deletions src/P2p/NetNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ namespace CryptoNote
bool handleConfig(const NetNodeConfig& config);
bool append_net_address(std::vector<NetworkAddress>& nodes, const std::string& addr);
bool idle_worker();
bool handle_remote_peerlist(const std::list<PeerlistEntry>& peerlist, time_t local_time, const CryptoNoteConnectionContext& context);
bool handle_remote_peerlist(const std::vector<PeerlistEntry>& peerlist, time_t local_time, const CryptoNoteConnectionContext& context);
bool get_local_node_data(basic_node_data& node_data);

bool fix_time_delta(std::list<PeerlistEntry>& local_peerlist, time_t local_time, int64_t& delta);
bool fix_time_delta(std::vector<PeerlistEntry>& local_peerlist, time_t local_time, int64_t& delta);

bool connections_maker();
bool make_new_connection_from_peerlist(bool use_white_list);
Expand Down Expand Up @@ -286,7 +286,7 @@ namespace CryptoNote
std::vector<NetworkAddress> m_priority_peers;
std::vector<NetworkAddress> m_exclusive_peers;
std::vector<NetworkAddress> m_seed_nodes;
std::list<PeerlistEntry> m_command_line_peers;
std::vector<PeerlistEntry> m_command_line_peers;
uint64_t m_peer_livetime;
boost::uuids::uuid m_network_id;
std::map<uint32_t, time_t> m_blocked_hosts;
Expand Down
8 changes: 4 additions & 4 deletions src/P2p/P2pProtocolDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace CryptoNote
{
basic_node_data node_data;
CORE_SYNC_DATA payload_data;
std::list<PeerlistEntry> local_peerlist;
std::vector<PeerlistEntry> local_peerlist;

void serialize(ISerializer& s) {
KV_MEMBER(node_data)
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace CryptoNote
{
uint64_t local_time;
CORE_SYNC_DATA payload_data;
std::list<PeerlistEntry> local_peerlist;
std::vector<PeerlistEntry> local_peerlist;

void serialize(ISerializer& s) {
KV_MEMBER(local_time)
Expand Down Expand Up @@ -260,8 +260,8 @@ namespace CryptoNote
struct response
{
std::list<AnchorPeerlistEntry> local_peerlist_anchor;
std::list<PeerlistEntry> local_peerlist_white;
std::list<PeerlistEntry> local_peerlist_gray;
std::vector<PeerlistEntry> local_peerlist_white;
std::vector<PeerlistEntry> local_peerlist_gray;
std::list<connection_entry> connections_list;
PeerIdType my_id;
uint64_t local_time;
Expand Down
6 changes: 3 additions & 3 deletions src/P2p/PeerListManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void PeerlistManager::trim_gray_peerlist() {
}

//--------------------------------------------------------------------------------------------------
bool PeerlistManager::merge_peerlist(const std::list<PeerlistEntry>& outer_bs)
bool PeerlistManager::merge_peerlist(const std::vector<PeerlistEntry>& outer_bs)
{
for(const PeerlistEntry& be : outer_bs) {
append_with_peer_gray(be);
Expand Down Expand Up @@ -161,7 +161,7 @@ bool PeerlistManager::is_ip_allowed(uint32_t ip) const
}

//--------------------------------------------------------------------------------------------------
bool PeerlistManager::get_peerlist_head(std::list<PeerlistEntry>& bs_head, uint32_t depth) const
bool PeerlistManager::get_peerlist_head(std::vector<PeerlistEntry>& bs_head, uint32_t depth) const
{
const peers_indexed::index<by_time>::type& by_time_index = m_peers_white.get<by_time>();
uint32_t cnt = 0;
Expand All @@ -180,7 +180,7 @@ bool PeerlistManager::get_peerlist_head(std::list<PeerlistEntry>& bs_head, uint3
}

//--------------------------------------------------------------------------------------------------
bool PeerlistManager::get_peerlist_full(std::list<AnchorPeerlistEntry>& pl_anchor, std::list<PeerlistEntry>& pl_gray, std::list<PeerlistEntry>& pl_white) const
bool PeerlistManager::get_peerlist_full(std::list<AnchorPeerlistEntry>& pl_anchor, std::vector<PeerlistEntry>& pl_gray, std::vector<PeerlistEntry>& pl_white) const
{
const anchor_peers_indexed::index<by_time>::type& by_time_index_an = m_peers_anchor.get<by_time>();
const peers_indexed::index<by_time>::type& by_time_index_gr = m_peers_gray.get<by_time>();
Expand Down
6 changes: 3 additions & 3 deletions src/P2p/PeerListManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerlistEntry>& outer_bs);
bool get_peerlist_head(std::list<PeerlistEntry>& bs_head, uint32_t depth = CryptoNote::P2P_DEFAULT_PEERS_IN_HANDSHAKE) const;
bool get_peerlist_full(std::list<AnchorPeerlistEntry>& pl_anchor, std::list<PeerlistEntry>& pl_gray, std::list<PeerlistEntry>& pl_white) const;
bool merge_peerlist(const std::vector<PeerlistEntry>& outer_bs);
bool get_peerlist_head(std::vector<PeerlistEntry>& bs_head, uint32_t depth = CryptoNote::P2P_DEFAULT_PEERS_IN_HANDSHAKE) const;
bool get_peerlist_full(std::list<AnchorPeerlistEntry>& pl_anchor, std::vector<PeerlistEntry>& pl_gray, std::vector<PeerlistEntry>& 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);
Expand Down
4 changes: 2 additions & 2 deletions src/Rpc/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,8 @@ bool RpcServer::onGetPeerList(const COMMAND_RPC_GET_PEER_LIST::request& req, COM
}

std::list<AnchorPeerlistEntry> pl_anchor;
std::list<PeerlistEntry> pl_wite;
std::list<PeerlistEntry> pl_gray;
std::vector<PeerlistEntry> pl_wite;
std::vector<PeerlistEntry> pl_gray;
m_p2p.getPeerlistManager().get_peerlist_full(pl_anchor, pl_gray, pl_wite);
for (const auto& pe : pl_anchor) {
std::stringstream ss;
Expand Down
6 changes: 3 additions & 3 deletions tests/UnitTests/TestPeerlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeerlistEntry> 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<PeerlistEntry> 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);
Expand All @@ -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<PeerlistEntry> bs_head;
std::vector<PeerlistEntry> bs_head;
bool r = plm.get_peerlist_head(bs_head, 100);
std::cout << bs_head.size() << std::endl;
ASSERT_TRUE(r);
Expand All @@ -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<PeerlistEntry> outer_bs;
std::vector<PeerlistEntry> outer_bs;


}

0 comments on commit f5ca243

Please sign in to comment.