53 lines
1.2 KiB
Python
53 lines
1.2 KiB
Python
from kasten import Kasten
|
|
|
|
from time import time
|
|
from math import floor
|
|
|
|
MAX_AGE_SECONDS = 60 * 60 * 24 * 30
|
|
MAX_FUTURE_SKEW_SECONDS = 60 * 2
|
|
TOTAL_MAX_SIZE = 6 * (10**6)
|
|
|
|
|
|
class BlockRulesException(Exception):
|
|
pass
|
|
|
|
|
|
class BlockOutdated(BlockRulesException):
|
|
pass
|
|
|
|
class BlockExpired(Exception):
|
|
pass
|
|
|
|
class BlockFutureSkewedBeyondMax(BlockRulesException):
|
|
pass
|
|
|
|
|
|
class BlockTooLarge(BlockRulesException):
|
|
pass
|
|
|
|
|
|
def check_block_sanity(raw_bytes):
|
|
kasten: Kasten = Kasten(None, raw_bytes, None, auto_check_generator=False)
|
|
|
|
# Ensure a block is not too large
|
|
if len(kasten.get_packed()) > TOTAL_MAX_SIZE:
|
|
raise BlockTooLarge()
|
|
|
|
# Ensure block is not future-skewed or expired
|
|
timestamp: int = floor(time())
|
|
block_timestamp = kasten.get_timestamp()
|
|
difference = timestamp - block_timestamp
|
|
if difference > MAX_AGE_SECONDS:
|
|
raise BlockOutdated()
|
|
if difference < -MAX_FUTURE_SKEW_SECONDS:
|
|
raise BlockFutureSkewedBeyondMax()
|
|
|
|
try:
|
|
if timestamp < block_timestamp + kasten.get_metadata()[
|
|
'expire']:
|
|
raise BlockExpired()
|
|
except (IndexError, TypeError):
|
|
pass
|
|
|
|
|