Remove self connection on peer discovery (#269)

* Resetting last shard syncing flags when sync is abandoned. (#268)

* Remove self loopback connection when self is added by peer discovery

* Resolved PR comments

Co-authored-by: Savinda Senevirathne <savindadilsara@gmail.com>
This commit is contained in:
Chalith Desaman
2021-03-12 19:01:58 +05:30
committed by GitHub
parent af4c9df9dc
commit 9999c28a37
3 changed files with 29 additions and 0 deletions

View File

@@ -112,6 +112,24 @@ namespace p2p
if (res == 0)
{
LOG_DEBUG << "Pubkey violation. Rejecting new peer connection [" << session.display_name() << "]";
// It's possible, Self node might've been added to the known peers by peer discovery.
// If so remove the self from known peers.
if (session.known_ipport.has_value())
{
// We set self ip port values so that we can remove self from the future known peer responses.
self::ip_port = conf::peer_ip_port{session.known_ipport->host_address, session.known_ipport->port};
{
std::scoped_lock lock(ctx.server->req_known_remotes_mutex);
ctx.server->req_known_remotes.erase(std::remove_if(ctx.server->req_known_remotes.begin(), ctx.server->req_known_remotes.end(),
[&](const p2p::peer_properties &peer) {
return peer.ip_port.port == session.known_ipport->port;
}));
ctx.server->known_remote_count = ctx.server->req_known_remotes.size();
}
LOG_DEBUG << "Loopback connection detected: Removed self from the peer list.";
}
return -1;
}
@@ -413,6 +431,13 @@ namespace p2p
for (const peer_properties &peer : peers)
{
// If the peer is self, we won't add to the known peer list.
if (self::ip_port.has_value() && self::ip_port == peer.ip_port)
{
LOG_DEBUG << "Rejecting " + peer.ip_port.host_address + ":" + std::to_string(peer.ip_port.port) + ". Loopback connection.";
continue;
}
const auto itr = std::find_if(ctx.server->req_known_remotes.begin(), ctx.server->req_known_remotes.end(), [&](peer_properties &p) { return p.ip_port == peer.ip_port; });
// If the new peer is not in the peer list then add to the req_known_remotes

View File

@@ -6,6 +6,8 @@ namespace p2p::self
// Holds self messages waiting to be processed.
moodycamel::ConcurrentQueue<std::string> msg_queue;
std::optional<conf::peer_ip_port> ip_port;
/**
* Processes the next queued message (if any).
* @return 0 if no messages in queue. 1 if message was processed successfully. -1 on error.

View File

@@ -5,6 +5,8 @@
namespace p2p::self
{
extern std::optional<conf::peer_ip_port> ip_port;
int process_next_message();
void send(std::string_view message);