Skip to content

Commit

Permalink
check whether accounts and snapshots are using the same path (#1853)
Browse files Browse the repository at this point in the history
* check whether accounts and snapshots are using the same path

* patch and update test

* remove new line

* align
  • Loading branch information
yihau authored Jun 27, 2024
1 parent 1d64076 commit 7117ed9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ thiserror = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
assert_cmd = { workspace = true }
predicates = { workspace = true }
solana-account-decoder = { workspace = true }
solana-inline-spl = { workspace = true }
solana-runtime = { workspace = true, features = ["dev-context-only-utils"] }
spl-token-2022 = { workspace = true, features = ["no-entrypoint"] }
tempfile = { workspace = true }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = { workspace = true }
Expand Down
42 changes: 28 additions & 14 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,23 @@ pub fn main() {
} else {
&ledger_path
};
let snapshots_dir = fs::canonicalize(snapshots_dir).unwrap_or_else(|err| {
eprintln!(
"Failed to canonicalize snapshots path '{}': {err}",
snapshots_dir.display(),
);
exit(1);
});
if account_paths
.iter()
.any(|account_path| account_path == &snapshots_dir)
{
eprintln!(
"Failed: The --accounts and --snapshots paths must be unique since they \
both create 'snapshots' subdirectories, otherwise there may be collisions",
);
exit(1);
}

let bank_snapshots_dir = snapshots_dir.join("snapshots");
fs::create_dir_all(&bank_snapshots_dir).unwrap_or_else(|err| {
Expand All @@ -1603,13 +1620,12 @@ pub fn main() {
exit(1);
});

let full_snapshot_archives_dir = PathBuf::from(
let full_snapshot_archives_dir =
if let Some(full_snapshot_archive_path) = matches.value_of("full_snapshot_archive_path") {
Path::new(full_snapshot_archive_path)
PathBuf::from(full_snapshot_archive_path)
} else {
snapshots_dir
},
);
snapshots_dir.clone()
};
fs::create_dir_all(&full_snapshot_archives_dir).unwrap_or_else(|err| {
eprintln!(
"Failed to create full snapshot archives directory '{}': {err}",
Expand All @@ -1618,15 +1634,13 @@ pub fn main() {
exit(1);
});

let incremental_snapshot_archives_dir = PathBuf::from(
if let Some(incremental_snapshot_archive_path) =
matches.value_of("incremental_snapshot_archive_path")
{
Path::new(incremental_snapshot_archive_path)
} else {
snapshots_dir
},
);
let incremental_snapshot_archives_dir = if let Some(incremental_snapshot_archive_path) =
matches.value_of("incremental_snapshot_archive_path")
{
PathBuf::from(incremental_snapshot_archive_path)
} else {
snapshots_dir.clone()
};
fs::create_dir_all(&incremental_snapshot_archives_dir).unwrap_or_else(|err| {
eprintln!(
"Failed to create incremental snapshot archives directory '{}': {err}",
Expand Down
36 changes: 36 additions & 0 deletions validator/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use {
assert_cmd::prelude::*,
solana_sdk::signer::keypair::{write_keypair_file, Keypair},
std::process::Command,
tempfile::TempDir,
};

#[test]
fn test_use_the_same_path_for_accounts_and_snapshots() {
let temp_dir = TempDir::new().unwrap();
let temp_dir_path = temp_dir.path();

let id_json_path = temp_dir_path.join("id.json");
let id_json_str = id_json_path.to_str().unwrap();

let keypair = Keypair::new();
write_keypair_file(&keypair, id_json_str).unwrap();

let temp_dir_str = temp_dir_path.to_str().unwrap();

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
cmd.args([
"--identity",
id_json_str,
"--log",
"-",
"--no-voting",
"--accounts",
temp_dir_str,
"--snapshots",
temp_dir_str,
]);
cmd.assert().failure().stderr(predicates::str::contains(
"The --accounts and --snapshots paths must be unique",
));
}

0 comments on commit 7117ed9

Please sign in to comment.