Onionr/src/onionrpeers/peerprofiles.py

82 lines
3.0 KiB
Python
Raw Normal View History

2019-07-12 17:51:35 +00:00
'''
Onionr - Private P2P Communication
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/>.
'''
2019-07-20 06:02:30 +00:00
from coredb import keydb
from onionrutils import epoch
2019-09-14 06:29:31 +00:00
from onionrutils import stringvalidators
from onionrblocks import onionrblacklist
2019-09-14 06:29:31 +00:00
import onionrexceptions
UPDATE_DELAY = 300
2019-07-12 17:51:35 +00:00
class PeerProfiles:
'''
PeerProfiles
'''
2019-07-19 19:49:56 +00:00
def __init__(self, address):
2019-09-14 06:29:31 +00:00
if not stringvalidators.validate_transport(address): raise onionrexceptions.InvalidAddress
2019-07-12 17:51:35 +00:00
self.address = address # node address
self.score = None
self.friendSigCount = 0
self.success = 0
self.failure = 0
self.connectTime = None
self.loadScore()
self.getConnectTime()
self.last_updated = {'connect_time': UPDATE_DELAY, 'score': UPDATE_DELAY} # Last time a given value was updated
2019-09-14 06:29:31 +00:00
if not address in keydb.listkeys.list_adders() and not onionrblacklist.OnionrBlackList().inBlacklist(address):
keydb.addkeys.add_address(address)
2019-07-12 17:51:35 +00:00
def loadScore(self):
'''Load the node's score from the database'''
try:
2019-07-20 06:02:30 +00:00
self.success = int(keydb.transportinfo.get_address_info(self.address, 'success'))
2019-07-12 17:51:35 +00:00
except (TypeError, ValueError) as e:
self.success = 0
self.score = self.success
def getConnectTime(self):
2019-09-14 06:29:31 +00:00
"""set the connectTime variable for when we last connected to them, using the db value"""
2019-07-12 17:51:35 +00:00
try:
2019-07-20 06:02:30 +00:00
self.connectTime = int(keydb.transportinfo.get_address_info(self.address, 'lastConnect'))
2019-07-12 17:51:35 +00:00
except (KeyError, ValueError, TypeError) as e:
pass
2019-09-14 06:29:31 +00:00
else:
return self.connectTime
def update_connect_time(self):
if epoch.get_epoch() - self.last_updated['connect_time'] >= UPDATE_DELAY:
self.last_updated['connect_time'] = epoch.get_epoch()
keydb.transportinfo.set_address_info(self.address, 'lastConnect', epoch.get_epoch())
2019-07-12 17:51:35 +00:00
def saveScore(self):
'''Save the node's score to the database'''
if epoch.get_epoch() - self.last_updated['score'] >= UPDATE_DELAY:
self.last_updated['score'] = epoch.get_epoch()
keydb.transportinfo.set_address_info(self.address, 'success', self.score)
2019-07-12 17:51:35 +00:00
return
def addScore(self, toAdd):
'''Add to the peer's score (can add negative)'''
self.score += toAdd
2019-09-14 06:29:31 +00:00
self.saveScore()