2020-08-25 08:13:32 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2018-07-23 07:45:48 +00:00
|
|
|
|
2020-08-25 08:13:32 +00:00
|
|
|
Import block data and save it
|
|
|
|
"""
|
2019-11-27 00:01:32 +00:00
|
|
|
from onionrexceptions import BlacklistedBlock
|
|
|
|
from onionrexceptions import DiskAllocationReached
|
|
|
|
from onionrexceptions import InvalidProof
|
2020-01-20 03:10:51 +00:00
|
|
|
from onionrexceptions import InvalidMetadata
|
2019-11-27 00:01:32 +00:00
|
|
|
import logger
|
|
|
|
from onionrutils import validatemetadata
|
2020-08-25 08:13:32 +00:00
|
|
|
from onionrutils import bytesconverter
|
2019-11-27 00:01:32 +00:00
|
|
|
from coredb import blockmetadb
|
2020-11-02 01:31:11 +00:00
|
|
|
from onionrblocks import blockmetadata
|
2019-11-27 00:01:32 +00:00
|
|
|
import onionrstorage
|
|
|
|
import onionrcrypto as crypto
|
|
|
|
from . import onionrblacklist
|
|
|
|
|
2020-08-25 08:13:32 +00:00
|
|
|
"""
|
2018-07-23 07:45:48 +00:00
|
|
|
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/>.
|
2020-08-25 08:13:32 +00:00
|
|
|
"""
|
2019-11-27 00:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def import_block_from_data(content):
|
2019-07-19 19:49:56 +00:00
|
|
|
blacklist = onionrblacklist.OnionrBlackList()
|
2019-11-27 00:01:32 +00:00
|
|
|
ret_data = False
|
2018-08-13 03:48:33 +00:00
|
|
|
|
2020-08-25 08:13:32 +00:00
|
|
|
content = bytesconverter.str_to_bytes(content)
|
2018-07-23 07:45:48 +00:00
|
|
|
|
2019-11-27 00:01:32 +00:00
|
|
|
data_hash = crypto.hashers.sha3_hash(content)
|
2019-09-12 15:54:36 +00:00
|
|
|
|
2019-11-27 00:01:32 +00:00
|
|
|
if blacklist.inBlacklist(data_hash):
|
|
|
|
raise BlacklistedBlock(f'%s is a blacklisted block {data_hash}')
|
2019-09-12 15:54:36 +00:00
|
|
|
|
2019-11-27 00:01:32 +00:00
|
|
|
# returns tuple(metadata, meta), meta is also in metadata
|
|
|
|
metas = blockmetadata.get_block_metadata_from_data(content)
|
2018-07-23 07:45:48 +00:00
|
|
|
metadata = metas[0]
|
2019-11-27 00:01:32 +00:00
|
|
|
|
|
|
|
# check if metadata is valid
|
|
|
|
if validatemetadata.validate_metadata(metadata, metas[2]):
|
|
|
|
# check if POW is enough/correct
|
|
|
|
if crypto.cryptoutils.verify_POW(content):
|
|
|
|
logger.info(f'Imported block passed proof, saving: {data_hash}.',
|
|
|
|
terminal=True)
|
2018-08-23 04:59:41 +00:00
|
|
|
try:
|
2020-08-25 08:13:32 +00:00
|
|
|
block_hash = onionrstorage.set_data(content)
|
2019-11-27 00:01:32 +00:00
|
|
|
except DiskAllocationReached:
|
2019-08-12 04:00:08 +00:00
|
|
|
logger.warn('Failed to save block due to full disk allocation')
|
2020-01-20 03:10:51 +00:00
|
|
|
raise
|
2018-08-23 04:59:41 +00:00
|
|
|
else:
|
2020-08-25 08:13:32 +00:00
|
|
|
blockmetadb.add_to_block_DB(block_hash, dataSaved=True)
|
2019-11-27 00:01:32 +00:00
|
|
|
# caches block metadata values to block database
|
2020-08-25 08:13:32 +00:00
|
|
|
blockmetadata.process_block_metadata(block_hash)
|
|
|
|
ret_data = block_hash
|
2019-09-12 15:54:36 +00:00
|
|
|
else:
|
2019-11-27 00:01:32 +00:00
|
|
|
raise InvalidProof
|
2020-01-20 03:10:51 +00:00
|
|
|
else:
|
|
|
|
raise InvalidMetadata
|
2019-11-27 00:01:32 +00:00
|
|
|
return ret_data
|