-
-
Notifications
You must be signed in to change notification settings - Fork 16
fix: address CodeRabbit comments for pr #46 #54
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
Merged
Zahnentferner
merged 6 commits into
StabilityNexus:main
from
SIDDHANTCOOKIE:fix-code-rabbits-comments
Mar 20, 2026
+64
−22
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9717a95
address CodeRabbit networking comments
SIDDHANTCOOKIE feb1026
fixed IPv6 peer
SIDDHANTCOOKIE 6184794
harden sync payload checks and move initial funding after bootstrap
SIDDHANTCOOKIE d72b23f
Merge branch 'main' into fix-code-rabbits-comments
SIDDHANTCOOKIE 9b7dac7
fix help box alignment
SIDDHANTCOOKIE f83bad7
Merge branch 'fix-code-rabbits-comments' of https://github.com/SIDDHA…
SIDDHANTCOOKIE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,8 @@ | |
| logger = logging.getLogger(__name__) | ||
|
|
||
| BURN_ADDRESS = "0" * 40 | ||
| TRUSTED_PEERS = set() | ||
| LOCALHOST_PEERS = {"127.0.0.1", "::1", "localhost", "0:0:0:0:0:0:0:1"} | ||
|
|
||
|
|
||
| # ────────────────────────────────────────────── | ||
|
|
@@ -90,6 +92,11 @@ def mine_and_process_block(chain, mempool, miner_pk): | |
| return mined_block | ||
| else: | ||
| logger.error("❌ Block rejected by chain") | ||
| restored = 0 | ||
| for tx in pending_txs: | ||
| if mempool.add_transaction(tx): | ||
| restored += 1 | ||
| logger.info("Mempool: Restored %d/%d txs after rejection", restored, len(pending_txs)) | ||
|
SIDDHANTCOOKIE marked this conversation as resolved.
|
||
| return None | ||
|
|
||
|
|
||
|
|
@@ -103,15 +110,31 @@ def make_network_handler(chain, mempool): | |
| async def handler(data): | ||
| msg_type = data.get("type") | ||
| payload = data.get("data") | ||
| peer_addr = data.get("_peer_addr", "unknown") | ||
|
|
||
| if msg_type == "sync": | ||
| peer_host = peer_addr.rsplit(":", 1)[0] if ":" in peer_addr else peer_addr | ||
| peer_host = peer_host.strip("[]") | ||
| is_trusted = peer_addr in TRUSTED_PEERS or peer_host in TRUSTED_PEERS | ||
| is_localhost = peer_host in LOCALHOST_PEERS | ||
| if chain.state.accounts and not (is_trusted or is_localhost): | ||
| logger.warning("🔒 Rejected sync from untrusted peer %s", peer_addr) | ||
| return | ||
|
coderabbitai[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| # Merge remote state into local state (for accounts we don't have yet) | ||
| remote_accounts = payload.get("accounts", {}) | ||
| remote_accounts = payload.get("accounts") if isinstance(payload, dict) else None | ||
| if not isinstance(remote_accounts, dict): | ||
| logger.warning("🔒 Rejected sync from %s with invalid accounts payload", peer_addr) | ||
| return | ||
|
|
||
| for addr, acc in remote_accounts.items(): | ||
| if not isinstance(acc, dict): | ||
| logger.warning("🔒 Skipping malformed account %r from %s", addr, peer_addr) | ||
| continue | ||
| if addr not in chain.state.accounts: | ||
| chain.state.accounts[addr] = acc | ||
| logger.info("🔄 Synced account %s... (balance=%d)", addr[:12], acc.get("balance", 0)) | ||
| logger.info("🔄 State sync complete — %d accounts", len(chain.state.accounts)) | ||
| logger.info("🔄 Accepted state sync from %s — %d accounts", peer_addr, len(chain.state.accounts)) | ||
|
|
||
| elif msg_type == "tx": | ||
| tx = Transaction(**payload) | ||
|
|
@@ -156,15 +179,15 @@ async def handler(data): | |
| ╔════════════════════════════════════════════════╗ | ||
| ║ MiniChain Commands ║ | ||
| ╠════════════════════════════════════════════════╣ | ||
| ║ balance — show all balances ║ | ||
| ║ send <to> <amount> — send coins ║ | ||
| ║ mine — mine a block ║ | ||
| ║ peers — show connected peers ║ | ||
| ║ connect <host:port> — connect to a peer ║ | ||
| ║ address — show your public key ║ | ||
| ║ chain — show chain summary ║ | ||
| ║ help — show this help ║ | ||
| ║ quit — shut down ║ | ||
| ║ balance - show all balances ║ | ||
| ║ send <to> <amount> - send coins ║ | ||
| ║ mine - mine a block ║ | ||
| ║ peers - show connected peers ║ | ||
| ║ connect <host>:<port> - connect to a peer ║ | ||
|
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. There is a misalignment here. |
||
| ║ address - show your public key ║ | ||
| ║ chain - show chain summary ║ | ||
| ║ help - show this help ║ | ||
| ║ quit - shut down ║ | ||
| ╚════════════════════════════════════════════════╝ | ||
| """ | ||
|
|
||
|
|
@@ -244,7 +267,11 @@ async def cli_loop(sk, pk, chain, mempool, network): | |
| except ValueError: | ||
| print(" Invalid format. Use host:port") | ||
| continue | ||
| await network.connect_to_peer(host, port) | ||
| success = await network.connect_to_peer(host, port) | ||
| if success: | ||
| print(f" Connected to {host}:{port}") | ||
| else: | ||
| print(f" Failed to connect to {host}:{port}") | ||
|
|
||
| # ── address ── | ||
| elif cmd == "address": | ||
|
|
@@ -311,14 +338,9 @@ async def on_peer_connected(writer): | |
| await writer.drain() | ||
| logger.info("🔄 Sent state sync to new peer") | ||
|
|
||
| network._on_peer_connected = on_peer_connected | ||
| network.set_on_peer_connected(on_peer_connected) | ||
|
|
||
| await network.start(port=port) | ||
|
|
||
| # Fund this node's wallet so it can transact in the demo | ||
| if fund > 0: | ||
| chain.state.credit_mining_reward(pk, reward=fund) | ||
| logger.info("💰 Funded %s... with %d coins", pk[:12], fund) | ||
| await network.start(port=port, host=host) | ||
|
|
||
| # Connect to a seed peer if requested | ||
| if connect_to: | ||
|
|
@@ -328,6 +350,14 @@ async def on_peer_connected(writer): | |
| except ValueError: | ||
| logger.error("Invalid --connect format. Use host:port") | ||
|
|
||
| # Fund this node's wallet so it can transact in the demo | ||
| if fund > 0: | ||
| chain.state.credit_mining_reward(pk, reward=fund) | ||
| logger.info("💰 Funded %s... with %d coins", pk[:12], fund) | ||
|
|
||
| # Nonce counter kept as a mutable list so the CLI closure can mutate it | ||
| nonce_counter = [0] | ||
|
SIDDHANTCOOKIE marked this conversation as resolved.
|
||
|
|
||
| try: | ||
| await cli_loop(sk, pk, chain, mempool, network) | ||
| finally: | ||
|
|
@@ -344,6 +374,7 @@ async def on_peer_connected(writer): | |
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="MiniChain Node — Testnet Demo") | ||
| parser.add_argument("--host", type=str, default="127.0.0.1", help="Host/IP to bind the P2P server (default: 127.0.0.1)") | ||
| parser.add_argument("--port", type=int, default=9000, help="TCP port to listen on (default: 9000)") | ||
| parser.add_argument("--connect", type=str, default=None, help="Peer address to connect to (host:port)") | ||
| parser.add_argument("--fund", type=int, default=100, help="Initial coins to fund this wallet (default: 100)") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.