From 0beffab96e0c1e46922981ce8c418d61ff4dba98 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 23 Jul 2018 02:45:48 -0500 Subject: [PATCH] + added blockimporter.py * removed outdated direct connection handler --- onionr/blockimporter.py | 40 ++++++++++++++++++++++++++++++++++++++++ onionr/core.py | 19 ------------------- 2 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 onionr/blockimporter.py diff --git a/onionr/blockimporter.py b/onionr/blockimporter.py new file mode 100644 index 00000000..a2695093 --- /dev/null +++ b/onionr/blockimporter.py @@ -0,0 +1,40 @@ +''' + Onionr - P2P Microblogging Platform & Social network + + Import block data and save it +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +import core, onionrexceptions, logger +def importBlockFromData(content, coreInst): + retData = False + if not isinstance(coreInst, core.Core): + raise Exception("coreInst must be an Onionr core instance") + + try: + content = content.encode() + except AttributeError: + pass + + metas = coreInst._utils.getBlockMetadataFromData(content) # returns tuple(metadata, meta), meta is also in metadata + metadata = metas[0] + if coreInst._utils.validateMetadata(metadata): # check if metadata is valid + if coreInst._crypto.verifyPow(content): # check if POW is enough/correct + logger.info('Block passed proof, saving.') + blockHash = coreInst.setData(content) + blockHash = coreInst.addToBlockDB(blockHash, dataSaved=True) + coreInst._utils.processBlockMetadata(blockHash) # caches block metadata values to block database + retData = True + return retData \ No newline at end of file diff --git a/onionr/core.py b/onionr/core.py index 2efa0d6c..b8b33c73 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -580,25 +580,6 @@ class Core: conn.close() return - def handle_direct_connection(self, data): - ''' - Handles direct messages - ''' - try: - data = json.loads(data) - - # TODO: Determine the sender, verify, etc - if ('callback' in data) and (data['callback'] is True): - # then this is a response to the message we sent earlier - self.daemonQueueAdd('checkCallbacks', json.dumps(data)) - else: - # then we should handle it and respond accordingly - self.daemonQueueAdd('incomingDirectConnection', json.dumps(data)) - except Exception as e: - logger.warn('Failed to handle incoming direct message: %s' % str(e)) - - return - def getBlockList(self, unsaved = False): # TODO: Use unsaved?? ''' Get list of our blocks