onionr can now import blocks from disk

This commit is contained in:
Kevin Froman 2018-05-10 02:42:24 -05:00
parent 5813190cc4
commit 193845104e
No known key found for this signature in database
GPG key ID: 0D414D0FE405B63B
3 changed files with 25 additions and 4 deletions

View file

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# 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))
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)