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
|
|
|
|
|
|
|
Lookup new blocks with the communicator using a random connected peer
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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 logger, onionrproofs
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import stringvalidators, epoch
|
2019-07-10 22:38:20 +00:00
|
|
|
from communicator import peeraction, onlinepeers
|
2019-07-17 16:25:29 +00:00
|
|
|
from coredb import blockmetadb
|
2019-05-08 03:28:06 +00:00
|
|
|
def lookup_blocks_from_communicator(comm_inst):
|
2019-06-19 20:29:27 +00:00
|
|
|
logger.info('Looking up new blocks...')
|
|
|
|
tryAmount = 2
|
|
|
|
newBlocks = ''
|
2019-07-17 16:25:29 +00:00
|
|
|
existingBlocks = blockmetadb.get_block_list()
|
2019-06-19 20:29:27 +00:00
|
|
|
triedPeers = [] # list of peers we've tried this time around
|
|
|
|
maxBacklog = 1560 # Max amount of *new* block hashes to have already in queue, to avoid memory exhaustion
|
|
|
|
lastLookupTime = 0 # Last time we looked up a particular peer's list
|
|
|
|
new_block_count = 0
|
|
|
|
for i in range(tryAmount):
|
|
|
|
listLookupCommand = 'getblocklist' # This is defined here to reset it each time
|
|
|
|
if len(comm_inst.blockQueue) >= maxBacklog:
|
|
|
|
break
|
|
|
|
if not comm_inst.isOnline:
|
|
|
|
break
|
|
|
|
# check if disk allocation is used
|
2019-07-18 23:07:18 +00:00
|
|
|
if comm_inst.storage_counter.isFull():
|
2019-06-19 20:29:27 +00:00
|
|
|
logger.debug('Not looking up new blocks due to maximum amount of allowed disk space used')
|
|
|
|
break
|
2019-07-10 22:38:20 +00:00
|
|
|
peer = onlinepeers.pick_online_peer(comm_inst) # select random online peer
|
2019-06-19 20:29:27 +00:00
|
|
|
# if we've already tried all the online peers this time around, stop
|
|
|
|
if peer in triedPeers:
|
|
|
|
if len(comm_inst.onlinePeers) == len(triedPeers):
|
2019-05-08 03:28:06 +00:00
|
|
|
break
|
|
|
|
else:
|
2019-06-19 20:29:27 +00:00
|
|
|
continue
|
|
|
|
triedPeers.append(peer)
|
|
|
|
|
|
|
|
# Get the last time we looked up a peer's stamp to only fetch blocks since then.
|
|
|
|
# Saved in memory only for privacy reasons
|
|
|
|
try:
|
|
|
|
lastLookupTime = comm_inst.dbTimestamps[peer]
|
|
|
|
except KeyError:
|
|
|
|
lastLookupTime = 0
|
|
|
|
else:
|
|
|
|
listLookupCommand += '?date=%s' % (lastLookupTime,)
|
|
|
|
try:
|
2019-07-10 22:38:20 +00:00
|
|
|
newBlocks = peeraction.peer_action(comm_inst, peer, listLookupCommand) # get list of new block hashes
|
2019-06-19 20:29:27 +00:00
|
|
|
except Exception as error:
|
|
|
|
logger.warn('Could not get new blocks from %s.' % peer, error = error)
|
|
|
|
newBlocks = False
|
|
|
|
else:
|
2019-06-25 23:07:35 +00:00
|
|
|
comm_inst.dbTimestamps[peer] = epoch.get_rounded_epoch(roundS=60)
|
2019-06-19 20:29:27 +00:00
|
|
|
if newBlocks != False:
|
|
|
|
# if request was a success
|
|
|
|
for i in newBlocks.split('\n'):
|
2019-06-25 23:07:35 +00:00
|
|
|
if stringvalidators.validate_hash(i):
|
2019-06-19 20:29:27 +00:00
|
|
|
# if newline seperated string is valid hash
|
|
|
|
if not i in existingBlocks:
|
|
|
|
# if block does not exist on disk and is not already in block queue
|
|
|
|
if i not in comm_inst.blockQueue:
|
2019-07-18 23:07:18 +00:00
|
|
|
if onionrproofs.hashMeetsDifficulty(i) and not comm_inst.blacklist.inBlacklist(i):
|
2019-06-19 20:29:27 +00:00
|
|
|
if len(comm_inst.blockQueue) <= 1000000:
|
|
|
|
comm_inst.blockQueue[i] = [peer] # add blocks to download queue
|
|
|
|
new_block_count += 1
|
|
|
|
else:
|
|
|
|
if peer not in comm_inst.blockQueue[i]:
|
|
|
|
if len(comm_inst.blockQueue[i]) < 10:
|
|
|
|
comm_inst.blockQueue[i].append(peer)
|
|
|
|
if new_block_count > 0:
|
2019-06-20 00:59:05 +00:00
|
|
|
block_string = ""
|
|
|
|
if new_block_count > 1:
|
|
|
|
block_string = "s"
|
|
|
|
logger.info('Discovered %s new block%s' % (new_block_count, block_string), terminal=True)
|
2019-06-19 20:29:27 +00:00
|
|
|
comm_inst.decrementThreadCount('lookupBlocks')
|
|
|
|
return
|