From d61b70213c08d2c9fd3ee6c1b91667c9c877f5dd Mon Sep 17 00:00:00 2001 From: grapeLux2 Date: Tue, 7 Jan 2020 21:46:01 +0000 Subject: [PATCH 1/4] peer limiting with best ping preference --- CMakeLists.txt | 2 +- src/net.cpp | 52 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eeae4c08..a5fdfe61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10.3) project(lux) set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") add_custom_target(build-lux ALL COMMAND ./autogen.sh diff --git a/src/net.cpp b/src/net.cpp index 41868d37..358863a5 100755 --- a/src/net.cpp +++ b/src/net.cpp @@ -1596,40 +1596,65 @@ 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); } + if (num >= 16){ // I hope this ever gets run..... + LogPrintf("Threadlimitpeers(): peer limit (%d) is too high. limit set to 16 peers.\n", num); + num = 16; + } + for ( ; getVerificationProgress(NULL) < 0.999 ; ){ // 99.9% limit_peers(num); - MilliSleep(1000); + MilliSleep(10000); } } } void limit_peers(int i) { - i--; LOCK(cs_main); + i--; + vector 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 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++; + } } } @@ -1664,7 +1689,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSem return true; } - void ThreadMessageHandler() { boost::mutex condition_mutex; boost::unique_lock lock(condition_mutex); From 8a2d2dff5c06e32e1bb45be6d7acc0023553226a Mon Sep 17 00:00:00 2001 From: grape_lux 2 <33639665+grapeLux2@users.noreply.github.com> Date: Tue, 7 Jan 2020 21:49:46 +0000 Subject: [PATCH 2/4] removed debug build flag --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5fdfe61..eeae4c08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10.3) project(lux) set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") add_custom_target(build-lux ALL COMMAND ./autogen.sh From ac9a4f6e14e2a63a015e24f26e0cc087a8be44bf Mon Sep 17 00:00:00 2001 From: grape_lux 2 <33639665+grapeLux2@users.noreply.github.com> Date: Tue, 7 Jan 2020 21:55:47 +0000 Subject: [PATCH 3/4] Update net.cpp --- src/net.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net.cpp b/src/net.cpp index 358863a5..2df597f9 100755 --- a/src/net.cpp +++ b/src/net.cpp @@ -1609,7 +1609,7 @@ void Threadlimitpeers(){ LogPrintf("Threadlimitpeers(): peer limit (%d) is too low. limit set to 4 peers.\n", num); } - if (num >= 16){ // I hope this ever gets run..... + 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; } From 16f1ab16934bfc474feaa89f8b44a850bb50c7fe Mon Sep 17 00:00:00 2001 From: grapeLux2 Date: Tue, 14 Jan 2020 18:59:10 +0000 Subject: [PATCH 4/4] lowered resource usage --- src/net.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 2df597f9..43fb97d9 100755 --- a/src/net.cpp +++ b/src/net.cpp @@ -1581,7 +1581,7 @@ 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(tipIn); if (!tip) @@ -1589,7 +1589,7 @@ double getVerificationProgress(const CBlockIndex *tipIn) // here to avoid includ 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); @@ -1614,7 +1614,7 @@ void Threadlimitpeers(){ num = 16; } - for ( ; getVerificationProgress(NULL) < 0.999 ; ){ // 99.9% + for ( ; getVerificationProgress(NULL) ; ){ // 99.9% limit_peers(num); MilliSleep(10000); }