Refactor genesis download/load/check functions (#17276)
* Refactor genesis ingest functions * Consolidate genesis.bin/genesis.tar.bz2 references
This commit is contained in:
22
genesis-utils/Cargo.toml
Normal file
22
genesis-utils/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "solana-genesis-utils"
|
||||
version = "1.7.0"
|
||||
description = "Solana Genesis Utils"
|
||||
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
documentation = "https://docs.rs/solana-download-utils"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
solana-sdk = { path = "../sdk", version = "=1.7.0" }
|
||||
solana-download-utils = { path = "../download-utils", version = "=1.7.0" }
|
||||
solana-runtime = { path = "../runtime", version = "=1.7.0" }
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib"]
|
||||
name = "solana_genesis_utils"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
75
genesis-utils/src/lib.rs
Normal file
75
genesis-utils/src/lib.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use solana_download_utils::download_genesis_if_missing;
|
||||
use solana_runtime::hardened_unpack::unpack_genesis_archive;
|
||||
use solana_sdk::{
|
||||
genesis_config::{GenesisConfig, DEFAULT_GENESIS_ARCHIVE},
|
||||
hash::Hash,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
fn check_genesis_hash(
|
||||
genesis_config: &GenesisConfig,
|
||||
expected_genesis_hash: Option<Hash>,
|
||||
) -> Result<(), String> {
|
||||
let genesis_hash = genesis_config.hash();
|
||||
|
||||
if let Some(expected_genesis_hash) = expected_genesis_hash {
|
||||
if expected_genesis_hash != genesis_hash {
|
||||
return Err(format!(
|
||||
"Genesis hash mismatch: expected {} but downloaded genesis hash is {}",
|
||||
expected_genesis_hash, genesis_hash,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_local_genesis(
|
||||
ledger_path: &std::path::Path,
|
||||
expected_genesis_hash: Option<Hash>,
|
||||
) -> Result<GenesisConfig, String> {
|
||||
let existing_genesis = GenesisConfig::load(&ledger_path)
|
||||
.map_err(|err| format!("Failed to load genesis config: {}", err))?;
|
||||
check_genesis_hash(&existing_genesis, expected_genesis_hash)?;
|
||||
|
||||
Ok(existing_genesis)
|
||||
}
|
||||
|
||||
pub fn download_then_check_genesis_hash(
|
||||
rpc_addr: &SocketAddr,
|
||||
ledger_path: &std::path::Path,
|
||||
expected_genesis_hash: Option<Hash>,
|
||||
max_genesis_archive_unpacked_size: u64,
|
||||
no_genesis_fetch: bool,
|
||||
use_progress_bar: bool,
|
||||
) -> Result<GenesisConfig, String> {
|
||||
if no_genesis_fetch {
|
||||
let genesis_config = load_local_genesis(ledger_path, expected_genesis_hash)?;
|
||||
return Ok(genesis_config);
|
||||
}
|
||||
|
||||
let genesis_package = ledger_path.join(DEFAULT_GENESIS_ARCHIVE);
|
||||
let genesis_config = if let Ok(tmp_genesis_package) =
|
||||
download_genesis_if_missing(rpc_addr, &genesis_package, use_progress_bar)
|
||||
{
|
||||
unpack_genesis_archive(
|
||||
&tmp_genesis_package,
|
||||
&ledger_path,
|
||||
max_genesis_archive_unpacked_size,
|
||||
)
|
||||
.map_err(|err| format!("Failed to unpack downloaded genesis config: {}", err))?;
|
||||
|
||||
let downloaded_genesis = GenesisConfig::load(&ledger_path)
|
||||
.map_err(|err| format!("Failed to load downloaded genesis config: {}", err))?;
|
||||
|
||||
check_genesis_hash(&downloaded_genesis, expected_genesis_hash)?;
|
||||
std::fs::rename(tmp_genesis_package, genesis_package)
|
||||
.map_err(|err| format!("Unable to rename: {:?}", err))?;
|
||||
|
||||
downloaded_genesis
|
||||
} else {
|
||||
load_local_genesis(ledger_path, expected_genesis_hash)?
|
||||
};
|
||||
|
||||
Ok(genesis_config)
|
||||
}
|
Reference in New Issue
Block a user