(probably) finished RPC block creation

This commit is contained in:
Kevin F 2023-04-22 13:20:42 -05:00
parent ae41fb1b72
commit 5a80f7f914
4 changed files with 120 additions and 16 deletions

View File

@ -74,18 +74,19 @@ def create_and_insert_block(
def prepare_block_for_vdf(block_data: 'base64', block_type, ttl: int, metadata: dict): def prepare_block_for_vdf(block_data: 'base64', block_type, ttl: int, metadata: dict):
# This allows for untrusted clients to create blocks, they just have to compute the VDF # This allows for untrusted clients to create blocks, they just have to compute the VDF
metadata['ttl'] = ttl metadata['ttl'] = ttl
block_data = base64.b64decode(block_data)
kasten_packed = kasten_pack.pack(block_data, block_type, metadata, int(time())) kasten_packed = kasten_pack.pack(block_data, block_type, metadata, int(time()))
kasten_obj = kasten.Kasten('', kasten_packed, kasten.generator.KastenBaseGenerator, auto_check_generator=False) kasten_obj = kasten.Kasten('', kasten_packed, kasten.generator.KastenBaseGenerator, auto_check_generator=False)
return { return {
'raw': base64.b64encode(kasten_obj).decode('utf-8'), 'raw': base64.b64encode(kasten_packed).decode('utf-8'),
'rounds_needed': onionrblocks.blockcreator.anonvdf.AnonVDFGenerator.get_rounds_for_ttl_seconds(ttl, len(kasten_packed)) 'rounds_needed': onionrblocks.blockcreator.anonvdf.AnonVDFGenerator.get_rounds_for_ttl_seconds(ttl, len(kasten_obj.get_packed()))
} }
@dispatcher.add_method @dispatcher.add_method
def assemble_and_insert_block( def assemble_and_insert_block(
kasten_packed: 'base64', vdf_result: 'base64') -> str: kasten_packed: 'base64', vdf_result: str) -> str:
bl = onionrblocks.Block( bl = onionrblocks.Block(
base64.b64decode(vdf_result), vdf_result,
base64.b64decode(kasten_packed), auto_verify=True) base64.b64decode(kasten_packed), auto_verify=True)
insert_block(bl) insert_block(bl)
return { return {

View File

@ -0,0 +1,61 @@
import os, uuid
import base64
import secrets
import time
from nacl import signing
import kasten
TEST_DIR = 'testdata/%s-%s' % (str(uuid.uuid4())[:6], os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import unittest
import sys
import kasten
from kasten.generator import pack as kasten_pack
sys.path.append('static-data/official-plugins/rpc/rpc')
sys.path.append("src/")
import queue
import onionrblocks
from onionrblocks import generators
import mimcvdf
from gossip import blockqueues
import blocks
byte_cost = 10
second_cost = 4
def _get_rounds(seconds: int, size_bytes: int):
return (seconds * second_cost) + (size_bytes * byte_cost)
class RPCAssembleAndInsertTest(unittest.TestCase):
def test_assemble_and_insert(self):
data = b'block data'
metadata = {'ttl': 3600}
kasten_packed = kasten_pack.pack(data, 'txt', metadata, int(time.time()))
kasten_obj = kasten.Kasten('', kasten_packed, kasten.generator.KastenBaseGenerator, auto_check_generator=False)
vdf_result = mimcvdf.vdf_create(kasten_packed, _get_rounds(3600, len(kasten_packed)))
blocks.assemble_and_insert_block(base64.b64encode(kasten_packed), vdf_result)
try:
bl = blockqueues.gossip_block_queues[0].get_nowait()
self.assertEqual(bl.data, data)
except queue.Empty:
bl = blockqueues.gossip_block_queues[1].get_nowait()
self.assertEqual(bl.data, data)
unittest.main()

View File

@ -26,18 +26,6 @@ from gossip import blockqueues
import blocks import blocks
class MockQueue:
def __init__(self):
self.data = []
def get_nowait(self):
return self.data.pop(0)
def put_nowait(self, data):
print("putting", data)
self.data.append(data)
return True
class RPCInsertBlockTest(unittest.TestCase): class RPCInsertBlockTest(unittest.TestCase):

View File

@ -0,0 +1,54 @@
import os, uuid
import base64
import secrets
import time
from nacl import signing
import kasten
TEST_DIR = 'testdata/%s-%s' % (str(uuid.uuid4())[:6], os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
import unittest
import sys
import kasten
from kasten.generator import pack as kasten_pack
sys.path.append('static-data/official-plugins/rpc/rpc')
sys.path.append("src/")
import queue
import onionrblocks
from gossip import blockqueues
import blocks
byte_cost = 10
second_cost = 4
def _get_rounds(seconds: int, size_bytes: int):
return (seconds * second_cost) + (size_bytes * byte_cost)
class RPCPrepareBlockForVDFTest(unittest.TestCase):
def test_prepare_block_for_vdf(self):
data_bytes = b'block data'
data = base64.b64encode(data_bytes)
resp_dict = blocks.prepare_block_for_vdf(data, 'txt', 3600, {})
expected_kasten_packed = kasten_pack.pack(data_bytes, 'txt', {'ttl': 3600}, int(time.time()))
expected_kasten_obj = kasten.Kasten('', expected_kasten_packed, kasten.generator.KastenBaseGenerator, auto_check_generator=False)
self.assertTrue(resp_dict['raw'])
self.assertEqual(base64.b64decode(resp_dict['raw']),expected_kasten_packed)
self.assertEqual(resp_dict['rounds_needed'], _get_rounds(3600, len(expected_kasten_obj.get_packed())))
unittest.main()