@ -114,7 +114,7 @@ def deserialize_proxy(s):
def deserialize_server ( server_str ) :
def deserialize_server ( server_str ) :
host , port , protocol = str ( server_str ) . split ( ' : ' )
host , port , protocol = str ( server_str ) . split ( ' : ' )
assert protocol in ' st '
assert protocol in ' st '
int ( port )
int ( port ) # Throw if cannot be converted to int
return host , port , protocol
return host , port , protocol
def serialize_server ( host , port , protocol ) :
def serialize_server ( host , port , protocol ) :
@ -205,12 +205,15 @@ class Network(util.DaemonThread):
return self . heights . get ( self . default_server , 0 )
return self . heights . get ( self . default_server , 0 )
def server_is_lagging ( self ) :
def server_is_lagging ( self ) :
h = self . get_server_height ( )
s h = self . get_server_height ( )
if not h :
if not s h:
self . print_error ( ' no height for main interface ' )
self . print_error ( ' no height for main interface ' )
return False
return False
lag = self . get_local_height ( ) - self . get_server_height ( )
lh = self . get_local_height ( )
return lag > 1
result = ( lh - sh ) > 1
if result :
self . print_error ( ' %s is lagging ( %d vs %d ) ' % ( self . default_server , sh , lh ) )
return result
def set_status ( self , status ) :
def set_status ( self , status ) :
self . connection_status = status
self . connection_status = status
@ -332,14 +335,22 @@ class Network(util.DaemonThread):
self . start_network ( protocol , proxy )
self . start_network ( protocol , proxy )
elif self . default_server != server :
elif self . default_server != server :
self . switch_to_interface ( server )
self . switch_to_interface ( server )
elif auto_connect and ( not self . is_connected ( ) or self . server_is_lagging ( ) ) :
else :
self . switch_to_random _interface ( )
self . switch_lagging _interface ( )
def switch_to_random_interface ( self ) :
def switch_to_random_interface ( self ) :
if self . interfaces :
if self . interfaces :
server = random . choice ( self . interfaces . keys ( ) )
server = random . choice ( self . interfaces . keys ( ) )
self . switch_to_interface ( server )
self . switch_to_interface ( server )
def switch_lagging_interface ( self , suggestion = None ) :
''' If auto_connect and lagging, switch interface '''
if self . server_is_lagging ( ) and self . auto_connect ( ) :
if suggestion and self . protocol == deserialize_server ( suggestion ) [ 2 ] :
self . switch_to_interface ( suggestion )
else :
self . switch_to_random_interface ( )
def switch_to_interface ( self , server ) :
def switch_to_interface ( self , server ) :
''' Switch to server as our interface. If not already connected, start a
''' Switch to server as our interface. If not already connected, start a
connection - we will switch on receipt of the connection notification '''
connection - we will switch on receipt of the connection notification '''
@ -361,16 +372,6 @@ class Network(util.DaemonThread):
self . interface . stop ( )
self . interface . stop ( )
self . interface = None
self . interface = None
def set_server ( self , server ) :
if self . default_server == server and self . is_connected ( ) :
return
if self . protocol != deserialize_server ( server ) [ 2 ] :
return
self . switch_to_interface ( server )
def add_recent_server ( self , i ) :
def add_recent_server ( self , i ) :
# list is ordered
# list is ordered
s = i . server
s = i . server
@ -381,11 +382,7 @@ class Network(util.DaemonThread):
self . save_recent_servers ( )
self . save_recent_servers ( )
def new_blockchain_height ( self , blockchain_height , i ) :
def new_blockchain_height ( self , blockchain_height , i ) :
if self . is_connected ( ) :
self . switch_lagging_interface ( i . server )
if self . server_is_lagging ( ) :
self . print_error ( " Server is lagging " , blockchain_height , self . get_server_height ( ) )
if self . auto_connect ( ) :
self . set_server ( i . server )
self . notify ( ' updated ' )
self . notify ( ' updated ' )
def process_if_notification ( self , i ) :
def process_if_notification ( self , i ) :
@ -526,9 +523,7 @@ class Network(util.DaemonThread):
self . blockchain . queue . put ( ( i , result ) )
self . blockchain . queue . put ( ( i , result ) )
if i == self . interface :
if i == self . interface :
if self . server_is_lagging ( ) and self . auto_connect ( ) :
self . switch_lagging_interface ( )
self . print_error ( " Server lagging, stopping interface " )
self . stop_interface ( )
self . notify ( ' updated ' )
self . notify ( ' updated ' )