Skip to content

Commit

Permalink
refactor(repair): Self = @this()
Browse files Browse the repository at this point in the history
use the Self pattern in the new repair code.
  • Loading branch information
dnut committed Apr 8, 2024
1 parent 1389fba commit 6ecb6da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/tvu/repair_message.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ pub const RepairMessage = union(enum(u8)) {
slot: Slot,
},

pub const Tag: type = @typeInfo(@This()).Union.tag_type.?;
pub const Tag: type = @typeInfo(Self).Union.tag_type.?;
const Self = @This();

const MAX_SERIALIZED_SIZE: usize = 160;

pub fn eql(self: *const @This(), other: *const @This()) bool {
pub fn eql(self: *const Self, other: *const Self) bool {
if (!std.mem.eql(u8, @tagName(self.*), @tagName(other.*))) {
return false;
}
Expand All @@ -135,7 +136,7 @@ pub const RepairMessage = union(enum(u8)) {

/// Analogous to `ServeRepair::verify_signed_packet`
pub fn verify(
self: *const @This(),
self: *const Self,
/// bincode serialized data, from which this struct was deserialized
serialized: []u8,
/// to compare to the header. typically is this validator's own pubkey
Expand Down
22 changes: 14 additions & 8 deletions src/tvu/repair_service.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ pub const RepairService = struct {
exit: *Atomic(bool),
slot_to_request: ?u64,

pub fn deinit(self: *@This()) void {
const Self = @This();

pub fn deinit(self: *Self) void {
self.peer_provider.deinit();
}

pub fn run(self: *@This()) !void {
pub fn run(self: *Self) !void {
self.logger.info("starting repair service");
defer self.logger.info("exiting repair service");
while (!self.exit.load(.Unordered)) {
Expand All @@ -48,7 +50,7 @@ pub const RepairService = struct {
}
}

fn initialSnapshotRepair(self: *@This()) !?AddressedRepairRequest {
fn initialSnapshotRepair(self: *Self) !?AddressedRepairRequest {
if (self.slot_to_request == null) return null;
const request: RepairRequest = .{ .HighestShred = .{ self.slot_to_request.?, 0 } };
const maybe_peer = try self.peer_provider.getRandomPeer(self.slot_to_request.?);
Expand All @@ -71,8 +73,10 @@ pub const RepairRequester = struct {
udp_send_socket: *Socket,
logger: Logger,

const Self = @This();

pub fn sendRepairRequest(
self: *const @This(),
self: *const Self,
request: AddressedRepairRequest,
) !void {
const timestamp = std.time.milliTimestamp();
Expand Down Expand Up @@ -134,6 +138,8 @@ pub const RepairPeerProvider = struct {
my_pubkey: Pubkey,
my_shred_version: *const Atomic(u16),

const Self = @This();

const RepairPeers = struct {
insertion_time_secs: u64,
peers: []RepairPeer,
Expand Down Expand Up @@ -161,21 +167,21 @@ pub const RepairPeerProvider = struct {
};
}

pub fn deinit(self: *@This()) void {
pub fn deinit(self: *Self) void {
self.cache.deinit();
}

/// Selects a peer at random from gossip or cache that is expected
/// to be able to handle a repair request for the specified slot.
pub fn getRandomPeer(self: *@This(), slot: Slot) !?RepairPeer {
pub fn getRandomPeer(self: *Self, slot: Slot) !?RepairPeer {
const peers = try self.getPeers(slot);
if (peers.len == 0) return null;
const index = self.rng.intRangeLessThan(usize, 0, peers.len);
return peers[index];
}

/// Tries to get peers that could have the slot. Checks cache, falling back to gossip.
fn getPeers(self: *@This(), slot: Slot) ![]RepairPeer {
fn getPeers(self: *Self, slot: Slot) ![]RepairPeer {
const now: u64 = @intCast(std.time.timestamp());

if (self.cache.get(slot)) |peers| {
Expand All @@ -196,7 +202,7 @@ pub const RepairPeerProvider = struct {
/// This will always acquire the gossip table lock.
/// Instead of using this function, access the cache when possible to avoid contention.
fn getRepairPeersFromGossip(
self: *@This(),
self: *Self,
allocator: Allocator,
slot: Slot,
) error{OutOfMemory}![]RepairPeer {
Expand Down

0 comments on commit 6ecb6da

Please sign in to comment.