Skip to content

Commit 2e9f7e8

Browse files
author
Ubuntu
committed
refactor(config): extract path derivation into reusable method
- Add derivePathFromValidatorDir to config.Cmd for DRY path resolution - Replace 7 snapshot_dir and 3 geyser.pipe_path inline derivations - Auto-detect existing genesis.bin in validator_dir before downloading - Minor formatting improvements for multiline help strings
1 parent bb256c1 commit 2e9f7e8

File tree

4 files changed

+121
-68
lines changed

4 files changed

+121
-68
lines changed

src/cmd.zig

Lines changed: 98 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,20 @@ pub fn main() !void {
129129
params.repair.apply(&current_config);
130130
current_config.shred_network.dump_shred_tracker = params.repair.dump_shred_tracker;
131131
current_config.shred_network.log_finished_slots = params.repair.log_finished_slots;
132-
// Derive snapshot_dir from validator_dir if using default
133-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
134-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
135-
} else {
136-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
137-
}
132+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
133+
gpa,
134+
params.snapshot_dir,
135+
"accounts_db",
136+
);
138137
current_config.genesis_file_path = params.genesis_file_path;
139138
params.accountsdb_base.apply(&current_config);
140139
params.accountsdb_download.apply(&current_config);
141140
params.geyser.apply(&current_config);
142-
// Derive geyser.pipe_path from validator_dir if using default
143-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
144-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
145-
}
141+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
142+
gpa,
143+
current_config.geyser.pipe_path,
144+
"geyser.pipe",
145+
);
146146
current_config.replay_threads = params.replay_threads;
147147
current_config.disable_consensus = params.disable_consensus;
148148
current_config.stop_at_slot = params.stop_at_slot;
@@ -156,20 +156,20 @@ pub fn main() !void {
156156
params.gossip_base.apply(&current_config);
157157
params.gossip_node.apply(&current_config);
158158
params.repair.apply(&current_config);
159-
// Derive snapshot_dir from validator_dir if using default
160-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
161-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
162-
} else {
163-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
164-
}
159+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
160+
gpa,
161+
params.snapshot_dir,
162+
"accounts_db",
163+
);
165164
current_config.genesis_file_path = params.genesis_file_path;
166165
params.accountsdb_base.apply(&current_config);
167166
params.accountsdb_download.apply(&current_config);
168167
params.geyser.apply(&current_config);
169-
// Derive geyser.pipe_path from validator_dir if using default
170-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
171-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
172-
}
168+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
169+
gpa,
170+
current_config.geyser.pipe_path,
171+
"geyser.pipe",
172+
);
173173
current_config.replay_threads = params.replay_threads;
174174
current_config.disable_consensus = params.disable_consensus;
175175
current_config.stop_at_slot = params.stop_at_slot;
@@ -191,31 +191,30 @@ pub fn main() !void {
191191
},
192192
.snapshot_download => |params| {
193193
current_config.shred_version = params.shred_version;
194-
// Derive snapshot_dir from validator_dir if using default
195-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
196-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
197-
} else {
198-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
199-
}
194+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
195+
gpa,
196+
params.snapshot_dir,
197+
"accounts_db",
198+
);
200199
params.accountsdb_download.apply(&current_config);
201200
params.gossip_base.apply(&current_config);
202201
try downloadSnapshot(gpa, gossip_gpa, current_config);
203202
},
204203
.snapshot_validate => |params| {
205-
// Derive snapshot_dir from validator_dir if using default
206-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
207-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
208-
} else {
209-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
210-
}
204+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
205+
gpa,
206+
params.snapshot_dir,
207+
"accounts_db",
208+
);
211209
current_config.genesis_file_path = params.genesis_file_path;
212210
params.accountsdb_base.apply(&current_config);
213211
current_config.gossip.cluster = params.gossip_cluster;
214212
params.geyser.apply(&current_config);
215-
// Derive geyser.pipe_path from validator_dir if using default
216-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
217-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
218-
}
213+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
214+
gpa,
215+
current_config.geyser.pipe_path,
216+
"geyser.pipe",
217+
);
219218
try validateSnapshot(gpa, current_config);
220219
},
221220
.snapshot_create => |params| {
@@ -226,25 +225,23 @@ pub fn main() !void {
226225
@panic("TODO: support snapshot creation");
227226
},
228227
.print_manifest => |params| {
229-
// Derive snapshot_dir from validator_dir if using default
230-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
231-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
232-
} else {
233-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
234-
}
228+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
229+
gpa,
230+
params.snapshot_dir,
231+
"accounts_db",
232+
);
235233
try printManifest(gpa, current_config);
236234
},
237235
.leader_schedule => |params| {
238236
current_config.shred_version = params.shred_version;
239237
current_config.leader_schedule_path = params.leader_schedule;
240238
params.gossip_base.apply(&current_config);
241239
params.gossip_node.apply(&current_config);
242-
// Derive snapshot_dir from validator_dir if using default
243-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
244-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
245-
} else {
246-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
247-
}
240+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
241+
gpa,
242+
params.snapshot_dir,
243+
"accounts_db",
244+
);
248245
current_config.genesis_file_path = params.genesis_file_path;
249246
params.accountsdb_base.apply(&current_config);
250247
params.accountsdb_download.apply(&current_config);
@@ -263,12 +260,12 @@ pub fn main() !void {
263260
.mock_rpc_server => |params| {
264261
params.gossip_base.apply(&current_config);
265262
params.gossip_node.apply(&current_config);
266-
// Derive snapshot_dir from validator_dir if using default
267-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
268-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
269-
} else {
270-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
271-
}
263+
264+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
265+
gpa,
266+
params.snapshot_dir,
267+
"accounts_db",
268+
);
272269
current_config.genesis_file_path = params.genesis_file_path;
273270
params.accountsdb_base.apply(&current_config);
274271
params.accountsdb_download.apply(&current_config);
@@ -373,8 +370,10 @@ const Cmd = struct {
373370
.alias = .d,
374371
.default_value = sig.VALIDATOR_DIR,
375372
.config = .string,
376-
.help = "base directory for all validator data (ledger, accounts_db, geyser pipe). " ++
377-
"Subdirectory paths are derived from this base unless explicitly overridden.",
373+
.help =
374+
\\base directory for all validator data (ledger, accounts_db, geyser pipe).
375+
\\Subdirectory paths are derived from this base unless explicitly overridden.
376+
,
378377
},
379378
},
380379
};
@@ -774,8 +773,10 @@ const Cmd = struct {
774773
.alias = .none,
775774
.default_value = sig.VALIDATOR_DIR ++ "geyser.pipe",
776775
.config = .string,
777-
.help = "path to the geyser pipe. " ++
778-
"Defaults to <validator-dir>/geyser.pipe. Overrides --validator-dir for this path.",
776+
.help =
777+
\\path to the geyser pipe.
778+
\\Defaults to <validator-dir>/geyser.pipe. Overrides --validator-dir for this path.
779+
,
779780
},
780781
.writer_fba_bytes = .{
781782
.kind = .named,
@@ -1158,15 +1159,21 @@ const Cmd = struct {
11581159
.alias = .none,
11591160
.default_value = null,
11601161
.config = {},
1161-
.help = "The first slot to retain (inclusive). Defaults to min slot in ledger.",
1162+
.help =
1163+
\\The first slot to retain (inclusive).
1164+
\\Defaults to min slot in ledger.
1165+
,
11621166
},
11631167
.end_slot = .{
11641168
.kind = .named,
11651169
.name_override = "end-slot",
11661170
.alias = .none,
11671171
.default_value = null,
11681172
.config = {},
1169-
.help = "The last slot to retain (inclusive). Defaults to max slot in ledger.",
1173+
.help =
1174+
\\The last slot to retain (inclusive).
1175+
\\Defaults to max slot in ledger.
1176+
,
11701177
},
11711178
},
11721179
},
@@ -1206,9 +1213,23 @@ fn ensureGenesis(
12061213
return try allocator.dupe(u8, provided_path);
12071214
}
12081215

