Skip to content

Commit

Permalink
Simplify getEntrypoints
Browse files Browse the repository at this point in the history
Just use a normal hash set duplicating the entrypoints in the arraylist,
and eschew the custom entrypoint list formatting.
  • Loading branch information
InKryption committed Jun 10, 2024
1 parent 00d76a0 commit f8e0fbd
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const base58 = @import("base58-zig");
const cli = @import("zig-cli");
const network = @import("zig-network");
const helpers = @import("helpers.zig");
const sig = @import("../lib.zig");

const Atomic = std.atomic.Value;
const KeyPair = std.crypto.sign.Ed25519.KeyPair;
Expand Down Expand Up @@ -617,26 +618,10 @@ fn getMyDataFromIpEcho(
}

fn getEntrypoints(logger: Logger) !std.ArrayList(SocketAddr) {
const EntrypointSet = std.ArrayHashMap(void, void, struct {
// zig fmt: off
pub fn hash(_: @This(), _: void) u32 { unreachable; }
pub fn eql(_: @This(), _: void, _: void, _: usize) bool { unreachable; }
// zig fmt: on
}, true);
const EntrypointCtx = struct {
entrypoints: []const SocketAddr,
pub fn hash(_: @This(), entrypoint: SocketAddr) u32 {
const array, const len = entrypoint.toString();
return std.array_hash_map.hashString(array[0..len]);
}
pub fn eql(ctx: @This(), a: SocketAddr, _: void, b_index: usize) bool {
return a.eql(&ctx.entrypoints[b_index]);
}
};

var entrypoints = std.ArrayList(SocketAddr).init(gpa_allocator);
errdefer entrypoints.deinit();

const EntrypointSet = std.AutoArrayHashMap(SocketAddr, void);
var entrypoint_set = EntrypointSet.init(gpa_allocator);
defer entrypoint_set.deinit();

Expand Down Expand Up @@ -677,29 +662,14 @@ fn getEntrypoints(logger: Logger) !std.ArrayList(SocketAddr) {
break :brk socket_addr;
};

const gop = entrypoint_set.getOrPutAssumeCapacityAdapted(socket_addr, EntrypointCtx{ .entrypoints = entrypoints.items });
const gop = entrypoint_set.getOrPutAssumeCapacity(socket_addr);
if (!gop.found_existing) {
entrypoints.appendAssumeCapacity(socket_addr);
}
}

// log entrypoints
const EntrypointsFmt = struct {
entrypoints: []const SocketAddr,

pub fn format(
entrypoints_fmt: @This(),
comptime fmt_str: []const u8,
fmt_options: std.fmt.FormatOptions,
writer: anytype,
) !void {
for (0.., entrypoints_fmt.entrypoints) |i, entrypoint| {
if (i != 0) try writer.writeAll(", ");
try entrypoint.toAddress().format(fmt_str, fmt_options, writer);
}
}
};
logger.infof("entrypoints: {}", .{EntrypointsFmt{ .entrypoints = entrypoints.items }});
logger.infof("entrypoints: {any}", .{entrypoints.items});

return entrypoints;
}
Expand Down

0 comments on commit f8e0fbd

Please sign in to comment.