replace blockchain.height with height(), and fix server_lag issue

This commit is contained in:
ThomasV 2013-10-09 10:04:32 +02:00
parent 858be17553
commit 116b10cc11
4 changed files with 31 additions and 15 deletions

View File

@ -235,7 +235,7 @@ def run_network_dialog( network, parent ):
if parent:
if network.is_connected():
interface = network.interface
status = "Connected to %s:%d\n%d blocks"%(interface.host, interface.port, network.blockchain.height)
status = "Connected to %s:%d\n%d blocks"%(interface.host, interface.port, network.blockchain.height())
else:
status = "Not connected"
else:
@ -1074,13 +1074,13 @@ class ElectrumWindow:
if self.funds_error:
text = "Not enough funds"
elif interface and interface.is_connected:
self.network_button.set_tooltip_text("Connected to %s:%d.\n%d blocks"%(interface.host, interface.port, self.network.blockchain.height))
self.network_button.set_tooltip_text("Connected to %s:%d.\n%d blocks"%(interface.host, interface.port, self.network.blockchain.height()))
if not self.wallet.up_to_date:
self.status_image.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_MENU)
text = "Synchronizing..."
else:
self.status_image.set_from_stock(gtk.STOCK_YES, gtk.ICON_SIZE_MENU)
self.network_button.set_tooltip_text("Connected to %s:%d.\n%d blocks"%(interface.host, interface.port, self.network.blockchain.height))
self.network_button.set_tooltip_text("Connected to %s:%d.\n%d blocks"%(interface.host, interface.port, self.network.blockchain.height()))
c, u = self.wallet.get_balance()
text = "Balance: %s "%( format_satoshis(c,False,self.num_zeros) )
if u: text += "[%s unconfirmed]"%( format_satoshis(u,True,self.num_zeros).strip() )

View File

@ -45,7 +45,7 @@ class NetworkDialog(QDialog):
if parent:
n = len(network.interfaces)
if n:
status = _("Blockchain") + ": " + "%d "%(network.blockchain.height) + _("blocks") + ".\n" + _("Getting block headers from %d nodes.")%n
status = _("Blockchain") + ": " + "%d "%(network.blockchain.height()) + _("blocks") + ".\n" + _("Getting block headers from %d nodes.")%n
else:
status = _("Not connected")

View File

@ -30,7 +30,6 @@ class Blockchain(threading.Thread):
self.config = config
self.network = network
self.lock = threading.Lock()
self.height = 0
self.local_height = 0
self.running = False
self.headers_url = 'http://headers.electrum.org/blockchain_headers'
@ -38,6 +37,10 @@ class Blockchain(threading.Thread):
self.queue = Queue.Queue()
def height(self):
return self.local_height
def stop(self):
with self.lock: self.running = False
@ -80,7 +83,9 @@ class Blockchain(threading.Thread):
chain = self.get_chain( i, header )
# skip that server if the result is not consistent
if not chain: continue
if not chain:
print_error('e')
continue
# verify the chain
if self.verify_chain( chain ):
@ -93,9 +98,7 @@ class Blockchain(threading.Thread):
continue
if self.height != height:
self.height = height
self.network.new_blockchain_height(height, i)
self.network.new_blockchain_height(height, i)
@ -253,7 +256,6 @@ class Blockchain(threading.Thread):
h = os.path.getsize(name)/80 - 1
if self.local_height != h:
self.local_height = h
self.height = self.local_height
def read_header(self, block_height):

View File

@ -198,7 +198,7 @@ class Network(threading.Thread):
self.switch_to_random_interface()
else:
if self.server_lag > 0:
self.interface.stop()
self.stop_interface()
else:
self.set_server(server)
@ -208,18 +208,23 @@ class Network(threading.Thread):
self.switch_to_interface(random.choice(self.interfaces.values()))
def switch_to_interface(self, interface):
assert self.interface is None
server = interface.server
print_error("switching to", server)
self.interface = interface
h = self.heights.get(server)
if h:
self.server_lag = self.blockchain.height - h
self.server_lag = self.blockchain.height() - h
self.config.set_key('server', server, False)
self.default_server = server
self.send_subscriptions()
self.trigger_callback('connected')
def stop_interface(self):
self.interface.stop()
self.interface = None
def set_server(self, server):
if self.default_server == server and self.interface:
return
@ -229,7 +234,7 @@ class Network(threading.Thread):
# stop the interface in order to terminate subscriptions
if self.interface:
self.interface.stop()
self.stop_interface()
# notify gui
self.trigger_callback('disconnecting')
@ -255,6 +260,7 @@ class Network(threading.Thread):
def new_blockchain_height(self, blockchain_height, i):
print_error('new_blockchain_height')
if self.is_connected():
h = self.heights.get(self.interface.server)
if h:
@ -263,6 +269,8 @@ class Network(threading.Thread):
print_error( "Server is lagging", blockchain_height, h)
if self.config.get('auto_cycle'):
self.set_server(i.server)
else:
print_error('no height for main interface')
self.trigger_callback('updated')
@ -304,11 +312,17 @@ class Network(threading.Thread):
def on_header(self, i, r):
result = r.get('result')
if not result: return
self.heights[i.server] = result.get('block_height')
height = result.get('block_height')
self.heights[i.server] = height
# notify blockchain about the new height
self.blockchain.queue.put((i,result))
if i == self.interface:
self.server_lag = self.blockchain.height - self.heights[i.server]
self.server_lag = self.blockchain.height() - height
if self.server_lag > 1 and self.config.get('auto_cycle'):
print_error( "Server lagging, stopping interface")
self.stop_interface()
self.trigger_callback('updated')