nullify and refresh tokens

This commit is contained in:
Victor Shyba 2018-09-28 14:19:51 -03:00
parent 7eb9f344f4
commit 9178ca701c
3 changed files with 10 additions and 9 deletions

View file

@ -40,7 +40,7 @@ class _Contact:
self._token = (None, 0) # token, timestamp
def update_token(self, token):
self._token = token, self.getTime()
self._token = token, self.getTime() if token else 0
@property
def token(self):

View file

@ -299,25 +299,24 @@ class Node(MockKademliaHelper):
@defer.inlineCallbacks
def storeToContact(self, blob_hash, contact):
try:
token = contact.token
if not token:
find_value_response = yield contact.findValue(blob_hash)
token = find_value_response[b'token']
contact.update_token(token)
res = yield contact.store(blob_hash, token, self.peerPort, self.node_id, 0)
if not contact.token:
yield contact.findValue(blob_hash)
res = yield contact.store(blob_hash, contact.token, self.peerPort, self.node_id, 0)
if res != b"OK":
raise ValueError(res)
defer.returnValue(True)
log.debug("Stored %s to %s (%s)", binascii.hexlify(blob_hash), contact.log_id(), contact.address)
return True
except protocol.TimeoutError:
log.debug("Timeout while storing blob_hash %s at %s",
binascii.hexlify(blob_hash), contact.log_id())
except ValueError as err:
log.error("Unexpected response: %s" % err)
except Exception as err:
if 'Invalid token' in str(err):
contact.update_token(None)
log.error("Unexpected error while storing blob_hash %s at %s: %s",
binascii.hexlify(blob_hash), contact, err)
defer.returnValue(False)
return False
@defer.inlineCallbacks
def announceHaveBlob(self, blob_hash):

View file

@ -184,6 +184,8 @@ class KademliaProtocol(protocol.DatagramProtocol):
def _update_contact(result): # refresh the contact in the routing table
contact.update_last_replied()
if method == b'findValue':
if b'token' in result:
contact.update_token(result[b'token'])
if b'protocolVersion' not in result:
contact.update_protocol_version(0)
else: