Skip to content

Commit 252cfad

Browse files
Ubuntuyewman
authored andcommitted
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 6a8313b commit 252cfad

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
@@ -130,20 +130,20 @@ pub fn main() !void {
130130
params.repair.apply(&current_config);
131131
current_config.shred_network.dump_shred_tracker = params.repair.dump_shred_tracker;
132132
current_config.shred_network.log_finished_slots = params.repair.log_finished_slots;
133-
// Derive snapshot_dir from validator_dir if using default
134-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
135-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
136-
} else {
137-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
138-
}
133+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
134+
gpa,
135+
params.snapshot_dir,
136+
"accounts_db",
137+
);
139138
current_config.genesis_file_path = params.genesis_file_path;
140139
params.accountsdb_base.apply(&current_config);
141140
params.accountsdb_download.apply(&current_config);
142141
params.geyser.apply(&current_config);
143-
// Derive geyser.pipe_path from validator_dir if using default
144-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
145-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
146-
}
142+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
143+
gpa,
144+
current_config.geyser.pipe_path,
145+
"geyser.pipe",
146+
);
147147
current_config.replay_threads = params.replay_threads;
148148
current_config.disable_consensus = params.disable_consensus;
149149
current_config.stop_at_slot = params.stop_at_slot;
@@ -157,20 +157,20 @@ pub fn main() !void {
157157
params.gossip_base.apply(&current_config);
158158
params.gossip_node.apply(&current_config);
159159
params.repair.apply(&current_config);
160-
// Derive snapshot_dir from validator_dir if using default
161-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
162-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
163-
} else {
164-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
165-
}
160+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
161+
gpa,
162+
params.snapshot_dir,
163+
"accounts_db",
164+
);
166165
current_config.genesis_file_path = params.genesis_file_path;
167166
params.accountsdb_base.apply(&current_config);
168167
params.accountsdb_download.apply(&current_config);
169168
params.geyser.apply(&current_config);
170-
// Derive geyser.pipe_path from validator_dir if using default
171-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
172-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
173-
}
169+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
170+
gpa,
171+
current_config.geyser.pipe_path,
172+
"geyser.pipe",
173+
);
174174
current_config.replay_threads = params.replay_threads;
175175
current_config.disable_consensus = params.disable_consensus;
176176
current_config.stop_at_slot = params.stop_at_slot;
@@ -192,31 +192,30 @@ pub fn main() !void {
192192
},
193193
.snapshot_download => |params| {
194194
current_config.shred_version = params.shred_version;
195-
// Derive snapshot_dir from validator_dir if using default
196-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
197-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
198-
} else {
199-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
200-
}
195+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
196+
gpa,
197+
params.snapshot_dir,
198+
"accounts_db",
199+
);
201200
params.accountsdb_download.apply(&current_config);
202201
params.gossip_base.apply(&current_config);
203202
try downloadSnapshot(gpa, gossip_gpa, current_config);
204203
},
205204
.snapshot_validate => |params| {
206-
// Derive snapshot_dir from validator_dir if using default
207-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
208-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
209-
} else {
210-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
211-
}
205+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
206+
gpa,
207+
params.snapshot_dir,
208+
"accounts_db",
209+
);
212210
current_config.genesis_file_path = params.genesis_file_path;
213211
params.accountsdb_base.apply(&current_config);
214212
current_config.gossip.cluster = params.gossip_cluster;
215213
params.geyser.apply(&current_config);
216-
// Derive geyser.pipe_path from validator_dir if using default
217-
if (std.mem.eql(u8, current_config.geyser.pipe_path, sig.VALIDATOR_DIR ++ "geyser.pipe")) {
218-
current_config.geyser.pipe_path = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "geyser.pipe" });
219-
}
214+
current_config.geyser.pipe_path = try current_config.derivePathFromValidatorDir(
215+
gpa,
216+
current_config.geyser.pipe_path,
217+
"geyser.pipe",
218+
);
220219
try validateSnapshot(gpa, current_config);
221220
},
222221
.snapshot_create => |params| {
@@ -227,25 +226,23 @@ pub fn main() !void {
227226
@panic("TODO: support snapshot creation");
228227
},
229228
.print_manifest => |params| {
230-
// Derive snapshot_dir from validator_dir if using default
231-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
232-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
233-
} else {
234-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
235-
}
229+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
230+
gpa,
231+
params.snapshot_dir,
232+
"accounts_db",
233+
);
236234
try printManifest(gpa, current_config);
237235
},
238236
.leader_schedule => |params| {
239237
current_config.shred_version = params.shred_version;
240238
current_config.leader_schedule_path = params.leader_schedule;
241239
params.gossip_base.apply(&current_config);
242240
params.gossip_node.apply(&current_config);
243-
// Derive snapshot_dir from validator_dir if using default
244-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
245-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
246-
} else {
247-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
248-
}
241+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
242+
gpa,
243+
params.snapshot_dir,
244+
"accounts_db",
245+
);
249246
current_config.genesis_file_path = params.genesis_file_path;
250247
params.accountsdb_base.apply(&current_config);
251248
params.accountsdb_download.apply(&current_config);
@@ -264,12 +261,12 @@ pub fn main() !void {
264261
.mock_rpc_server => |params| {
265262
params.gossip_base.apply(&current_config);
266263
params.gossip_node.apply(&current_config);
267-
// Derive snapshot_dir from validator_dir if using default
268-
if (std.mem.eql(u8, params.snapshot_dir, sig.VALIDATOR_DIR ++ "accounts_db")) {
269-
current_config.accounts_db.snapshot_dir = try std.fs.path.join(gpa, &.{ current_config.validator_dir, "accounts_db" });
270-
} else {
271-
current_config.accounts_db.snapshot_dir = params.snapshot_dir;
272-
}
264+
265+
current_config.accounts_db.snapshot_dir = try current_config.derivePathFromValidatorDir(
266+
gpa,
267+
params.snapshot_dir,
268+
"accounts_db",
269+
);
273270
current_config.genesis_file_path = params.genesis_file_path;
274271
params.accountsdb_base.apply(&current_config);
275272
params.accountsdb_download.apply(&current_config);
@@ -400,8 +397,10 @@ const Cmd = struct {
400397
.alias = .d,
401398
.default_value = sig.VALIDATOR_DIR,
402399
.config = .string,
403-
.help = "base directory for all validator data (ledger, accounts_db, geyser pipe). " ++
404-
"Subdirectory paths are derived from this base unless explicitly overridden.",
400+
.help =
401+
\\base directory for all validator data (ledger, accounts_db, geyser pipe).
402+
\\Subdirectory paths are derived from this base unless explicitly overridden.
403+
,
405404
},
406405
},
407406
};
@@ -801,8 +800,10 @@ const Cmd = struct {
801800
.alias = .none,
802801
.default_value = sig.VALIDATOR_DIR ++ "geyser.pipe",
803802
.config = .string,
804-
.help = "path to the geyser pipe. " ++
805-
"Defaults to <validator-dir>/geyser.pipe. Overrides --validator-dir for this path.",
803+
.help =
804+
\\path to the geyser pipe.
805+
\\Defaults to <validator-dir>/geyser.pipe. Overrides --validator-dir for this path.
806+
,
806807
},
807808
.writer_fba_bytes = .{
808809
.kind = .named,
@@ -1259,15 +1260,21 @@ const Cmd = struct {
12591260
.alias = .none,
12601261
.default_value = null,
12611262
.config = {},
1262-
.help = "The first slot to retain (inclusive). Defaults to min slot in ledger.",
1263+
.help =
1264+
\\The first slot to retain (inclusive).
1265+
\\Defaults to min slot in ledger.
1266+
,
12631267
},
12641268
.end_slot = .{
12651269
.kind = .named,
12661270
.name_override = "end-slot",
12671271
.alias = .none,
12681272
.default_value = null,
12691273
.config = {},
1270-
.help = "The last slot to retain (inclusive). Defaults to max slot in ledger.",
1274+
.help =
1275+
\\The last slot to retain (inclusive).
1276+
\\Defaults to max slot in ledger.
1277+
,
12711278
},
12721279
},
12731280
},
@@ -1315,9 +1322,23 @@ fn ensureGenesis(
13151322
return try allocator.dupe(u8, provided_path);
13161323
}
13171324

