2020-12-13 03:49:43 +00:00
|
|
|
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)
|
2020-12-25 21:06:26 +00:00
|
|
|
test_vdf = mimcvdf.vdf_create(test_data, 1000, dec=True)
|
2020-12-13 03:49:43 +00:00
|
|
|
|
2020-12-25 21:06:26 +00:00
|
|
|
generated = anonvdf.AnonVDFGenerator.generate(test_data, rounds=1000)
|
2020-12-13 03:49:43 +00:00
|
|
|
self.assertEqual(
|
2020-12-25 21:06:26 +00:00
|
|
|
generated.get_packed(),
|
2020-12-13 03:49:43 +00:00
|
|
|
test_data
|
|
|
|
)
|
2020-12-25 21:06:26 +00:00
|
|
|
self.assertEqual(generated.id, int(test_vdf).to_bytes(64, byteorder="big"))
|
2020-12-13 03:49:43 +00:00
|
|
|
|
|
|
|
def test_vdf_block_validate_ok_time(self):
|
2021-01-20 20:31:50 +00:00
|
|
|
rds = 90000
|
2020-12-13 03:49:43 +00:00
|
|
|
test_data = kasten.generator.pack.pack(b"test", "tst", 0)
|
2021-01-20 20:31:50 +00:00
|
|
|
rds += len(test_data) * 1000
|
2020-12-13 03:49:43 +00:00
|
|
|
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):
|
2021-01-20 20:31:50 +00:00
|
|
|
rds = 90000
|
2020-12-13 03:49:43 +00:00
|
|
|
t = floor(time()) - 60
|
|
|
|
test_data = kasten.generator.pack.pack(b"test", "tst", 0, timestamp=t)
|
|
|
|
test_id = mimcvdf.vdf_create(test_data, rds)
|
|
|
|
self.assertRaises(ValueError, anonvdf.AnonVDFGenerator.validate_id, test_id, test_data, rounds=rds)
|
|
|
|
|
2021-01-20 20:31:50 +00:00
|
|
|
def test_vdf_incermental(self):
|
|
|
|
last = 1
|
|
|
|
for i in range(1, 20):
|
|
|
|
t = time()
|
|
|
|
rds = 90000
|
|
|
|
test_data = kasten.generator.pack.pack(b"test" * i, "tst", 0)
|
|
|
|
rds += len(test_data) * 1000
|
|
|
|
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)
|
|
|
|
|
2020-12-13 03:49:43 +00:00
|
|
|
|
|
|
|
unittest.main()
|