Onionr/onionr/blockimporter.py

52 lines
2.1 KiB
Python
Raw Normal View History

'''
Onionr - Private P2P Communication
Import block data and save it
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
2019-07-19 19:49:56 +00:00
import onionrexceptions, logger
from onionrutils import validatemetadata, blockmetadata
from coredb import blockmetadb
2019-07-19 19:49:56 +00:00
import onionrcrypto, onionrblacklist, onionrstorage
def importBlockFromData(content):
crypto = onionrcrypto.OnionrCrypto()
blacklist = onionrblacklist.OnionrBlackList()
retData = False
2018-08-13 03:48:33 +00:00
2019-07-19 19:49:56 +00:00
dataHash = crypto.sha3Hash(content)
2018-08-13 03:48:33 +00:00
2019-07-19 19:49:56 +00:00
if blacklist.inBlacklist(dataHash):
2018-08-13 03:48:33 +00:00
raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,))
try:
content = content.encode()
except AttributeError:
pass
metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata
metadata = metas[0]
2019-07-19 19:49:56 +00:00
if validatemetadata.validate_metadata(metadata, metas[2]): # check if metadata is valid
if crypto.verifyPow(content): # check if POW is enough/correct
logger.info('Block passed proof, saving.', terminal=True)
try:
2019-07-19 19:49:56 +00:00
blockHash = onionrstorage.setdata(content)
except onionrexceptions.DiskAllocationReached:
pass
else:
blockmetadb.add_to_block_DB(blockHash, dataSaved=True)
2019-07-19 19:49:56 +00:00
blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database
retData = True
return retData