1216+
// If genesis already exists in validator dir, use it
1217+
const existing_path = try std.fs.path.join(
1218+
allocator,
1219+
&.{ cfg.validator_dir, "genesis.bin" },
1220+
);
1221+
errdefer allocator.free(existing_path);
1222+
if (!std.meta.isError(std.fs.accessAbsolute(existing_path, .{}))) {
1223+
logger.info().logf("Using existing genesis file: {s}", .{existing_path});
1224+
return existing_path;
1225+
}
1226+
12091227
// Determine cluster for genesis
12101228
const cluster = try cfg.gossip.getCluster() orelse {
1211-
logger.err().log("No genesis file path provided and no cluster specified. Use --genesis-file-path or --cluster");
1229+
logger.err().log(
1230+
\\No genesis file path provided and no cluster specified.
1231+
\\Use --genesis-file-path or --cluster"
1232+
);
12121233
return error.GenesisPathNotProvided;
12131234
};
12141235

@@ -2152,12 +2173,21 @@ fn ledgerTool(
21522173
const end_slot = retain_params.end_slot orelse highest_slot;
21532174

21542175
if (start_slot > end_slot) {
2155-
logger.err().logf("Invalid range: start-slot ({d}) > end-slot ({d})", .{ start_slot, end_slot });
2176+
logger.err().logf("Invalid range: start-slot ({d}) > end-slot ({d})", .{
2177+
start_slot,
2178+
end_slot,
2179+
});
21562180
return error.InvalidSlotRange;
21572181
}
21582182

2159-
try stdout.print("Current ledger bounds: [{d}, {d}]\n", .{ lowest_slot, highest_slot });
2160-
try stdout.print("Retaining slots in range: [{d}, {d}]\n", .{ start_slot, end_slot });
2183+
try stdout.print("Current ledger bounds: [{d}, {d}]\n", .{
2184+
lowest_slot,
2185+
highest_slot,
2186+
});
2187+
try stdout.print("Retaining slots in range: [{d}, {d}]\n", .{
2188+
start_slot,
2189+
end_slot,
2190+
});
21612191

21622192
// Purge slots before start_slot
21632193
if (start_slot > lowest_slot) {
@@ -2185,7 +2215,10 @@ fn ledgerTool(
21852215
}
21862216
}
21872217

2188-
try stdout.print("Done. Retained slots in range [{d}, {d}]\n", .{ start_slot, end_slot });
2218+
try stdout.print("Done. Retained slots in range [{d}, {d}]\n", .{
2219+
start_slot,
2220+
end_slot,
2221+
});
21892222
},
21902223
}
21912224
}

