add annotations

This commit is contained in:
Florian Förster 2025-12-16 15:04:50 +01:00
parent d155d1afad
commit 2b17757456
2 changed files with 59 additions and 8 deletions

View File

@ -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: ...

View File

@ -199,11 +199,10 @@ cdef class PyBlock:
digest = perform_hash_c(self.BlockC, &digest_size) digest = perform_hash_c(self.BlockC, &digest_size)
if digest is NULL: if digest is NULL:
raise MemoryError() raise MemoryError()
# TODO out: hash assignment in blockchain
self.BlockC.hash = bytes(digest[:digest_size]).hex().encode("UTF-8") self.BlockC.hash = bytes(digest[:digest_size]).hex().encode("UTF-8")
finally: finally:
free(digest) free(digest)
# TODO rework
return self.hash return self.hash
@ -544,6 +543,8 @@ cdef class Blockchain:
if res != 0: if res != 0:
raise RuntimeError("Could not mine block. No nonce found") raise RuntimeError("Could not mine block. No nonce found")
return self._index
def validate(self): def validate(self):
cdef: cdef:
Block *block Block *block
@ -572,20 +573,20 @@ cdef class Blockchain:
return True return True
def get_saving_entries(self, max_idx): def get_saving_entries(self, min_idx):
entries = [] entries = []
cdef: cdef:
Block *block Block *block
int idx = 0 int idx = 0
int _max_idx int _min_idx
if max_idx is None: if min_idx is None:
_max_idx = -1 _min_idx = -1
else: else:
_max_idx = max_idx _min_idx = min_idx
for idx in range(self._chain[0].size()): for idx in range(self._chain[0].size()):
if idx <= _max_idx: if idx <= _min_idx:
continue continue
block = self._chain[0][idx] block = self._chain[0][idx]
contents = {} contents = {}