This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track external UDP endpoints seen by the peers.
- Loading branch information
Showing
4 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Aleth: Ethereum C++ client, tools and libraries. | ||
// Copyright 2019 Aleth Authors. | ||
// Licensed under the GNU General Public License, Version 3. | ||
|
||
#include "EndpointTracker.h" | ||
|
||
namespace dev | ||
{ | ||
namespace p2p | ||
{ | ||
namespace | ||
{ | ||
// Interval during which each endpoint statement is kept | ||
constexpr std::chrono::minutes c_statementTimeToLiveMin{5}; | ||
} // namespace | ||
|
||
// Register the statement about endpoint from one othe peers. | ||
// Returns number of currently kept statements in favor of _externalEndpoint | ||
size_t EndpointTracker::addEndpointStatement( | ||
bi::udp::endpoint const& _sourceEndpoint, bi::udp::endpoint const& _externalEndpoint) | ||
{ | ||
// remove previous statement by this peer | ||
auto it = m_statementsMap.find(_sourceEndpoint); | ||
if (it != m_statementsMap.end()) | ||
removeStatement(it); | ||
|
||
return addStatement(_sourceEndpoint, _externalEndpoint); | ||
} | ||
|
||
// Find endpoint with max number of statemens | ||
bi::udp::endpoint EndpointTracker::bestEndpoint() const | ||
{ | ||
size_t maxCount = 0; | ||
bi::udp::endpoint bestEndpoint; | ||
for (auto const& endpointAndCount : m_endpointStatementCountMap) | ||
if (endpointAndCount.second > maxCount) | ||
std::tie(bestEndpoint, maxCount) = endpointAndCount; | ||
|
||
return bestEndpoint; | ||
} | ||
|
||
// Remove old statements | ||
void EndpointTracker::garbageCollectStatements() | ||
{ | ||
auto const expiration = std::chrono::steady_clock::now() - c_statementTimeToLiveMin; | ||
for (auto it = m_statementsMap.begin(); it != m_statementsMap.end();) | ||
{ | ||
if (it->second.second > expiration) | ||
it = removeStatement(it); | ||
else | ||
++it; | ||
} | ||
} | ||
|
||
size_t EndpointTracker::addStatement( | ||
bi::udp::endpoint const& _sourceEndpoint, bi::udp::endpoint const& _externalEndpoint) | ||
{ | ||
EndpointAndTimePoint endpointAndTime{_externalEndpoint, std::chrono::steady_clock::now()}; | ||
m_statementsMap.insert({_sourceEndpoint, endpointAndTime}); | ||
return ++m_endpointStatementCountMap[_externalEndpoint]; | ||
} | ||
|
||
EndpointTracker::SourceToStatementMap::iterator EndpointTracker::removeStatement( | ||
SourceToStatementMap::iterator _it) | ||
{ | ||
// first decrement statement counter | ||
auto itCount = m_endpointStatementCountMap.find(_it->second.first); | ||
assert(itCount != m_endpointStatementCountMap.end() && itCount->second > 0); | ||
if (--itCount->second == 0) | ||
m_endpointStatementCountMap.erase(itCount); | ||
|
||
return m_statementsMap.erase(_it); | ||
} | ||
|
||
} // namespace p2p | ||
} // namespace dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Aleth: Ethereum C++ client, tools and libraries. | ||
// Copyright 2019 Aleth Authors. | ||
// Licensed under the GNU General Public License, Version 3. | ||
|
||
#pragma once | ||
|
||
#include "Common.h" | ||
|
||
namespace dev | ||
{ | ||
namespace p2p | ||
{ | ||
// Class for keeping track of our external endpoint as seen by our peers. | ||
// Keeps track of what external endpoint is seen by every peer | ||
// and finds which endpoint is reported most often | ||
class EndpointTracker | ||
{ | ||
public: | ||
// Register the statement about endpoint from one othe peers. | ||
// Returns number of currently kept statements in favor of _externalEndpoint | ||
size_t addEndpointStatement( | ||
bi::udp::endpoint const& _sourceEndpoint, bi::udp::endpoint const& _externalEndpoint); | ||
|
||
// Find endpoint with max number of statemens | ||
bi::udp::endpoint bestEndpoint() const; | ||
|
||
// Remove old statements | ||
void garbageCollectStatements(); | ||
|
||
private: | ||
using EndpointAndTimePoint = | ||
std::pair<bi::udp::endpoint, std::chrono::steady_clock::time_point>; | ||
using SourceToStatementMap = std::map<bi::udp::endpoint, EndpointAndTimePoint>; | ||
|
||
size_t addStatement( | ||
bi::udp::endpoint const& _sourceEndpoint, bi::udp::endpoint const& _externalEndpoint); | ||
|
||
SourceToStatementMap::iterator removeStatement(SourceToStatementMap::iterator _it); | ||
|
||
// Statements about our external endpoint, maps statement source peer => endpoint, timestamp | ||
SourceToStatementMap m_statementsMap; | ||
// map external endpoint => how many sources reported it | ||
std::map<bi::udp::endpoint, size_t> m_endpointStatementCountMap; | ||
}; | ||
|
||
} // namespace p2p | ||
} // namespace dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters