From eb5f791cb627713d270fb27ff29b38fbfb40aa7e Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Mon, 1 Aug 2022 00:19:40 +0300 Subject: [PATCH] Finalize invalid stream handling: - prioritize the check for invalid stream - use BMObjectInvalidError exception, remove unused classes --- src/network/bmobject.py | 20 +++++--------------- src/network/bmproto.py | 12 ++++-------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/network/bmobject.py b/src/network/bmobject.py index c6684f3c..5d7fdcbd 100644 --- a/src/network/bmobject.py +++ b/src/network/bmobject.py @@ -19,12 +19,6 @@ class BMObjectInsufficientPOWError(Exception): 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): """Exception indicating the object's lifetime has expired.""" errorCodes = ("Object expired") @@ -36,12 +30,6 @@ class BMObjectUnwantedStreamError(Exception): 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): """The object's data does not match object specification.""" errorCodes = ("Invalid object") @@ -107,14 +95,16 @@ class BMObject(object): # pylint: disable=too-many-instance-attributes def checkStream(self): """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: logger.debug( 'The streamNumber %i isn\'t one we are interested in.', self.streamNumber) raise BMObjectUnwantedStreamError() - if self.streamNumber < protocol.MIN_VALID_STREAM \ - or self.streamNumber > protocol.MAX_VALID_STREAM: - raise BMObjectInvalidStreamError() def checkAlreadyHave(self): """ diff --git a/src/network/bmproto.py b/src/network/bmproto.py index ee43523d..9e6d5044 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -21,9 +21,8 @@ from inventory import Inventory from network.advanceddispatcher import AdvancedDispatcher from network.bmobject import ( BMObject, BMObjectAlreadyHaveError, BMObjectExpiredError, - BMObjectInsufficientPOWError, BMObjectInvalidDataError, - BMObjectInvalidError, BMObjectUnwantedStreamError, - BMObjectInvalidStreamError + BMObjectInsufficientPOWError, BMObjectInvalidError, + BMObjectUnwantedStreamError ) from network.constants import ( ADDRESS_ALIVE, MAX_MESSAGE_SIZE, MAX_OBJECT_COUNT, @@ -130,8 +129,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker): logger.debug('too much data, skipping') except BMObjectInsufficientPOWError: logger.debug('insufficient PoW, skipping') - except BMObjectInvalidDataError: - logger.debug('object invalid data, skipping') except BMObjectExpiredError: logger.debug('object expired, skipping') except BMObjectUnwantedStreamError: @@ -410,9 +407,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.object.inventoryHash, acceptmismatch) if not acceptmismatch: raise - except BMObjectInvalidStreamError: - BMProto.stopDownloadingObject( - self.object.inventoryHash) + except BMObjectInvalidError: + BMProto.stopDownloadingObject(self.object.inventoryHash) raise try: