added processBlocks function

This commit is contained in:
Kevin Froman 2018-01-25 16:39:09 -06:00
parent 3f3a29439e
commit 67a84e2a19
3 changed files with 31 additions and 8 deletions

View File

@ -129,11 +129,7 @@ class API:
pass pass
elif action == 'getPGP': elif action == 'getPGP':
resp = Response(self._utils.exportMyPubkey()) resp = Response(self._utils.exportMyPubkey())
elif action == 'setData': # setData should be something the communicator initiates, not this api
if data == None:
abort(401)
else:
self._core.setData(data)
elif action == 'getData': elif action == 'getData':
resp = Response(self._core.getData(data)) resp = Response(self._core.getData(data))

View File

@ -28,6 +28,8 @@ class OnionrCommunicate:
This class handles communication with nodes in the Onionr network. This class handles communication with nodes in the Onionr network.
''' '''
self._core = core.Core() self._core = core.Core()
blockProcessTimer = 0
blockProccesAmount = 5
if debug: if debug:
print('Communicator debugging enabled') print('Communicator debugging enabled')
torID = open('data/hs/hostname').read() torID = open('data/hs/hostname').read()
@ -42,8 +44,15 @@ class OnionrCommunicate:
while True: while True:
command = self._core.daemonQueue() command = self._core.daemonQueue()
# Process blocks based on a timer
blockProcessTimer += 1
if blockProcessTimer == blockProcessAmount:
self._core.processBlocks()
blockProcessTimer = 0
if debug: if debug:
print('Daemon heartbeat') print('Communicator daemon heartbeat')
if command != False: if command != False:
if command[0] == 'shutdown': if command[0] == 'shutdown':
print('Daemon recieved exit command.') print('Daemon recieved exit command.')

View File

@ -134,7 +134,18 @@ class Core:
def setData(self, data): def setData(self, data):
'''set the data assciated with a hash''' '''set the data assciated with a hash'''
hasher = hashlib.sha3_256 data = data.encode()
hasher = hashlib.sha3_256()
hasher.update(data)
dataHash = hasher.hexdigest()
blockFileName = self.blockDataLocation + dataHash + '.dat'
if os.path.exists(blockFileName):
raise Exception("Data is already set for " + dataHash)
else:
blockFile = open(blockFileName, 'w')
blockFile.write(data)
blockFile.close()
return dataHash
def dataDirEncrypt(self, password): def dataDirEncrypt(self, password):
''' '''
@ -217,3 +228,10 @@ class Core:
''' '''
key = base64.b64encode(os.urandom(32)) key = base64.b64encode(os.urandom(32))
return key return key
def processBlocks(self):
'''
Work with the block database and download any missing blocks
This is meant to be called from the communicator daemon on its timer.
'''
return