Onionr/onionr/onionrpeers/__init__.py

65 lines
2.1 KiB
Python
Raw Normal View History

'''
2019-06-16 06:06:32 +00:00
Onionr - Private P2P Communication
2018-07-27 03:07:50 +00:00
This file contains both the PeerProfiles class for network profiling of Onionr nodes
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-07-27 03:07:50 +00:00
'''
2019-07-12 17:21:08 +00:00
from . import scoresortedpeerlist, peercleanup
get_score_sorted_peer_list = scoresortedpeerlist.get_score_sorted_peer_list
peer_cleanup = peercleanup.peer_cleanup
2018-07-27 03:07:50 +00:00
class PeerProfiles:
'''
PeerProfiles
'''
def __init__(self, address, coreInst):
self.address = address # node address
self.score = None
self.friendSigCount = 0
self.success = 0
self.failure = 0
2019-02-02 03:49:11 +00:00
self.connectTime = None
2018-07-27 03:07:50 +00:00
self.coreInst = coreInst
self.loadScore()
2019-02-02 03:49:11 +00:00
self.getConnectTime()
2018-07-27 03:07:50 +00:00
return
def loadScore(self):
'''Load the node's score from the database'''
try:
self.success = int(self.coreInst.getAddressInfo(self.address, 'success'))
except (TypeError, ValueError) as e:
2018-07-27 03:07:50 +00:00
self.success = 0
self.score = self.success
2019-02-02 03:49:11 +00:00
def getConnectTime(self):
try:
self.connectTime = int(self.coreInst.getAddressInfo(self.address, 'lastConnect'))
except (KeyError, ValueError, TypeError) as e:
pass
2018-07-27 03:07:50 +00:00
def saveScore(self):
'''Save the node's score to the database'''
self.coreInst.setAddressInfo(self.address, 'success', self.score)
2018-07-27 03:07:50 +00:00
return
def addScore(self, toAdd):
'''Add to the peer's score (can add negative)'''
self.score += toAdd
self.saveScore()