Fix bad peer looping
JWU42 pointed out an issue where peer discovery could get in a failure loop for bad peers; this fixes the the root cause and the immediate retries
This commit is contained in:
parent
7b263c5c49
commit
3236989165
@ -56,7 +56,7 @@ class PeerSession(JSONSession):
|
|||||||
self.peer = peer
|
self.peer = peer
|
||||||
self.peer_mgr = peer_mgr
|
self.peer_mgr = peer_mgr
|
||||||
self.kind = kind
|
self.kind = kind
|
||||||
self.failed = False
|
self.bad = False
|
||||||
self.log_prefix = '[{}] '.format(self.peer)
|
self.log_prefix = '[{}] '.format(self.peer)
|
||||||
|
|
||||||
async def wait_on_items(self):
|
async def wait_on_items(self):
|
||||||
@ -82,6 +82,7 @@ class PeerSession(JSONSession):
|
|||||||
[version.VERSION, proto_ver])
|
[version.VERSION, proto_ver])
|
||||||
self.send_request(self.on_features, 'server.features')
|
self.send_request(self.on_features, 'server.features')
|
||||||
self.send_request(self.on_headers, 'blockchain.headers.subscribe')
|
self.send_request(self.on_headers, 'blockchain.headers.subscribe')
|
||||||
|
self.send_request(self.on_peers_subscribe, 'server.peers.subscribe')
|
||||||
|
|
||||||
def connection_lost(self, exc):
|
def connection_lost(self, exc):
|
||||||
'''Handle disconnection.'''
|
'''Handle disconnection.'''
|
||||||
@ -91,7 +92,7 @@ class PeerSession(JSONSession):
|
|||||||
def on_peers_subscribe(self, result, error):
|
def on_peers_subscribe(self, result, error):
|
||||||
'''Handle the response to the peers.subcribe message.'''
|
'''Handle the response to the peers.subcribe message.'''
|
||||||
if error:
|
if error:
|
||||||
self.failed = True
|
self.bad = True
|
||||||
self.log_error('server.peers.subscribe: {}'.format(error))
|
self.log_error('server.peers.subscribe: {}'.format(error))
|
||||||
else:
|
else:
|
||||||
self.check_remote_peers(result)
|
self.check_remote_peers(result)
|
||||||
@ -133,54 +134,40 @@ class PeerSession(JSONSession):
|
|||||||
'''Handle the response to the add_peer message.'''
|
'''Handle the response to the add_peer message.'''
|
||||||
self.close_if_done()
|
self.close_if_done()
|
||||||
|
|
||||||
def peer_verified(self, is_good):
|
|
||||||
'''Call when it has been determined whether or not the peer seems to
|
|
||||||
be on the same network.
|
|
||||||
'''
|
|
||||||
if is_good:
|
|
||||||
self.send_request(self.on_peers_subscribe,
|
|
||||||
'server.peers.subscribe')
|
|
||||||
else:
|
|
||||||
self.peer.mark_bad()
|
|
||||||
self.failed = True
|
|
||||||
|
|
||||||
def on_features(self, features, error):
|
def on_features(self, features, error):
|
||||||
# Several peers don't implement this. If they do, check they are
|
# Several peers don't implement this. If they do, check they are
|
||||||
# the same network with the genesis hash.
|
# the same network with the genesis hash.
|
||||||
verified = False
|
|
||||||
if not error and isinstance(features, dict):
|
if not error and isinstance(features, dict):
|
||||||
our_hash = self.peer_mgr.env.coin.GENESIS_HASH
|
our_hash = self.peer_mgr.env.coin.GENESIS_HASH
|
||||||
if our_hash != features.get('genesis_hash'):
|
if our_hash != features.get('genesis_hash'):
|
||||||
self.peer_verified(False)
|
self.bad = True
|
||||||
self.log_warning('incorrect genesis hash')
|
self.log_warning('incorrect genesis hash')
|
||||||
else:
|
else:
|
||||||
self.peer_verified(True)
|
|
||||||
self.peer.update_features(features)
|
self.peer.update_features(features)
|
||||||
verified = True
|
|
||||||
self.close_if_done()
|
self.close_if_done()
|
||||||
|
|
||||||
def on_headers(self, result, error):
|
def on_headers(self, result, error):
|
||||||
'''Handle the response to the version message.'''
|
'''Handle the response to the version message.'''
|
||||||
if error:
|
if error:
|
||||||
self.failed = True
|
self.bad = True
|
||||||
self.log_error('blockchain.headers.subscribe returned an error')
|
self.log_error('blockchain.headers.subscribe returned an error')
|
||||||
elif not isinstance(result, dict):
|
elif not isinstance(result, dict):
|
||||||
|
self.bad = True
|
||||||
self.log_error('bad blockchain.headers.subscribe response')
|
self.log_error('bad blockchain.headers.subscribe response')
|
||||||
self.peer_verified(False)
|
|
||||||
else:
|
else:
|
||||||
our_height = self.peer_mgr.controller.bp.db_height
|
our_height = self.peer_mgr.controller.bp.db_height
|
||||||
their_height = result.get('block_height')
|
their_height = result.get('block_height')
|
||||||
is_good = (isinstance(their_height, int) and
|
is_good = (isinstance(their_height, int) and
|
||||||
abs(our_height - their_height) <= 5)
|
abs(our_height - their_height) <= 5)
|
||||||
self.peer_verified(is_good)
|
|
||||||
if not is_good:
|
if not is_good:
|
||||||
|
self.bad = True
|
||||||
self.log_warning('bad height {}'.format(their_height))
|
self.log_warning('bad height {}'.format(their_height))
|
||||||
self.close_if_done()
|
self.close_if_done()
|
||||||
|
|
||||||
def on_version(self, result, error):
|
def on_version(self, result, error):
|
||||||
'''Handle the response to the version message.'''
|
'''Handle the response to the version message.'''
|
||||||
if error:
|
if error:
|
||||||
self.failed = True
|
self.bad = True
|
||||||
self.log_error('server.version returned an error')
|
self.log_error('server.version returned an error')
|
||||||
elif isinstance(result, str):
|
elif isinstance(result, str):
|
||||||
self.peer.server_version = result
|
self.peer.server_version = result
|
||||||
@ -189,14 +176,15 @@ class PeerSession(JSONSession):
|
|||||||
|
|
||||||
def close_if_done(self):
|
def close_if_done(self):
|
||||||
if not self.has_pending_requests():
|
if not self.has_pending_requests():
|
||||||
is_good = not self.failed
|
if self.bad:
|
||||||
self.peer_mgr.set_connection_status(self.peer, is_good)
|
peer.mark_bad()
|
||||||
|
self.peer_mgr.set_connection_status(self.peer, not self.bad)
|
||||||
if self.peer.is_tor:
|
if self.peer.is_tor:
|
||||||
how = 'via {} over Tor'.format(self.kind)
|
how = 'via {} over Tor'.format(self.kind)
|
||||||
else:
|
else:
|
||||||
how = 'via {} at {}'.format(self.kind,
|
how = 'via {} at {}'.format(self.kind,
|
||||||
self.peer_addr(anon=False))
|
self.peer_addr(anon=False))
|
||||||
status = 'verified' if is_good else 'failed to verify'
|
status = 'failed to verify' if self.bad else 'verified'
|
||||||
elapsed = time.time() - self.peer.last_try
|
elapsed = time.time() - self.peer.last_try
|
||||||
self.log_info('{} {} in {:.1f}s'.format(status, how, elapsed))
|
self.log_info('{} {} in {:.1f}s'.format(status, how, elapsed))
|
||||||
self.close_connection()
|
self.close_connection()
|
||||||
@ -559,11 +547,8 @@ class PeerManager(util.LoggedClass):
|
|||||||
|
|
||||||
def maybe_forget_peer(self, peer):
|
def maybe_forget_peer(self, peer):
|
||||||
'''Forget the peer if appropriate, e.g. long-term unreachable.'''
|
'''Forget the peer if appropriate, e.g. long-term unreachable.'''
|
||||||
if peer.bad:
|
try_limit = 10 if peer.last_connect else 3
|
||||||
forget = peer.last_connect < time.time() - STALE_SECS // 2
|
forget = peer.try_count >= try_limit
|
||||||
else:
|
|
||||||
try_limit = 10 if peer.last_connect else 3
|
|
||||||
forget = peer.try_count >= try_limit
|
|
||||||
|
|
||||||
if forget:
|
if forget:
|
||||||
desc = 'bad' if peer.bad else 'unreachable'
|
desc = 'bad' if peer.bad else 'unreachable'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user