diff --git a/main.py b/main.py index add86c1..2125c74 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from nacl.signing import SigningKey from nacl.encoding import HexEncoder -from minichain import Transaction, Blockchain, Block, State, Mempool, P2PNetwork, mine_block +from minichain import Transaction, Blockchain, Block, Mempool, P2PNetwork, mine_block logger = logging.getLogger(__name__) diff --git a/minichain/chain.py b/minichain/chain.py index 78ac73f..96523db 100644 --- a/minichain/chain.py +++ b/minichain/chain.py @@ -12,9 +12,9 @@ class Blockchain: Manages the blockchain, validates blocks, and commits state transitions. """ - def __init__(self): + def __init__(self, state=None): self.chain = [] - self.state = State() + self.state = state if state is not None else State() self._lock = threading.RLock() self._create_genesis_block() diff --git a/minichain/p2p.py b/minichain/p2p.py index aacbf49..8786b9f 100644 --- a/minichain/p2p.py +++ b/minichain/p2p.py @@ -19,7 +19,7 @@ class P2PNetwork: } """ - def __init__(self, handler_callback=None): + def __init__(self, handler_callback=None) -> None: self._handler_callback = None if handler_callback is not None: self.register_handler(handler_callback) @@ -44,7 +44,7 @@ async def stop(self): if hasattr(self.pubsub, method_name): shutdown_meth = getattr(self.pubsub, method_name) break - + if shutdown_meth: import asyncio res = shutdown_meth() @@ -83,6 +83,9 @@ async def handle_message(self, msg): """ Callback when a p2p message is received. """ + if not callable(self._handler_callback): + logger.debug("Network: No handler callback set, ignoring message") + return try: if not hasattr(msg, "data"): diff --git a/minichain/transaction.py b/minichain/transaction.py index cdf4d99..e039084 100644 --- a/minichain/transaction.py +++ b/minichain/transaction.py @@ -12,7 +12,7 @@ def __init__(self, sender, receiver, amount, nonce, data=None, signature=None, t self.amount = amount self.nonce = nonce self.data = data # Preserve None (do NOT normalize to "") - self.timestamp = round(timestamp * 1000) if timestamp is not None else round(time.time() * 1000) # Integer milliseconds for determinism + self.timestamp = int(timestamp) if timestamp is not None else int(time.time() * 1000) self.signature = signature # Hex str def to_dict(self):