Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peer limiting with low peer ping Preference #97

Open
wants to merge 5 commits into
base: master_candidate
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,55 +1581,80 @@ void ThreadOpenAddedConnections()
}
}

double getVerificationProgress(const CBlockIndex *tipIn) // here to avoid include loops
bool getVerificationProgress(const CBlockIndex *tipIn) // here to avoid include loops
{
CBlockIndex *tip = const_cast<CBlockIndex *>(tipIn);
if (!tip)
{
LOCK(cs_main);
tip = chainActive.Tip();
}
return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), tip);
return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), tip) < 0.999;
}

void limit_peers(int i);
void Threadlimitpeers(){

if(mapArgs.count("-peerlimit")){ // don't read what is not readable
std::string str = mapArgs["-peerlimit"];
int num = std::stoi( str );
std::string str2 = mapArgs["-peerlimit"];
int num = std::stoi( str2 );

if (num==0){ // peer sync limit off. mapArgs should do the job but, peerlimit=0 is more reassuring to the user then peerlimit not being there
if (num==0){ // Peer sync limit off. mapArgs should do the job but, peerlimit=0 is more reassuring to the user then peerlimit not being there
LogPrintf("Threadlimitpeers(): peer limiting disabled\n");
return;
}

if (num <= 3){ // don't allow less then 4 peers
if (num <= 3){ // Don't allow less then 4 peers
num = 4;
LogPrintf("Threadlimitpeers(): peer limit (%d) is too low. limit set to 4 peers.\n", num);
}

for ( ; getVerificationProgress(NULL) < 0.999 ; ){ // 99.9%
if (num >= 16){ // I hope this never gets run.....
LogPrintf("Threadlimitpeers(): peer limit (%d) is too high. limit set to 16 peers.\n", num);
num = 16;
}

for ( ; getVerificationProgress(NULL) ; ){ // 99.9%
limit_peers(num);
MilliSleep(1000);
MilliSleep(10000);
}
}
}

void limit_peers(int i)
{
i--;
LOCK(cs_main);
i--;

vector<CNodeStats> vstats;
CNodeStats temp;
CopyNodeStats_h(vstats);
int k = 0;
for (CNodeStats& stats : vstats) {
if (vstats.size() > 0){ // avoid a nice Segmentation fault if we have no peers and avoid locking-up resources for nothing
int a=0, j=0, k=0, n = vstats.size();

if (i < k){
string strNode = stats.addrName;
CNode *bannedNode = FindNode(strNode);
bannedNode->CloseSocketDisconnect();
for(a=0;a<n;a++)
{
for(j=a+1;j<n;j++)
{
if(vstats[a].dPingTime > vstats[j].dPingTime)
{
temp = vstats[a];
vstats[a] = vstats[j];
vstats[j] = temp;
}
}
}
k++;

for (CNodeStats& stats : vstats) {

if (i < k){
string strNode = stats.addrName;
CNode *bannedNode = FindNode(strNode);
bannedNode->CloseSocketDisconnect();
LogPrintf("(peer_limit() disconnected a peer) peer ip = %d node-id = %d ping = %dms \n",stats.addrName, stats.nodeid, stats.dPingTime*1000);
}
k++;
}
}
}

Expand Down Expand Up @@ -1664,7 +1689,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSem
return true;
}


void ThreadMessageHandler() {
boost::mutex condition_mutex;
boost::unique_lock<boost::mutex> lock(condition_mutex);
Expand Down