Skip to content

feat: change topic str follow the lean spec#227

Merged
g11tech merged 7 commits intoblockblaz:mainfrom
GrapeBaBa:spec_topic
Sep 23, 2025
Merged

feat: change topic str follow the lean spec#227
g11tech merged 7 commits intoblockblaz:mainfrom
GrapeBaBa:spec_topic

Conversation

@GrapeBaBa
Copy link
Copy Markdown
Member

This pull request introduces significant improvements and refactoring to the gossip topic handling and network initialization logic in the networking and CLI layers. The core changes revolve around making gossip topics more structured and type-safe, passing network names explicitly, and improving memory management and deinitialization for dynamically allocated resources.

Key changes:

Gossip Topic Refactor and Type Safety

  • Replaced the old GossipTopic enum with a new GossipTopic struct, which now includes kind, encoding, and network fields, making topics more explicit and type-safe. Added encoding/decoding logic for topics and a new GossipEncoding enum. (pkgs/network/src/interface.zig) [1] [2]
  • Updated all usages of gossip topics throughout the codebase to work with the new struct, including topic creation, encoding, decoding, and handler registration. (pkgs/network/src/interface.zig, pkgs/network/src/ethlibp2p.zig) [1] [2] [3] [4] [5] [6]

Network Initialization and Memory Management

  • Modified network initialization (EthLibp2p.init) to require a network_name parameter, which is now passed and managed explicitly from CLI and node code. The network name is used for topic construction and is properly freed in deinit. (pkgs/cli/src/main.zig, pkgs/cli/src/node.zig, pkgs/network/src/ethlibp2p.zig) [1] [2] [3] [4]
  • Improved memory management by ensuring that all dynamically allocated resources, including network names and topic strings, are freed correctly on deinitialization. Added deinit methods where necessary. (pkgs/network/src/ethlibp2p.zig, pkgs/network/src/interface.zig, pkgs/configs/src/lib.zig) [1] [2] [3]

CLI and Node Changes

  • Updated CLI and node startup logic to construct and pass the network name to the network layer, and added comments to clarify ownership transfer of configuration objects. (pkgs/cli/src/main.zig, pkgs/cli/src/node.zig, pkgs/configs/src/lib.zig) [1] [2] [3]

FFI and Rust Bridge Updates

  • Updated FFI function signatures to handle the new topic string format and ensure proper freeing of topic string memory when releasing network parameters. (pkgs/network/src/ethlibp2p.zig)

These changes collectively make the gossip protocol more robust and extensible, improve the clarity and safety of network configuration, and ensure proper memory management throughout the networking stack.

Fix #195

Copilot AI review requested due to automatic review settings September 23, 2025 08:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors the gossip topic system to follow the lean consensus specification by introducing a structured topic format and improving type safety across the networking layer. The changes replace the simple enum-based topic system with a comprehensive struct-based approach that includes encoding, network name, and proper memory management.

Key changes:

  • Replaced GossipTopic enum with a structured GossipTopic struct containing kind, encoding, and network fields
  • Updated all networking interfaces to use the new topic system with proper encoding/decoding
  • Added explicit network name parameter to network initialization and improved memory management

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
rust/src/libp2p_bridge.rs Updated FFI interface to accept topics string parameter and parse it for gossip subscription
pkgs/types/src/lib.zig Added deinit method to ChainSpec for proper memory cleanup
pkgs/node/src/node.zig Updated gossip subscription to use TopicKind instead of GossipTopic
pkgs/node/src/chain.zig Added chain config deinitialization in BeamChain cleanup
pkgs/network/src/mock.zig Updated mock implementation to use TopicKind in subscribe function
pkgs/network/src/interface.zig Major refactor introducing GossipTopic struct, TopicKind enum, and encoding logic
pkgs/network/src/ethlibp2p.zig Updated to work with new topic system and added network name parameter
pkgs/configs/src/lib.zig Added deinit method to ChainConfig for proper cleanup
pkgs/cli/src/node.zig Updated network initialization to pass network name and handle memory ownership
pkgs/cli/src/main.zig Updated network setup with network name parameter and ownership transfer comments

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +54 to +59
let topics = CStr::from_ptr(topics_str)
.to_string_lossy()
.split(",")
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The topics_str parameter should be validated for null pointer before dereferencing. Add a null check before calling CStr::from_ptr(topics_str) to prevent potential segmentation fault.

Suggested change
let topics = CStr::from_ptr(topics_str)
.to_string_lossy()
.split(",")
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
let topics = if topics_str.is_null() {
Vec::new()
} else {
CStr::from_ptr(topics_str)
.to_string_lossy()
.split(",")
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
};

Copilot uses AI. Check for mistakes.
const Self = @This();
pub fn init(allocator: Allocator, loop: *xev.Loop, networkId: u32, logger: zeam_utils.ModuleLogger) !Self {
const timer = try xev.Timer.init();
errdefer timer.deinit();
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errdefer for timer.deinit() is misplaced. It should be after the timer initialization succeeds but before any other operations that could fail. Move this line immediately after const timer = try xev.Timer.init();

Copilot uses AI. Check for mistakes.
Comment on lines +191 to +198
errdefer onGossipHandlers.deinit();
for (std.enums.values(TopicKind)) |topic| {
errdefer {
var it = onGossipHandlers.iterator();
while (it.next()) |entry| {
entry.value_ptr.deinit();
}
}
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errdefer cleanup logic is placed inside the for loop, which means it will be executed for each topic iteration. This should be moved outside the loop to only execute once if any iteration fails.

Suggested change
errdefer onGossipHandlers.deinit();
for (std.enums.values(TopicKind)) |topic| {
errdefer {
var it = onGossipHandlers.iterator();
while (it.next()) |entry| {
entry.value_ptr.deinit();
}
}
errdefer {
var it = onGossipHandlers.iterator();
while (it.next()) |entry| {
entry.value_ptr.deinit();
}
onGossipHandlers.deinit();
}
for (std.enums.values(TopicKind)) |topic| {

Copilot uses AI. Check for mistakes.
Signed-off-by: Chen Kai <281165273grape@gmail.com>
}

pub fn subscribe(ptr: *anyopaque, topics: []interface.GossipTopic, handler: interface.OnGossipCbHandler) anyerror!void {
pub fn subscribe(ptr: *anyopaque, topics: []interface.TopicKind, handler: interface.OnGossipCbHandler) anyerror!void {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would let it it remain GossipTopic and the new topic string LeanNetworkTopic

for (std.enums.values(GossipTopic)) |variant| {
if (std.mem.eql(u8, topic, @ptrCast(@tagName(variant)))) {
pub fn decode(encoded: []const u8) !TopicKind {
for (std.enums.values(TopicKind)) |variant| {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment
// TODO the network topic string to topic lookup can be optimized by a map lookup

unless you want to address it now

GrapeBaBa and others added 3 commits September 23, 2025 20:14
Co-authored-by: g11tech <develop@g11tech.io>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
@GrapeBaBa GrapeBaBa requested a review from g11tech September 23, 2025 12:33
Copy link
Copy Markdown
Member

@g11tech g11tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm good work @GrapeBaBa

@g11tech g11tech merged commit 3ef1bbe into blockblaz:main Sep 23, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subscribe to correct topic url strings as per the spec

3 participants