2018-07-23 07:45:48 +00:00
|
|
|
'''
|
2019-06-12 00:05:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2018-07-23 07:45:48 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
'''
|
|
|
|
import core, onionrexceptions, logger
|
2019-06-25 08:21:36 +00:00
|
|
|
from onionrutils import validatemetadata, blockmetadata
|
2018-07-23 07:45:48 +00:00
|
|
|
def importBlockFromData(content, coreInst):
|
|
|
|
retData = False
|
2018-08-13 03:48:33 +00:00
|
|
|
|
2018-08-17 21:50:16 +00:00
|
|
|
dataHash = coreInst._crypto.sha3Hash(content)
|
2018-08-13 03:48:33 +00:00
|
|
|
|
|
|
|
if coreInst._blacklist.inBlacklist(dataHash):
|
|
|
|
raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,))
|
|
|
|
|
2018-07-23 07:45:48 +00:00
|
|
|
if not isinstance(coreInst, core.Core):
|
|
|
|
raise Exception("coreInst must be an Onionr core instance")
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = content.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2019-06-25 08:21:36 +00:00
|
|
|
metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata
|
2018-07-23 07:45:48 +00:00
|
|
|
metadata = metas[0]
|
2019-06-26 00:15:04 +00:00
|
|
|
if validatemetadata.validate_metadata(coreInst, metadata, metas[2]): # check if metadata is valid
|
2018-07-23 07:45:48 +00:00
|
|
|
if coreInst._crypto.verifyPow(content): # check if POW is enough/correct
|
2019-06-25 08:21:36 +00:00
|
|
|
logger.info('Block passed proof, saving.', terminal=True)
|
2018-08-23 04:59:41 +00:00
|
|
|
try:
|
|
|
|
blockHash = coreInst.setData(content)
|
|
|
|
except onionrexceptions.DiskAllocationReached:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
coreInst.addToBlockDB(blockHash, dataSaved=True)
|
2019-06-26 00:15:04 +00:00
|
|
|
blockmetadata.process_block_metadata(coreInst, blockHash) # caches block metadata values to block database
|
2018-08-23 04:59:41 +00:00
|
|
|
retData = True
|
2018-07-23 07:45:48 +00:00
|
|
|
return retData
|