From 5f67cc388fadd78c2f547f577eb9a52a9ab775bc Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 30 Sep 2018 11:53:39 -0500 Subject: [PATCH] delete expired blocks --- onionr/core.py | 12 +++++++++--- onionr/onionr.py | 4 ++-- onionr/onionrblockapi.py | 3 ++- onionr/onionrutils.py | 17 ++++++++++++----- onionr/onionrvalues.py | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/onionr/core.py b/onionr/core.py index bb889813..8a6f5b15 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -628,7 +628,7 @@ class Core: c = conn.cursor() date = int(self._utils.getEpoch()) - execute = 'SELECT hash FROM hashes WHERE expire >= %s ORDER BY dateReceived;' % (date,) + execute = 'SELECT hash FROM hashes WHERE expire <= %s ORDER BY dateReceived;' % (date,) rows = list() for row in c.execute(execute): @@ -675,7 +675,7 @@ class Core: conn.close() return True - def insertBlock(self, data, header='txt', sign=False, encryptType='', symKey='', asymPeer='', meta = None): + def insertBlock(self, data, header='txt', sign=False, encryptType='', symKey='', asymPeer='', meta = None, expire=None): ''' Inserts a block into the network encryptType must be specified to encrypt a block @@ -753,6 +753,11 @@ class Core: metadata['sig'] = signature metadata['signer'] = signer metadata['time'] = str(self._utils.getEpoch()) + + # ensure expire is integer and of sane length + if type(expire) is not type(None): + assert len(str(int(expire))) < 14 + metadata['expire'] = expire # send block data (and metadata) to POW module to get tokenized block data proof = onionrproofs.POW(metadata, data) @@ -760,7 +765,8 @@ class Core: if payload != False: retData = self.setData(payload) self.addToBlockDB(retData, selfInsert=True, dataSaved=True) - self.setBlockType(retData, meta['type']) + #self.setBlockType(retData, meta['type']) + self._utils.processBlockMetadata(retData) self.daemonQueueAdd('uploadBlock', retData) if retData != False: diff --git a/onionr/onionr.py b/onionr/onionr.py index c0c39d23..80737bde 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -41,9 +41,9 @@ except ImportError: raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)") ONIONR_TAGLINE = 'Anonymous P2P Platform - GPLv3 - https://Onionr.VoidNet.Tech' -ONIONR_VERSION = '0.2.0' # for debugging and stuff +ONIONR_VERSION = '0.3.0' # for debugging and stuff ONIONR_VERSION_TUPLE = tuple(ONIONR_VERSION.split('.')) # (MAJOR, MINOR, VERSION) -API_VERSION = '4' # increments of 1; only change when something fundemental about how the API works changes. This way other nodes know how to communicate without learning too much information about you. +API_VERSION = '5' # increments of 1; only change when something fundemental about how the API works changes. This way other nodes know how to communicate without learning too much information about you. class Onionr: def __init__(self): diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index 78b4b5bc..fea9df1e 100644 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -25,7 +25,7 @@ class Block: blockCacheOrder = list() # NEVER write your own code that writes to this! blockCache = dict() # should never be accessed directly, look at Block.getCache() - def __init__(self, hash = None, core = None, type = None, content = None): + def __init__(self, hash = None, core = None, type = None, content = None, expire=None): # take from arguments # sometimes people input a bytes object instead of str in `hash` if (not hash is None) and isinstance(hash, bytes): @@ -35,6 +35,7 @@ class Block: self.core = core self.btype = type self.bcontent = content + self.expire = expire # initialize variables self.valid = True diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 1c9cf637..59b8829b 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -263,7 +263,7 @@ class OnionrUtils: myBlock = Block(blockHash, self._core) if myBlock.isEncrypted: myBlock.decrypt() - if myBlock.decrypted: + if (myBlock.isEncrypted and myBlock.decrypted) or (not myBlock.isEncrypted): blockType = myBlock.getMetadata('type') # we would use myBlock.getType() here, but it is bugged with encrypted blocks signer = self.bytesToStr(myBlock.signer) valid = myBlock.verifySig() @@ -276,9 +276,9 @@ class OnionrUtils: pass # Set block expire time if specified try: - expireTime = myBlock.getMetadata('expire') - assert len(int(expireTime)) < 20 # test that expire time is an integer of sane length (for epoch) - except (AssertionError, ValueError) as e: + expireTime = myBlock.getHeader('expire') + assert len(str(int(expireTime))) < 20 # test that expire time is an integer of sane length (for epoch) + except (AssertionError, ValueError, TypeError) as e: pass else: self._core.updateBlockInfo(blockHash, 'expire', expireTime) @@ -379,8 +379,14 @@ class OnionrUtils: if not self.isIntegerString(metadata[i]): logger.warn('Block metadata time stamp is not integer string') break + elif i == 'expire': + try: + assert int(metadata[i]) > self.getEpoch() + except AssertionError: + logger.warn('Block is expired') + break else: - # if metadata loop gets no errors, it does not break, therefore metadata is valid + # if metadata loop gets no errors, it does not break, therefore metadata is valid # make sure we do not have another block with the same data content (prevent data duplication and replay attacks) nonce = self._core._utils.bytesToStr(self._core._crypto.sha3Hash(blockData)) try: @@ -532,6 +538,7 @@ class OnionrUtils: if self._core._crypto.sha3Hash(newBlock.read()) == block.replace('.dat', ''): self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True) logger.info('Imported block %s.' % block) + self._core._utils.processBlockMetadata(block) else: logger.warn('Failed to verify hash for %s' % block) diff --git a/onionr/onionrvalues.py b/onionr/onionrvalues.py index 3f806702..84203d88 100644 --- a/onionr/onionrvalues.py +++ b/onionr/onionrvalues.py @@ -21,4 +21,4 @@ class OnionrValues: def __init__(self): self.passwordLength = 20 - self.blockMetadataLengths = {'meta': 1000, 'sig': 200, 'signer': 200, 'time': 10, 'powRandomToken': 1000, 'encryptType': 4} #TODO properly refine values to minimum needed \ No newline at end of file + self.blockMetadataLengths = {'meta': 1000, 'sig': 200, 'signer': 200, 'time': 10, 'powRandomToken': 1000, 'encryptType': 4, 'expire': 14} #TODO properly refine values to minimum needed \ No newline at end of file