peer/key exchange work (mostly done)

This commit is contained in:
Kevin Froman 2018-03-16 10:35:37 -05:00
parent a5e98b4787
commit cb3015652a
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 54 additions and 8 deletions

View File

@ -19,7 +19,7 @@ and code to operate as a daemon, getting commands from the command queue databas
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 sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse
import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse, random
import core, onionrutils, onionrcrypto, onionrproofs, btc, config, onionrplugins as plugins
class OnionrCommunicate:
@ -49,7 +49,7 @@ class OnionrCommunicate:
blockProcessAmount = 5
heartBeatTimer = 0
heartBeatRate = 5
pexTimer = 900 # How often we should check for new peers
pexTimer = 5 # How often we should check for new peers
pexCount = 0
logger.debug('Communicator debugging enabled.')
torID = open('data/hs/hostname').read()
@ -89,14 +89,42 @@ class OnionrCommunicate:
'''
Get new peers
'''
peersCheck = 5 # Amount of peers to ask for new peers + keys
peersChecked = 0
peerList = list(self._core.listAdders()) # random ordered list of peers
logger.warn(len(peerList))
newKeys = []
newAdders = []
if len(peerList) > peersCheck:
peersCheck = len(peerList)
while peersCheck > peersChecked:
i = random.randint(0, len(peerList))
logger.info('Using ' + peerList[i] + ' to find new peers')
try:
newAdders = self.performGet('pex', peerList[i])
self._utils.mergeAdders(newAdders)
except requests.exceptions.ConnectionError:
logger.info(peerList[i] + ' connection failed')
continue
else:
try:
logger.info('Using ' + peerList[i] + ' to find new keys')
newKeys = self.performGet('kex', peerList[i])
# TODO: Require keys to come with POW token (very large amount of POW)
self._utils.mergeKeys(newKeys)
except requests.exceptions.ConnectionError:
logger.info(peerList[i] + ' connection failed')
continue
else:
peersChecked += 1
return
def lookupBlocks(self):
'''
Lookup blocks and merge new ones
'''
peerList = self._core.listAdders()
blocks = ''
for i in peerList:

View File

@ -57,7 +57,7 @@ class Core:
def addPeer(self, peerID, name=''):
'''
Add a peer by their ID, with an optional name, to the peer database
Adds a public key to the key database (misleading function name)
DOES NO SAFETY CHECKS if the ID is valid, but prepares the insertion
'''
@ -346,7 +346,7 @@ class Core:
def listPeers(self, randomOrder=True):
'''
Return a list of peers
Return a list of public keys (misleading function name)
randomOrder determines if the list should be in a random order
'''

View File

@ -41,6 +41,24 @@ class OnionrUtils:
'''High level function to encrypt a message to a peer and insert it as a block'''
return
def mergeKeys(self, newKeyList):
'''Merge ed25519 key list to our database'''
retVal = False
for key in newKeyList:
if not key in self._core.listPeers(randomOrder=False):
if self._core.addPeer(key):
retVal = True
return retVal
def mergeAdders(self, newAdderList):
'''Merge peer adders list to our database'''
retVal = False
for adder in newAdderList:
if not adder in self._core.listAdders(randomOrder=False):
if self._core.addAddress(adder):
retVal = True
return retVal
def localCommand(self, command):
'''
Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.

View File

@ -120,10 +120,10 @@ class OnionrTests(unittest.TestCase):
def testBitcoinNode(self):
# temporarily disabled- this takes a lot of time the CI doesn't have
self.assertTrue(True)
logger.debug('-'*26 + '\n')
logger.info('Running bitcoin node test...')
#logger.debug('-'*26 + '\n')
#logger.info('Running bitcoin node test...')
sbitcoin = btc.OnionrBTC()
#sbitcoin = btc.OnionrBTC()
def testPluginReload(self):
logger.debug('-'*26 + '\n')