| 33 | |
| 34 | |
| 35 | class BMObject(object): |
| 36 | # max TTL, 28 days and 3 hours |
| 37 | maxTTL = 28 * 24 * 60 * 60 + 10800 |
| 38 | # min TTL, 3 hour (in the past |
| 39 | minTTL = -3600 |
| 40 | |
| 41 | def __init__(self, nonce, expiresTime, objectType, version, streamNumber, data, payloadOffset): |
| 42 | self.nonce = nonce |
| 43 | self.expiresTime = expiresTime |
| 44 | self.objectType = objectType |
| 45 | self.version = version |
| 46 | self.streamNumber = streamNumber |
| 47 | self.inventoryHash = calculateInventoryHash(data) |
| 48 | # copy to avoid memory issues |
| 49 | self.data = bytearray(data) |
| 50 | self.tag = self.data[payloadOffset:payloadOffset+32] |
| 51 | |
| 52 | def checkProofOfWorkSufficient(self): |
| 53 | # Let us check to make sure that the proof of work is sufficient. |
| 54 | if not protocol.isProofOfWorkSufficient(self.data): |
| 55 | logger.info('Proof of work is insufficient.') |
| 56 | raise BMObjectInsufficientPOWError() |
| 57 | |
| 58 | def checkEOLSanity(self): |
| 59 | # EOL sanity check |
| 60 | if self.expiresTime - int(time.time()) > BMObject.maxTTL: |
| 61 | logger.info('This object\'s End of Life time is too far in the future. Ignoring it. Time is %i', self.expiresTime) |
| 62 | # TODO: remove from download queue |
| 63 | raise BMObjectExpiredError() |
| 64 | |
| 65 | if self.expiresTime - int(time.time()) < BMObject.minTTL: |
| 66 | logger.info('This object\'s End of Life time was too long ago. Ignoring the object. Time is %i', self.expiresTime) |
| 67 | # TODO: remove from download queue |
| 68 | raise BMObjectExpiredError() |
| 69 | |
| 70 | def checkStream(self): |
| 71 | if self.streamNumber not in state.streamsInWhichIAmParticipating: |
| 72 | logger.debug('The streamNumber %i isn\'t one we are interested in.', self.streamNumber) |
| 73 | raise BMObjectUnwantedStreamError() |
| 74 | |
| 75 | def checkAlreadyHave(self): |
| 76 | # if it's a stem duplicate, pretend we don't have it |
| 77 | if Dandelion().hasHash(self.inventoryHash): |
| 78 | return |
| 79 | if self.inventoryHash in Inventory(): |
| 80 | raise BMObjectAlreadyHaveError() |
| 81 | |
| 82 | def checkObjectByType(self): |
| 83 | if self.objectType == protocol.OBJECT_GETPUBKEY: |
| 84 | self.checkGetpubkey() |
| 85 | elif self.objectType == protocol.OBJECT_PUBKEY: |
| 86 | self.checkPubkey() |
| 87 | elif self.objectType == protocol.OBJECT_MSG: |
| 88 | self.checkMessage() |
| 89 | elif self.objectType == protocol.OBJECT_BROADCAST: |
| 90 | self.checkBroadcast() |
| 91 | # other objects don't require other types of tests |
| 92 | |