From 505eee6aa368d692eef47774a8c944e45c3ce23e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 24 Jan 2021 00:24:10 +0000 Subject: [PATCH] added block creator wrapper for anonvdf and changed cost rule to be consistent --- onionrblocks/blockcreator/__init__.py | 50 +++++++++++++++++++++++++++ onionrblocks/generators/anonvdf.py | 7 ++-- tests/test_blockcreator.py | 33 ++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 onionrblocks/blockcreator/__init__.py create mode 100644 tests/test_blockcreator.py diff --git a/onionrblocks/blockcreator/__init__.py b/onionrblocks/blockcreator/__init__.py new file mode 100644 index 0000000..bb160d8 --- /dev/null +++ b/onionrblocks/blockcreator/__init__.py @@ -0,0 +1,50 @@ +""" +Copyright (C) <2020> Kevin Froman + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" +from time import time + +from kasten import Kasten +from kasten.generator.pack import pack + +from ..generators import anonvdf + + +def create_anonvdf_block( + block_data: bytes, + block_type: bytes, + ttl: int, + **block_metadata) -> Kasten: + try: + block_data = block_data.encode('utf-8') + except AttributeError: + pass + try: + block_type = block_type.encode('utf-8') + except AttributeError: + pass + ts = int(time()) + packed = pack( + block_data, block_type, + app_metadata=block_metadata, timestamp=ts) + rounds_needed = anonvdf.AnonVDFGenerator.get_rounds_for_ttl_seconds( + ttl, len(packed) + 10) + block_metadata['rds'] = rounds_needed + packed = pack( + block_data, + block_type, app_metadata=block_metadata, timestamp=ts) + return anonvdf.AnonVDFGenerator.generate( + packed, block_metadata['rds']) + diff --git a/onionrblocks/generators/anonvdf.py b/onionrblocks/generators/anonvdf.py index f35fc26..61fdcb5 100644 --- a/onionrblocks/generators/anonvdf.py +++ b/onionrblocks/generators/anonvdf.py @@ -21,7 +21,7 @@ class AnonVDFGenerator(generator.KastenBaseGenerator): # Max cost of block storage + American mobile data transfer = 0.0751 USD byte_cost = 1000 - second_cost = 25 + second_cost = 26 @classmethod def get_rounds_for_ttl_seconds(cls, seconds: int, size_bytes: int): @@ -29,10 +29,7 @@ class AnonVDFGenerator(generator.KastenBaseGenerator): @classmethod def get_ttl_seconds_per_rounds(cls, rounds: int, size_bytes: int): - result = (rounds // cls.second_cost) - (cls.byte_cost * size_bytes) - if result < 0: - raise NotEnoughRounds - return result + return (rounds - (size_bytes * cls.byte_cost)) // cls.second_cost @classmethod def generate( diff --git a/tests/test_blockcreator.py b/tests/test_blockcreator.py new file mode 100644 index 0000000..b3a04af --- /dev/null +++ b/tests/test_blockcreator.py @@ -0,0 +1,33 @@ +from onionrblocks.generators import anonvdf +import unittest +import kasten +from onionrblocks.blockcreator import create_anonvdf_block + +class TestBlockCreator(unittest.TestCase): + def test_create_anonvdf(self): + bl = create_anonvdf_block(b"Test", "txt", 3600) + # (rounds - (size_bytes * cls.byte_cost)) // cls.second_cost + expected_rounds = (3600 * 26) + (len(bl.get_packed()) * 1000) - 1000 + self.assertEqual(expected_rounds, bl.get_metadata()['rds']) + + 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) - 1000 + self.assertEqual(expected_rounds, bl.get_metadata()['rds']) + + 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) + 1000 + self.assertEqual(expected_rounds, bl.get_metadata()['rds']) + + def test_create_anonvdf_verify(self): + bl = create_anonvdf_block(b"Test", "txt", 3600) + expected_rounds = (len(bl.get_packed()) * anonvdf.AnonVDFGenerator.byte_cost) + (3600 * anonvdf.AnonVDFGenerator.second_cost) - 1000 + self.assertEqual(expected_rounds, bl.get_metadata()['rds']) + + packed = bl.get_packed() + id = bl.id + kasten.Kasten(id, packed, anonvdf.AnonVDFGenerator, auto_check_generator=True) + +unittest.main() \ No newline at end of file