Fix metadata not being None when empty

This commit is contained in:
Kevin Froman 2020-12-07 01:58:09 +00:00
parent c10a75e558
commit 7c5962ea2c
2 changed files with 32 additions and 9 deletions

View File

@ -1,8 +1,20 @@
from typing import Union
from time import time
from binascii import hexlify
import kasten
from .types import AnonymousVDFBlockResult
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:
@ -16,7 +28,7 @@ def _packer(block_data: bytes, **kasten_header) -> kasten.main.KastenPacked:
kasten_header['encrypt mode'],
__get_optional('signer', b''),
__get_optional('signature', b''),
__get_optional('metadata', {}),
__get_optional('metadata', None),
int(time()//60 * 60)
)
@ -25,8 +37,16 @@ def _packer(block_data: bytes, **kasten_header) -> kasten.main.KastenPacked:
# 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):
rounds: int,
block_data: Union[str, bytes],
**block_header) -> AnonymousVDFBlockResult:
block_data = __str_to_bytes(block_data)
# Block with anonymous VDF as proof
packed = _packer(block_data, **block_header)
return kasten.generator.KastenMimcGenerator.generate(
packed, rounds)
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

@ -9,10 +9,13 @@ class TestVDFBlock(unittest.TestCase):
def test_basic(self):
rounds = 10**4
k = onionrblocks.create_block_anonymous_vdf(rounds, b'test', **{'type': 'test', 'encrypt mode': 0})
raw = k.data
id = binascii.hexlify(k.id)
kasten.generator.vdf_verify(raw, id, rounds)
data = b'test data'
k = onionrblocks.create_block_anonymous_vdf(rounds, data, **{'type': 'test', 'encrypt mode': 0})
raw = k.raw_bytes
id = int.from_bytes(k.hex_checksum, byteorder="big")
#print(id, raw)
print(raw)
kasten.generator.KastenMimcGenerator.validate_id(id, raw, rounds)
unittest.main()