1325+
// If genesis already exists in validator dir, use it
1326+
const existing_path = try std.fs.path.join(
1327+
allocator,
1328+
&.{ cfg.validator_dir, "genesis.bin" },
1329+
);
1330+
errdefer allocator.free(existing_path);
1331+
if (!std.meta.isError(std.fs.accessAbsolute(existing_path, .{}))) {
1332+
logger.info().logf("Using existing genesis file: {s}", .{existing_path});
1333+
return existing_path;
1334+
}
1335+
13181336
// Determine cluster for genesis
13191337
const cluster = try cfg.gossip.getCluster() orelse {
1320-
logger.err().log("No genesis file path provided and no cluster specified. Use --genesis-file-path or --cluster");
1338+
logger.err().log(
1339+
\\No genesis file path provided and no cluster specified.
1340+
\\Use --genesis-file-path or --cluster"
1341+
);
13211342
return error.GenesisPathNotProvided;
13221343
};
13231344

@@ -2332,12 +2353,21 @@ fn ledgerTool(
23322353
const end_slot = retain_params.end_slot orelse highest_slot;
23332354

23342355
if (start_slot > end_slot) {
2335-
logger.err().logf("Invalid range: start-slot ({d}) > end-slot ({d})", .{ start_slot, end_slot });
2356+
logger.err().logf("Invalid range: start-slot ({d}) > end-slot ({d})", .{
2357+
start_slot,
2358+
end_slot,
2359+
});
23362360
return error.InvalidSlotRange;
23372361
}
23382362

2339-
try stdout.print("Current ledger bounds: [{d}, {d}]\n", .{ lowest_slot, highest_slot });
2340-
try stdout.print("Retaining slots in range: [{d}, {d}]\n", .{ start_slot, end_slot });
2363+
try stdout.print("Current ledger bounds: [{d}, {d}]\n", .{
2364+
lowest_slot,
2365+
highest_slot,
2366+
});
2367+
try stdout.print("Retaining slots in range: [{d}, {d}]\n", .{
2368+
start_slot,
2369+
end_slot,
2370+
});
23412371

23422372
// Purge slots before start_slot
23432373
if (start_slot > lowest_slot) {
@@ -2365,7 +2395,10 @@ fn ledgerTool(
23652395
}
23662396
}
23672397

2368-
try stdout.print("Done. Retained slots in range [{d}, {d}]\n", .{ start_slot, end_slot });
2398+
try stdout.print("Done. Retained slots in range [{d}, {d}]\n", .{
2399+
start_slot,
2400+
end_slot,
2401+
});
23692402
},
23702403
}
23712404
}

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)