Skip to content

Commit

Permalink
Pass around []const T instead of ArrayList &
Browse files Browse the repository at this point in the history
also add better toString methods to SocketAddr
  • Loading branch information
InKryption committed Jun 10, 2024
1 parent 11a3dd3 commit 5c71adb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/accountsdb/download.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ const PeerSearchResult = struct {
pub fn findPeersToDownloadFromAssumeCapacity(
allocator: std.mem.Allocator,
table: *const GossipTable,
contact_infos: []ContactInfo,
contact_infos: []const ContactInfo,
my_shred_version: usize,
my_pubkey: Pubkey,
blacklist: []Pubkey,
trusted_validators: ?std.ArrayList(Pubkey),
blacklist: []const Pubkey,
trusted_validators: ?[]const Pubkey,
/// `.capacity` must be >= `contact_infos.len`.
/// The arraylist is first cleared, and then the outputs
/// are appended to it.
valid_peers: *std.ArrayList(PeerSnapshotHash),
) !PeerSearchResult {
// clear the list
valid_peers.clearRetainingCapacity();
std.debug.assert(valid_peers.capacity >= contact_infos.len);

const TrustedMapType = std.AutoHashMap(
SlotAndHash, // full snapshot hash
Expand All @@ -62,7 +66,7 @@ pub fn findPeersToDownloadFromAssumeCapacity(
// populate with the hashes of trusted validators
var trusted_count: usize = 0;
// SAFE: the perf is safe because maybe_ is non null only if trusted_validators is non-null
for (trusted_validators.?.items) |trusted_validator| {
for (trusted_validators.?) |trusted_validator| {
const gossip_data = table.get(.{ .SnapshotHashes = trusted_validator }) orelse continue;
const trusted_hashes = gossip_data.value.data.SnapshotHashes;
trusted_count += 1;
Expand Down Expand Up @@ -154,7 +158,7 @@ pub fn downloadSnapshotsFromGossip(
allocator: std.mem.Allocator,
logger: Logger,
// if null, then we trust any peer for snapshot download
maybe_trusted_validators: ?std.ArrayList(Pubkey),
maybe_trusted_validators: ?[]const Pubkey,
gossip_service: *GossipService,
output_dir: []const u8,
min_mb_per_sec: usize,
Expand Down Expand Up @@ -209,8 +213,8 @@ pub fn downloadSnapshotsFromGossip(
defer allocator.free(snapshot_filename);

const rpc_socket = peer.contact_info.getSocket(socket_tag.RPC).?;
const r = rpc_socket.toString();
const rpc_url = r[0][0..r[1]];
const rpc_url_bounded = rpc_socket.toStringBounded();
const rpc_url = rpc_url_bounded.constSlice();

const snapshot_url = try std.fmt.allocPrintZ(allocator, "http://{s}/{s}", .{
rpc_url,
Expand Down Expand Up @@ -520,7 +524,7 @@ test "accounts_db.download: test remove untrusted peers" {
my_shred_version,
my_pubkey,
&.{},
trusted_validators,
trusted_validators.items,
&valid_peers,
);
try std.testing.expectEqual(valid_peers.items.len, 10);
Expand All @@ -535,7 +539,7 @@ test "accounts_db.download: test remove untrusted peers" {
my_shred_version,
my_pubkey,
&.{},
trusted_validators,
trusted_validators.items,
&valid_peers,
);
try std.testing.expectEqual(valid_peers.items.len, 8);
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ fn downloadSnapshot() !void {
try downloadSnapshotsFromGossip(
gpa_allocator,
logger,
trusted_validators,
if (trusted_validators) |trusted| trusted.items else null,
&gossip_service,
snapshot_dir_str,
@intCast(min_mb_per_sec),
Expand Down Expand Up @@ -795,7 +795,7 @@ fn getOrDownloadSnapshots(
try downloadSnapshotsFromGossip(
allocator,
logger,
trusted_validators,
if (trusted_validators) |trusted| trusted.items else null,
gossip_service orelse return error.SnapshotsNotFoundAndNoGossipService,
snapshot_dir_str,
@intCast(min_mb_per_sec),
Expand Down
14 changes: 12 additions & 2 deletions src/net/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,19 @@ pub const SocketAddr = union(enum(u8)) {
/// - integer: length of the string within the array
pub fn toString(self: Self) struct { [53]u8, usize } {
var buf: [53]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
const len = self.toStringBuf(&buf);
return .{ buf, len };
}

pub fn toStringBounded(self: Self) std.BoundedArray(u8, 53) {
var buf: [53]u8 = undefined;
return std.BoundedArray(u8, 53).fromSlice(buf[0..self.toStringBuf(&buf)]) catch unreachable;
}

pub fn toStringBuf(self: Self, buf: *[53]u8) std.math.IntFittingRange(0, 53) {
var stream = std.io.fixedBufferStream(buf);
self.toAddress().format("", .{}, stream.writer()) catch unreachable;
return .{ buf, stream.pos };
return @intCast(stream.pos);
}

pub fn isUnspecified(self: *const Self) bool {
Expand Down

0 comments on commit 5c71adb

Please sign in to comment.