onionrblocks/tests/test_blockcreator.py

56 lines
2.2 KiB
Python
Raw Normal View History

from binascii import a2b_hex
from onionrblocks.generators import anonvdf
import unittest
import kasten
from onionrblocks.blockcreator import create_anonvdf_block
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)
class TestBlockCreator(unittest.TestCase):
"""
def test_create_anonvdf(self):
seconds = 3600
bl = create_anonvdf_block(b"Test", "txt", seconds)
byte_cost = 10
second_cost = 4
# (rounds - (size_bytes * cls.byte_cost)) // cls.second_cost
expected_rounds = (seconds * second_cost) + (len(bl.get_packed()) * byte_cost)
self.assertTrue(abs(expected_rounds - bl.get_metadata()['rds']) < 91)
def test_create_anonvdf_half_hour(self):
bl = create_anonvdf_block(b"Test", "txt", 1800)
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)
def test_create_anonvdf_odd(self):
#(rounds - (size_bytes * cls.byte_cost)) // cls.second_cost
bl = create_anonvdf_block(b"Test", "txt", 1303)
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)
"""
def test_create_anonvdf_verify(self):
bl = create_anonvdf_block(b"Test", "txt", 3600)
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)
unittest.main()