Onionr/src/blockdb/__init__.py

34 lines
871 B
Python
Raw Permalink Normal View History

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
2022-10-01 04:25:46 +00:00
from onionrplugins import onionrevents
2022-02-03 06:32:26 +00:00
import db
2022-02-03 18:55:07 +00:00
2022-03-26 23:24:40 +00:00
from .dbpath import block_db_path
2022-07-31 05:32:43 +00:00
from .blockcleaner import clean_block_database
from .getblocks import get_blocks_after_timestamp, get_blocks_by_type
from .deleteblock import delete_block
2022-03-26 23:24:40 +00:00
block_storage_observers: List[Callable] = []
2022-02-03 18:55:07 +00:00
2022-02-03 06:32:26 +00:00
2022-07-31 05:32:43 +00:00
2022-03-13 01:28:18 +00:00
def add_block_to_db(block: Block):
2022-10-01 04:25:46 +00:00
onionrevents.event('before_block_db_add', block, threaded=False)
db.set_if_new(block_db_path, block.id, block.raw) # Raises db.DuplicateKey if dupe
onionrevents.event('after_block_db_add', block, threaded=False)
2022-03-26 23:24:40 +00:00
2022-04-03 06:16:58 +00:00
def has_block(block_hash):
return block_hash in db.list_keys(block_db_path)
2022-04-03 06:16:58 +00:00
def get_block(block_hash) -> Block:
return Block(
block_hash,
db.get_db_obj(block_db_path, 'u').get(block_hash),
auto_verify=False)