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

@ -253,7 +253,7 @@ class Core:
return return
def addToBlockDB(self, newHash, selfInsert=False): def addToBlockDB(self, newHash, selfInsert=False, dataSaved=False):
''' '''
Add a hash value to the block db Add a hash value to the block db
@ -266,7 +266,7 @@ class Core:
conn = sqlite3.connect(self.blockDB) conn = sqlite3.connect(self.blockDB)
c = conn.cursor() c = conn.cursor()
currentTime = math.floor(time.time()) currentTime = math.floor(time.time())
if selfInsert: if selfInsert or dataSaved:
selfInsert = 1 selfInsert = 1
else: else:
selfInsert = 0 selfInsert = 0

View File

@ -191,6 +191,8 @@ class Onionr:
'addaddress': self.addAddress, 'addaddress': self.addAddress,
'addfile': self.addFile, 'addfile': self.addFile,
'importblocks': self.onionrUtils.importNewBlocks,
'introduce': self.onionrCore.introduceNode, 'introduce': self.onionrCore.introduceNode,
'connect': self.addAddress 'connect': self.addAddress
} }
@ -212,6 +214,7 @@ class Onionr:
'pm': 'Adds a private message to block', 'pm': 'Adds a private message to block',
'get-pms': 'Shows private messages sent to you', 'get-pms': 'Shows private messages sent to you',
'addfile': 'Create an Onionr block from a file', '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', 'introduce': 'Introduce your node to the public Onionr network',
} }

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. 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 # 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 import nacl.signing, nacl.encoding
if sys.version_info < (3, 6): if sys.version_info < (3, 6):
@ -422,4 +422,22 @@ class OnionrUtils:
return False return False
def token(self, size = 32): 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)