2022-03-26 23:24:40 +00:00
|
|
|
from typing import Callable, Generator, List
|
|
|
|
|
2022-02-03 06:32:26 +00:00
|
|
|
from onionrblocks import Block
|
|
|
|
|
|
|
|
import db
|
2022-02-03 18:55:07 +00:00
|
|
|
|
2022-03-26 23:24:40 +00:00
|
|
|
from .dbpath import block_db_path
|
|
|
|
|
|
|
|
|
|
|
|
block_storage_observers: List[Callable] = []
|
2022-02-03 18:55:07 +00:00
|
|
|
|
2022-02-03 06:32:26 +00:00
|
|
|
|
2022-03-13 01:28:18 +00:00
|
|
|
def add_block_to_db(block: Block):
|
2022-03-26 23:24:40 +00:00
|
|
|
# Raises db.DuplicateKey if dupe
|
|
|
|
db.set_if_new(block_db_path, block.id, block.raw)
|
|
|
|
|
|
|
|
for func in block_storage_observers:
|
|
|
|
func(block)
|
2022-02-03 06:32:26 +00:00
|
|
|
|
2022-02-03 18:55:07 +00:00
|
|
|
|
2022-03-26 23:24:40 +00:00
|
|
|
def get_blocks_by_type(block_type: str) -> Generator[Block]:
|
2022-02-03 06:32:26 +00:00
|
|
|
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
|
2022-02-03 18:55:07 +00:00
|
|
|
|
2022-02-04 06:18:57 +00:00
|
|
|
|
2022-03-26 23:24:40 +00:00
|
|
|
def get_blocks_after_timestamp(
|
|
|
|
timestamp: int, block_type: str = '') -> Generator[Block]:
|
2022-02-03 18:55:07 +00:00
|
|
|
block_db = db.get_db_obj(block_db_path, 'u')
|
2022-02-05 19:25:28 +00:00
|
|
|
|
2022-02-03 18:55:07 +00:00
|
|
|
for block_hash in db.list_keys(block_db_path):
|
|
|
|
block = Block(block_hash, block_db[block_hash], auto_verify=False)
|
2022-02-04 06:18:57 +00:00
|
|
|
if block.timestamp > timestamp:
|
|
|
|
if block_type:
|
|
|
|
if block_type == block.type:
|
|
|
|
yield block
|
|
|
|
else:
|
|
|
|
yield block
|