Don't panic in sdk/ when genesis fails to load (#6892)
This commit is contained in:
parent
bb158a9b48
commit
c6b108ef4f
|
@ -426,8 +426,10 @@ pub fn new_banks_from_blocktree(
|
||||||
LeaderScheduleCache,
|
LeaderScheduleCache,
|
||||||
PohConfig,
|
PohConfig,
|
||||||
) {
|
) {
|
||||||
let genesis_config =
|
let genesis_config = GenesisConfig::load(blocktree_path).unwrap_or_else(|err| {
|
||||||
GenesisConfig::load(blocktree_path).expect("Failed to load genesis config");
|
error!("Failed to load genesis from {:?}: {}", blocktree_path, err);
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
let genesis_hash = genesis_config.hash();
|
let genesis_hash = genesis_config.hash();
|
||||||
info!("genesis hash: {}", genesis_hash);
|
info!("genesis hash: {}", genesis_hash);
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ use memmap::Mmap;
|
||||||
use std::{
|
use std::{
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
||||||
|
@ -92,26 +92,50 @@ impl GenesisConfig {
|
||||||
hash(&serialized.into_bytes())
|
hash(&serialized.into_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn genesis_filename(ledger_path: &Path) -> PathBuf {
|
||||||
|
Path::new(ledger_path).join("genesis.bin")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load(ledger_path: &Path) -> Result<Self, std::io::Error> {
|
pub fn load(ledger_path: &Path) -> Result<Self, std::io::Error> {
|
||||||
|
let filename = Self::genesis_filename(&ledger_path);
|
||||||
let file = OpenOptions::new()
|
let file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.open(&Path::new(ledger_path).join("genesis.bin"))
|
.open(&filename)
|
||||||
.expect("Unable to open genesis file");
|
.map_err(|err| {
|
||||||
|
std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!("Unable to open {:?}: {:?}", filename, err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
//UNSAFE: Required to create a Mmap
|
//UNSAFE: Required to create a Mmap
|
||||||
let mem = unsafe { Mmap::map(&file).expect("failed to map the genesis file") };
|
let mem = unsafe { Mmap::map(&file) }.map_err(|err| {
|
||||||
let genesis_config = deserialize(&mem)
|
std::io::Error::new(
|
||||||
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", err)))?;
|
std::io::ErrorKind::Other,
|
||||||
|
format!("Unable to map {:?}: {:?}", filename, err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let genesis_config = deserialize(&mem).map_err(|err| {
|
||||||
|
std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!("Unable to deserialize {:?}: {:?}", filename, err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
Ok(genesis_config)
|
Ok(genesis_config)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self, ledger_path: &Path) -> Result<(), std::io::Error> {
|
pub fn write(&self, ledger_path: &Path) -> Result<(), std::io::Error> {
|
||||||
let serialized = serialize(&self)
|
let serialized = serialize(&self).map_err(|err| {
|
||||||
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", err)))?;
|
std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!("Unable to serialize: {:?}", err),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
std::fs::create_dir_all(&ledger_path)?;
|
std::fs::create_dir_all(&ledger_path)?;
|
||||||
|
|
||||||
let mut file = File::create(&ledger_path.join("genesis.bin"))?;
|
let mut file = File::create(Self::genesis_filename(&ledger_path))?;
|
||||||
file.write_all(&serialized)
|
file.write_all(&serialized)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue