URI: 
       treplace blockchain.height with height(), and fix server_lag issue - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 116b10cc11addb497a4e0be76459412c85127784
   DIR parent 858be17553a2557a44eb33150e67df62fda15233
  HTML Author: ThomasV <thomasv@gitorious>
       Date:   Wed,  9 Oct 2013 10:04:32 +0200
       
       replace blockchain.height with height(), and fix server_lag issue
       
       Diffstat:
         M gui/gtk.py                          |       6 +++---
         M gui/qt/network_dialog.py            |       2 +-
         M lib/blockchain.py                   |      14 ++++++++------
         M lib/network.py                      |      24 +++++++++++++++++++-----
       
       4 files changed, 31 insertions(+), 15 deletions(-)
       ---
   DIR diff --git a/gui/gtk.py b/gui/gtk.py
       t@@ -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:
       t@@ -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() )
   DIR diff --git a/gui/qt/network_dialog.py b/gui/qt/network_dialog.py
       t@@ -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")
        
   DIR diff --git a/lib/blockchain.py b/lib/blockchain.py
       t@@ -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'
       t@@ -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
        
       t@@ -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 ):
       t@@ -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)
        
        
                            
       t@@ -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):
   DIR diff --git a/lib/network.py b/lib/network.py
       t@@ -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)
        
       t@@ -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
       t@@ -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')
       t@@ -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:
       t@@ -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')
        
       t@@ -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')