added block creator wrapper for anonvdf and changed cost rule to be consistent
This commit is contained in:
parent
1db58e923a
commit
505eee6aa3
50
onionrblocks/blockcreator/__init__.py
Normal file
50
onionrblocks/blockcreator/__init__.py
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
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'])
|
||||
|
@ -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(
|
||||
|
33
tests/test_blockcreator.py
Normal file
33
tests/test_blockcreator.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user