2022-01-20 00:48:42 +00:00
|
|
|
from binascii import a2b_hex
|
2021-01-24 00:24:10 +00:00
|
|
|
from onionrblocks.generators import anonvdf
|
|
|
|
import unittest
|
|
|
|
import kasten
|
|
|
|
from onionrblocks.blockcreator import create_anonvdf_block
|
|
|
|
|
2022-01-20 00:48:42 +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)
|
|
|
|
|
2021-01-24 00:24:10 +00:00
|
|
|
class TestBlockCreator(unittest.TestCase):
|
2022-01-20 00:48:42 +00:00
|
|
|
"""
|
2021-01-24 00:24:10 +00:00
|
|
|
def test_create_anonvdf(self):
|
2022-01-20 00:48:42 +00:00
|
|
|
seconds = 3600
|
|
|
|
bl = create_anonvdf_block(b"Test", "txt", seconds)
|
|
|
|
byte_cost = 10
|
|
|
|
second_cost = 4
|
2021-01-24 00:24:10 +00:00
|
|
|
# (rounds - (size_bytes * cls.byte_cost)) // cls.second_cost
|
2022-01-20 00:48:42 +00:00
|
|
|
expected_rounds = (seconds * second_cost) + (len(bl.get_packed()) * byte_cost)
|
|
|
|
self.assertTrue(abs(expected_rounds - bl.get_metadata()['rds']) < 91)
|
2021-01-24 00:24:10 +00:00
|
|
|
|
|
|
|
def test_create_anonvdf_half_hour(self):
|
|
|
|
bl = create_anonvdf_block(b"Test", "txt", 1800)
|
2022-01-20 00:48:42 +00:00
|
|
|
expected_rounds = (len(bl.get_packed()) * anonvdf.AnonVDFGenerator.byte_cost) + (1800 * anonvdf.AnonVDFGenerator.second_cost)
|
|
|
|
self.assertTrue(abs(expected_rounds - bl.get_metadata()['rds']) < 91)
|
2021-01-24 00:24:10 +00:00
|
|
|
|
|
|
|
def test_create_anonvdf_odd(self):
|
|
|
|
#(rounds - (size_bytes * cls.byte_cost)) // cls.second_cost
|
|
|
|
bl = create_anonvdf_block(b"Test", "txt", 1303)
|
2022-01-20 00:48:42 +00:00
|
|
|
expected_rounds = (len(bl.get_packed()) * anonvdf.AnonVDFGenerator.byte_cost) + (1303 * anonvdf.AnonVDFGenerator.second_cost)
|
|
|
|
self.assertTrue(abs(expected_rounds - bl.get_metadata()['rds']) < 91)
|
|
|
|
"""
|
2021-01-24 00:24:10 +00:00
|
|
|
|
|
|
|
def test_create_anonvdf_verify(self):
|
|
|
|
bl = create_anonvdf_block(b"Test", "txt", 3600)
|
|
|
|
|
2022-01-20 00:48:42 +00:00
|
|
|
packed = bl.raw
|
|
|
|
|
|
|
|
kasten.Kasten(bl.id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True)
|
|
|
|
print(bl.id)
|
|
|
|
fake_id = b'01' + bl.id[2:]
|
|
|
|
|
|
|
|
self.assertRaises(kasten.exceptions.InvalidID, kasten.Kasten, fake_id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True)
|
|
|
|
|
|
|
|
fake_id = bytearray(bl.id)
|
|
|
|
fake_id[32] = 13
|
|
|
|
fake_id[34] = 13
|
|
|
|
fake_id[120] = 13
|
|
|
|
fake_id = bytes(fake_id).replace(b'\r', b'')
|
|
|
|
print(fake_id.replace(b'\r', b''))
|
|
|
|
|
|
|
|
kasten.Kasten(fake_id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True)
|
|
|
|
|
2021-01-24 00:24:10 +00:00
|
|
|
|
|
|
|
unittest.main()
|