2018-10-04 16:23:01 +02:00
|
|
|
"""
|
|
|
|
src/network/bmobject.py
|
|
|
|
======================
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
import time
|
|
|
|
|
2018-10-04 16:23:01 +02:00
|
|
|
import protocol
|
|
|
|
import state
|
2017-05-24 16:51:49 +02:00
|
|
|
from addresses import calculateInventoryHash
|
|
|
|
from debug import logger
|
2017-05-30 23:53:43 +02:00
|
|
|
from inventory import Inventory
|
2017-10-20 01:21:49 +02:00
|
|
|
from network.dandelion import Dandelion
|
2018-10-04 16:23:01 +02:00
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
class BMObjectInsufficientPOWError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Exception indicating the object doesn't have sufficient proof of work."""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Insufficient proof of work")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
class BMObjectInvalidDataError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Exception indicating the data being parsed does not match the specification."""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Data invalid")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
class BMObjectExpiredError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Exception indicating the object's lifetime has expired."""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Object expired")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
class BMObjectUnwantedStreamError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Exception indicating the object is in a stream we didn't advertise as being interested in."""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Object in unwanted stream")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
class BMObjectInvalidError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""The object's data does not match object specification."""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Invalid object")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BMObjectAlreadyHaveError(Exception):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""We received a duplicate object (one we already have)"""
|
2017-12-29 08:49:08 +01:00
|
|
|
errorCodes = ("Already have this object")
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BMObject(object):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Bitmessage Object as a class."""
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
# max TTL, 28 days and 3 hours
|
|
|
|
maxTTL = 28 * 24 * 60 * 60 + 10800
|
|
|
|
# min TTL, 3 hour (in the past
|
|
|
|
minTTL = -3600
|
|
|
|
|
2018-10-04 16:23:01 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
nonce,
|
|
|
|
expiresTime,
|
|
|
|
objectType,
|
|
|
|
version,
|
|
|
|
streamNumber,
|
|
|
|
data,
|
|
|
|
payloadOffset
|
|
|
|
): # pylint: disable=too-many-arguments
|
2017-05-24 16:51:49 +02:00
|
|
|
self.nonce = nonce
|
|
|
|
self.expiresTime = expiresTime
|
|
|
|
self.objectType = objectType
|
|
|
|
self.version = version
|
|
|
|
self.streamNumber = streamNumber
|
|
|
|
self.inventoryHash = calculateInventoryHash(data)
|
2017-11-17 23:53:46 +01:00
|
|
|
# copy to avoid memory issues
|
|
|
|
self.data = bytearray(data)
|
2018-10-04 16:23:01 +02:00
|
|
|
self.tag = self.data[payloadOffset:payloadOffset + 32]
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def checkProofOfWorkSufficient(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Perform a proof of work check for sufficiency."""
|
2017-05-24 16:51:49 +02:00
|
|
|
# Let us check to make sure that the proof of work is sufficient.
|
|
|
|
if not protocol.isProofOfWorkSufficient(self.data):
|
|
|
|
logger.info('Proof of work is insufficient.')
|
|
|
|
raise BMObjectInsufficientPOWError()
|
|
|
|
|
|
|
|
def checkEOLSanity(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Check if object's lifetime isn't ridiculously far in the past or future."""
|
2017-05-24 16:51:49 +02:00
|
|
|
# EOL sanity check
|
|
|
|
if self.expiresTime - int(time.time()) > BMObject.maxTTL:
|
2018-10-04 16:23:01 +02:00
|
|
|
logger.info(
|
|
|
|
'This object\'s End of Life time is too far in the future. Ignoring it. Time is %i',
|
|
|
|
self.expiresTime)
|
|
|
|
# .. todo:: remove from download queue
|
2017-05-24 16:51:49 +02:00
|
|
|
raise BMObjectExpiredError()
|
|
|
|
|
|
|
|
if self.expiresTime - int(time.time()) < BMObject.minTTL:
|
2018-10-04 16:23:01 +02:00
|
|
|
logger.info(
|
|
|
|
'This object\'s End of Life time was too long ago. Ignoring the object. Time is %i',
|
|
|
|
self.expiresTime)
|
|
|
|
# .. todo:: remove from download queue
|
2017-05-24 16:51:49 +02:00
|
|
|
raise BMObjectExpiredError()
|
|
|
|
|
|
|
|
def checkStream(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Check if object's stream matches streams we are interested in"""
|
2017-05-24 16:51:49 +02:00
|
|
|
if self.streamNumber not in state.streamsInWhichIAmParticipating:
|
2017-06-24 12:13:35 +02:00
|
|
|
logger.debug('The streamNumber %i isn\'t one we are interested in.', self.streamNumber)
|
2017-05-24 16:51:49 +02:00
|
|
|
raise BMObjectUnwantedStreamError()
|
|
|
|
|
2017-05-30 23:53:43 +02:00
|
|
|
def checkAlreadyHave(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""
|
|
|
|
Check if we already have the object (so that we don't duplicate it in inventory or advertise it unnecessarily)
|
|
|
|
"""
|
2017-09-25 01:17:04 +02:00
|
|
|
# if it's a stem duplicate, pretend we don't have it
|
2018-02-03 11:46:39 +01:00
|
|
|
if Dandelion().hasHash(self.inventoryHash):
|
2017-09-25 01:17:04 +02:00
|
|
|
return
|
2017-05-30 23:53:43 +02:00
|
|
|
if self.inventoryHash in Inventory():
|
|
|
|
raise BMObjectAlreadyHaveError()
|
|
|
|
|
2017-06-24 12:21:06 +02:00
|
|
|
def checkObjectByType(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
"""Call a object type specific check (objects can have additional checks based on their types)"""
|
2017-06-24 12:21:06 +02:00
|
|
|
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
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
def checkMessage(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
""""Message" object type checks."""
|
|
|
|
# pylint: disable=no-self-use
|
2017-05-24 16:51:49 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
def checkGetpubkey(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
""""Getpubkey" object type checks."""
|
2017-05-24 16:51:49 +02:00
|
|
|
if len(self.data) < 42:
|
|
|
|
logger.info('getpubkey message doesn\'t contain enough data. Ignoring.')
|
|
|
|
raise BMObjectInvalidError()
|
|
|
|
|
2017-06-24 12:21:06 +02:00
|
|
|
def checkPubkey(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
""""Pubkey" object type checks."""
|
2017-05-24 16:51:49 +02:00
|
|
|
if len(self.data) < 146 or len(self.data) > 440: # sanity check
|
|
|
|
logger.info('pubkey object too short or too long. Ignoring.')
|
|
|
|
raise BMObjectInvalidError()
|
|
|
|
|
2017-06-24 12:21:06 +02:00
|
|
|
def checkBroadcast(self):
|
2018-10-04 16:23:01 +02:00
|
|
|
""""Broadcast" object type checks."""
|
2017-05-24 16:51:49 +02:00
|
|
|
if len(self.data) < 180:
|
2018-10-04 16:23:01 +02:00
|
|
|
logger.debug(
|
|
|
|
'The payload length of this broadcast packet is unreasonably low.'
|
|
|
|
' Someone is probably trying funny business. Ignoring message.')
|
2017-05-24 16:51:49 +02:00
|
|
|
raise BMObjectInvalidError()
|
|
|
|
|
|
|
|
# this isn't supported anymore
|
|
|
|
if self.version < 2:
|
|
|
|
raise BMObjectInvalidError()
|