From e7b324966de3a10ed744f89f834d80803d7a1fbc Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 8 Sep 2019 20:55:15 -0400 Subject: [PATCH] Log all states and blocks processed --- beacon_node/beacon_chain/src/beacon_chain.rs | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index aa9332c01d..b6bf4e053e 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -11,6 +11,7 @@ use operation_pool::{OperationPool, PersistedOperationPool}; use parking_lot::{RwLock, RwLockReadGuard}; use slog::{error, info, warn, Logger}; use slot_clock::SlotClock; +use ssz::Encode; use state_processing::per_block_processing::{ errors::{ AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, @@ -21,6 +22,8 @@ use state_processing::per_block_processing::{ use state_processing::{ per_block_processing, per_slot_processing, BlockProcessingError, BlockSignatureStrategy, }; +use std::fs; +use std::io::prelude::*; use std::sync::Arc; use std::time::Duration; use store::iter::{BlockRootsIterator, StateRootsIterator}; @@ -1035,6 +1038,13 @@ impl BeaconChain { metrics::stop_timer(db_read_timer); + write_block(&block, block_root, &self.log); + write_state( + &format!("state_pre_block_{}", block_root), + &parent_state, + &self.log, + ); + let catchup_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_CATCHUP_STATE); // Keep a list of any states that were "skipped" (block-less) in between the parent state @@ -1083,6 +1093,12 @@ impl BeaconChain { let state_root = state.canonical_root(); + write_state( + &format!("state_post_block_{}", block_root), + &state, + &self.log, + ); + if block.state_root != state_root { return Ok(BlockProcessingOutcome::StateRootMismatch { block: block.state_root, @@ -1445,6 +1461,45 @@ impl BeaconChain { } } +fn write_state(prefix: &str, state: &BeaconState, log: &Logger) { + let root = Hash256::from_slice(&state.tree_hash_root()); + let filename = format!("{}_slot_{}_root_{}.ssz", prefix, state.slot, root); + let mut path = std::env::temp_dir().join("lighthouse"); + let _ = fs::create_dir_all(path.clone()); + path = path.join(filename); + + match fs::File::create(path.clone()) { + Ok(mut file) => { + let _ = file.write_all(&state.as_ssz_bytes()); + } + Err(e) => error!( + log, + "Failed to log state"; + "path" => format!("{:?}", path), + "error" => format!("{:?}", e) + ), + } +} + +fn write_block(block: &BeaconBlock, root: Hash256, log: &Logger) { + let filename = format!("block_slot_{}_root{}.ssz", block.slot, root); + let mut path = std::env::temp_dir().join("lighthouse"); + let _ = fs::create_dir_all(path.clone()); + path = path.join(filename); + + match fs::File::create(path.clone()) { + Ok(mut file) => { + let _ = file.write_all(&block.as_ssz_bytes()); + } + Err(e) => error!( + log, + "Failed to log block"; + "path" => format!("{:?}", path), + "error" => format!("{:?}", e) + ), + } +} + impl From for Error { fn from(e: DBError) -> Error { Error::DBError(e)