Work on blockdb functions

This commit is contained in:
Kevin F 2022-02-03 12:55:07 -06:00
parent ff9eb13579
commit eb763cf293
2 changed files with 18 additions and 7 deletions

View File

@ -1,16 +1,27 @@
from typing import Generator
from onionrblocks import Block from onionrblocks import Block
import db import db
from .. import identifyhome from utils import identifyhome
block_db_path = identifyhome.identify_home() + 'blocks.db' block_db_path = identifyhome.identify_home() + 'blockdata'
def get_blocks_by_type(block_type) -> Generator[Block]:
def store_vdf_block(block: Block):
db.set(block_db_path, block.id, block.raw)
def get_blocks_by_type(block_type):
block_db = db.get_db_obj(block_db_path, 'u') block_db = db.get_db_obj(block_db_path, 'u')
for block_hash in db.list_keys(block_db_path): for block_hash in db.list_keys(block_db_path):
block = Block(block_hash, block_db[block_hash], auto_verify=False) block = Block(block_hash, block_db[block_hash], auto_verify=False)
if block.type == block_type: if block.type == block_type:
yield block yield block
def get_blocks_after_timestamp(timestamp: int, block_type: bytes = ''):
block_db = db.get_db_obj(block_db_path, 'u')
for block_hash in db.list_keys(block_db_path):
block = Block(block_hash, block_db[block_hash], auto_verify=False)
if block.type == block_type:
yield block

View File

@ -17,7 +17,7 @@ def _do_timeout(func, *args):
continue continue
if time.time() - ts > timeout: if time.time() - ts > timeout:
raise TimeoutError() raise TimeoutError()
time.sleep(0.1) time.sleep(0.01)
else: else:
return res return res
@ -43,7 +43,7 @@ def get_db_obj(db_path, extra_flag=''):
"""For when you should keep a db obj open""" """For when you should keep a db obj open"""
def _get_db(): def _get_db():
return dbm.open(db_path, "c" + extra_flag) return dbm.open(db_path, "c" + extra_flag)
return _do_timeout(_get_db, db_path) return _do_timeout(_get_db)
def list_keys(db_path): def list_keys(db_path):