feat: add Zig peer manager module with NAPI bindings#308
feat: add Zig peer manager module with NAPI bindings#308nazarhussain wants to merge 27 commits intomainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical stability and compatibility issues within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive PeerManager implementation in Zig, including NAPI bindings for Node.js integration. The system manages peer lifecycles, scoring with exponential decay, and subnet-aware prioritization. The review feedback focuses on aligning error handling with the repository's fail-fast style guide by treating out-of-memory conditions as fatal and optimizing HashMap operations in the PeerStore to avoid redundant lookups.
| ) catch |err| switch (err) { | ||
| error.PeerAlreadyExists => return self.actions.items, | ||
| error.OutOfMemory => return err, | ||
| }; |
There was a problem hiding this comment.
The error handling here is redundant. store.addPeer only returns error.PeerAlreadyExists or error.OutOfMemory. Since error.OutOfMemory is a fatal error that should crash the program (per the style guide), you can simplify this to try self.store.addPeer(...) and let the caller handle the OOM or let it propagate.
try self.store.addPeer(peer_id, direction, self.clock_fn(), self.config);
References
- Assertions and error handling: The style guide mandates that assertion failures (and by extension, unrecoverable errors like OOM) should crash the program to downgrade catastrophic bugs into liveness bugs.
| const owned_key = try self.allocator.dupe(u8, peer_id); | ||
| errdefer self.allocator.free(owned_key); |
There was a problem hiding this comment.
The errdefer block here is good, but owned_key is not actually used if put fails. It is better to perform the allocation only after confirming the peer does not exist, or use getOrPut to avoid double-lookup.
const result = try self.peers.getOrPut(peer_id); if (result.found_existing) return error.PeerAlreadyExists; const owned_key = try self.allocator.dupe(u8, peer_id); errdefer self.allocator.free(owned_key); result.key_ptr.* = owned_key;
References
- Efficiency: Avoid redundant lookups in HashMaps by using
getOrPut.
Design spec for the Zig peer manager module covering all three delivery phases: PeerStore, PeerScorer, and full PeerManager. Includes TS source reference archive from Lodestar unstable branch. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- BLOCK-1: Fix checkPingAndStatus — use initial timestamp trick for inbound grace period instead of fabricated disconnect check - BLOCK-2: onConnectionOpen emits send_ping + send_status for outbound - BLOCK-3: onConnectionClose applies cooldown for inbound peers - BLOCK-4: addPeer takes Config for direction-dependent timestamps - FLAG-1: Add HEARTBEAT_INTERVAL_MS and CHECK_PING_STATUS_INTERVAL - FLAG-2: Add negative_gossip_score_ignore_threshold to Config - FLAG-3: Document gossipsub weight derivation formula - FLAG-4: onStatusReceived takes current_slot parameter - FLAG-5: Remove incorrect cooldown from onGoodbye - FLAG-6: Add getGossipScore accessor to PeerScorer - FLAG-7: Fix misleading "prune after decay" comment - NOTE-3: Clarify StatusScore values in sort description - NOTE-5: Move emit_peer_connected to onStatusReceived Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
10 tasks across 6 chunks: types/constants, PeerStore, PeerScorer, relevance + prioritization, PeerManager orchestrator, NAPI bindings + TS integration tests. Bottom-up build order with TDD approach. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…vals Port all score thresholds, peer manager intervals, and prioritization constants from the TypeScript implementation. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Port all shared enums, structs, action types, config, and bitvector helpers from the TypeScript peer manager implementation. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Create module entry point with re-exports and register peer_manager module and test step in the build system. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…d cooldowns Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…lidation Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…connect logic Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…prioritization Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
….zig Add peer_manager to zbuild.zon modules and bindings library imports. Run zbuild sync to regenerate build.zig with correct registration. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Use 4 mid_tolerance reports (-20) instead of 10 (-50) to stay above the ban threshold, which would trigger a cooldown and block decay. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Replace getValueUint64 with getValueInt64 + @intcast (zapi has no u64 getter) - Replace typeOf with typeof (zapi naming convention) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
3acc71e to
f1e8047
Compare
Summary
packages/beacon-node/src/network/peersfrom Lodestar TSArchitecture
Layered modules in
src/peer_manager/:StringHashMap-backed peer data storageAction[]for caller dispatchNAPI bindings in
bindings/napi/peer_manager.zigwith 23 exported functions.Test plan
zig build test:peer_manager)bindings/test/peerManager.test.ts) — requires NAPI buildCurrent Architecture
Zig Transition Architecture