Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
4 changes: 2 additions & 2 deletions minichain/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 5 additions & 2 deletions minichain/p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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
Comment on lines +86 to +88
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Redundant check creates unreachable code.

The early guard at lines 86-88 returns if self._handler_callback is not callable. This makes the else branch at lines 118-119 unreachable—if execution reaches line 116, the callback is guaranteed to be callable (and thus truthy).

♻️ Suggested simplification
         try:
-            if self._handler_callback:
-                await self._handler_callback(data)
-            else:
-                logger.warning("Network Error: No handler_callback registered")
+            await self._handler_callback(data)
         except Exception:
             logger.exception("Error in network handler callback for data: %s", data)

Also applies to: 116-119

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@minichain/p2p.py` around lines 86 - 88, The guard that returns when
self._handler_callback is not callable makes the later "else" branch
unreachable; remove the redundant conditional and else branch and directly use
the callable path: ensure the initial check either is removed or changed to a
warning without return, and replace the later "if/else" around invoking
self._handler_callback with a single direct invocation (or a simple callable
check + invoke) so only one canonical call-site for self._handler_callback
exists; target references: self._handler_callback and the message-handling block
that currently contains the unreachable else (the two redundant checks noted).


try:
if not hasattr(msg, "data"):
Expand Down
2 changes: 1 addition & 1 deletion minichain/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down