Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 6 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ pub fn build(b: *Builder) !void {
.optimize = optimize,
}).module("yaml");

const snappyz = b.dependency("zig_snappy", .{
.target = target,
.optimize = optimize,
}).module("snappyz");

// add zeam-utils
const zeam_utils = b.addModule("@zeam/utils", .{
.target = target,
Expand Down Expand Up @@ -159,6 +164,7 @@ pub fn build(b: *Builder) !void {
zeam_network.addImport("xev", xev);
zeam_network.addImport("ssz", ssz);
zeam_network.addImport("multiformats", multiformats);
zeam_network.addImport("snappyz", snappyz);

// add beam node
const zeam_beam_node = b.addModule("@zeam/node", .{
Expand Down
6 changes: 5 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = .zeam,
.fingerprint = 0x243fd12ce9ee69c2,
.fingerprint = 0x243fd12c24350f93,
.version = "0.0.0",
.dependencies = .{
.ssz = .{
Expand Down Expand Up @@ -31,6 +31,10 @@
.url = "git+https://github.com/kubkon/zig-yaml#a37be441b042d8aa119213f82f715c0dd1019655",
.hash = "zig_yaml-0.1.0-C1161hmEAgBeNkAzDaTpcf-2HBydmS1KpJ6FVfGIZfr_",
},
.zig_snappy = .{
.url = "git+https://github.com/blockblaz/zig-snappy#dd63108bd8e41c7cd799385dd9aa5085314eeafa",
.hash = "zig_snappy-0.0.1-bDFzXpJWAAA3c5bLc8JZK_HCwd0mGLTuFgz4LndjnVao",
},
},
.paths = .{""},
}
34 changes: 26 additions & 8 deletions pkgs/network/src/ethlibp2p.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const zeam_utils = @import("@zeam/utils");

const interface = @import("./interface.zig");
const NetworkInterface = interface.NetworkInterface;
const snappyz = @import("snappyz");

/// Writes failed deserialization bytes to disk for debugging purposes
/// Returns the filename if the file was successfully created, null otherwise
Expand Down Expand Up @@ -55,12 +56,23 @@ export fn handleMsgFromRustBridge(zigHandler: *EthLibp2p, topic_str: [*:0]const
};

const message_bytes: []const u8 = message_ptr[0..message_len];

const uncompressed_message = snappyz.decode(zigHandler.allocator, message_bytes) catch |e| {
zigHandler.logger.err("Error in snappyz decoding the message for topic={s}: {any}", .{ std.mem.span(topic_str), e });
if (writeFailedBytes(message_bytes, "snappyz_decode", zigHandler.allocator, null, zigHandler.logger)) |filename| {
zigHandler.logger.err("Snappyz decode failed - debug file created: {s}", .{filename});
} else {
zigHandler.logger.err("Snappyz decode failed - could not create debug file", .{});
}
return;
};
defer zigHandler.allocator.free(uncompressed_message);
const message: interface.GossipMessage = switch (topic.gossip_topic) {
.block => blockmessage: {
var message_data: types.SignedBeamBlock = undefined;
ssz.deserialize(types.SignedBeamBlock, message_bytes, &message_data, zigHandler.allocator) catch |e| {
ssz.deserialize(types.SignedBeamBlock, uncompressed_message, &message_data, zigHandler.allocator) catch |e| {
zigHandler.logger.err("Error in deserializing the signed block message: {any}", .{e});
if (writeFailedBytes(message_bytes, "block", zigHandler.allocator, null, zigHandler.logger)) |filename| {
if (writeFailedBytes(uncompressed_message, "block", zigHandler.allocator, null, zigHandler.logger)) |filename| {
zigHandler.logger.err("Block deserialization failed - debug file created: {s}", .{filename});
} else {
zigHandler.logger.err("Block deserialization failed - could not create debug file", .{});
Expand All @@ -72,9 +84,9 @@ export fn handleMsgFromRustBridge(zigHandler: *EthLibp2p, topic_str: [*:0]const
},
.vote => votemessage: {
var message_data: types.SignedVote = undefined;
ssz.deserialize(types.SignedVote, message_bytes, &message_data, zigHandler.allocator) catch |e| {
ssz.deserialize(types.SignedVote, uncompressed_message, &message_data, zigHandler.allocator) catch |e| {
zigHandler.logger.err("Error in deserializing the signed vote message: {any}", .{e});
if (writeFailedBytes(message_bytes, "vote", zigHandler.allocator, null, zigHandler.logger)) |filename| {
if (writeFailedBytes(uncompressed_message, "vote", zigHandler.allocator, null, zigHandler.logger)) |filename| {
zigHandler.logger.err("Vote deserialization failed - debug file created: {s}", .{filename});
} else {
zigHandler.logger.err("Vote deserialization failed - could not create debug file", .{});
Expand Down Expand Up @@ -189,19 +201,25 @@ pub const EthLibp2p = struct {
const message = switch (topic.gossip_topic) {
.block => blockbytes: {
var serialized = std.ArrayList(u8).init(self.allocator);
defer serialized.deinit();
try ssz.serialize(types.SignedBeamBlock, data.block, &serialized);

break :blockbytes serialized.items;
break :blockbytes try serialized.toOwnedSlice();
},
.vote => votebytes: {
var serialized = std.ArrayList(u8).init(self.allocator);
defer serialized.deinit();
Comment on lines +204 to +211
Copy link

Copilot AI Sep 27, 2025

Choose a reason for hiding this comment

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

Using defer serialized.deinit() followed by serialized.toOwnedSlice() is incorrect. The toOwnedSlice() method transfers ownership of the memory to the caller and clears the ArrayList, so the subsequent deinit() call will attempt to free already-transferred memory. Remove the defer serialized.deinit() line since toOwnedSlice() handles the cleanup.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

that is wrong, toOwnedSlice() allocated a new memory

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.

actually toOwnedSlice does clear the memory as well so deinit is unnecessary but not harmful either
image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, got it. unnecessary to call

Comment on lines +204 to +211
Copy link

Copilot AI Sep 27, 2025

Choose a reason for hiding this comment

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

Using defer serialized.deinit() followed by serialized.toOwnedSlice() is incorrect. The toOwnedSlice() method transfers ownership of the memory to the caller and clears the ArrayList, so the subsequent deinit() call will attempt to free already-transferred memory. Remove the defer serialized.deinit() line since toOwnedSlice() handles the cleanup.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

that is wrong, toOwnedSlice() allocated a new memory

try ssz.serialize(types.SignedVote, data.vote, &serialized);

break :votebytes serialized.items;
break :votebytes try serialized.toOwnedSlice();
},
};
self.logger.debug("network-{d}:: calling publish_msg_to_rust_bridge with message={any} for data={any}", .{ self.params.networkId, message, data });
publish_msg_to_rust_bridge(self.params.networkId, topic_str.ptr, message.ptr, message.len);
defer self.allocator.free(message);

const compressed_message = try snappyz.encode(self.allocator, message);
defer self.allocator.free(compressed_message);
self.logger.debug("network-{d}:: calling publish_msg_to_rust_bridge with message={any} for data={any}", .{ self.params.networkId, compressed_message, data });
publish_msg_to_rust_bridge(self.params.networkId, topic_str.ptr, compressed_message.ptr, compressed_message.len);
}

pub fn subscribe(ptr: *anyopaque, topics: []interface.GossipTopic, handler: interface.OnGossipCbHandler) anyerror!void {
Expand Down