From 4decda916dfd3fc90d267008e8b627911501c0ac Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 30 Jul 2018 17:48:29 -0500 Subject: [PATCH] peers scores are now calculated on request success and are saved, WIP --- onionr/communicator2.py | 26 +++++++++++++++++++++++++- onionr/onionrpeers.py | 16 ++++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index b420fcba..3ddcc566 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -293,6 +293,14 @@ class OnionrCommunicatorDaemon: if address not in self.onlinePeers: self.onlinePeers.append(address) retData = address + + # add peer to profile list if they're not in it + for profile in self.peerProfiles: + if profile.address == address: + break + else: + self.peerProfiles.append(onionrpeers.PeerProfiles(address, self._core)) + self.getPeerProfileInstance(address).addScore(1) break else: tried.append(address) @@ -306,7 +314,8 @@ class OnionrCommunicatorDaemon: else: logger.info('Online peers:') for i in self.onlinePeers: - logger.info(i) + score = str(self.getPeerProfileInstance(i).score) + logger.info(i + ', score: ' + score) def peerAction(self, peer, action, data=''): '''Perform a get request to a peer''' @@ -320,10 +329,25 @@ class OnionrCommunicatorDaemon: # if request failed, (error), mark peer offline if retData == False: try: + self.getPeerProfileInstance(peer).addScore(-1) self.onlinePeers.remove(peer) self.getOnlinePeers() # Will only add a new peer to pool if needed except ValueError: pass + else: + self.getPeerProfileInstance(peer).addScore(1) + return retData + + def getPeerProfileInstance(self, peer): + '''Gets a peer profile instance from the list of profiles, by address name''' + for i in self.peerProfiles: + # if the peer's profile is already loaded, return that + if i.address == peer: + retData = i + break + else: + # if the peer's profile is not loaded, return a new one. connectNewPeer adds it the list on connect + retData = onionrpeers.PeerProfiles(peer, self._core) return retData def heartbeat(self): diff --git a/onionr/onionrpeers.py b/onionr/onionrpeers.py index b83fa9bc..7ca275b6 100644 --- a/onionr/onionrpeers.py +++ b/onionr/onionrpeers.py @@ -40,21 +40,21 @@ class PeerProfiles: def loadScore(self): '''Load the node's score from the database''' try: - self.success = int(self.coreInst.getAddressInfo('success')) + self.success = int(self.coreInst.getAddressInfo('vd33hdxlp342nnzc.onion', 'success')) except TypeError: self.success = 0 - try: - self.failure = int(self.coreInst.getAddressInfo('failure')) - except TypeError: - self.failure = 0 - self.score = self.success - self.failure + self.score = self.success def saveScore(self): '''Save the node's score to the database''' - self.coreInst.setAddressInfo(self.address, 'success', self.success) - self.coreInst.setAddressInfo(self.address, 'failure', self.failure) + self.coreInst.setAddressInfo(self.address, 'success', self.score) return + def addScore(self, toAdd): + '''Add to the peer's score (can add negative)''' + self.score += toAdd + self.saveScore() + def getScoreSortedPeerList(coreInst): if not type(coreInst is core.Core): raise TypeError('coreInst must be instance of core.Core')