completed validateMetadata function

This commit is contained in:
Kevin Froman 2018-06-25 23:39:45 -05:00
parent 956f720741
commit 65ea5cf6f0
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
2 changed files with 38 additions and 8 deletions

View File

@ -134,15 +134,18 @@ class OnionrCommunicatorDaemon:
content = base64.b64decode(content) content = base64.b64decode(content)
if self._core._crypto.sha3Hash(content) == blockHash: if self._core._crypto.sha3Hash(content) == blockHash:
content = content.decode() # decode here because sha3Hash needs bytes above content = content.decode() # decode here because sha3Hash needs bytes above
metas = self._core._utils.getBlockMetadataFromData(content) metas = self._core._utils.getBlockMetadataFromData(content) # returns tuple(metadata, meta), meta is also in metadata
metadata = metas[0] metadata = metas[0]
meta = metas[1] meta = metas[1]
if self._core._crypto.verifyPow(metas[2], metadata) and self._core._utils.validateMetadata(metadata): if self._core._utils.validateMetadata(metadata):
logger.info('Block passed proof, saving.') if self._core._crypto.verifyPow(metas[2], metadata):
self._core.setData(content) logger.info('Block passed proof, saving.')
self._core.addToBlockDB(blockHash, dataSaved=True) self._core.setData(content)
self._core.addToBlockDB(blockHash, dataSaved=True)
else:
logger.warn('POW failed for block ' + blockHash)
else: else:
logger.warn('POW failed for block ' + blockHash) logger.warn('Metadata for ' + blockHash + ' is invalid.')
self.blockQueue.remove(blockHash) self.blockQueue.remove(blockHash)
else: else:
logger.warn('Block hash validation failed for ' + blockHash + ' got ' + self._core._crypto.sha3Hash(content)) logger.warn('Block hash validation failed for ' + blockHash + ' got ' + self._core._crypto.sha3Hash(content))

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, glob, shutil, math import getpass, sys, requests, os, socket, hashlib, logger, sqlite3, config, binascii, time, base64, json, glob, shutil, math, json
import nacl.signing, nacl.encoding import nacl.signing, nacl.encoding
from onionrblockapi import Block from onionrblockapi import Block
import onionrexceptions import onionrexceptions
@ -313,7 +313,34 @@ class OnionrUtils:
def validateMetadata(metadata): def validateMetadata(metadata):
'''Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string''' '''Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string'''
# TODO, make this check sane sizes # TODO, make this check sane sizes
return True retData = False
# convert to dict if it is json string
if type(metadata) is str:
try:
metadata = json.loads(metadata)
except json.JSONDecodeError:
pass
# Validate metadata dict for invalid keys to sizes that are too large
if type(metadata) is dict:
for i in metadata:
try:
self._core.requirements.blockMetadataLengths[i]
except KeyError:
logger.warn('Block has invalid metadata key ' + i)
break
else:
if self._core.requirements.blockMetadataLengths[i] < len(metadata[i]):
logger.warn('Block metadata key ' + i + ' exceeded maximum size')
break
else:
# if metadata loop gets no errors, it does not break, therefore metadata is valid
retData = True
else:
logger.warn('In call to utils.validateMetadata, metadata must be JSON string or a dictionary object')
return retData
def validatePubKey(self, key): def validatePubKey(self, key):
''' '''