better peer exchange

This commit is contained in:
Kevin Froman 2019-02-12 13:18:08 -06:00
parent 7c57829ec3
commit baf9d3a3c6
2 changed files with 36 additions and 9 deletions

View File

@ -188,11 +188,9 @@ class PublicAPI:
except AttributeError: except AttributeError:
pass pass
if powHash.startswith('0000'): if powHash.startswith('0000'):
try: newNode = clientAPI._core._utils.bytesToStr(newNode)
newNode = newNode.decode() if clientAPI._core._utils.validateID(newNode) and not newNode in clientAPI._core.onionrInst.communicatorInst.newPeers:
except AttributeError: clientAPI._core.onionrInst.communicatorInst.newPeers.append(newNode)
pass
if clientAPI._core.addAddress(newNode):
resp = 'Success' resp = 'Success'
else: else:
logger.warn(newNode.decode() + ' failed to meet POW: ' + powHash) logger.warn(newNode.decode() + ' failed to meet POW: ' + powHash)

View File

@ -40,11 +40,11 @@ class OnionrCommunicatorDaemon:
# list of timer instances # list of timer instances
self.timers = [] self.timers = []
# initalize core with Tor socks port being 3rd argument # initialize core with Tor socks port being 3rd argument
self.proxyPort = proxyPort self.proxyPort = proxyPort
self._core = onionrInst.onionrCore self._core = onionrInst.onionrCore
# intalize NIST beacon salt and time # initialize NIST beacon salt and time
self.nistSaltTimestamp = 0 self.nistSaltTimestamp = 0
self.powSalt = 0 self.powSalt = 0
@ -59,11 +59,12 @@ class OnionrCommunicatorDaemon:
self.cooldownPeer = {} self.cooldownPeer = {}
self.connectTimes = {} self.connectTimes = {}
self.peerProfiles = [] # list of peer's profiles (onionrpeers.PeerProfile instances) self.peerProfiles = [] # list of peer's profiles (onionrpeers.PeerProfile instances)
self.newPeers = [] # Peers merged to us. Don't add to db until we know they're reachable
# amount of threads running by name, used to prevent too many # amount of threads running by name, used to prevent too many
self.threadCounts = {} self.threadCounts = {}
# set true when shutdown command recieved # set true when shutdown command received
self.shutdown = False self.shutdown = False
# list of new blocks to download, added to when new block lists are fetched from peers # list of new blocks to download, added to when new block lists are fetched from peers
@ -156,11 +157,27 @@ class OnionrCommunicatorDaemon:
'''Lookup new peer addresses''' '''Lookup new peer addresses'''
logger.info('Looking up new addresses...') logger.info('Looking up new addresses...')
tryAmount = 1 tryAmount = 1
newPeers = []
for i in range(tryAmount): for i in range(tryAmount):
# Download new peer address list from random online peers # Download new peer address list from random online peers
if len(newPeers) > 10000:
# Dont get new peers if we have too many queued up
break
peer = self.pickOnlinePeer() peer = self.pickOnlinePeer()
newAdders = self.peerAction(peer, action='pex') newAdders = self.peerAction(peer, action='pex')
networkmerger.mergeAdders(newAdders, self._core) try:
newPeers = newAdders.split(',')
except AttributeError:
pass
else:
# Validate new peers are good format and not already in queue
invalid = []
for x in newPeers:
if not self._core._utils.validateID(x) or x in self.newPeers:
invalid.append(x)
for x in invalid:
newPeers.remove(x)
self.newPeers.extend(newPeers)
self.decrementThreadCount('lookupAdders') self.decrementThreadCount('lookupAdders')
def lookupBlocks(self): def lookupBlocks(self):
@ -397,8 +414,18 @@ class OnionrCommunicatorDaemon:
else: else:
peerList = self._core.listAdders() peerList = self._core.listAdders()
mainPeerList = self._core.listAdders()
peerList = onionrpeers.getScoreSortedPeerList(self._core) peerList = onionrpeers.getScoreSortedPeerList(self._core)
if len(peerList) < 8 or secrets.randbelow(4) == 3:
tryingNew = []
for x in self.newPeers:
if x not in peerList:
peerList.append(x)
tryingNew.append(x)
for i in tryingNew:
self.newPeers.remove(i)
if len(peerList) == 0 or useBootstrap: if len(peerList) == 0 or useBootstrap:
# Avoid duplicating bootstrap addresses in peerList # Avoid duplicating bootstrap addresses in peerList
self.addBootstrapListToPeerList(peerList) self.addBootstrapListToPeerList(peerList)
@ -413,6 +440,8 @@ class OnionrCommunicatorDaemon:
if self.peerAction(address, 'ping') == 'pong!': if self.peerAction(address, 'ping') == 'pong!':
logger.info('Connected to ' + address) logger.info('Connected to ' + address)
time.sleep(0.1) time.sleep(0.1)
if address not in mainPeerList:
networkmerger.mergeAdders(address, self._core)
if address not in self.onlinePeers: if address not in self.onlinePeers:
self.onlinePeers.append(address) self.onlinePeers.append(address)
self.connectTimes[address] = self._core._utils.getEpoch() self.connectTimes[address] = self._core._utils.getEpoch()