Skip to content

Commit

Permalink
Reconfigure genesis hash download and check (#30949)
Browse files Browse the repository at this point in the history
* Reconfigure genesis hash download and check

* Propagate get genesis errors
  • Loading branch information
bw-solana authored Apr 10, 2023
1 parent d63359a commit 861bca8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions genesis-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
log = { workspace = true }
solana-download-utils = { workspace = true }
solana-rpc-client = { workspace = true }
solana-runtime = { workspace = true }
solana-sdk = { workspace = true }

Expand Down
63 changes: 55 additions & 8 deletions genesis-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use {
log::*,
solana_download_utils::download_genesis_if_missing,
solana_rpc_client::rpc_client::RpcClient,
solana_runtime::hardened_unpack::unpack_genesis_archive,
solana_sdk::{
genesis_config::{GenesisConfig, DEFAULT_GENESIS_ARCHIVE},
Expand Down Expand Up @@ -36,7 +38,7 @@ fn load_local_genesis(
Ok(existing_genesis)
}

pub fn download_then_check_genesis_hash(
fn get_genesis_config(
rpc_addr: &SocketAddr,
ledger_path: &std::path::Path,
expected_genesis_hash: Option<Hash>,
Expand All @@ -45,12 +47,11 @@ pub fn download_then_check_genesis_hash(
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);
return load_local_genesis(ledger_path, expected_genesis_hash);
}

let genesis_package = ledger_path.join(DEFAULT_GENESIS_ARCHIVE);
let genesis_config = if let Ok(tmp_genesis_package) =
if let Ok(tmp_genesis_package) =
download_genesis_if_missing(rpc_addr, &genesis_package, use_progress_bar)
{
unpack_genesis_archive(
Expand All @@ -67,10 +68,56 @@ pub fn download_then_check_genesis_hash(
std::fs::rename(tmp_genesis_package, genesis_package)
.map_err(|err| format!("Unable to rename: {err:?}"))?;

downloaded_genesis
Ok(downloaded_genesis)
} else {
load_local_genesis(ledger_path, expected_genesis_hash)?
};
load_local_genesis(ledger_path, expected_genesis_hash)
}
}

fn set_and_verify_expected_genesis_hash(
genesis_config: GenesisConfig,
expected_genesis_hash: &mut Option<Hash>,
rpc_client: &RpcClient,
) -> Result<(), String> {
let genesis_hash = genesis_config.hash();
if expected_genesis_hash.is_none() {
info!("Expected genesis hash set to {}", genesis_hash);
*expected_genesis_hash = Some(genesis_hash);
}
let expected_genesis_hash = expected_genesis_hash.unwrap();

// Sanity check that the RPC node is using the expected genesis hash before
// downloading a snapshot from it
let rpc_genesis_hash = rpc_client
.get_genesis_hash()
.map_err(|err| format!("Failed to get genesis hash: {err}"))?;

if expected_genesis_hash != rpc_genesis_hash {
return Err(format!(
"Genesis hash mismatch: expected {expected_genesis_hash} but RPC node genesis hash is {rpc_genesis_hash}"
));
}

Ok(())
}

pub fn download_then_check_genesis_hash(
rpc_addr: &SocketAddr,
ledger_path: &std::path::Path,
expected_genesis_hash: &mut Option<Hash>,
max_genesis_archive_unpacked_size: u64,
no_genesis_fetch: bool,
use_progress_bar: bool,
rpc_client: &RpcClient,
) -> Result<(), String> {
let genesis_config = get_genesis_config(
rpc_addr,
ledger_path,
*expected_genesis_hash,
max_genesis_archive_unpacked_size,
no_genesis_fetch,
use_progress_bar,
)?;

Ok(genesis_config)
set_and_verify_expected_genesis_hash(genesis_config, expected_genesis_hash, rpc_client)
}
2 changes: 2 additions & 0 deletions programs/sbf/Cargo.lock

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

29 changes: 4 additions & 25 deletions validator/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,36 +398,15 @@ pub fn attempt_download_genesis_and_snapshot(
vote_account: &Pubkey,
authorized_voter_keypairs: Arc<RwLock<Vec<Arc<Keypair>>>>,
) -> Result<(), String> {
let genesis_config = download_then_check_genesis_hash(
download_then_check_genesis_hash(
&rpc_contact_info.rpc,
ledger_path,
validator_config.expected_genesis_hash,
&mut validator_config.expected_genesis_hash,
bootstrap_config.max_genesis_archive_unpacked_size,
bootstrap_config.no_genesis_fetch,
use_progress_bar,
);

if let Ok(genesis_config) = genesis_config {
let genesis_hash = genesis_config.hash();
if validator_config.expected_genesis_hash.is_none() {
info!("Expected genesis hash set to {}", genesis_hash);
validator_config.expected_genesis_hash = Some(genesis_hash);
}
}

if let Some(expected_genesis_hash) = validator_config.expected_genesis_hash {
// Sanity check that the RPC node is using the expected genesis hash before
// downloading a snapshot from it
let rpc_genesis_hash = rpc_client
.get_genesis_hash()
.map_err(|err| format!("Failed to get genesis hash: {err}"))?;

if expected_genesis_hash != rpc_genesis_hash {
return Err(format!(
"Genesis hash mismatch: expected {expected_genesis_hash} but RPC node genesis hash is {rpc_genesis_hash}"
));
}
}
rpc_client,
)?;

if let Some(gossip) = gossip.take() {
shutdown_gossip_service(gossip);
Expand Down

0 comments on commit 861bca8

Please sign in to comment.