onionrblocks/tests/test_anonvdf.py

72 lines
2.7 KiB
Python

import unittest
from onionrblocks.generators import anonvdf
import kasten
import mimcvdf
from time import time
from math import floor
class TestAnonVDF(unittest.TestCase):
def test_vdf_create(self):
test_data = kasten.generator.pack.pack(b"test", "tst", 0)
test_vdf = mimcvdf.vdf_create(test_data, 1000, dec=True)
generated = anonvdf.AnonVDFGenerator.generate(test_data, rounds=1000)
self.assertEqual(
generated.get_packed(),
test_data
)
self.assertEqual(generated.id, int(test_vdf).to_bytes(64, byteorder="big"))
def test_vdf_block_validate_ok_time(self):
rds = 0
test_data = kasten.generator.pack.pack(b"test", "tst")
rds += len(test_data) * 90000
test_id = mimcvdf.vdf_create(test_data, rds)
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data, rounds=rds)
def test_vdf_block_validate_not_enough(self):
rds = 133240
t = floor(time()) - 60
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata=None, timestamp=t)
test_id = mimcvdf.vdf_create(test_data, rds)
self.assertRaises(anonvdf.NotEnoughRounds, anonvdf.AnonVDFGenerator.validate_id, test_id, test_data, rounds=rds)
def test_vdf_incermental(self):
last = 1
for i in range(1, 5):
t = time()
rds = 0
test_data = kasten.generator.pack.pack(b"test" * i, "tst", 0)
rds += len(test_data) * 90000
test_id = mimcvdf.vdf_create(test_data, rds)
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data, rounds=rds)
newT = time()
self.assertGreater(newT - t, last)
def test_block_embedded_vdf_rounds(self):
t = floor(time())
rds = 90000 + (4000 * 1000)
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"rds": rds}, timestamp=t)
test_vdf = anonvdf.AnonVDFGenerator.generate(test_data, None)
anonvdf.AnonVDFGenerator.validate_id(test_vdf.id, test_data, rounds=None)
def test_block_embedded_vdf_rounds_invalid(self):
t = floor(time())
rds = 90000
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"rds": rds}, timestamp=t)
test_vdf = anonvdf.AnonVDFGenerator.generate(test_data, None)
self.assertRaises(
anonvdf.NotEnoughRounds,
anonvdf.AnonVDFGenerator.validate_id, test_vdf.id, test_data, rounds=None)
def test_vdf_rounds_seconds(self):
size = 10000
per_byte = 1000
per_second = 25
expected_rounds = (3600 * 25) + 100000
self.assertEqual(anonvdf.AnonVDFGenerator.get_rounds_for_ttl_seconds(3600, 100), expected_rounds)
unittest.main()