From eb763cf2934394fd90eaed287f861a7aec8507d2 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Thu, 3 Feb 2022 12:55:07 -0600 Subject: [PATCH] Work on blockdb functions --- src/blockdb/__init__.py | 21 ++++++++++++++++----- src/db/__init__.py | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/blockdb/__init__.py b/src/blockdb/__init__.py index 83d84ea0..25c0a7a4 100644 --- a/src/blockdb/__init__.py +++ b/src/blockdb/__init__.py @@ -1,16 +1,27 @@ -from typing import Generator - from onionrblocks import Block 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') 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 + +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 + diff --git a/src/db/__init__.py b/src/db/__init__.py index 1e9a4a86..9758b628 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -17,7 +17,7 @@ def _do_timeout(func, *args): continue if time.time() - ts > timeout: raise TimeoutError() - time.sleep(0.1) + time.sleep(0.01) else: return res @@ -43,7 +43,7 @@ def get_db_obj(db_path, extra_flag=''): """For when you should keep a db obj open""" def _get_db(): 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):