Persist discovered peers to config. (#246)

This commit is contained in:
Ravin Perera
2021-02-16 16:20:19 +05:30
committed by GitHub
parent 9d1163c8c3
commit 90641e0849
9 changed files with 41 additions and 16 deletions

View File

@@ -789,6 +789,21 @@ namespace conf
return 0;
}
/**
* Persists the specified known peers to the config file.
*/
int persist_known_peers_config(const std::vector<peer_properties> &peers)
{
if (peers.empty())
return 0;
const size_t max_count = conf::cfg.mesh.max_known_connections == 0
? peers.size()
: MIN(peers.size(), conf::cfg.mesh.max_known_connections);
cfg.mesh.known_peers = std::vector<peer_properties>(peers.begin(), peers.begin() + max_count);
return write_config(cfg);
}
/**
* Locks the config file. If already locked means there's another hpcore instance running in the same directory.
* If so, log error and return, Otherwise lock the config.

View File

@@ -12,17 +12,17 @@ namespace conf
constexpr size_t CONCURRENT_READ_REQUEST_MAX_LIMIT = 32;
// Struct to represent ip and port of the peer.
struct ip_port_prop
struct peer_ip_port
{
std::string host_address;
uint16_t port;
bool operator==(ip_port_prop ip_port)
bool operator==(const peer_ip_port &ip_port)
{
return host_address == ip_port.host_address && port == ip_port.port;
}
bool operator!=(ip_port_prop ip_port)
bool operator!=(const peer_ip_port &ip_port)
{
return !(host_address == ip_port.host_address && port == ip_port.port);
}
@@ -33,7 +33,7 @@ namespace conf
// Later it will be updated according to the capacity anouncement from the peers.
struct peer_properties
{
ip_port_prop ip_port;
peer_ip_port ip_port;
int16_t available_capacity = -1;
uint64_t timestamp = 0;
};
@@ -234,6 +234,8 @@ namespace conf
int apply_patch_config(std::string_view hpfs_session_name);
int persist_known_peers_config(const std::vector<peer_properties> &peers);
int set_config_lock();
int release_config_lock();

View File

@@ -670,7 +670,7 @@ namespace msg::fbuf::p2pmsg
* @param skipping_peer Peer that does not need to be sent.
* @param lcl Lcl value to be passed in the container message.
*/
void create_msg_from_peer_list_response(flatbuffers::FlatBufferBuilder &container_builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::ip_port_prop> &skipping_ip_port, std::string_view lcl)
void create_msg_from_peer_list_response(flatbuffers::FlatBufferBuilder &container_builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::peer_ip_port> &skipping_ip_port, std::string_view lcl)
{
flatbuffers::FlatBufferBuilder builder(1024);
@@ -864,7 +864,7 @@ namespace msg::fbuf::p2pmsg
* @param skipping_peer Peer that does not need to be sent.
*/
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Peer_Properties>>>
peer_propertiesvector_to_flatbuf_peer_propertieslist(flatbuffers::FlatBufferBuilder &builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::ip_port_prop> &skipping_ip_port)
peer_propertiesvector_to_flatbuf_peer_propertieslist(flatbuffers::FlatBufferBuilder &builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::peer_ip_port> &skipping_ip_port)
{
std::vector<flatbuffers::Offset<Peer_Properties>> fbvec;
fbvec.reserve(peers.size());

View File

@@ -76,7 +76,7 @@ namespace msg::fbuf::p2pmsg
void create_msg_from_peer_list_request(flatbuffers::FlatBufferBuilder &container_builder, std::string_view lcl);
void create_msg_from_peer_list_response(flatbuffers::FlatBufferBuilder &container_builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::ip_port_prop> &skipping_ip_port, std::string_view lcl);
void create_msg_from_peer_list_response(flatbuffers::FlatBufferBuilder &container_builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::peer_ip_port> &skipping_ip_port, std::string_view lcl);
//---Conversion helpers from flatbuffers data types to std data types---//
@@ -98,7 +98,7 @@ namespace msg::fbuf::p2pmsg
historyledgermap_to_flatbuf_historyledgermap(flatbuffers::FlatBufferBuilder &builder, const std::map<uint64_t, const p2p::history_ledger_block> &map);
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Peer_Properties>>>
peer_propertiesvector_to_flatbuf_peer_propertieslist(flatbuffers::FlatBufferBuilder &builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::ip_port_prop> &skipping_ip_port);
peer_propertiesvector_to_flatbuf_peer_propertieslist(flatbuffers::FlatBufferBuilder &builder, const std::vector<conf::peer_properties> &peers, const std::optional<conf::peer_ip_port> &skipping_ip_port);
void flatbuf_hpfsfshashentry_to_hpfsfshashentry(std::unordered_map<std::string, p2p::hpfs_fs_hash_entry> &fs_entries,
const flatbuffers::Vector<flatbuffers::Offset<Hpfs_FS_Hash_Entry>> *fhashes);

View File

@@ -44,7 +44,15 @@ namespace p2p
void deinit()
{
if (init_success)
{
// Persist latest known peers information to config before the peer server is stopped.
{
std::scoped_lock lock(ctx.server->req_known_remotes_mutex);
conf::persist_known_peers_config(ctx.server->req_known_remotes);
}
ctx.server->stop();
}
}
int start_peer_connections()
@@ -325,7 +333,7 @@ namespace p2p
* @param available_capacity Available capacity of the known peer, -1 if number of connections is unlimited.
* @param timestamp Capacity announced time.
*/
void update_known_peer_available_capacity(const conf::ip_port_prop &ip_port, const int16_t available_capacity, const uint64_t &timestamp)
void update_known_peer_available_capacity(const conf::peer_ip_port &ip_port, const int16_t available_capacity, const uint64_t &timestamp)
{
std::scoped_lock<std::mutex> lock(ctx.server->req_known_remotes_mutex);

View File

@@ -183,7 +183,7 @@ namespace p2p
void send_peer_list_request();
void update_known_peer_available_capacity(const conf::ip_port_prop &ip_port, const int16_t available_capacity, const uint64_t &timestamp);
void update_known_peer_available_capacity(const conf::peer_ip_port &ip_port, const int16_t available_capacity, const uint64_t &timestamp);
void merge_peer_list(const std::vector<conf::peer_properties> &peers);

View File

@@ -16,9 +16,9 @@ namespace p2p
peer_comm_server::peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[5], const uint64_t max_msg_size,
const uint64_t max_in_connections, const uint64_t max_in_connections_per_host,
std::vector<conf::peer_properties> &req_known_remotes)
const std::vector<conf::peer_properties> &req_known_remotes)
: comm::comm_server<peer_comm_session>("Peer", port, metric_thresholds, max_msg_size, max_in_connections, max_in_connections_per_host),
req_known_remotes(req_known_remotes)
req_known_remotes(req_known_remotes) // Copy over known peers into internal collection.
{
}
@@ -115,7 +115,7 @@ namespace p2p
void peer_comm_server::maintain_known_connections()
{
// Find already connected known remote parties list.
std::vector<conf::ip_port_prop> known_remotes;
std::vector<conf::peer_ip_port> known_remotes;
{
std::scoped_lock<std::mutex> lock(sessions_mutex);

View File

@@ -30,10 +30,10 @@ namespace p2p
public:
std::atomic<uint16_t> known_remote_count = 0;
std::mutex req_known_remotes_mutex;
std::vector<conf::peer_properties> &req_known_remotes;
std::vector<conf::peer_properties> req_known_remotes;
peer_comm_server(const uint16_t port, const uint64_t (&metric_thresholds)[5], const uint64_t max_msg_size,
const uint64_t max_in_connections, const uint64_t max_in_connections_per_host,
std::vector<conf::peer_properties> &req_known_remotes);
const std::vector<conf::peer_properties> &req_known_remotes);
};
} // namespace p2p

View File

@@ -21,7 +21,7 @@ namespace p2p
void handle_on_verified();
public:
std::optional<conf::ip_port_prop> known_ipport; // A known ip/port information that matches with our peer list configuration.
std::optional<conf::peer_ip_port> known_ipport; // A known ip/port information that matches with our peer list configuration.
bool need_consensus_msg_forwarding = false; // Holds whether this node requires consensus message forwarding.
bool is_unl = false; // Whether this session's pubkey is in unl list.
uint16_t reported_roundtime = 0; // Initial roundtime reported by this peer on peer challenge.