-
-
Notifications
You must be signed in to change notification settings - Fork 7
refactor: flatten directory structure to single minichain package #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2546772
59b0093
b9bdaf7
f340d17
2305a54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| from .pow import mine_block, calculate_hash, MiningExceededError | ||
| from .block import Block | ||
| from .chain import Blockchain | ||
| from .transaction import Transaction | ||
| from .state import State | ||
| from .contract import ContractMachine | ||
| from .p2p import P2PNetwork | ||
| from .mempool import Mempool | ||
|
|
||
| __all__ = [ | ||
| "mine_block", | ||
| "calculate_hash", | ||
| "MiningExceededError", | ||
| "Block", | ||
| "Blockchain", | ||
| "Transaction", | ||
| "State", | ||
| "ContractMachine", | ||
| "P2PNetwork", | ||
| "Mempool", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| from consensus.pow import calculate_hash | ||
| from .pow import calculate_hash | ||
| import logging | ||
| import threading | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,16 +19,42 @@ class P2PNetwork: | |||||||||||||||||
| } | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| def __init__(self, handler_callback): | ||||||||||||||||||
| def __init__(self, handler_callback=None): | ||||||||||||||||||
| self._handler_callback = None | ||||||||||||||||||
| if handler_callback is not None: | ||||||||||||||||||
| self.register_handler(handler_callback) | ||||||||||||||||||
| self.pubsub = None # Will be set in real implementation | ||||||||||||||||||
|
|
||||||||||||||||||
| def register_handler(self, handler_callback): | ||||||||||||||||||
| if not callable(handler_callback): | ||||||||||||||||||
| raise ValueError("handler_callback must be callable") | ||||||||||||||||||
| self.handler_callback = handler_callback | ||||||||||||||||||
| self.pubsub = None # Will be set in real implementation | ||||||||||||||||||
| self._handler_callback = handler_callback | ||||||||||||||||||
|
Comment on lines
+28
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial
Raising ♻️ Proposed fix def register_handler(self, handler_callback):
- if not callable(handler_callback):
- raise ValueError("handler_callback must be callable")
+ if not callable(handler_callback):
+ raise TypeError("handler_callback must be callable")
self._handler_callback = handler_callback🧰 Tools🪛 Ruff (0.15.1)[warning] 30-30: Prefer (TRY004) [warning] 30-30: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| async def start(self): | ||||||||||||||||||
| logger.info("Network: Listening on /ip4/0.0.0.0/tcp/0") | ||||||||||||||||||
| # In real libp2p, we would await host.start() here | ||||||||||||||||||
|
|
||||||||||||||||||
| async def stop(self): | ||||||||||||||||||
| """Clean up network resources cleanly upon shutdown.""" | ||||||||||||||||||
| logger.info("Network: Shutting down") | ||||||||||||||||||
| if self.pubsub: | ||||||||||||||||||
| try: | ||||||||||||||||||
| shutdown_meth = None | ||||||||||||||||||
| for method_name in ('close', 'stop', 'aclose', 'shutdown'): | ||||||||||||||||||
| if hasattr(self.pubsub, method_name): | ||||||||||||||||||
| shutdown_meth = getattr(self.pubsub, method_name) | ||||||||||||||||||
| break | ||||||||||||||||||
|
|
||||||||||||||||||
| if shutdown_meth: | ||||||||||||||||||
| import asyncio | ||||||||||||||||||
| res = shutdown_meth() | ||||||||||||||||||
| if asyncio.iscoroutine(res): | ||||||||||||||||||
| await res | ||||||||||||||||||
|
Comment on lines
+49
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Move Two issues:
♻️ Proposed fixAt the top of the file: import json
import logging
+import inspectIn - if shutdown_meth:
- import asyncio
- res = shutdown_meth()
- if asyncio.iscoroutine(res):
- await res
+ if shutdown_meth:
+ res = shutdown_meth()
+ if inspect.isawaitable(res):
+ await res🤖 Prompt for AI Agents |
||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| logger.error("Network: Error shutting down pubsub: %s", e) | ||||||||||||||||||
|
Comment on lines
+53
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Use
♻️ Proposed fix- except Exception as e:
- logger.error("Network: Error shutting down pubsub: %s", e)
+ except Exception:
+ logger.exception("Network: Error shutting down pubsub")📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.1)[warning] 53-53: Do not catch blind exception: (BLE001) [warning] 54-54: Use Replace with (TRY400) 🤖 Prompt for AI Agents |
||||||||||||||||||
| finally: | ||||||||||||||||||
| self.pubsub = None | ||||||||||||||||||
|
|
||||||||||||||||||
| async def _broadcast_message(self, topic, msg_type, payload): | ||||||||||||||||||
| msg = json.dumps({"type": msg_type, "data": payload}) | ||||||||||||||||||
| if self.pubsub: | ||||||||||||||||||
|
|
@@ -84,6 +110,9 @@ async def handle_message(self, msg): | |||||||||||||||||
| return | ||||||||||||||||||
|
|
||||||||||||||||||
| try: | ||||||||||||||||||
| await self.handler_callback(data) | ||||||||||||||||||
| if self._handler_callback: | ||||||||||||||||||
| await self._handler_callback(data) | ||||||||||||||||||
|
Comment on lines
+113
to
+114
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🛡️ Proposed fix in
|
||||||||||||||||||
| else: | ||||||||||||||||||
| logger.warning("Network Error: No handler_callback registered") | ||||||||||||||||||
| except Exception: | ||||||||||||||||||
| logger.exception("Error in network handler callback for data: %s", data) | ||||||||||||||||||
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.