diff --git a/beacon_node/beacon_chain/src/checkpoint.rs b/beacon_node/beacon_chain/src/checkpoint.rs index b2e0f9baa..bef97d2ed 100644 --- a/beacon_node/beacon_chain/src/checkpoint.rs +++ b/beacon_node/beacon_chain/src/checkpoint.rs @@ -1,7 +1,9 @@ +use serde_derive::Serialize; use types::{BeaconBlock, BeaconState, Hash256}; /// Represents some block and it's associated state. Generally, this will be used for tracking the /// head, justified head and finalized head. +#[derive(PartialEq, Clone, Serialize)] pub struct CheckPoint { pub beacon_block: BeaconBlock, pub beacon_block_root: Hash256, diff --git a/beacon_node/beacon_chain/src/dump.rs b/beacon_node/beacon_chain/src/dump.rs index 613534328..b2b031504 100644 --- a/beacon_node/beacon_chain/src/dump.rs +++ b/beacon_node/beacon_chain/src/dump.rs @@ -1,18 +1,11 @@ -use super::{BeaconChain, ClientDB, DBError, SlotClock}; -use serde_derive::Serialize; -use types::{BeaconBlock, BeaconState, Hash256}; - -#[derive(Debug, Clone, Serialize)] -pub struct SlotDump { - pub beacon_block: BeaconBlock, - pub beacon_block_root: Hash256, - pub beacon_state: BeaconState, - pub beacon_state_root: Hash256, -} +use super::{BeaconChain, CheckPoint, ClientDB, DBError, SlotClock}; +use types::Hash256; #[derive(Debug, Clone)] pub enum Error { + /// There was an error reading from the database. This is an internal error. DBError(String), + /// There is a missing (or invalid) block in the database. This is an internal error. MissingBlock(Hash256), } @@ -21,10 +14,14 @@ where T: ClientDB, U: SlotClock, { - pub fn chain_dump(&self) -> Result, Error> { + /// Dumps the entire canonical chain, from the head to genesis to a vector for analysis. + /// + /// This could be a very expensive operation and should only be done in testing/analysis + /// activities. + pub fn chain_dump(&self) -> Result, Error> { let mut dump = vec![]; - let mut last_slot = SlotDump { + let mut last_slot = CheckPoint { beacon_block: self.head().beacon_block.clone(), beacon_block_root: self.head().beacon_block_root, beacon_state: self.head().beacon_state.clone(), @@ -37,8 +34,7 @@ where let beacon_block_root = last_slot.beacon_block.parent_root; if beacon_block_root == self.spec.zero_hash { - // Genesis has been reached. - break; + break; // Genesis has been reached. } let beacon_block = self @@ -51,7 +47,7 @@ where .get_deserialized(&beacon_state_root)? .ok_or_else(|| Error::MissingBlock(beacon_state_root))?; - let slot = SlotDump { + let slot = CheckPoint { beacon_block, beacon_block_root, beacon_state, diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index ddbb2732c..459e5c509 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -15,7 +15,6 @@ mod state; use self::attestation_targets::AttestationTargets; use self::block_graph::BlockGraph; -use self::checkpoint::CheckPoint; use attestation_aggregator::AttestationAggregator; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, @@ -29,6 +28,7 @@ use std::sync::Arc; use types::{BeaconState, ChainSpec, Hash256}; pub use self::block_processing::Outcome as BlockProcessingOutcome; +pub use self::checkpoint::CheckPoint; #[derive(Debug, PartialEq)] pub enum BeaconChainError { diff --git a/beacon_node/beacon_chain/test_harness/src/harness.rs b/beacon_node/beacon_chain/test_harness/src/harness.rs index ce88395cb..f55fb7666 100644 --- a/beacon_node/beacon_chain/test_harness/src/harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/harness.rs @@ -1,6 +1,6 @@ use super::TestValidator; -pub use beacon_chain::dump::{Error as DumpError, SlotDump}; use beacon_chain::BeaconChain; +pub use beacon_chain::{dump::Error as DumpError, CheckPoint}; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, MemoryDB, @@ -208,11 +208,11 @@ impl BeaconChainHarness { debug!("Free attestations processed."); } - pub fn chain_dump(&self) -> Result, DumpError> { + pub fn chain_dump(&self) -> Result, DumpError> { self.beacon_chain.chain_dump() } - pub fn dump_to_file(&self, filename: String, chain_dump: &Vec) { + pub fn dump_to_file(&self, filename: String, chain_dump: &Vec) { let json = serde_json::to_string(chain_dump).unwrap(); let mut file = File::create(filename).unwrap(); file.write_all(json.as_bytes())