use ttl in block instead of rounds
This commit is contained in:
parent
6537362e3d
commit
a7bb1bc27f
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
This project uses Semantic Versioning
|
This project uses Semantic Versioning
|
||||||
|
|
||||||
|
## 6.0.0
|
||||||
|
|
||||||
|
- Use TTL instead of rounds
|
||||||
|
|
||||||
|
|
||||||
## 5.0.0
|
## 5.0.0
|
||||||
|
|
||||||
- Removed signedby
|
- Removed signedby
|
||||||
|
@ -19,6 +19,7 @@ class Block:
|
|||||||
generator, auto_check_generator=auto_verify)
|
generator, auto_check_generator=auto_verify)
|
||||||
self.timestamp = bl.get_timestamp()
|
self.timestamp = bl.get_timestamp()
|
||||||
self.metadata = bl.get_metadata()
|
self.metadata = bl.get_metadata()
|
||||||
|
self.ttl = self.metadata['ttl']
|
||||||
self.id = block_hash
|
self.id = block_hash
|
||||||
self.type = bl.get_data_type()
|
self.type = bl.get_data_type()
|
||||||
self.data = bl.data
|
self.data = bl.data
|
||||||
|
@ -13,6 +13,9 @@ from onionrblocks.universalrules import check_block_sanity
|
|||||||
class NotEnoughRounds(Exception):
|
class NotEnoughRounds(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NoTTLSet(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AnonVDFGenerator(generator.KastenBaseGenerator):
|
class AnonVDFGenerator(generator.KastenBaseGenerator):
|
||||||
|
|
||||||
@ -37,18 +40,22 @@ class AnonVDFGenerator(generator.KastenBaseGenerator):
|
|||||||
def generate(
|
def generate(
|
||||||
cls, packed_bytes: KastenPacked, ttl: int = None) -> Kasten:
|
cls, packed_bytes: KastenPacked, ttl: int = None) -> Kasten:
|
||||||
|
|
||||||
ttl = int(Kasten(
|
k: Kasten = Kasten(
|
||||||
None,
|
None,
|
||||||
packed_bytes, 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))
|
rounds = AnonVDFGenerator.get_rounds_for_ttl_seconds(ttl, len(packed_bytes))
|
||||||
check_block_sanity(packed_bytes)
|
check_block_sanity(packed_bytes)
|
||||||
return Kasten(
|
vdf = vdf_create(
|
||||||
vdf_create(
|
|
||||||
packed_bytes,
|
packed_bytes,
|
||||||
rounds, dec=True
|
rounds, dec=True
|
||||||
).to_bytes(
|
).to_bytes(64, "big")
|
||||||
64, "big"), packed_bytes, cls, auto_check_generator=False)
|
return Kasten(vdf, packed_bytes, cls, auto_check_generator=False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_id(
|
def validate_id(
|
||||||
@ -56,14 +63,20 @@ class AnonVDFGenerator(generator.KastenBaseGenerator):
|
|||||||
packed_bytes: KastenPacked) -> None:
|
packed_bytes: KastenPacked) -> None:
|
||||||
|
|
||||||
check_block_sanity(packed_bytes)
|
check_block_sanity(packed_bytes)
|
||||||
|
k: Kasten = Kasten(
|
||||||
ttl = int(Kasten(
|
|
||||||
None,
|
None,
|
||||||
packed_bytes, 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))
|
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)
|
hash = int(hash, 16)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
2
setup.py
2
setup.py
@ -1,7 +1,7 @@
|
|||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
setup(name='onionrblocks',
|
setup(name='onionrblocks',
|
||||||
version='5.0.0',
|
version='6.0.0',
|
||||||
description='Onionr message format',
|
description='Onionr message format',
|
||||||
author='Kevin Froman',
|
author='Kevin Froman',
|
||||||
author_email='beardog@mailbox.org',
|
author_email='beardog@mailbox.org',
|
||||||
|
@ -6,72 +6,56 @@ import mimcvdf
|
|||||||
from time import time
|
from time import time
|
||||||
from math import floor
|
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):
|
class TestAnonVDF(unittest.TestCase):
|
||||||
|
|
||||||
def test_vdf_create(self):
|
def test_vdf_create(self):
|
||||||
test_data = kasten.generator.pack.pack(b"test", "tst", 0)
|
ttl = 1200
|
||||||
test_vdf = mimcvdf.vdf_create(test_data, 1000, dec=True)
|
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(
|
self.assertEqual(
|
||||||
generated.get_packed(),
|
generated.get_packed(),
|
||||||
test_data
|
test_data
|
||||||
)
|
)
|
||||||
self.assertEqual(generated.id, int(test_vdf).to_bytes(64, byteorder="big"))
|
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):
|
def test_vdf_block_validate_not_enough(self):
|
||||||
|
|
||||||
rds = 133240
|
|
||||||
t = floor(time()) - 60
|
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)
|
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):
|
def test_vdf_incermental(self):
|
||||||
last = 1
|
last = 1
|
||||||
|
ttl = 30000
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
t = time()
|
t = time()
|
||||||
rds = 0
|
rds = 0
|
||||||
test_data = kasten.generator.pack.pack(b"test" * i, "tst", 0)
|
test_data = kasten.generator.pack.pack(b"test" * i, "tst", app_metadata={"ttl": ttl})
|
||||||
rds += len(test_data) * 90000
|
test_id = mimcvdf.vdf_create(test_data, get_rounds_for_ttl_seconds(ttl, len(test_data)))
|
||||||
test_id = mimcvdf.vdf_create(test_data, rds)
|
|
||||||
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data, rounds=rds)
|
|
||||||
newT = time()
|
newT = time()
|
||||||
self.assertGreater(newT - t, last)
|
self.assertGreater(newT - t, last)
|
||||||
|
print(newT - t)
|
||||||
|
anonvdf.AnonVDFGenerator.validate_id(test_id, test_data)
|
||||||
|
|
||||||
def test_block_embedded_vdf_rounds(self):
|
def test_block_invalid_ttl(self):
|
||||||
|
return
|
||||||
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_vdf_rounds_seconds(self):
|
def test_vdf_rounds_seconds(self):
|
||||||
size = 10000
|
size = 10000
|
||||||
per_byte = 10
|
per_byte = 10
|
||||||
per_second = 4
|
per_second = 4
|
||||||
expected_rounds = (3600 * per_second) + (size * per_byte)
|
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()
|
unittest.main()
|
@ -10,27 +10,7 @@ def get_rounds_for_ttl_seconds(seconds: int, size_bytes: int):
|
|||||||
return (seconds * second_cost) + (size_bytes * byte_cost)
|
return (seconds * second_cost) + (size_bytes * byte_cost)
|
||||||
|
|
||||||
class TestBlockCreator(unittest.TestCase):
|
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):
|
def test_create_anonvdf_verify(self):
|
||||||
bl = create_anonvdf_block(b"Test", "txt", 3600)
|
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'')
|
fake_id = bytes(fake_id).replace(b'\r', b'')
|
||||||
print(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()
|
unittest.main()
|
@ -43,10 +43,10 @@ class TestUniversalRules(unittest.TestCase):
|
|||||||
t = floor(time()) - 6
|
t = floor(time()) - 6
|
||||||
bls = []
|
bls = []
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", timestamp=t)
|
packed = kasten.generator.pack.pack(b"1"*2*i, b"tst", timestamp=t, app_metadata={"ttl": 60})
|
||||||
k = anonvdf.AnonVDFGenerator.generate(packed, 1000)
|
k = anonvdf.AnonVDFGenerator.generate(packed, 60)
|
||||||
bls.append(k.id)
|
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)
|
k = anonvdf.AnonVDFGenerator.generate(packed, 1000)
|
||||||
self.assertRaises(universalrules.BlockExistsError, universalrules.checksum_exists_in_list, k.id, bls)
|
self.assertRaises(universalrules.BlockExistsError, universalrules.checksum_exists_in_list, k.id, bls)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user