ledger-tool: Condense repeated error handling (#34439)

Several commands call load_and_process_ledger() which can fail in a
number of ways. These callers currently all handle the result in the
same way by matching the return Result:
- The Ok(_) case uses the returned types as normal
- The Err(_) case prints an error message and exits

This error handling is redundant, and a helper could remove the
duplicate code. So, this PR adds a wrapper around that checks the
result and unwraps OR prints error messages and exits.
This commit is contained in:
steviez 2023-12-13 14:50:20 -06:00 committed by GitHub
parent 0b6d939e21
commit 2a67fa8d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 860 additions and 932 deletions

View File

@ -106,6 +106,28 @@ pub fn get_shred_storage_type(ledger_path: &Path, message: &str) -> ShredStorage
}
}
pub fn load_and_process_ledger_or_exit(
arg_matches: &ArgMatches,
genesis_config: &GenesisConfig,
blockstore: Arc<Blockstore>,
process_options: ProcessOptions,
snapshot_archive_path: Option<PathBuf>,
incremental_snapshot_archive_path: Option<PathBuf>,
) -> (Arc<RwLock<BankForks>>, Option<StartingSnapshotHashes>) {
load_and_process_ledger(
arg_matches,
genesis_config,
blockstore,
process_options,
snapshot_archive_path,
incremental_snapshot_archive_path,
)
.unwrap_or_else(|err| {
eprintln!("Exiting. Failed to load and process ledger: {err}");
exit(1);
})
}
pub fn load_and_process_ledger(
arg_matches: &ArgMatches,
genesis_config: &GenesisConfig,

File diff suppressed because it is too large Load Diff

View File

@ -119,18 +119,14 @@ fn load_blockstore(ledger_path: &Path, arg_matches: &ArgMatches<'_>) -> Arc<Bank
force_update_to_open,
enforce_ulimit_nofile,
);
let (bank_forks, ..) = load_and_process_ledger(
let (bank_forks, ..) = load_and_process_ledger_or_exit(
arg_matches,
&genesis_config,
Arc::new(blockstore),
process_options,
snapshot_archive_path,
incremental_snapshot_archive_path,
)
.unwrap_or_else(|err| {
eprintln!("Ledger loading failed: {err:?}");
exit(1);
});
);
let bank = bank_forks.read().unwrap().working_bank();
bank
}