src/config.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ pub const Cmd = struct {
4747
return null;
4848
};
4949
}
50+
51+
/// Derives a path relative to validator_dir if the param equals the default value.
52+
/// This is used to allow paths like snapshot_dir and geyser.pipe_path to be relative
53+
/// to validator_dir when using their default values, while still allowing explicit
54+
/// overrides.
55+
pub fn derivePathFromValidatorDir(
56+
self: Cmd,
57+
allocator: std.mem.Allocator,
58+
param_value: []const u8,
59+
comptime default_suffix: []const u8,
60+
) ![]const u8 {
61+
if (std.mem.eql(u8, param_value, sig.VALIDATOR_DIR ++ default_suffix)) {
62+
return try std.fs.path.join(allocator, &.{ self.validator_dir, default_suffix });
63+
}
64+
return param_value;
65+
}
5066
};
5167

5268
pub const TestTransactionSender = struct {

src/core/genesis_download.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const bzlib_c = @cImport({
1111
});
1212

1313
const Allocator = std.mem.Allocator;
14-
const Cluster = sig.core.Cluster;
15-
const Hash = sig.core.Hash;
1614

1715
const Logger = sig.trace.Logger("core.genesis_download");
1816

src/shred_network/service.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ pub fn start(
8686

8787
// tracker (shared state, internal to Shred Network)
8888
const shred_tracker = try arena.create(BasicShredTracker);
89-
try shred_tracker.init(deps.allocator, conf.root_slot + 1, .from(deps.logger), deps.registry, conf.log_finished_slots);
89+
try shred_tracker.init(
90+
deps.allocator,
91+
conf.root_slot + 1,
92+
.from(deps.logger),
93+
deps.registry,
94+
conf.log_finished_slots,
95+
);
9096
try defers.deferCall(BasicShredTracker.deinit, .{shred_tracker});
9197

9298
// channels (cant use arena as they need to alloc/free frequently &

0 commit comments

Comments
 (0)