From 193845104eef1dfe6c87542b1fd893997b4b38f8 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 10 May 2018 02:42:24 -0500 Subject: [PATCH] onionr can now import blocks from disk --- onionr/core.py | 4 ++-- onionr/onionr.py | 3 +++ onionr/onionrutils.py | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/onionr/core.py b/onionr/core.py index 2920bdee..04e1f023 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -253,7 +253,7 @@ class Core: return - def addToBlockDB(self, newHash, selfInsert=False): + def addToBlockDB(self, newHash, selfInsert=False, dataSaved=False): ''' Add a hash value to the block db @@ -266,7 +266,7 @@ class Core: conn = sqlite3.connect(self.blockDB) c = conn.cursor() currentTime = math.floor(time.time()) - if selfInsert: + if selfInsert or dataSaved: selfInsert = 1 else: selfInsert = 0 diff --git a/onionr/onionr.py b/onionr/onionr.py index a15e31b1..2143c320 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -191,6 +191,8 @@ class Onionr: 'addaddress': self.addAddress, 'addfile': self.addFile, + 'importblocks': self.onionrUtils.importNewBlocks, + 'introduce': self.onionrCore.introduceNode, 'connect': self.addAddress } @@ -212,6 +214,7 @@ class Onionr: 'pm': 'Adds a private message to block', 'get-pms': 'Shows private messages sent to you', 'addfile': 'Create an Onionr block from a file', + 'importblocks': 'import blocks from the disk (Onionr is transport-agnostic!)', 'introduce': 'Introduce your node to the public Onionr network', } diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 02d26592..27f7b67d 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' # Misc functions that do not fit in the main api, but are useful -import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json +import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob import nacl.signing, nacl.encoding if sys.version_info < (3, 6): @@ -422,4 +422,22 @@ class OnionrUtils: return False def token(self, size = 32): - return binascii.hexlify(os.urandom(size)) \ No newline at end of file + return binascii.hexlify(os.urandom(size)) + + def importNewBlocks(self, scanDir=''): + '''This function is intended to scan for new blocks ON THE DISK and import them''' + blockList = self._core.getBlockList() + if scanDir == '': + scanDir = self._core.blockDataLocation + if not scanDir.endswith('/'): + scanDir += '/' + for block in glob.glob(scanDir + "*.dat"): + if block.replace(scanDir, '').replace('.dat', '') not in blockList: + logger.info("Found new block on dist " + block) + with open(block, 'rb') as newBlock: + block = block.replace(scanDir, '').replace('.dat', '') + if self._core._crypto.sha3Hash(newBlock.read()) == block.replace('.dat', ''): + self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True) + logger.info('Imported block.') + else: + logger.warn('Failed to verify hash for ' + block) \ No newline at end of file