2019-05-09 05:27:15 +00:00
|
|
|
'''
|
2019-06-12 06:44:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-05-09 05:27:15 +00:00
|
|
|
|
|
|
|
Download blocks using the communicator instance
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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-05-08 03:28:06 +00:00
|
|
|
import communicator, onionrexceptions
|
2019-05-15 23:25:36 +00:00
|
|
|
import logger, onionrpeers
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import blockmetadata, stringvalidators, validatemetadata
|
2019-07-22 05:24:42 +00:00
|
|
|
from coredb import blockmetadb
|
2019-07-09 06:25:20 +00:00
|
|
|
from . import shoulddownload
|
2019-07-10 22:38:20 +00:00
|
|
|
from communicator import peeraction, onlinepeers
|
2019-09-21 23:49:24 +00:00
|
|
|
import onionrcrypto, onionrstorage
|
|
|
|
from onionrblocks import onionrblacklist, storagecounter
|
2019-05-08 03:28:06 +00:00
|
|
|
def download_blocks_from_communicator(comm_inst):
|
2019-08-13 22:28:53 +00:00
|
|
|
'''Use Onionr communicator instance to download blocks in the communicator's queue'''
|
2019-05-08 03:28:06 +00:00
|
|
|
assert isinstance(comm_inst, communicator.OnionrCommunicatorDaemon)
|
2019-07-18 17:40:48 +00:00
|
|
|
blacklist = onionrblacklist.OnionrBlackList()
|
|
|
|
storage_counter = storagecounter.StorageCounter()
|
2019-09-05 09:40:31 +00:00
|
|
|
LOG_SKIP_COUNT = 50 # for how many iterations we skip logging the counter
|
2019-09-17 01:16:06 +00:00
|
|
|
count: int = 0
|
|
|
|
metadata_validation_result: bool = False
|
2019-08-13 22:28:53 +00:00
|
|
|
# Iterate the block queue in the communicator
|
2019-05-08 03:28:06 +00:00
|
|
|
for blockHash in list(comm_inst.blockQueue):
|
2019-08-13 09:16:11 +00:00
|
|
|
count += 1
|
2019-05-15 23:25:36 +00:00
|
|
|
if len(comm_inst.onlinePeers) == 0:
|
|
|
|
break
|
2019-05-08 03:28:06 +00:00
|
|
|
triedQueuePeers = [] # List of peers we've tried for a block
|
|
|
|
try:
|
|
|
|
blockPeers = list(comm_inst.blockQueue[blockHash])
|
|
|
|
except KeyError:
|
|
|
|
blockPeers = []
|
|
|
|
removeFromQueue = True
|
2019-07-04 02:28:16 +00:00
|
|
|
|
2019-07-11 07:39:35 +00:00
|
|
|
if not shoulddownload.should_download(comm_inst, blockHash):
|
2019-07-04 02:28:16 +00:00
|
|
|
continue
|
|
|
|
|
2019-09-08 09:48:16 +00:00
|
|
|
if comm_inst.shutdown or not comm_inst.isOnline or storage_counter.is_full():
|
2019-07-04 02:28:16 +00:00
|
|
|
# Exit loop if shutting down or offline, or disk allocation reached
|
2019-05-08 03:28:06 +00:00
|
|
|
break
|
2019-07-09 06:25:20 +00:00
|
|
|
# Do not download blocks being downloaded
|
2019-05-08 03:28:06 +00:00
|
|
|
if blockHash in comm_inst.currentDownloading:
|
|
|
|
#logger.debug('Already downloading block %s...' % blockHash)
|
|
|
|
continue
|
2019-07-04 02:28:16 +00:00
|
|
|
|
2019-05-08 03:28:06 +00:00
|
|
|
comm_inst.currentDownloading.append(blockHash) # So we can avoid concurrent downloading in other threads of same block
|
|
|
|
if len(blockPeers) == 0:
|
2019-07-10 22:38:20 +00:00
|
|
|
peerUsed = onlinepeers.pick_online_peer(comm_inst)
|
2019-05-08 03:28:06 +00:00
|
|
|
else:
|
2019-07-22 05:24:42 +00:00
|
|
|
blockPeers = onionrcrypto.cryptoutils.random_shuffle(blockPeers)
|
2019-05-08 03:28:06 +00:00
|
|
|
peerUsed = blockPeers.pop(0)
|
|
|
|
|
|
|
|
if not comm_inst.shutdown and peerUsed.strip() != '':
|
|
|
|
logger.info("Attempting to download %s from %s..." % (blockHash[:12], peerUsed))
|
2019-07-10 22:38:20 +00:00
|
|
|
content = peeraction.peer_action(comm_inst, peerUsed, 'getdata/' + blockHash, max_resp_size=3000000) # block content from random peer (includes metadata)
|
2019-09-11 21:50:09 +00:00
|
|
|
|
2019-05-08 03:28:06 +00:00
|
|
|
if content != False and len(content) > 0:
|
|
|
|
try:
|
|
|
|
content = content.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2019-07-22 05:24:42 +00:00
|
|
|
realHash = onionrcrypto.hashers.sha3_hash(content)
|
2019-05-08 03:28:06 +00:00
|
|
|
try:
|
|
|
|
realHash = realHash.decode() # bytes on some versions for some reason
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
if realHash == blockHash:
|
2019-09-12 06:28:20 +00:00
|
|
|
#content = content.decode() # decode here because sha3Hash needs bytes above
|
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
|
2019-05-08 03:28:06 +00:00
|
|
|
metadata = metas[0]
|
2019-09-17 01:16:06 +00:00
|
|
|
try:
|
|
|
|
metadata_validation_result = validatemetadata.validate_metadata(metadata, metas[2])
|
|
|
|
except onionrexceptions.DataExists:
|
|
|
|
metadata_validation_result = False
|
|
|
|
if metadata_validation_result: # check if metadata is valid, and verify nonce
|
2019-07-22 05:24:42 +00:00
|
|
|
if onionrcrypto.cryptoutils.verify_POW(content): # check if POW is enough/correct
|
2019-05-08 03:28:06 +00:00
|
|
|
logger.info('Attempting to save block %s...' % blockHash[:12])
|
|
|
|
try:
|
2019-07-24 17:22:19 +00:00
|
|
|
onionrstorage.set_data(content)
|
2019-06-29 18:18:31 +00:00
|
|
|
except onionrexceptions.DataExists:
|
|
|
|
logger.warn('Data is already set for %s ' % (blockHash,))
|
2019-05-08 03:28:06 +00:00
|
|
|
except onionrexceptions.DiskAllocationReached:
|
2019-06-29 18:18:31 +00:00
|
|
|
logger.error('Reached disk allocation allowance, cannot save block %s.' % (blockHash,))
|
2019-05-08 03:28:06 +00:00
|
|
|
removeFromQueue = False
|
|
|
|
else:
|
2019-07-17 16:58:40 +00:00
|
|
|
blockmetadb.add_to_block_DB(blockHash, dataSaved=True) # add block to meta db
|
2019-07-18 17:40:48 +00:00
|
|
|
blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database
|
2019-05-08 03:28:06 +00:00
|
|
|
else:
|
2019-06-29 18:18:31 +00:00
|
|
|
logger.warn('POW failed for block %s.' % (blockHash,))
|
2019-05-08 03:28:06 +00:00
|
|
|
else:
|
2019-07-18 17:40:48 +00:00
|
|
|
if blacklist.inBlacklist(realHash):
|
2019-05-08 03:28:06 +00:00
|
|
|
logger.warn('Block %s is blacklisted.' % (realHash,))
|
|
|
|
else:
|
2019-06-29 18:18:31 +00:00
|
|
|
logger.warn('Metadata for block %s is invalid.' % (blockHash,))
|
2019-07-18 17:40:48 +00:00
|
|
|
blacklist.addToDB(blockHash)
|
2019-05-08 03:28:06 +00:00
|
|
|
else:
|
|
|
|
# if block didn't meet expected hash
|
2019-08-26 02:18:09 +00:00
|
|
|
tempHash = onionrcrypto.hashers.sha3_hash(content) # lazy hack, TODO use var
|
2019-05-08 03:28:06 +00:00
|
|
|
try:
|
|
|
|
tempHash = tempHash.decode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
# Punish peer for sharing invalid block (not always malicious, but is bad regardless)
|
2019-07-18 17:40:48 +00:00
|
|
|
onionrpeers.PeerProfiles(peerUsed).addScore(-50)
|
2019-05-08 03:28:06 +00:00
|
|
|
if tempHash != 'ed55e34cb828232d6c14da0479709bfa10a0923dca2b380496e6b2ed4f7a0253':
|
|
|
|
# Dumb hack for 404 response from peer. Don't log it if 404 since its likely not malicious or a critical error.
|
|
|
|
logger.warn('Block hash validation failed for ' + blockHash + ' got ' + tempHash)
|
|
|
|
else:
|
|
|
|
removeFromQueue = False # Don't remove from queue if 404
|
|
|
|
if removeFromQueue:
|
|
|
|
try:
|
|
|
|
del comm_inst.blockQueue[blockHash] # remove from block queue both if success or false
|
2019-08-13 09:16:11 +00:00
|
|
|
if count == LOG_SKIP_COUNT:
|
|
|
|
logger.info('%s blocks remaining in queue' % [len(comm_inst.blockQueue)], terminal=True)
|
|
|
|
count = 0
|
2019-05-08 03:28:06 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
comm_inst.currentDownloading.remove(blockHash)
|
|
|
|
comm_inst.decrementThreadCount('getBlocks')
|