work on new peer profiling system

This commit is contained in:
Kevin Froman 2018-07-26 22:07:50 -05:00
parent 5f1a02e42d
commit afdee2a7a5
No known key found for this signature in database
GPG key ID: 0D414D0FE405B63B
6 changed files with 77 additions and 97 deletions

View file

@ -20,7 +20,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import sys, os, core, config, json, onionrblockapi as block, requests, time, logger, threading, onionrplugins as plugins, base64, onionr
import onionrexceptions
import onionrexceptions, onionrpeers
from defusedxml import minidom
class OnionrCommunicatorDaemon:
@ -48,6 +48,7 @@ class OnionrCommunicatorDaemon:
# lists of connected peers and peers we know we can't reach currently
self.onlinePeers = []
self.offlinePeers = []
self.peerProfiles = [] # list of peer's profiles (onionrpeers.PeerProfile instances)
# amount of threads running by name, used to prevent too many
self.threadCounts = {}
@ -262,6 +263,7 @@ class OnionrCommunicatorDaemon:
'''Adds a new random online peer to self.onlinePeers'''
retData = False
tried = self.offlinePeers
peerScores = {}
if peer != '':
if self._core._utils.validateID(peer):
peerList = [peer]
@ -274,6 +276,14 @@ class OnionrCommunicatorDaemon:
# Avoid duplicating bootstrap addresses in peerList
self.addBootstrapListToPeerList(peerList)
for address in peerList:
# Load peer's profiles into a list
profile = onionrpeers.PeerProfiles(address, self._core)
peerScores[address] = profile.score
# Sort peers by their score, greatest to least
peerList = sorted(peerScores, key=peerScores.get, reverse=True)
for address in peerList:
if len(address) == 0 or address in tried or address in self.onlinePeers:
continue
@ -299,7 +309,7 @@ class OnionrCommunicatorDaemon:
logger.info(i)
def peerAction(self, peer, action, data=''):
'''Perform a get request to a peer'''
'''Perform a get request to a peer'''
if len(peer) == 0:
return False
logger.info('Performing ' + action + ' with ' + peer + ' on port ' + str(self.proxyPort))
@ -348,6 +358,8 @@ class OnionrCommunicatorDaemon:
self.decrementThreadCount('daemonCommands')
def uploadBlock(self):
'''Upload our block to a few peers'''
# when inserting a block, we try to upload it to a few peers to add some deniability
triedPeers = []
if not self._core._utils.validateHash(self.blockToUpload):
logger.warn('Requested to upload invalid block')

View file

@ -575,9 +575,10 @@ class Core:
# TODO: validate key on whitelist
if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'DBHash', 'failure', 'lastConnect'):
raise Exception("Got invalid database key when setting address info")
c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command)
conn.commit()
conn.close()
else:
c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command)
conn.commit()
conn.close()
return
def getBlockList(self, unsaved = False): # TODO: Use unsaved??

View file

@ -1,7 +1,7 @@
'''
Onionr - P2P Microblogging Platform & Social network.
This file contains both the OnionrCommunicate class for communcating with peers
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
@ -16,4 +16,57 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
'''
import core
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
if not isinstance(coreInst, core.Core):
raise TypeError("coreInst must be a type of core.Core")
self.coreInst = coreInst
assert isinstance(self.coreInst, core.Core)
self.loadScore()
return
def loadScore(self):
'''Load the node's score from the database'''
try:
self.success = int(self.coreInst.getAddressInfo('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
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)
return
def getScoreSortedPeerList(coreInst):
if not type(coreInst is core.Core):
raise TypeError('coreInst must be instance of core.Core')
peerList = coreInst.listAdders()
peerScores = {}
for address in peerList:
# Load peer's profiles into a list
profile = PeerProfiles(address, coreInst)
peerScores[address] = profile.score
# Sort peers by their score, greatest to least
peerList = sorted(peerScores, key=peerScores.get, reverse=True)
return peerList

View file

@ -37,5 +37,8 @@
"netTotal": 1000000000,
"blockCache" : 5000000,
"blockCacheTotal" : 50000000
},
"peers":{
"minimumScore": 5
}
}