Tidy slot_dump, remove SlotDump, use CheckPoint

This commit is contained in:
Paul Hauner 2019-02-01 17:04:25 +11:00
parent 4d062d77f9
commit b99e4ed9f4
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 18 additions and 20 deletions

View File

@ -1,7 +1,9 @@
use serde_derive::Serialize;
use types::{BeaconBlock, BeaconState, Hash256}; use types::{BeaconBlock, BeaconState, Hash256};
/// Represents some block and it's associated state. Generally, this will be used for tracking the /// Represents some block and it's associated state. Generally, this will be used for tracking the
/// head, justified head and finalized head. /// head, justified head and finalized head.
#[derive(PartialEq, Clone, Serialize)]
pub struct CheckPoint { pub struct CheckPoint {
pub beacon_block: BeaconBlock, pub beacon_block: BeaconBlock,
pub beacon_block_root: Hash256, pub beacon_block_root: Hash256,

View File

@ -1,18 +1,11 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock}; use super::{BeaconChain, CheckPoint, ClientDB, DBError, SlotClock};
use serde_derive::Serialize; use types::Hash256;
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,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Error { pub enum Error {
/// There was an error reading from the database. This is an internal error.
DBError(String), DBError(String),
/// There is a missing (or invalid) block in the database. This is an internal error.
MissingBlock(Hash256), MissingBlock(Hash256),
} }
@ -21,10 +14,14 @@ where
T: ClientDB, T: ClientDB,
U: SlotClock, U: SlotClock,
{ {
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, 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<Vec<CheckPoint>, Error> {
let mut dump = vec![]; let mut dump = vec![];
let mut last_slot = SlotDump { let mut last_slot = CheckPoint {
beacon_block: self.head().beacon_block.clone(), beacon_block: self.head().beacon_block.clone(),
beacon_block_root: self.head().beacon_block_root, beacon_block_root: self.head().beacon_block_root,
beacon_state: self.head().beacon_state.clone(), beacon_state: self.head().beacon_state.clone(),
@ -37,8 +34,7 @@ where
let beacon_block_root = last_slot.beacon_block.parent_root; let beacon_block_root = last_slot.beacon_block.parent_root;
if beacon_block_root == self.spec.zero_hash { if beacon_block_root == self.spec.zero_hash {
// Genesis has been reached. break; // Genesis has been reached.
break;
} }
let beacon_block = self let beacon_block = self
@ -51,7 +47,7 @@ where
.get_deserialized(&beacon_state_root)? .get_deserialized(&beacon_state_root)?
.ok_or_else(|| Error::MissingBlock(beacon_state_root))?; .ok_or_else(|| Error::MissingBlock(beacon_state_root))?;
let slot = SlotDump { let slot = CheckPoint {
beacon_block, beacon_block,
beacon_block_root, beacon_block_root,
beacon_state, beacon_state,

View File

@ -15,7 +15,6 @@ mod state;
use self::attestation_targets::AttestationTargets; use self::attestation_targets::AttestationTargets;
use self::block_graph::BlockGraph; use self::block_graph::BlockGraph;
use self::checkpoint::CheckPoint;
use attestation_aggregator::AttestationAggregator; use attestation_aggregator::AttestationAggregator;
use db::{ use db::{
stores::{BeaconBlockStore, BeaconStateStore}, stores::{BeaconBlockStore, BeaconStateStore},
@ -29,6 +28,7 @@ use std::sync::Arc;
use types::{BeaconState, ChainSpec, Hash256}; use types::{BeaconState, ChainSpec, Hash256};
pub use self::block_processing::Outcome as BlockProcessingOutcome; pub use self::block_processing::Outcome as BlockProcessingOutcome;
pub use self::checkpoint::CheckPoint;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum BeaconChainError { pub enum BeaconChainError {

View File

@ -1,6 +1,6 @@
use super::TestValidator; use super::TestValidator;
pub use beacon_chain::dump::{Error as DumpError, SlotDump};
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
pub use beacon_chain::{dump::Error as DumpError, CheckPoint};
use db::{ use db::{
stores::{BeaconBlockStore, BeaconStateStore}, stores::{BeaconBlockStore, BeaconStateStore},
MemoryDB, MemoryDB,
@ -208,11 +208,11 @@ impl BeaconChainHarness {
debug!("Free attestations processed."); debug!("Free attestations processed.");
} }
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, DumpError> { pub fn chain_dump(&self) -> Result<Vec<CheckPoint>, DumpError> {
self.beacon_chain.chain_dump() self.beacon_chain.chain_dump()
} }
pub fn dump_to_file(&self, filename: String, chain_dump: &Vec<SlotDump>) { pub fn dump_to_file(&self, filename: String, chain_dump: &Vec<CheckPoint>) {
let json = serde_json::to_string(chain_dump).unwrap(); let json = serde_json::to_string(chain_dump).unwrap();
let mut file = File::create(filename).unwrap(); let mut file = File::create(filename).unwrap();
file.write_all(json.as_bytes()) file.write_all(json.as_bytes())