peers scores are now calculated on request success and are saved, WIP

master
Kevin Froman 2018-07-30 17:48:29 -05:00
parent dda5c2885d
commit 4decda916d
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
2 changed files with 33 additions and 9 deletions

View File

@ -293,6 +293,14 @@ class OnionrCommunicatorDaemon:
if address not in self.onlinePeers: if address not in self.onlinePeers:
self.onlinePeers.append(address) self.onlinePeers.append(address)
retData = 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 break
else: else:
tried.append(address) tried.append(address)
@ -306,7 +314,8 @@ class OnionrCommunicatorDaemon:
else: else:
logger.info('Online peers:') logger.info('Online peers:')
for i in self.onlinePeers: 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=''): def peerAction(self, peer, action, data=''):
'''Perform a get request to a peer''' '''Perform a get request to a peer'''
@ -320,10 +329,25 @@ class OnionrCommunicatorDaemon:
# if request failed, (error), mark peer offline # if request failed, (error), mark peer offline
if retData == False: if retData == False:
try: try:
self.getPeerProfileInstance(peer).addScore(-1)
self.onlinePeers.remove(peer) self.onlinePeers.remove(peer)
self.getOnlinePeers() # Will only add a new peer to pool if needed self.getOnlinePeers() # Will only add a new peer to pool if needed
except ValueError: except ValueError:
pass 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 return retData
def heartbeat(self): def heartbeat(self):

View File

@ -40,21 +40,21 @@ class PeerProfiles:
def loadScore(self): def loadScore(self):
'''Load the node's score from the database''' '''Load the node's score from the database'''
try: try:
self.success = int(self.coreInst.getAddressInfo('success')) self.success = int(self.coreInst.getAddressInfo('vd33hdxlp342nnzc.onion', 'success'))
except TypeError: except TypeError:
self.success = 0 self.success = 0
try: self.score = self.success
self.failure = int(self.coreInst.getAddressInfo('failure'))
except TypeError:
self.failure = 0
self.score = self.success - self.failure
def saveScore(self): def saveScore(self):
'''Save the node's score to the database''' '''Save the node's score to the database'''
self.coreInst.setAddressInfo(self.address, 'success', self.success) self.coreInst.setAddressInfo(self.address, 'success', self.score)
self.coreInst.setAddressInfo(self.address, 'failure', self.failure)
return return
def addScore(self, toAdd):
'''Add to the peer's score (can add negative)'''
self.score += toAdd
self.saveScore()
def getScoreSortedPeerList(coreInst): def getScoreSortedPeerList(coreInst):
if not type(coreInst is core.Core): if not type(coreInst is core.Core):
raise TypeError('coreInst must be instance of core.Core') raise TypeError('coreInst must be instance of core.Core')