Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions crates/validator/src/keys/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,18 @@ impl ValidatorDefinitions {
reason: "a discovered keystore must name its public key".to_string(),
})?;
let pubkey = format!("0x{}", pubkey.trim_start_matches("0x"));
// Absolute, whatever the directories were given as. A relative
// path in this file is read as relative to the validators
// directory (`ValidatorStore::load`), so one written as it was
// found, relative to the working directory, would be looked for
// under that directory a second time: `--validators-dir keys`
// wrote `keys/0x…/voting-keystore.json` and then failed to start
// on `keys/keys/0x…/voting-keystore.json`.
definitions.push(ValidatorDefinition {
enabled: true,
signing: SigningDefinition::LocalKeystore {
voting_keystore_path: keystore_path,
voting_keystore_password_path: secrets_dir.join(&pubkey),
voting_keystore_path: absolute(&keystore_path)?,
voting_keystore_password_path: absolute(&secrets_dir.join(&pubkey))?,
},
voting_public_key: pubkey,
suggested_fee_recipient: None,
Expand Down Expand Up @@ -533,6 +540,15 @@ impl ValidatorDefinitions {
}
}

/// `path` made absolute against the working directory, without touching the
/// filesystem: no symlink is followed, and the path need not exist.
fn absolute(path: &Path) -> Result<PathBuf> {
std::path::absolute(path).map_err(|source| Error::Io {
path: path.display().to_string(),
source,
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -578,6 +594,55 @@ mod tests {
assert!(definitions.0.iter().all(|definition| definition.enabled));
}

/// Directories given relative to the working directory are written to
/// the file as absolute paths. A relative path there is read against the
/// validators directory, so writing them as found pointed the loader at
/// `<validators-dir>/<validators-dir>/0x…` and the client did not start.
#[test]
fn keystores_discovered_under_relative_directories_are_written_absolute() {
// Under the working directory, so that a relative path reaches them.
// `tempfile` hands the directories back absolute, hence the strip.
let here = std::env::current_dir().unwrap().canonicalize().unwrap();
let under_here = |prefix: &str| {
let dir = tempfile::Builder::new()
.prefix(prefix)
.tempdir_in(".")
.unwrap();
let absolute = dir.path().canonicalize().unwrap();
let relative = absolute.strip_prefix(&here).unwrap().to_path_buf();
(dir, relative)
};
let (_validators_dir, validators) = under_here("relative-validators-");
let (_secrets_dir, secrets) = under_here("relative-secrets-");
assert!(validators.is_relative() && secrets.is_relative());
write_keystore(&validators, "aa");

let written = ValidatorDefinitions::discover_if_absent(&validators, &secrets).unwrap();
assert_eq!(written, 1);

let definitions = ValidatorDefinitions::open(&validators).unwrap();
let SigningDefinition::LocalKeystore {
voting_keystore_path,
voting_keystore_password_path,
} = &definitions.0[0].signing
else {
panic!("a local keystore");
};
assert!(voting_keystore_path.is_absolute());
assert!(
voting_keystore_path.is_file(),
"it names the keystore found"
);
assert_eq!(
voting_keystore_password_path,
&std::path::absolute(&secrets).unwrap().join("0xaa")
);
// What the loader does with a path from the file: an absolute one is
// taken as it is, where the relative one was joined onto the
// validators directory again.
assert_eq!(&validators.join(voting_keystore_path), voting_keystore_path);
}

#[test]
fn an_existing_definitions_file_is_never_rescanned() {
let validators = tempfile::tempdir().unwrap();
Expand Down