2020-06-30 22:08:32 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-05-09 05:27:15 +00:00
|
|
|
|
2020-02-23 08:12:13 +00:00
|
|
|
Lookup new blocks with the communicator using a random connected peer
|
|
|
|
"""
|
|
|
|
from gevent import time
|
|
|
|
|
|
|
|
import logger
|
|
|
|
import onionrproofs
|
|
|
|
from onionrutils import stringvalidators, epoch
|
|
|
|
from communicator import peeraction, onlinepeers
|
2020-06-30 22:08:32 +00:00
|
|
|
from coredb.blockmetadb import get_block_list
|
2020-02-23 08:12:13 +00:00
|
|
|
from utils import reconstructhash
|
|
|
|
from onionrblocks import onionrblacklist
|
|
|
|
import onionrexceptions
|
|
|
|
import config
|
|
|
|
from etc import onionrvalues
|
|
|
|
"""
|
2019-05-09 05:27:15 +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-02-23 08:12:13 +00:00
|
|
|
"""
|
2019-12-20 07:24:38 +00:00
|
|
|
|
2019-07-22 05:24:42 +00:00
|
|
|
blacklist = onionrblacklist.OnionrBlackList()
|
2020-02-23 08:12:13 +00:00
|
|
|
|
|
|
|
|
2019-05-08 03:28:06 +00:00
|
|
|
def lookup_blocks_from_communicator(comm_inst):
|
2019-08-27 08:26:14 +00:00
|
|
|
logger.info('Looking up new blocks')
|
2019-06-19 20:29:27 +00:00
|
|
|
tryAmount = 2
|
|
|
|
newBlocks = ''
|
2020-06-30 22:08:32 +00:00
|
|
|
# List of existing saved blocks
|
|
|
|
existingBlocks = get_block_list()
|
|
|
|
triedPeers = [] # list of peers we've tried this time around
|
|
|
|
# Max amount of *new* block hashes to have in queue
|
|
|
|
maxBacklog = 1560
|
|
|
|
lastLookupTime = 0 # Last time we looked up a particular peer's list
|
2019-06-19 20:29:27 +00:00
|
|
|
new_block_count = 0
|
|
|
|
for i in range(tryAmount):
|
2020-06-30 22:08:32 +00:00
|
|
|
# Defined here to reset it each time, time offset is added later
|
|
|
|
listLookupCommand = 'getblocklist'
|
2019-06-19 20:29:27 +00:00
|
|
|
if len(comm_inst.blockQueue) >= maxBacklog:
|
|
|
|
break
|
|
|
|
if not comm_inst.isOnline:
|
|
|
|
break
|
|
|
|
# check if disk allocation is used
|
2019-09-08 09:48:16 +00:00
|
|
|
if comm_inst.storage_counter.is_full():
|
2020-06-30 22:08:32 +00:00
|
|
|
logger.debug(
|
|
|
|
'Not looking up new blocks due to maximum amount of disk used')
|
2019-06-19 20:29:27 +00:00
|
|
|
break
|
2019-12-20 07:24:38 +00:00
|
|
|
try:
|
|
|
|
# select random online peer
|
|
|
|
peer = onlinepeers.pick_online_peer(comm_inst)
|
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
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)
|
|
|
|
|
2020-06-30 22:08:32 +00:00
|
|
|
# Get the last time we looked up a peer's stamp,
|
|
|
|
# to only fetch blocks since then.
|
2019-06-19 20:29:27 +00:00
|
|
|
# Saved in memory only for privacy reasons
|
|
|
|
try:
|
|
|
|
lastLookupTime = comm_inst.dbTimestamps[peer]
|
|
|
|
except KeyError:
|
2020-06-30 22:08:32 +00:00
|
|
|
lastLookupTime = epoch.get_epoch() - \
|
|
|
|
config.get("general.max_block_age",
|
|
|
|
onionrvalues.DEFAULT_EXPIRE)
|
2020-02-23 08:12:13 +00:00
|
|
|
listLookupCommand += '?date=%s' % (lastLookupTime,)
|
2019-06-19 20:29:27 +00:00
|
|
|
try:
|
2020-06-30 22:08:32 +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:
|
2020-06-30 22:08:32 +00:00
|
|
|
logger.warn(
|
|
|
|
f'Could not get new blocks from {peer}.',
|
|
|
|
error=error)
|
2019-06-19 20:29:27 +00:00
|
|
|
newBlocks = False
|
2020-06-30 22:08:32 +00:00
|
|
|
|
|
|
|
if newBlocks != False: # noqa
|
2019-06-19 20:29:27 +00:00
|
|
|
# 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-07-27 21:56:06 +00:00
|
|
|
i = reconstructhash.reconstruct_hash(i)
|
2019-06-19 20:29:27 +00:00
|
|
|
# if newline seperated string is valid hash
|
2020-06-30 22:08:32 +00:00
|
|
|
|
|
|
|
# if block does not exist on disk + is not already in queue
|
|
|
|
if i not in existingBlocks:
|
2019-06-19 20:29:27 +00:00
|
|
|
if i not in comm_inst.blockQueue:
|
2020-06-30 22:08:32 +00:00
|
|
|
if onionrproofs.hashMeetsDifficulty(i) and \
|
|
|
|
not blacklist.inBlacklist(i):
|
2019-06-19 20:29:27 +00:00
|
|
|
if len(comm_inst.blockQueue) <= 1000000:
|
2020-06-30 22:08:32 +00:00
|
|
|
# add blocks to download queue
|
|
|
|
comm_inst.blockQueue[i] = [peer]
|
2019-06-19 20:29:27 +00:00
|
|
|
new_block_count += 1
|
2020-06-30 22:09:18 +00:00
|
|
|
comm_inst.dbTimestamps[peer] = \
|
|
|
|
epoch.get_rounded_epoch(roundS=60)
|
2019-06-19 20:29:27 +00:00
|
|
|
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"
|
2020-06-30 22:08:32 +00:00
|
|
|
logger.info(
|
|
|
|
f'Discovered {new_block_count} new block{block_string}',
|
|
|
|
terminal=True)
|
|
|
|
comm_inst.download_blocks_timer.count = \
|
|
|
|
int(comm_inst.download_blocks_timer.frequency * 0.99)
|
2019-08-11 20:44:16 +00:00
|
|
|
comm_inst.decrementThreadCount('lookup_blocks_from_communicator')
|