revert 175bfae9e6
. store last known height in wallet instead
This commit is contained in:
parent
1d517abf39
commit
233fd8ed77
|
@ -32,10 +32,15 @@ class Blockchain(util.DaemonThread):
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
self.headers_url = 'http://headers.electrum.org/blockchain_headers'
|
self.headers_url = 'http://headers.electrum.org/blockchain_headers'
|
||||||
self.queue = Queue.Queue()
|
self.queue = Queue.Queue()
|
||||||
|
self.local_height = 0
|
||||||
|
self.set_local_height()
|
||||||
|
|
||||||
|
def height(self):
|
||||||
|
return self.local_height
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.init_headers_file()
|
self.init_headers_file()
|
||||||
self.print_error("%d blocks" % self.config.height)
|
self.print_error("%d blocks" % self.local_height)
|
||||||
|
|
||||||
while self.is_running():
|
while self.is_running():
|
||||||
try:
|
try:
|
||||||
|
@ -48,12 +53,12 @@ class Blockchain(util.DaemonThread):
|
||||||
if not header:
|
if not header:
|
||||||
continue
|
continue
|
||||||
height = header.get('block_height')
|
height = header.get('block_height')
|
||||||
if height <= self.config.height:
|
if height <= self.local_height:
|
||||||
continue
|
continue
|
||||||
if height > self.config.height + 50:
|
if height > self.local_height + 50:
|
||||||
if not self.get_and_verify_chunks(i, header, height):
|
if not self.get_and_verify_chunks(i, header, height):
|
||||||
continue
|
continue
|
||||||
if height > self.config.height:
|
if height > self.local_height:
|
||||||
# get missing parts from interface (until it connects to my chain)
|
# get missing parts from interface (until it connects to my chain)
|
||||||
chain = self.get_chain( i, header )
|
chain = self.get_chain( i, header )
|
||||||
# skip that server if the result is not consistent
|
# skip that server if the result is not consistent
|
||||||
|
@ -155,7 +160,7 @@ class Blockchain(util.DaemonThread):
|
||||||
return rev_hex(Hash(self.header_to_string(header).decode('hex')).encode('hex'))
|
return rev_hex(Hash(self.header_to_string(header).decode('hex')).encode('hex'))
|
||||||
|
|
||||||
def path(self):
|
def path(self):
|
||||||
return self.config.headers_filename()
|
return os.path.join(self.config.path, 'blockchain_headers')
|
||||||
|
|
||||||
def init_headers_file(self):
|
def init_headers_file(self):
|
||||||
filename = self.path()
|
filename = self.path()
|
||||||
|
@ -178,7 +183,7 @@ class Blockchain(util.DaemonThread):
|
||||||
f.seek(index*2016*80)
|
f.seek(index*2016*80)
|
||||||
h = f.write(chunk)
|
h = f.write(chunk)
|
||||||
f.close()
|
f.close()
|
||||||
self.config.refresh_height()
|
self.set_local_height()
|
||||||
|
|
||||||
def save_header(self, header):
|
def save_header(self, header):
|
||||||
data = self.header_to_string(header).decode('hex')
|
data = self.header_to_string(header).decode('hex')
|
||||||
|
@ -189,7 +194,14 @@ class Blockchain(util.DaemonThread):
|
||||||
f.seek(height*80)
|
f.seek(height*80)
|
||||||
h = f.write(data)
|
h = f.write(data)
|
||||||
f.close()
|
f.close()
|
||||||
self.config.refresh_height()
|
self.set_local_height()
|
||||||
|
|
||||||
|
def set_local_height(self):
|
||||||
|
name = self.path()
|
||||||
|
if os.path.exists(name):
|
||||||
|
h = os.path.getsize(name)/80 - 1
|
||||||
|
if self.local_height != h:
|
||||||
|
self.local_height = h
|
||||||
|
|
||||||
def read_header(self, block_height):
|
def read_header(self, block_height):
|
||||||
name = self.path()
|
name = self.path()
|
||||||
|
@ -202,7 +214,6 @@ class Blockchain(util.DaemonThread):
|
||||||
h = self.header_from_string(h)
|
h = self.header_from_string(h)
|
||||||
return h
|
return h
|
||||||
|
|
||||||
|
|
||||||
def get_target(self, index, chain=None):
|
def get_target(self, index, chain=None):
|
||||||
if chain is None:
|
if chain is None:
|
||||||
chain = [] # Do not use mutables as default values!
|
chain = [] # Do not use mutables as default values!
|
||||||
|
@ -307,7 +318,7 @@ class Blockchain(util.DaemonThread):
|
||||||
def get_and_verify_chunks(self, i, header, height):
|
def get_and_verify_chunks(self, i, header, height):
|
||||||
|
|
||||||
queue = Queue.Queue()
|
queue = Queue.Queue()
|
||||||
min_index = (self.config.height + 1)/2016
|
min_index = (self.local_height + 1)/2016
|
||||||
max_index = (height + 1)/2016
|
max_index = (height + 1)/2016
|
||||||
n = min_index
|
n = min_index
|
||||||
while n < max_index + 1:
|
while n < max_index + 1:
|
||||||
|
|
|
@ -562,4 +562,4 @@ class Network(util.DaemonThread):
|
||||||
return self.blockchain.read_header(tx_height)
|
return self.blockchain.read_header(tx_height)
|
||||||
|
|
||||||
def get_local_height(self):
|
def get_local_height(self):
|
||||||
return self.config.height
|
return self.blockchain.height()
|
||||||
|
|
|
@ -71,16 +71,12 @@ class SimpleConfig(object):
|
||||||
# update the current options with the command line options last (to
|
# update the current options with the command line options last (to
|
||||||
# override both others).
|
# override both others).
|
||||||
self.read_only_options.update(options)
|
self.read_only_options.update(options)
|
||||||
|
|
||||||
# init path
|
# init path
|
||||||
self.init_path()
|
self.init_path()
|
||||||
|
|
||||||
# user config.
|
# user config.
|
||||||
self.user_config = read_user_config_function(self.path)
|
self.user_config = read_user_config_function(self.path)
|
||||||
|
# Make a singleton instance of 'self'
|
||||||
self.refresh_height()
|
set_config(self)
|
||||||
|
|
||||||
set_config(self) # Make a singleton instance of 'self'
|
|
||||||
|
|
||||||
def init_path(self):
|
def init_path(self):
|
||||||
# Read electrum path in the command line configuration
|
# Read electrum path in the command line configuration
|
||||||
|
@ -106,7 +102,6 @@ class SimpleConfig(object):
|
||||||
self.user_config[key] = value
|
self.user_config[key] = value
|
||||||
if save:
|
if save:
|
||||||
self.save_user_config()
|
self.save_user_config()
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
|
@ -124,16 +119,6 @@ class SimpleConfig(object):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def headers_filename(self):
|
|
||||||
return os.path.join(self.path, 'blockchain_headers')
|
|
||||||
|
|
||||||
def refresh_height(self):
|
|
||||||
name = self.headers_filename()
|
|
||||||
if os.path.exists(name):
|
|
||||||
self.height = os.path.getsize(name) / 80 - 1
|
|
||||||
else:
|
|
||||||
self.height = 0
|
|
||||||
|
|
||||||
def save_user_config(self):
|
def save_user_config(self):
|
||||||
if not self.path:
|
if not self.path:
|
||||||
return
|
return
|
||||||
|
|
|
@ -139,6 +139,7 @@ class Abstract_Wallet(object):
|
||||||
self.seed = storage.get('seed', '') # encrypted
|
self.seed = storage.get('seed', '') # encrypted
|
||||||
self.labels = storage.get('labels', {})
|
self.labels = storage.get('labels', {})
|
||||||
self.frozen_addresses = storage.get('frozen_addresses',[])
|
self.frozen_addresses = storage.get('frozen_addresses',[])
|
||||||
|
self.stored_height = storage.get('stored_height', 0) # last known height (for offline mode)
|
||||||
|
|
||||||
self.history = storage.get('addr_history',{}) # address -> list(txid, height)
|
self.history = storage.get('addr_history',{}) # address -> list(txid, height)
|
||||||
self.fee_per_kb = int(storage.get('fee_per_kb', RECOMMENDED_FEE))
|
self.fee_per_kb = int(storage.get('fee_per_kb', RECOMMENDED_FEE))
|
||||||
|
@ -397,9 +398,8 @@ class Abstract_Wallet(object):
|
||||||
return txs
|
return txs
|
||||||
|
|
||||||
def get_local_height(self):
|
def get_local_height(self):
|
||||||
""" todo: fetch height in offline mode """
|
""" return last known height if we are offline """
|
||||||
return self.network.get_local_height() if self.network else 0
|
return self.network.get_local_height() if self.network else self.stored_height
|
||||||
|
|
||||||
|
|
||||||
def get_confirmations(self, tx):
|
def get_confirmations(self, tx):
|
||||||
""" return the number of confirmations of a monitored transaction. """
|
""" return the number of confirmations of a monitored transaction. """
|
||||||
|
@ -1088,6 +1088,7 @@ class Abstract_Wallet(object):
|
||||||
if self.network:
|
if self.network:
|
||||||
self.verifier.stop()
|
self.verifier.stop()
|
||||||
self.synchronizer.stop()
|
self.synchronizer.stop()
|
||||||
|
self.storage.put('stored_height', self.get_local_height(), True)
|
||||||
|
|
||||||
def restore(self, cb):
|
def restore(self, cb):
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in New Issue