From 2b177574562cbcb3aed8d6d1b69548e69f684aa9 Mon Sep 17 00:00:00 2001 From: foefl Date: Tue, 16 Dec 2025 15:04:50 +0100 Subject: [PATCH] add annotations --- src/dopt_pollublock_blockchain/blockchain.pyi | 50 +++++++++++++++++++ src/dopt_pollublock_blockchain/blockchain.pyx | 17 ++++--- 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/dopt_pollublock_blockchain/blockchain.pyi diff --git a/src/dopt_pollublock_blockchain/blockchain.pyi b/src/dopt_pollublock_blockchain/blockchain.pyi new file mode 100644 index 0000000..d4cc79a --- /dev/null +++ b/src/dopt_pollublock_blockchain/blockchain.pyi @@ -0,0 +1,50 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import datetime + from pathlib import Path + + from dopt_pollublock_blockchain import types as t + +class PyBlock: + def __init__(self) -> None: ... + def __repr__(self) -> str: ... + def __str__(self) -> str: ... + def serialize_dict(self) -> t.SerializedPyBlock: ... + @property + def index(self) -> int: ... + @property + def timestamp(self) -> datetime.datetime: ... + @property + def nonce(self) -> int: ... + @property + def prev_hash(self) -> str: ... + @property + def hash(self) -> str: ... + @property + def data(self) -> str: ... + def bytes_serialize(self) -> bytes: ... + def perform_hash(self) -> str: ... + +class Blockchain: + def __init__(self, db_path: Path) -> None: ... + def __len__(self) -> int: ... + @property + def difficulty(self) -> int: ... + @difficulty.setter + def difficulty(self, value: int) -> None: ... + @property + def index(self) -> int: ... + @property + def genesis_done(self) -> bool: ... + def print_blocks(self, max_num: int) -> None: ... + def get_block(self, idx: t.BlockIndex) -> PyBlock: ... + def create_genesis_block(self) -> None: ... + def new_block(self, data: str) -> t.BlockIndex: ... + def validate(self) -> bool: ... + def get_saving_entries(self, min_idx: t.BlockIndex) -> list[t.SerializedPyBlock]: ... + def save(self) -> None: ... + def close_db_connections(self) -> None: ... + def load(self, batch_size: int) -> None: ... diff --git a/src/dopt_pollublock_blockchain/blockchain.pyx b/src/dopt_pollublock_blockchain/blockchain.pyx index a3631e2..50f9840 100644 --- a/src/dopt_pollublock_blockchain/blockchain.pyx +++ b/src/dopt_pollublock_blockchain/blockchain.pyx @@ -199,11 +199,10 @@ cdef class PyBlock: digest = perform_hash_c(self.BlockC, &digest_size) if digest is NULL: raise MemoryError() - # TODO out: hash assignment in blockchain self.BlockC.hash = bytes(digest[:digest_size]).hex().encode("UTF-8") finally: free(digest) - # TODO rework + return self.hash @@ -543,6 +542,8 @@ cdef class Blockchain: cdef int res = self.add_block(block) if res != 0: raise RuntimeError("Could not mine block. No nonce found") + + return self._index def validate(self): cdef: @@ -572,20 +573,20 @@ cdef class Blockchain: return True - def get_saving_entries(self, max_idx): + def get_saving_entries(self, min_idx): entries = [] cdef: Block *block int idx = 0 - int _max_idx + int _min_idx - if max_idx is None: - _max_idx = -1 + if min_idx is None: + _min_idx = -1 else: - _max_idx = max_idx + _min_idx = min_idx for idx in range(self._chain[0].size()): - if idx <= _max_idx: + if idx <= _min_idx: continue block = self._chain[0][idx] contents = {}