Skip to content

Commit 3e8935b

Browse files
committed
Fix incorrect default http token path when datadir flag is present.
1 parent 7e0cdde commit 3e8935b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lighthouse/tests/validator_client.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use sensitive_url::SensitiveUrl;
77
use std::fs::File;
88
use std::io::Write;
99
use std::net::IpAddr;
10+
use std::path::Path;
1011
use std::path::PathBuf;
1112
use std::process::Command;
1213
use std::str::FromStr;
@@ -345,7 +346,7 @@ fn http_store_keystore_passwords_in_secrets_dir_present() {
345346
}
346347

347348
#[test]
348-
fn http_token_path_flag() {
349+
fn http_token_path_flag_present() {
349350
let dir = TempDir::new().expect("Unable to create temporary directory");
350351
CommandLineTest::new()
351352
.flag("http", None)
@@ -359,6 +360,21 @@ fn http_token_path_flag() {
359360
});
360361
}
361362

363+
#[test]
364+
fn http_token_path_default() {
365+
let custom_datadir = "/custom-datadir";
366+
CommandLineTest::new()
367+
.flag("datadir", Some(custom_datadir))
368+
.flag("http", None)
369+
.run()
370+
.with_config(|config| {
371+
assert_eq!(
372+
config.http_api.http_token_path,
373+
Path::new(custom_datadir).join("api-token.txt")
374+
);
375+
});
376+
}
377+
362378
// Tests for Metrics flags.
363379
#[test]
364380
fn metrics_flag() {

validator_client/http_api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub struct Config {
106106

107107
impl Default for Config {
108108
fn default() -> Self {
109+
// This value is always overridden when building config from CLI.
109110
let http_token_path = dirs::home_dir()
110111
.unwrap_or_else(|| PathBuf::from("."))
111112
.join(DEFAULT_ROOT_DIR)

validator_client/src/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,11 @@ impl Config {
314314
config.http_api.store_passwords_in_secrets_dir = true;
315315
}
316316

317-
if cli_args.get_one::<String>("http-token-path").is_some() {
318-
config.http_api.http_token_path = parse_required(cli_args, "http-token-path")
319-
// For backward compatibility, default to the path under the validator dir if not provided.
320-
.unwrap_or_else(|_| config.validator_dir.join(PK_FILENAME));
317+
if let Some(http_token_path) = cli_args.get_one::<String>("http-token-path") {
318+
config.http_api.http_token_path = PathBuf::from(http_token_path);
319+
} else {
320+
// For backward compatibility, default to the path under the validator dir if not provided.
321+
config.http_api.http_token_path = config.validator_dir.join(PK_FILENAME);
321322
}
322323

323324
/*

0 commit comments

Comments
 (0)