From cb63941fc9a7ff0746f45d7ac95793414a06af1b Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 26 Jan 2018 19:16:15 -0600 Subject: [PATCH] work on processing blocks --- onionr/communicator.py | 15 ++++++++------- onionr/core.py | 22 +++++++++++++++------- onionr/onionrutils.py | 1 + 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index febbd103..b8c50ad0 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -20,7 +20,7 @@ and code to operate as a daemon, getting commands from the command queue databas along with this program. If not, see . ''' import sqlite3, requests, hmac, hashlib, time, sys, os, logger -import core +import core, onionrutils class OnionrCommunicate: def __init__(self, debug, developmentMode): ''' OnionrCommunicate @@ -28,6 +28,7 @@ class OnionrCommunicate: This class handles communication with nodes in the Onionr network. ''' self._core = core.Core() + self._utils = onionrutils.OnionrUtils(self._core) blockProcessTimer = 0 blockProcessAmount = 5 logger.debug('Communicator debugging enabled.') @@ -40,7 +41,7 @@ class OnionrCommunicate: with open(fingerprintFile,'r') as f: self.pgpOwnFingerprint = f.read() logger.info('My PGP fingerprint is ' + logger.colors.underline + self.pgpOwnFingerprint + logger.colors.reset + logger.colors.fg.green + '.') - + self._core.clearDaemonQueue() while True: command = self._core.daemonQueue() @@ -84,17 +85,17 @@ class OnionrCommunicate: for i in peerList: lastDB = self._core.getPeerInfo(i, 'blockDBHash') currentDB = self.performGet('getDBHash', i) - if lastDB != currentDB: - blocks += self.performGet('getBlockHashes', i) + if currentDB != False: + if lastDB != currentDB: + blocks += self.performGet('getBlockHashes', i) blockList = blocks.split('\n') for i in blockList: - if not self._core.validateHash(i): + if not self._utils.validateHash(i): # skip hash if it isn't valid continue else: logger.debug('Adding ' + i + ' to hash database...') self._core.addToBlockDB(i) - return def performGet(self, action, peer, data=None, type='tor'): @@ -115,7 +116,7 @@ class OnionrCommunicate: shouldRun = False -debug = False +debug = True developmentMode = False if os.path.exists('dev-enabled'): developmentMode = True diff --git a/onionr/core.py b/onionr/core.py index fe1b824c..905762ad 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -228,6 +228,13 @@ class Core: conn.commit() conn.close() return + def clearDaemonQueue(self): + '''clear the daemon queue (somewhat dangerousous)''' + conn = sqlite3.connect(self.queueDB) + c = conn.cursor() + c.execute('Delete from commands;') + conn.commit() + conn.close() def generateHMAC(self): ''' @@ -253,11 +260,8 @@ class Core: Work with the block database and download any missing blocks This is meant to be called from the communicator daemon on its timer. ''' - conn = sqlite3.connect(self.blockDB) - c = conn.cursor() - for i in blocks: - pass - conn.close() + for i in self.getBlockList(True): + print('UNSAVED BLOCK:', i) return def getPeerInfo(self, peer, info): ''' @@ -291,12 +295,16 @@ class Core: conn.close() return retVal - def getBlockList(self): + def getBlockList(self, unsaved=False): '''get list of our blocks''' conn = sqlite3.connect(self.blockDB) c = conn.cursor() retData = '' - for row in c.execute('SELECT hash FROM hashes;'): + if unsaved: + execute = 'SELECT hash FROM hashes where dataSaved != 1;' + else: + execute = 'SELECT hash FROM hashes;' + for row in c.execute(execute): for i in row: retData += i return retData diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 30216770..e9642f3b 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -70,6 +70,7 @@ class OnionrUtils: sock.close() return retVal def checkIsIP(self, ip): + '''Check if a string is a valid ipv4 address''' try: socket.inet_aton(ip) except: