Object validator trigger moved

- from bmproto to bmobject
This commit is contained in:
Peter Šurda 2017-06-24 12:21:06 +02:00
parent dc5a91f326
commit d57b0c55ee
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
2 changed files with 18 additions and 21 deletions

View File

@ -32,7 +32,7 @@ class BMObject(object):
# min TTL, 3 hour (in the past # min TTL, 3 hour (in the past
minTTL = -3600 minTTL = -3600
def __init__(self, nonce, expiresTime, objectType, version, streamNumber, data): def __init__(self, nonce, expiresTime, objectType, version, streamNumber, data, payloadOffset):
self.nonce = nonce self.nonce = nonce
self.expiresTime = expiresTime self.expiresTime = expiresTime
self.objectType = objectType self.objectType = objectType
@ -40,7 +40,7 @@ class BMObject(object):
self.streamNumber = streamNumber self.streamNumber = streamNumber
self.inventoryHash = calculateInventoryHash(data) self.inventoryHash = calculateInventoryHash(data)
self.data = data self.data = data
self.tag = '' self.tag = data[payloadOffset:payloadOffset+32]
def checkProofOfWorkSufficient(self): def checkProofOfWorkSufficient(self):
# Let us check to make sure that the proof of work is sufficient. # Let us check to make sure that the proof of work is sufficient.
@ -69,6 +69,17 @@ class BMObject(object):
if self.inventoryHash in Inventory(): if self.inventoryHash in Inventory():
raise BMObjectAlreadyHaveError() raise BMObjectAlreadyHaveError()
def checkObjectByType(self):
if self.objectType == protocol.OBJECT_GETPUBKEY:
self.checkGetpubkey()
elif self.objectType == protocol.OBJECT_PUBKEY:
self.checkPubkey()
elif self.objectType == protocol.OBJECT_MSG:
self.checkMessage()
elif self.objectType == protocol.OBJECT_BROADCAST:
self.checkBroadcast()
# other objects don't require other types of tests
def checkMessage(self): def checkMessage(self):
return return
@ -77,15 +88,12 @@ class BMObject(object):
logger.info('getpubkey message doesn\'t contain enough data. Ignoring.') logger.info('getpubkey message doesn\'t contain enough data. Ignoring.')
raise BMObjectInvalidError() raise BMObjectInvalidError()
def checkPubkey(self, tag): def checkPubkey(self):
if len(self.data) < 146 or len(self.data) > 440: # sanity check if len(self.data) < 146 or len(self.data) > 440: # sanity check
logger.info('pubkey object too short or too long. Ignoring.') logger.info('pubkey object too short or too long. Ignoring.')
raise BMObjectInvalidError() raise BMObjectInvalidError()
if self.version >= 4:
self.tag = tag
logger.debug('tag in received pubkey is: %s' % hexlify(tag))
def checkBroadcast(self, tag): def checkBroadcast(self):
if len(self.data) < 180: if len(self.data) < 180:
logger.debug('The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.') logger.debug('The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.')
raise BMObjectInvalidError() raise BMObjectInvalidError()
@ -93,7 +101,3 @@ class BMObject(object):
# this isn't supported anymore # this isn't supported anymore
if self.version < 2: if self.version < 2:
raise BMObjectInvalidError() raise BMObjectInvalidError()
if self.version >= 3:
self.tag = tag
logger.debug('tag in received broadcast is: %s' % hexlify(tag))

View File

@ -263,7 +263,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
def bm_command_object(self): def bm_command_object(self):
objectOffset = self.payloadOffset objectOffset = self.payloadOffset
nonce, expiresTime, objectType, version, streamNumber = self.decode_payload_content("QQIvv") nonce, expiresTime, objectType, version, streamNumber = self.decode_payload_content("QQIvv")
self.object = BMObject(nonce, expiresTime, objectType, version, streamNumber, self.payload) self.object = BMObject(nonce, expiresTime, objectType, version, streamNumber, self.payload, self.payloadOffset)
if len(self.payload) - self.payloadOffset > BMProto.maxObjectPayloadSize: if len(self.payload) - self.payloadOffset > BMProto.maxObjectPayloadSize:
logger.info('The payload length of this object is too large (%s bytes). Ignoring it.' % len(self.payload) - self.payloadOffset) logger.info('The payload length of this object is too large (%s bytes). Ignoring it.' % len(self.payload) - self.payloadOffset)
@ -291,15 +291,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
isinstance(e, BMObjectExpiredError): isinstance(e, BMObjectExpiredError):
raise e raise e
if self.object.objectType == protocol.OBJECT_GETPUBKEY: self.object.checkObjectByType()
self.object.checkGetpubkey()
elif self.object.objectType == protocol.OBJECT_PUBKEY:
self.object.checkPubkey(self.payload[self.payloadOffset:self.payloadOffset+32])
elif self.object.objectType == protocol.OBJECT_MSG:
self.object.checkMessage()
elif self.object.objectType == protocol.OBJECT_BROADCAST:
self.object.checkBroadcast(self.payload[self.payloadOffset:self.payloadOffset+32])
# other objects don't require other types of tests
Inventory()[self.object.inventoryHash] = ( Inventory()[self.object.inventoryHash] = (
self.object.objectType, self.object.streamNumber, self.payload[objectOffset:], self.object.expiresTime, self.object.tag) self.object.objectType, self.object.streamNumber, self.payload[objectOffset:], self.object.expiresTime, self.object.tag)
objectProcessorQueue.put((self.object.objectType,self.object.data)) objectProcessorQueue.put((self.object.objectType,self.object.data))