diff --git a/lbrynet/dht/constants.py b/lbrynet/dht/constants.py index b3d19022b..f84c89d2e 100644 --- a/lbrynet/dht/constants.py +++ b/lbrynet/dht/constants.py @@ -59,3 +59,5 @@ from lbrynet.core.cryptoutils import get_lbry_hash_obj h = get_lbry_hash_obj() key_bits = h.digest_size * 8 # 384 bits + +rpc_id_length = 20 diff --git a/lbrynet/dht/msgtypes.py b/lbrynet/dht/msgtypes.py index 3c1aeabe2..6eb2d3e74 100644 --- a/lbrynet/dht/msgtypes.py +++ b/lbrynet/dht/msgtypes.py @@ -8,12 +8,17 @@ # may be created by processing this file with epydoc: http://epydoc.sf.net from lbrynet.core.utils import generate_id +import constants class Message(object): """ Base class for messages - all "unknown" messages use this class """ def __init__(self, rpcID, nodeID): + if len(rpcID) != constants.rpc_id_length: + raise ValueError("invalid rpc id: %i bytes (expected 20)" % len(rpcID)) + if len(nodeID) != constants.key_bits / 8: + raise ValueError("invalid node id: %i bytes (expected 48)" % len(nodeID)) self.id = rpcID self.nodeID = nodeID @@ -23,7 +28,7 @@ class RequestMessage(Message): def __init__(self, nodeID, method, methodArgs, rpcID=None): if rpcID is None: - rpcID = generate_id() + rpcID = generate_id()[:constants.rpc_id_length] Message.__init__(self, rpcID, nodeID) self.request = method self.args = methodArgs diff --git a/lbrynet/dht/protocol.py b/lbrynet/dht/protocol.py index fa6670c66..e1d1fc122 100644 --- a/lbrynet/dht/protocol.py +++ b/lbrynet/dht/protocol.py @@ -422,8 +422,10 @@ class KademliaProtocol(protocol.DatagramProtocol): self._sentMessages[messageID] = (remoteContactID, df, timeoutCall, method, args) else: # No progress has been made - del self._partialMessagesProgress[messageID] - del self._partialMessages[messageID] + if messageID in self._partialMessagesProgress: + del self._partialMessagesProgress[messageID] + if messageID in self._partialMessages: + del self._partialMessages[messageID] df.errback(TimeoutError(remoteContactID)) def _hasProgressBeenMade(self, messageID):