Demote payloadLength from class instance variable to processData local variable as no other function was using it
Improve processData:
-Utilise shared.Header
-Use a memoryview to reduce memory overhead
-Clean up variables before a recursive call
-Strip null bytes from command
Refactor sendData
Various functions:
-Use shared.CreatePacket to generate packets
Fix typo in _checkIPv4Address
# print 'The magic bytes were not correct. First 40 bytes of data: ' + repr(self.data[0:40])
self.data=""
return
self.payloadLength,=unpack('>L',self.data[16:20])
ifself.payloadLength>20000000:
logger.info('The incoming message, which we have not yet download, is too large. Ignoring it. (unfortunately there is no way to tell the other node to stop sending it except to disconnect.) Message size: %s'%self.payloadLength)
self.data=self.data[self.payloadLength+24:]
ifpayloadLength>20000000:
logger.info('The incoming message, which we have not yet download, is too large. Ignoring it. (unfortunately there is no way to tell the other node to stop sending it except to disconnect.) Message size: %s'%payloadLength)
self.data=view[payloadLength:].tobytes()
delview,magic,command,payloadLength,checksum#we don't need these anymore and better to clean them now before the recursive call rather than after
self.processData()
return
iflen(self.data)<self.payloadLength+24:# check if the whole message has arrived yet.
iflen(view)<payloadLength:# check if the whole message has arrived yet.
return
ifself.data[20:24]!=hashlib.sha512(self.data[24:self.payloadLength+24]).digest()[0:4]:# test the checksum in the message. If it is correct...
payload=view[:payloadLength]
ifchecksum!=hashlib.sha512(payload).digest()[0:4]:# test the checksum in the message. If it is correct...
print'Checksum incorrect. Clearing this message.'
self.data=self.data[self.payloadLength+24:]
self.data=view[payloadLength:].tobytes()
delview,magic,command,payloadLength,checksum,payload#again better to clean up before the recursive call
self.processData()
return
#We can now revert back to bytestrings and take this message out
payload=payload.tobytes()
self.data=view[payloadLength:].tobytes()
delview,magic,payloadLength,checksum
# The time we've last seen this node is obviously right now since we
# just received valid data from it. So update the knownNodes list so
# that other peers can be made aware of its existance.
@ -143,37 +153,39 @@ class receiveDataThread(threading.Thread):
@ -297,7 +309,7 @@ class receiveDataThread(threading.Thread):
forhash,storedValueinbigInvList.items():
payload+=hash
numberOfObjectsInInvMessage+=1
ifnumberOfObjectsInInvMessage>= 50000:# We can only send a max of 50000 items per inv message but we may have more objects to advertise. They must be split up into multiple inv messages.
ifnumberOfObjectsInInvMessage== 50000:# We can only send a max of 50000 items per inv message but we may have more objects to advertise. They must be split up into multiple inv messages.
self.sendinvMessageToJustThisOnePeer(
numberOfObjectsInInvMessage,payload)
payload=''
@ -311,13 +323,9 @@ class receiveDataThread(threading.Thread):
# function for broadcasting invs to everyone in our stream.