work on loading block

This commit is contained in:
Kevin Froman 2020-12-07 14:54:25 +00:00
parent 7c5962ea2c
commit af0ca4a33d
4 changed files with 11 additions and 49 deletions

View File

@ -6,47 +6,6 @@ import kasten
from .types import AnonymousVDFBlockResult from .types import AnonymousVDFBlockResult
from .create import create_block_anonymous_vdf
def __str_to_bytes(data: str):
try:
return data.encode('utf-8')
except AttributeError:
return data
def _packer(block_data: bytes, **kasten_header) -> kasten.main.KastenPacked:
def __get_optional(key, default):
try:
return kasten_header[key]
except KeyError:
return default
# Todo: validate length sanities
return kasten.generator.pack.pack(
block_data,
kasten_header['type'],
kasten_header['encrypt mode'],
__get_optional('signer', b''),
__get_optional('signature', b''),
__get_optional('metadata', None),
int(time()//60 * 60)
)
# Block creation functions handle calling the proof and compiling blocks
# They do not handle sigs or encryption (at least never directly)
def create_block_anonymous_vdf(
rounds: int,
block_data: Union[str, bytes],
**block_header) -> AnonymousVDFBlockResult:
block_data = __str_to_bytes(block_data)
# Block with anonymous VDF as proof
packed: kasten.types.KastenPacked = _packer(block_data, **block_header)
kasten_result: kasten.Kasten = \
kasten.generator.KastenMimcGenerator.generate(packed, rounds)
print('orig packed', packed)
return AnonymousVDFBlockResult(
kasten_result.id,
kasten_result.get_packed())

View File

@ -1 +1 @@
kasten==0.0.7 kasten==0.1.0

View File

@ -4,9 +4,9 @@
# #
# pip-compile --generate-hashes requirements.in # pip-compile --generate-hashes requirements.in
# #
kasten==0.0.7 \ kasten==0.1.0 \
--hash=sha256:01edc0ac72a49c851b4fbbc0ec2d2c9c9d9a114c1492c0f5af1b10602de6d82e \ --hash=sha256:21214afc1e61eb32bafdebbedc1856de973a0ac0798c1710787b889c7f9e9899 \
--hash=sha256:29baf5270b2dda51c8fed14a3d7108bd86e73e34f30ead9c054ce66d478e0c12 \ --hash=sha256:a5d583f383778d256e56f69fa50c3c22e8fb1aa95923ff3d1c8f915e1235418e \
# via -r requirements.in # via -r requirements.in
mimcvdf==1.1.0 \ mimcvdf==1.1.0 \
--hash=sha256:97a4ccdebb58352c64c268d2e57ef8817c9fe4ac3dcc922410bfcc72033f344a \ --hash=sha256:97a4ccdebb58352c64c268d2e57ef8817c9fe4ac3dcc922410bfcc72033f344a \

View File

@ -7,15 +7,18 @@ import onionrblocks
class TestVDFBlock(unittest.TestCase): class TestVDFBlock(unittest.TestCase):
def test_basic(self): def test_vdf_create(self):
rounds = 10**4 rounds = 10**4
data = b'test data' data = b'test data'
k = onionrblocks.create_block_anonymous_vdf(rounds, data, **{'type': 'test', 'encrypt mode': 0}) k = onionrblocks.create_block_anonymous_vdf(rounds, data, **{'type': 'test', 'encrypt mode': 0})
raw = k.raw_bytes raw = k.raw_bytes
id = int.from_bytes(k.hex_checksum, byteorder="big") id = int.from_bytes(k.dec_checksum, byteorder="big")
#print(id, raw) #print(id, raw)
print(raw) print(id, raw)
kasten.generator.KastenMimcGenerator.validate_id(id, raw, rounds) kasten.generator.KastenMimcGenerator.validate_id(id, raw, rounds)
self.assertEqual(len(raw), 22)
def test_vdf_auto_validate_loader
unittest.main() unittest.main()