Finalize invalid stream handling:

- prioritize the check for invalid stream
 - use BMObjectInvalidError exception, remove unused classes
This commit is contained in:
Lee Miller 2022-08-01 00:19:40 +03:00
parent 69e540b504
commit eb5f791cb6
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 9 additions and 23 deletions

View File

@ -19,12 +19,6 @@ class BMObjectInsufficientPOWError(Exception):
errorCodes = ("Insufficient proof of work") errorCodes = ("Insufficient proof of work")
class BMObjectInvalidDataError(Exception):
"""Exception indicating the data being parsed
does not match the specification."""
errorCodes = ("Data invalid")
class BMObjectExpiredError(Exception): class BMObjectExpiredError(Exception):
"""Exception indicating the object's lifetime has expired.""" """Exception indicating the object's lifetime has expired."""
errorCodes = ("Object expired") errorCodes = ("Object expired")
@ -36,12 +30,6 @@ class BMObjectUnwantedStreamError(Exception):
errorCodes = ("Object in unwanted stream") errorCodes = ("Object in unwanted stream")
class BMObjectInvalidStreamError(Exception):
"""Exception indicating the object is in a stream
outside of specification."""
errorCodes = ("Object in invalid stream")
class BMObjectInvalidError(Exception): class BMObjectInvalidError(Exception):
"""The object's data does not match object specification.""" """The object's data does not match object specification."""
errorCodes = ("Invalid object") errorCodes = ("Invalid object")
@ -107,14 +95,16 @@ class BMObject(object): # pylint: disable=too-many-instance-attributes
def checkStream(self): def checkStream(self):
"""Check if object's stream matches streams we are interested in""" """Check if object's stream matches streams we are interested in"""
if self.streamNumber < protocol.MIN_VALID_STREAM \
or self.streamNumber > protocol.MAX_VALID_STREAM:
logger.warning(
'The object has invalid stream: %s', self.streamNumber)
raise BMObjectInvalidError()
if self.streamNumber not in state.streamsInWhichIAmParticipating: if self.streamNumber not in state.streamsInWhichIAmParticipating:
logger.debug( logger.debug(
'The streamNumber %i isn\'t one we are interested in.', 'The streamNumber %i isn\'t one we are interested in.',
self.streamNumber) self.streamNumber)
raise BMObjectUnwantedStreamError() raise BMObjectUnwantedStreamError()
if self.streamNumber < protocol.MIN_VALID_STREAM \
or self.streamNumber > protocol.MAX_VALID_STREAM:
raise BMObjectInvalidStreamError()
def checkAlreadyHave(self): def checkAlreadyHave(self):
""" """

View File

@ -21,9 +21,8 @@ from inventory import Inventory
from network.advanceddispatcher import AdvancedDispatcher from network.advanceddispatcher import AdvancedDispatcher
from network.bmobject import ( from network.bmobject import (
BMObject, BMObjectAlreadyHaveError, BMObjectExpiredError, BMObject, BMObjectAlreadyHaveError, BMObjectExpiredError,
BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectInsufficientPOWError, BMObjectInvalidError,
BMObjectInvalidError, BMObjectUnwantedStreamError, BMObjectUnwantedStreamError
BMObjectInvalidStreamError
) )
from network.constants import ( from network.constants import (
ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT, ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT,
@ -130,8 +129,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
logger.debug('too much data, skipping') logger.debug('too much data, skipping')
except BMObjectInsufficientPOWError: except BMObjectInsufficientPOWError:
logger.debug('insufficient PoW, skipping') logger.debug('insufficient PoW, skipping')
except BMObjectInvalidDataError:
logger.debug('object invalid data, skipping')
except BMObjectExpiredError: except BMObjectExpiredError:
logger.debug('object expired, skipping') logger.debug('object expired, skipping')
except BMObjectUnwantedStreamError: except BMObjectUnwantedStreamError:
@ -410,9 +407,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.object.inventoryHash, acceptmismatch) self.object.inventoryHash, acceptmismatch)
if not acceptmismatch: if not acceptmismatch:
raise raise
except BMObjectInvalidStreamError: except BMObjectInvalidError:
BMProto.stopDownloadingObject( BMProto.stopDownloadingObject(self.object.inventoryHash)
self.object.inventoryHash)
raise raise
try: try: