From 8694ab078e845a59c97e378a6faa560caadf7649 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 3 Aug 2018 01:28:26 -0500 Subject: [PATCH] + remove peers who do not meet the minimum score * minor bug fixes and documentation improvements * do not cleanup peers if we have no online peers (probably not connected anyway) * do not run peer cleanup until a min * do not spam block list on shutdown during block sync * use correct thread decrement in peerCleanup * added trust int to address table, not used yet --- onionr/communicator2.py | 9 ++++++--- onionr/core.py | 3 ++- onionr/onionrpeers.py | 20 ++++++++++++++++---- onionr/static-data/default_config.json | 6 +++--- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index 51f9ac12..1f343186 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -77,6 +77,7 @@ class OnionrCommunicatorDaemon: self.header() # Set timers, function reference, seconds + # requiresPeer True means the timer function won't fire if we have no connected peers OnionrCommunicatorTimers(self, self.daemonCommands, 5) OnionrCommunicatorTimers(self, self.detectAPICrash, 5) peerPoolTimer = OnionrCommunicatorTimers(self, self.getOnlinePeers, 60) @@ -85,11 +86,11 @@ class OnionrCommunicatorDaemon: OnionrCommunicatorTimers(self, self.clearOfflinePeer, 58) OnionrCommunicatorTimers(self, self.lookupKeys, 60, requiresPeer=True) OnionrCommunicatorTimers(self, self.lookupAdders, 60, requiresPeer=True) - cleanupTimer = OnionrCommunicatorTimers(self, self.peerCleanup, 300) + cleanupTimer = OnionrCommunicatorTimers(self, self.peerCleanup, 300, requiresPeer=True) # set loop to execute instantly to load up peer pool (replaced old pool init wait) peerPoolTimer.count = (peerPoolTimer.frequency - 1) - cleanupTimer = (cleanupTimer.frequency - 200) + cleanupTimer.count = (cleanupTimer.frequency - 60) # Main daemon loop, mainly for calling timers, don't do any complex operations here to avoid locking try: @@ -171,6 +172,8 @@ class OnionrCommunicatorDaemon: def getBlocks(self): '''download new blocks in queue''' for blockHash in self.blockQueue: + if self.shutdown: + break if blockHash in self.currentDownloading: logger.debug('ALREADY DOWNLOADING ' + blockHash) continue @@ -320,7 +323,7 @@ class OnionrCommunicatorDaemon: def peerCleanup(self): '''This just calls onionrpeers.cleanupPeers, which removes dead or bad peers (offline too long, too slow)''' onionrpeers.peerCleanup(self._core) - self.decrementThreadCount('getOnlinePeers') + self.decrementThreadCount('peerCleanup') def printOnlinePeers(self): '''logs online peer list''' diff --git a/onionr/core.py b/onionr/core.py index c27596df..3219d8aa 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -200,7 +200,8 @@ class Core: powValue text, failure int, lastConnect int, - lastConnectAttempt int + lastConnectAttempt int, + trust int ); ''') conn.commit() diff --git a/onionr/onionrpeers.py b/onionr/onionrpeers.py index e5148c02..135b8d12 100644 --- a/onionr/onionrpeers.py +++ b/onionr/onionrpeers.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import core +import core, config, logger class PeerProfiles: ''' PeerProfiles @@ -72,8 +72,20 @@ def getScoreSortedPeerList(coreInst): return peerList def peerCleanup(coreInst): - # TODO, remove peers that have been offline for too long + '''Removes peers who have been offline too long''' if not type(coreInst is core.Core): raise TypeError('coreInst must be instance of core.Core') - - \ No newline at end of file + + logger.info('Cleaning peers...') + + minScore = int(config.get('peers.minimumScore')) + maxPeers = int(config.get('peers.maxStoredPeers')) + + adders = getScoreSortedPeerList(coreInst) + adders.reverse() + + for address in adders: + # Remove peers that go below the negative score + if PeerProfiles(address, coreInst).score < minScore: + coreInst.removeAddress(address) + logger.warn('Removed address ' + address + '.') \ No newline at end of file diff --git a/onionr/static-data/default_config.json b/onionr/static-data/default_config.json index 5f928f6d..81ba610f 100644 --- a/onionr/static-data/default_config.json +++ b/onionr/static-data/default_config.json @@ -57,8 +57,8 @@ "blockCacheTotal" : 50000000 }, "peers":{ - "minimumScore": 5, - "maxStoredPeers": 100, - "maxConnect": 3 + "minimumScore": -4000, + "maxStoredPeers": 100, + "maxConnect": 3 } }