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
|
|
|
|
|
2022-01-30 02:19:07 +00:00
|
|
|
def get_rounds_for_ttl_seconds(seconds: int, size_bytes: int):
|
|
|
|
second_cost = 4
|
|
|
|
byte_cost = 10
|
|
|
|
return (seconds * second_cost) + (size_bytes * byte_cost)
|
|
|
|
|
2020-12-13 03:49:43 +00:00
|
|
|
class TestAnonVDF(unittest.TestCase):
|
2021-01-22 18:05:33 +00:00
|
|
|
|
2020-12-13 03:49:43 +00:00
|
|
|
def test_vdf_create(self):
|
2022-01-30 23:19:36 +00:00
|
|
|
ttl = 10000
|
2022-01-30 02:19:07 +00:00
|
|
|
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"ttl": ttl})
|
|
|
|
test_vdf = mimcvdf.vdf_create(test_data, get_rounds_for_ttl_seconds(ttl, len(test_data)), dec=True)
|
2020-12-13 03:49:43 +00:00
|
|
|
|
2022-01-30 02:19:07 +00:00
|
|
|
generated = anonvdf.AnonVDFGenerator.generate(test_data, get_rounds_for_ttl_seconds(ttl, len(test_data)))
|
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_not_enough(self):
|
2021-01-24 03:24:01 +00:00
|
|
|
|
2020-12-13 03:49:43 +00:00
|
|
|
t = floor(time()) - 60
|
2022-01-30 02:19:07 +00:00
|
|
|
ttl = 3600
|
|
|
|
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"ttl": ttl}, timestamp=t)
|
|
|
|
rds = get_rounds_for_ttl_seconds(3500, len(test_data))
|
2020-12-13 03:49:43 +00:00
|
|
|
test_id = mimcvdf.vdf_create(test_data, rds)
|
2022-01-30 02:19:07 +00:00
|
|
|
self.assertRaises(anonvdf.InvalidID, anonvdf.AnonVDFGenerator.validate_id, test_id, test_data)
|
2020-12-13 03:49:43 +00:00
|
|
|
|
2021-01-20 20:31:50 +00:00
|
|
|
def test_vdf_incermental(self):
|
|
|
|
last = 1
|
2022-01-30 02:19:07 +00:00
|
|
|
ttl = 30000
|
2021-01-22 18:05:33 +00:00
|
|
|
for i in range(1, 5):
|
2021-01-20 20:31:50 +00:00
|
|
|
t = time()
|
2021-01-22 18:05:33 +00:00
|
|
|
rds = 0
|
2022-01-30 02:19:07 +00:00
|
|
|
test_data = kasten.generator.pack.pack(b"test" * i, "tst", app_metadata={"ttl": ttl})
|
|
|
|
test_id = mimcvdf.vdf_create(test_data, get_rounds_for_ttl_seconds(ttl, len(test_data)))
|
2021-01-20 20:31:50 +00:00
|
|
|
newT = time()
|
|
|
|
self.assertGreater(newT - t, last)
|
2022-01-30 02:19:07 +00:00
|
|
|
print(newT - t)
|
|
|
|
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data)
|
2021-01-20 20:31:50 +00:00
|
|
|
|
2022-01-30 02:19:07 +00:00
|
|
|
def test_block_invalid_ttl(self):
|
|
|
|
return
|
2021-01-22 18:05:33 +00:00
|
|
|
|
|
|
|
def test_vdf_rounds_seconds(self):
|
|
|
|
size = 10000
|
2021-01-24 22:05:21 +00:00
|
|
|
per_byte = 10
|
|
|
|
per_second = 4
|
2021-01-24 03:24:01 +00:00
|
|
|
expected_rounds = (3600 * per_second) + (size * per_byte)
|
2022-01-30 02:19:07 +00:00
|
|
|
self.assertEqual(get_rounds_for_ttl_seconds(3600, size), expected_rounds)
|
|
|
|
|
2020-12-13 03:49:43 +00:00
|
|
|
|
|
|
|
unittest.main()
|