Add const to control writing of ssz files

This commit is contained in:
Paul Hauner 2019-09-08 21:57:48 -04:00
parent 09b0db2535
commit 7b7a44e2f2
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -37,6 +37,12 @@ use types::*;
// |-------must be this long------| // |-------must be this long------|
pub const GRAFFITI: &str = "sigp/lighthouse-0.0.0-prerelease"; pub const GRAFFITI: &str = "sigp/lighthouse-0.0.0-prerelease";
/// If true, everytime a block is processed the pre-state, post-state and block are written to SSZ
/// files in the temp directory.
///
/// Only useful for testing.
const WRITE_BLOCK_PROCESSING_SSZ: bool = true;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum BlockProcessingOutcome { pub enum BlockProcessingOutcome {
/// Block was valid and imported into the block graph. /// Block was valid and imported into the block graph.
@ -1463,41 +1469,45 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
} }
fn write_state<T: EthSpec>(prefix: &str, state: &BeaconState<T>, log: &Logger) { fn write_state<T: EthSpec>(prefix: &str, state: &BeaconState<T>, log: &Logger) {
let root = Hash256::from_slice(&state.tree_hash_root()); if WRITE_BLOCK_PROCESSING_SSZ {
let filename = format!("{}_slot_{}_root_{}.ssz", prefix, state.slot, root); let root = Hash256::from_slice(&state.tree_hash_root());
let mut path = std::env::temp_dir().join("lighthouse"); let filename = format!("{}_slot_{}_root_{}.ssz", prefix, state.slot, root);
let _ = fs::create_dir_all(path.clone()); let mut path = std::env::temp_dir().join("lighthouse");
path = path.join(filename); let _ = fs::create_dir_all(path.clone());
path = path.join(filename);
match fs::File::create(path.clone()) { match fs::File::create(path.clone()) {
Ok(mut file) => { Ok(mut file) => {
let _ = file.write_all(&state.as_ssz_bytes()); let _ = file.write_all(&state.as_ssz_bytes());
}
Err(e) => error!(
log,
"Failed to log state";
"path" => format!("{:?}", path),
"error" => format!("{:?}", e)
),
} }
Err(e) => error!(
log,
"Failed to log state";
"path" => format!("{:?}", path),
"error" => format!("{:?}", e)
),
} }
} }
fn write_block<T: EthSpec>(block: &BeaconBlock<T>, root: Hash256, log: &Logger) { fn write_block<T: EthSpec>(block: &BeaconBlock<T>, root: Hash256, log: &Logger) {
let filename = format!("block_slot_{}_root{}.ssz", block.slot, root); if WRITE_BLOCK_PROCESSING_SSZ {
let mut path = std::env::temp_dir().join("lighthouse"); let filename = format!("block_slot_{}_root{}.ssz", block.slot, root);
let _ = fs::create_dir_all(path.clone()); let mut path = std::env::temp_dir().join("lighthouse");
path = path.join(filename); let _ = fs::create_dir_all(path.clone());
path = path.join(filename);
match fs::File::create(path.clone()) { match fs::File::create(path.clone()) {
Ok(mut file) => { Ok(mut file) => {
let _ = file.write_all(&block.as_ssz_bytes()); let _ = file.write_all(&block.as_ssz_bytes());
}
Err(e) => error!(
log,
"Failed to log block";
"path" => format!("{:?}", path),
"error" => format!("{:?}", e)
),
} }
Err(e) => error!(
log,
"Failed to log block";
"path" => format!("{:?}", path),
"error" => format!("{:?}", e)
),
} }
} }