diff --git a/onionr/api.py b/onionr/api.py index 6b45f97e..97a4d247 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -129,11 +129,7 @@ class API: pass elif action == 'getPGP': resp = Response(self._utils.exportMyPubkey()) - elif action == 'setData': - if data == None: - abort(401) - else: - self._core.setData(data) + # setData should be something the communicator initiates, not this api elif action == 'getData': resp = Response(self._core.getData(data)) diff --git a/onionr/communicator.py b/onionr/communicator.py index 230911ae..73dd0bd8 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -28,6 +28,8 @@ class OnionrCommunicate: This class handles communication with nodes in the Onionr network. ''' self._core = core.Core() + blockProcessTimer = 0 + blockProccesAmount = 5 if debug: print('Communicator debugging enabled') torID = open('data/hs/hostname').read() @@ -42,8 +44,15 @@ class OnionrCommunicate: while True: command = self._core.daemonQueue() + + # Process blocks based on a timer + blockProcessTimer += 1 + if blockProcessTimer == blockProcessAmount: + self._core.processBlocks() + blockProcessTimer = 0 + if debug: - print('Daemon heartbeat') + print('Communicator daemon heartbeat') if command != False: if command[0] == 'shutdown': print('Daemon recieved exit command.') diff --git a/onionr/core.py b/onionr/core.py index 9a755445..143f14bb 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -134,7 +134,18 @@ class Core: def setData(self, data): '''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): ''' @@ -216,4 +227,11 @@ class Core: generate and return an HMAC key ''' key = base64.b64encode(os.urandom(32)) - return key \ No newline at end of file + 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 \ No newline at end of file