onionrblocks/tests/test_universal_rules.py

54 lines
2.0 KiB
Python
Raw Permalink Normal View History

2020-12-11 04:18:03 +00:00
import unittest
from onionrblocks import universalrules
from onionrblocks.generators import anonvdf
2020-12-11 04:18:03 +00:00
import kasten
from time import time
from math import floor
class TestUniversalRules(unittest.TestCase):
def test_block_is_sane_max_size(self):
2022-01-30 23:19:36 +00:00
packed = kasten.generator.pack.pack(b"1"*1000000000, b"tst", 0)
2020-12-11 04:18:03 +00:00
self.assertRaises(
universalrules.BlockTooLarge,
universalrules.check_block_sanity, packed)
def test_block_future_skew_ok(self):
t = floor(time()) + 120
2020-12-27 04:25:05 +00:00
packed = kasten.generator.pack.pack(b"1"*10, b"tst", timestamp=t)
2020-12-11 04:18:03 +00:00
universalrules.check_block_sanity(packed)
def test_block_future_skew_not_ok(self):
t = floor(time()) + 121
2020-12-27 04:25:05 +00:00
packed = kasten.generator.pack.pack(b"1"*10, b"tst", timestamp=t)
2020-12-11 04:18:03 +00:00
self.assertRaises(
universalrules.BlockFutureSkewedBeyondMax,
universalrules.check_block_sanity, packed)
def test_block_not_expired(self):
2020-12-27 04:25:05 +00:00
packed = kasten.generator.pack.pack(b"1"*10, b"tst", app_metadata={"expire": 7})
2020-12-11 04:18:03 +00:00
self.assertRaises(
universalrules.BlockExpired,
universalrules.check_block_sanity, packed)
def test_block_expired(self):
t = floor(time()) - 6
2020-12-27 04:25:05 +00:00
packed = kasten.generator.pack.pack(b"1"*10, b"tst", timestamp=t, app_metadata={"expire": 6})
2020-12-11 04:18:03 +00:00
self.assertRaises(
universalrules.BlockExpired,
universalrules.check_block_sanity, packed)
def test_not_in_set(self):
t = floor(time()) - 6
bls = []
for i in range(3):
2022-01-30 23:19:36 +00:00
packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", timestamp=t, app_metadata={"ttl": 10000})
k = anonvdf.AnonVDFGenerator.generate(packed, 10000)
bls.append(k.id)
2022-01-30 23:19:36 +00:00
packed = kasten.generator.pack.pack(b"1"*2*1, b"tst", timestamp=t, app_metadata={"ttl": 10000})
k = anonvdf.AnonVDFGenerator.generate(packed, 1000)
self.assertRaises(universalrules.BlockExistsError, universalrules.checksum_exists_in_list, k.id, bls)
2020-12-11 04:18:03 +00:00
unittest.main()