add download progress and is downloading flag to daemon status ()

This commit is contained in:
Akinwale Ariwodola 2018-06-21 23:49:22 +01:00 committed by GitHub
parent 4bcf0ab7fb
commit 2fa2269cc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 8 deletions

View file

@ -25,7 +25,7 @@ at anytime.
* *
### Added ### Added
* * added blockchain_headers download progress percentage to daemon status call
* *
### Removed ### Removed

View file

@ -2,6 +2,7 @@ import os
from collections import defaultdict, deque from collections import defaultdict, deque
import datetime import datetime
import logging import logging
import math
from decimal import Decimal from decimal import Decimal
import treq import treq
@ -86,6 +87,10 @@ class Wallet(object):
self.max_expected_payment_time = datetime.timedelta(minutes=3) self.max_expected_payment_time = datetime.timedelta(minutes=3)
self.stopped = True self.stopped = True
# blockchain_headers progress
self.headers_download = False
self.headers_progress_percent = 0
self.manage_running = False self.manage_running = False
self._manage_count = 0 self._manage_count = 0
self._balance_refresh_time = 3 self._balance_refresh_time = 3
@ -105,14 +110,24 @@ class Wallet(object):
elif final_size_after_download and not final_size_after_download % HEADER_SIZE: elif final_size_after_download and not final_size_after_download % HEADER_SIZE:
s3_height = (final_size_after_download / HEADER_SIZE) - 1 s3_height = (final_size_after_download / HEADER_SIZE) - 1
local_height = self.local_header_file_height() local_height = self.local_header_file_height()
if s3_height > local_height: if s3_height > local_height:
self.headers_download = True
def collector(data, h_file):
h_file.write(data)
local_size = float(h_file.tell())
final_size = float(final_size_after_download)
self.headers_progress_percent = math.ceil(local_size / final_size * 100)
self.headers_download = self.headers_progress_percent < 100
if local_header_size: if local_header_size:
log.info("Resuming download of %i bytes from s3", response.length) log.info("Resuming download of %i bytes from s3", response.length)
with open(os.path.join(self.config.path, "blockchain_headers"), "a+b") as headers_file: with open(os.path.join(self.config.path, "blockchain_headers"), "a+b") as headers_file:
yield treq.collect(response, headers_file.write) yield treq.collect(response, lambda d: collector(d, headers_file))
else: else:
with open(os.path.join(self.config.path, "blockchain_headers"), "wb") as headers_file: with open(os.path.join(self.config.path, "blockchain_headers"), "wb") as headers_file:
yield treq.collect(response, headers_file.write) yield treq.collect(response, lambda d: collector(d, headers_file))
log.info("fetched headers from s3 (s3 height: %i), now verifying integrity after download.", s3_height) log.info("fetched headers from s3 (s3 height: %i), now verifying integrity after download.", s3_height)
self._check_header_file_integrity() self._check_header_file_integrity()
else: else:
@ -129,6 +144,12 @@ class Wallet(object):
return os.stat(headers_path).st_size return os.stat(headers_path).st_size
return 0 return 0
def is_downloading_headers(self):
return self.headers_download
def get_headers_progress_percent(self):
return self.headers_progress_percent if (self.headers_download or self.headers_progress_percent > 0) else 0
@defer.inlineCallbacks @defer.inlineCallbacks
def get_remote_height(self, server, port): def get_remote_height(self, server, port):
connected = defer.Deferred() connected = defer.Deferred()
@ -192,6 +213,7 @@ class Wallet(object):
try: try:
yield self.fetch_headers_from_s3() yield self.fetch_headers_from_s3()
except Exception as err: except Exception as err:
self.headers_download = False
log.error("failed to fetch headers from s3: %s", err) log.error("failed to fetch headers from s3: %s", err)
log.info("Starting wallet.") log.info("Starting wallet.")
yield self._start() yield self._start()

View file

@ -1,5 +1,6 @@
import binascii import binascii
import logging.handlers import logging.handlers
import math
import mimetypes import mimetypes
import os import os
import base58 import base58
@ -1053,6 +1054,8 @@ class Daemon(AuthJSONRPCServer):
'blocks': local blockchain height, 'blocks': local blockchain height,
'blocks_behind': remote_height - local_height, 'blocks_behind': remote_height - local_height,
'best_blockhash': block hash of most recent block, 'best_blockhash': block hash of most recent block,
'headers_download_progress': the download progress of the blockchain_headers file,
'is_downloading_headers': bool, flag to indicate if the blockchain_headers file is being downloaded
}, },
'wallet_is_encrypted': bool, 'wallet_is_encrypted': bool,
@ -1067,12 +1070,18 @@ class Daemon(AuthJSONRPCServer):
""" """
# on startup, the wallet or network won't be available but we still need this call to work # on startup, the wallet or network won't be available but we still need this call to work
has_wallet = self.session and self.session.wallet and self.session.wallet.network has_wallet = self.session and self.session.wallet
local_height = self.session.wallet.network.get_local_height() if has_wallet else 0 downloading_headers = self.session.wallet.is_downloading_headers() if has_wallet else False
remote_height = self.session.wallet.network.get_server_height() if has_wallet else 0 has_wallet_with_network = has_wallet and self.session.wallet.network
best_hash = (yield self.session.wallet.get_best_blockhash()) if has_wallet else None local_height = self.session.wallet.network.get_local_height() if has_wallet_with_network else 0
wallet_is_encrypted = has_wallet and self.session.wallet.wallet and \ remote_height = self.session.wallet.network.get_server_height() if has_wallet_with_network else 0
best_hash = (yield self.session.wallet.get_best_blockhash()) if has_wallet_with_network else None
wallet_is_encrypted = has_wallet_with_network and self.session.wallet.wallet and \
self.session.wallet.wallet.use_encryption self.session.wallet.wallet.use_encryption
headers_progress_percent = self.session.wallet.get_headers_progress_percent() if has_wallet else 0
headers_download_progress = headers_progress_percent \
if downloading_headers or headers_progress_percent == 100 \
else math.ceil((local_height / float(remote_height)) * 100) if remote_height else 0
response = { response = {
'lbry_id': base58.b58encode(self.node_id), 'lbry_id': base58.b58encode(self.node_id),
@ -1097,6 +1106,8 @@ class Daemon(AuthJSONRPCServer):
'blocks': local_height, 'blocks': local_height,
'blocks_behind': remote_height - local_height, 'blocks_behind': remote_height - local_height,
'best_blockhash': best_hash, 'best_blockhash': best_hash,
'headers_download_progress': headers_download_progress,
'is_downloading_headers': downloading_headers,
} }
} }
if session_status: if session_status: