use ttl in block instead of rounds

This commit is contained in:
Kevin F 2022-01-29 20:19:07 -06:00
parent 6537362e3d
commit a7bb1bc27f
7 changed files with 58 additions and 73 deletions

View File

@ -2,6 +2,11 @@
This project uses Semantic Versioning
## 6.0.0
- Use TTL instead of rounds
## 5.0.0
- Removed signedby

View File

@ -19,6 +19,7 @@ class Block:
generator, auto_check_generator=auto_verify)
self.timestamp = bl.get_timestamp()
self.metadata = bl.get_metadata()
self.ttl = self.metadata['ttl']
self.id = block_hash
self.type = bl.get_data_type()
self.data = bl.data

View File

@ -13,6 +13,9 @@ from onionrblocks.universalrules import check_block_sanity
class NotEnoughRounds(Exception):
pass
class NoTTLSet(Exception):
pass
class AnonVDFGenerator(generator.KastenBaseGenerator):
@ -37,18 +40,22 @@ class AnonVDFGenerator(generator.KastenBaseGenerator):
def generate(
cls, packed_bytes: KastenPacked, ttl: int = None) -> Kasten:
ttl = int(Kasten(
k: Kasten = Kasten(
None,
packed_bytes, None,
auto_check_generator=False).get_metadata()['ttl'])
auto_check_generator=False)
try:
ttl: int = k.get_metadata()['ttl']
except (KeyError, TypeError) as e:
raise NoTTLSet(e)
rounds = AnonVDFGenerator.get_rounds_for_ttl_seconds(ttl, len(packed_bytes))
check_block_sanity(packed_bytes)
return Kasten(
vdf_create(
vdf = vdf_create(
packed_bytes,
rounds, dec=True
).to_bytes(
64, "big"), packed_bytes, cls, auto_check_generator=False)
).to_bytes(64, "big")
return Kasten(vdf, packed_bytes, cls, auto_check_generator=False)
@staticmethod
def validate_id(
@ -56,14 +63,20 @@ class AnonVDFGenerator(generator.KastenBaseGenerator):
packed_bytes: KastenPacked) -> None:
check_block_sanity(packed_bytes)
ttl = int(Kasten(
k: Kasten = Kasten(
None,
packed_bytes, None,
auto_check_generator=False).get_metadata()['ttl'])
auto_check_generator=False)
try:
ttl: int = k.get_metadata()['ttl']
except (KeyError, TypeError) as e:
raise NoTTLSet(e)
ttl = int(ttl)
rounds = AnonVDFGenerator.get_rounds_for_ttl_seconds(ttl, len(packed_bytes))
hash = hash.lstrip(b"0")
#hash = hash.lstrip(b"0")
hash = int(hash, 16)
try:

View File

@ -1,7 +1,7 @@
from setuptools import setup, find_packages
setup(name='onionrblocks',
version='5.0.0',
version='6.0.0',
description='Onionr message format',
author='Kevin Froman',
author_email='beardog@mailbox.org',

View File

@ -6,72 +6,56 @@ import mimcvdf
from time import time
from math import floor
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 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)
ttl = 1200
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)
generated = anonvdf.AnonVDFGenerator.generate(test_data, rounds=1000)
generated = anonvdf.AnonVDFGenerator.generate(test_data, get_rounds_for_ttl_seconds(ttl, len(test_data)))
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)
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))
test_id = mimcvdf.vdf_create(test_data, rds)
self.assertRaises(anonvdf.InvalidID, anonvdf.AnonVDFGenerator.validate_id, test_id, test_data, rounds=rds-1)
self.assertRaises(anonvdf.InvalidID, anonvdf.AnonVDFGenerator.validate_id, test_id, test_data)
def test_vdf_incermental(self):
last = 1
ttl = 30000
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)
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)))
newT = time()
self.assertGreater(newT - t, last)
print(newT - t)
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data)
def test_block_embedded_vdf_rounds(self):
t = floor(time())
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"rds": 900000}, timestamp=t)
rds = (3600 * 26) + (len(test_data) * 1000) - 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())
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"rds": 900000}, timestamp=t)
rds = (3600 * 26) + (len(test_data) * 1000) - 2000
test_data = kasten.generator.pack.pack(b"test", "tst", app_metadata={"rds": rds}, timestamp=t)
test_vdf = anonvdf.AnonVDFGenerator.generate(test_data)
self.assertRaises(
anonvdf.InvalidID,
anonvdf.AnonVDFGenerator.validate_id, test_vdf.id, test_data, rounds=50000)
def test_block_invalid_ttl(self):
return
def test_vdf_rounds_seconds(self):
size = 10000
per_byte = 10
per_second = 4
expected_rounds = (3600 * per_second) + (size * per_byte)
self.assertEqual(anonvdf.AnonVDFGenerator.get_rounds_for_ttl_seconds(3600, size), expected_rounds)
self.assertEqual(get_rounds_for_ttl_seconds(3600, size), expected_rounds)
unittest.main()

View File

@ -10,27 +10,7 @@ def get_rounds_for_ttl_seconds(seconds: int, size_bytes: int):
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)
@ -50,7 +30,9 @@ class TestBlockCreator(unittest.TestCase):
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)
kasten.Kasten(bl.id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True)
self.assertRaises(kasten.exceptions.InvalidID, kasten.Kasten, fake_id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True)
unittest.main()

View File

@ -43,10 +43,10 @@ class TestUniversalRules(unittest.TestCase):
t = floor(time()) - 6
bls = []
for i in range(3):
packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", timestamp=t)
k = anonvdf.AnonVDFGenerator.generate(packed, 1000)
packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", timestamp=t, app_metadata={"ttl": 60})
k = anonvdf.AnonVDFGenerator.generate(packed, 60)
bls.append(k.id)
packed = kasten.generator.pack.pack(b"1"*2*1, b"tst", timestamp=t)
packed = kasten.generator.pack.pack(b"1"*2*1, b"tst", timestamp=t, app_metadata={"ttl": 60})
k = anonvdf.AnonVDFGenerator.generate(packed, 1000)
self.assertRaises(universalrules.BlockExistsError, universalrules.checksum_exists_in_list, k.id, bls)