Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async def cli_loop(sk, pk, chain, mempool, network):
# Main entry point
# ──────────────────────────────────────────────

async def run_node(port: int, connect_to: str | None, fund: int, datadir: str | None):
async def run_node(port: int, host: str, connect_to: str | None, fund: int, datadir: str | None):
"""Boot the node, optionally connect to a peer, then enter the CLI."""
sk, pk = create_wallet()

Expand Down Expand Up @@ -326,7 +326,7 @@ async def on_peer_connected(writer):
await writer.drain()
logger.info("🔄 Sent state sync to new peer")

network.set_on_peer_connected(on_peer_connected)
network._on_peer_connected = on_peer_connected
Comment thread
quickquickcode marked this conversation as resolved.
Outdated

await network.start(port=port, host=host)

Expand Down Expand Up @@ -373,7 +373,7 @@ def main():
)

try:
asyncio.run(run_node(args.port, args.connect, args.fund, args.datadir))
asyncio.run(run_node(args.port, args.host, args.connect, args.fund, args.datadir))
except KeyboardInterrupt:
print("\nNode shut down.")

Expand Down
10 changes: 8 additions & 2 deletions minichain/p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def register_handler(self, handler_callback):
raise ValueError("handler_callback must be callable")
self._handler_callback = handler_callback

async def start(self, port: int = 9000):
async def start(self, port: int = 9000, host: str = "127.0.0.1"):
"""Start listening for incoming peer connections on the given port."""
self._port = port
self._server = await asyncio.start_server(
Expand Down Expand Up @@ -206,7 +206,13 @@ def _validate_block_payload(self, payload):
def _validate_message(self, message):
if not isinstance(message, dict):
return False
if set(message) != {"type", "data"}:
# Allow _peer_addr field added by _listen_to_peer
required_fields = {"type", "data"}
if not required_fields.issubset(set(message)):
return False
# Reject messages with unexpected fields (except _peer_addr)
allowed_fields = {"type", "data", "_peer_addr"}
if not set(message).issubset(allowed_fields):
Comment thread
quickquickcode marked this conversation as resolved.
Outdated
return False
Comment on lines +213 to 217
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

Consider simplifying to a single equality check.

The bidirectional subset checks (required_fields.issubset(set(message)) AND set(message).issubset(required_fields)) are logically equivalent to set(message) == required_fields. A single equality check would be more direct.

This properly addresses the past review feedback to keep _peer_addr out of the wire-schema validator.

🔧 Proposed simplification
     def _validate_message(self, message):
         if not isinstance(message, dict):
             return False
-        required_fields = {"type", "data"}
-        if not required_fields.issubset(set(message)):
-            return False
-        if not set(message).issubset(required_fields):
+        if set(message) != {"type", "data"}:
             return False
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@minichain/p2p.py` around lines 213 - 217, Replace the two bidirectional
subset checks with a single equality check: ensure the validator uses
required_fields = {"type", "data"} and then return False if set(message) !=
required_fields; update the code around the current checks (the variables
required_fields and message in this scope) so the logic is a single equality
comparison and continues to exclude _peer_addr from the wire-schema validation.


msg_type = message.get("type")
Expand Down
Loading