-
-
Notifications
You must be signed in to change notification settings - Fork 17
Feat/libp2p #111
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
Feat/libp2p #111
Changes from all commits
3c9992f
824e21a
9c83bd3
251ad68
baadfbb
127b008
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,6 +257,68 @@ def _load_snapshot_from_sqlite(db_path: str) -> dict[str, Any]: | |
| return {"chain": chain, "state": state} | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Banned Peers (Track 1) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| import time | ||
|
Comment on lines
+260
to
+264
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. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value Move Placing ♻️ Suggested fix # ---------------------------------------------------------------------------
# Banned Peers (Track 1)
# ---------------------------------------------------------------------------
-import time
-
def _ensure_banned_peers_table(conn: sqlite3.Connection) -> None:And add 🤖 Prompt for AI Agents |
||
|
|
||
| def _ensure_banned_peers_table(conn: sqlite3.Connection) -> None: | ||
| conn.execute( | ||
| "CREATE TABLE IF NOT EXISTS banned_peers (peer_id TEXT PRIMARY KEY, reason TEXT, timestamp REAL)" | ||
| ) | ||
|
|
||
| def ban_peer(peer_id: str, reason: str, path: str = ".") -> None: | ||
| db_path = os.path.join(path, _DB_FILE) | ||
| os.makedirs(path, exist_ok=True) | ||
| conn = _connect(db_path) | ||
| try: | ||
| _ensure_banned_peers_table(conn) | ||
| with conn: | ||
| conn.execute( | ||
| "INSERT OR REPLACE INTO banned_peers (peer_id, reason, timestamp) VALUES (?, ?, ?)", | ||
| (peer_id, reason, time.time()) | ||
| ) | ||
| finally: | ||
| conn.close() | ||
|
Comment on lines
+271
to
+283
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. 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value No validation on
🤖 Prompt for AI Agents |
||
|
|
||
| def unban_peer(peer_id: str, path: str = ".") -> None: | ||
| db_path = os.path.join(path, _DB_FILE) | ||
| if not os.path.exists(db_path): | ||
| return | ||
| conn = _connect(db_path) | ||
| try: | ||
| _ensure_banned_peers_table(conn) | ||
| with conn: | ||
| conn.execute("DELETE FROM banned_peers WHERE peer_id = ?", (peer_id,)) | ||
| finally: | ||
| conn.close() | ||
|
|
||
| def is_peer_banned(peer_id: str, path: str = ".") -> bool: | ||
| db_path = os.path.join(path, _DB_FILE) | ||
| if not os.path.exists(db_path): | ||
| return False | ||
| conn = _connect(db_path) | ||
| try: | ||
| _ensure_banned_peers_table(conn) | ||
| row = conn.execute("SELECT peer_id FROM banned_peers WHERE peer_id = ?", (peer_id,)).fetchone() | ||
| return row is not None | ||
| finally: | ||
| conn.close() | ||
|
|
||
| def get_banned_peers(path: str = ".") -> list[dict[str, Any]]: | ||
| db_path = os.path.join(path, _DB_FILE) | ||
| if not os.path.exists(db_path): | ||
| return [] | ||
| conn = _connect(db_path) | ||
| try: | ||
| _ensure_banned_peers_table(conn) | ||
| rows = conn.execute("SELECT peer_id, reason, timestamp FROM banned_peers ORDER BY timestamp DESC").fetchall() | ||
| return [{"peer_id": r["peer_id"], "reason": r["reason"], "timestamp": r["timestamp"]} for r in rows] | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
Comment on lines
+266
to
+320
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. 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win Repeated connect/ensure-table/close boilerplate across all four functions. Each of ♻️ Example consolidation+from contextlib import contextmanager
+
+@contextmanager
+def _banned_peers_conn(path: str, create_if_missing: bool = True):
+ db_path = os.path.join(path, _DB_FILE)
+ if not create_if_missing and not os.path.exists(db_path):
+ yield None
+ return
+ os.makedirs(path, exist_ok=True)
+ conn = _connect(db_path)
+ try:
+ _ensure_banned_peers_table(conn)
+ yield conn
+ finally:
+ conn.close()Each function can then use this helper, handling the 🤖 Prompt for AI Agents
Comment on lines
+271
to
+320
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. 🚀 Performance & Scalability | 🔵 Trivial | ⚖️ Poor tradeoff Synchronous, blocking sqlite calls invoked from async CLI context.
🤖 Prompt for AI Agents |
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Legacy JSON helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| import re | ||
| from enum import Enum, auto | ||
|
|
||
|
|
||
| class ValidationStatus(Enum): | ||
| VALID = auto() | ||
| INVALID = auto() | ||
| FAILED = auto() | ||
| MALFORMED = auto() | ||
|
|
||
|
|
||
| def is_valid_receiver(receiver): | ||
| return bool(re.fullmatch(r"[0-9a-fA-F]{40}|[0-9a-fA-F]{64}", receiver)) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: StabilityNexus/MiniChain
Length of output: 162
🏁 Script executed:
Repository: StabilityNexus/MiniChain
Length of output: 28629
🏁 Script executed:
Repository: StabilityNexus/MiniChain
Length of output: 50381
🏁 Script executed:
Repository: StabilityNexus/MiniChain
Length of output: 12428
Wire banned-peer checks into the P2P path.
ban_peer()only persists to SQLite;minichain/p2p.pynever callsis_peer_banned(), so a banned peer can remain connected and reconnect normally.🤖 Prompt for AI Agents