2021-02-04 01:52:36 +00:00
|
|
|
from base64 import b85encode
|
2021-01-29 21:15:18 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2021-02-21 23:22:18 +00:00
|
|
|
import threading
|
2021-01-29 21:15:18 +00:00
|
|
|
|
|
|
|
import ujson as json
|
|
|
|
import kasten
|
2021-02-21 23:22:18 +00:00
|
|
|
|
|
|
|
from onionrplugins import onionrevents
|
|
|
|
from blockio import store_block
|
2021-01-29 21:15:18 +00:00
|
|
|
from onionrblocks.generators.anonvdf import AnonVDFGenerator
|
|
|
|
|
|
|
|
_DIR = os.path.dirname(os.path.realpath(__file__)) + '/../'
|
|
|
|
|
|
|
|
|
|
|
|
def vdf_block(data, data_type, ttl, **metadata):
|
2021-02-21 23:22:18 +00:00
|
|
|
try:
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2021-02-04 01:52:36 +00:00
|
|
|
data = b85encode(data)
|
2021-01-29 21:15:18 +00:00
|
|
|
generated = subprocess.Popen(
|
|
|
|
[
|
|
|
|
f'{_DIR}anonvdf-block-creator.py',
|
|
|
|
json.dumps(metadata),
|
|
|
|
data_type,
|
|
|
|
str(ttl)],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
generated = generated.communicate(data)[0]
|
|
|
|
return kasten.Kasten(
|
|
|
|
generated[:64], generated[64:],
|
|
|
|
AnonVDFGenerator, auto_check_generator=True)
|
|
|
|
|
2021-02-21 23:22:18 +00:00
|
|
|
|
|
|
|
def gen_and_store_vdf_block(shared_state, *args, **kwargs):
|
|
|
|
safe_db = shared_state.get_by_string('SafeDB')
|
|
|
|
k = vdf_block(*args, **kwargs)
|
|
|
|
store_block(
|
|
|
|
k,
|
|
|
|
safe_db,
|
|
|
|
own_block=True
|
|
|
|
)
|
|
|
|
onionrevents.event('blockcreated', data=shared_state, threaded=True)
|
|
|
|
|