mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 22:04:44 +00:00
Add chain_dump fn to beacon_chain
This commit is contained in:
72
beacon_node/beacon_chain/src/dump.rs
Normal file
72
beacon_node/beacon_chain/src/dump.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use super::{BeaconChain, ClientDB, DBError, SlotClock};
|
||||
use types::{BeaconBlock, BeaconState, Hash256};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SlotDump {
|
||||
pub beacon_block: BeaconBlock,
|
||||
pub beacon_block_root: Hash256,
|
||||
pub beacon_state: BeaconState,
|
||||
pub beacon_state_root: Hash256,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Error {
|
||||
DBError(String),
|
||||
MissingBlock(Hash256),
|
||||
}
|
||||
|
||||
impl<T, U> BeaconChain<T, U>
|
||||
where
|
||||
T: ClientDB,
|
||||
U: SlotClock,
|
||||
{
|
||||
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, Error> {
|
||||
let mut dump = vec![];
|
||||
|
||||
let mut last_slot = SlotDump {
|
||||
beacon_block: self.canonical_head().beacon_block.clone(),
|
||||
beacon_block_root: self.canonical_head().beacon_block_root,
|
||||
beacon_state: self.canonical_head().beacon_state.clone(),
|
||||
beacon_state_root: self.canonical_head().beacon_state_root,
|
||||
};
|
||||
|
||||
dump.push(last_slot.clone());
|
||||
|
||||
loop {
|
||||
let beacon_block_root = last_slot.beacon_block.parent_root;
|
||||
|
||||
if beacon_block_root == self.spec.zero_hash {
|
||||
// Genesis has been reached.
|
||||
break;
|
||||
}
|
||||
|
||||
let beacon_block = self
|
||||
.block_store
|
||||
.get_deserialized(&beacon_block_root)?
|
||||
.ok_or_else(|| Error::MissingBlock(beacon_block_root))?;
|
||||
let beacon_state_root = beacon_block.state_root;
|
||||
let beacon_state = self
|
||||
.state_store
|
||||
.get_deserialized(&beacon_state_root)?
|
||||
.ok_or_else(|| Error::MissingBlock(beacon_state_root))?;
|
||||
|
||||
let slot = SlotDump {
|
||||
beacon_block,
|
||||
beacon_block_root,
|
||||
beacon_state,
|
||||
beacon_state_root,
|
||||
};
|
||||
|
||||
dump.push(slot.clone());
|
||||
last_slot = slot;
|
||||
}
|
||||
|
||||
Ok(dump)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DBError> for Error {
|
||||
fn from(e: DBError) -> Error {
|
||||
Error::DBError(e.message)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ mod block_graph;
|
||||
pub mod block_processing;
|
||||
pub mod block_production;
|
||||
mod canonical_head;
|
||||
pub mod dump;
|
||||
mod finalized_head;
|
||||
mod info;
|
||||
mod lmd_ghost;
|
||||
|
||||
Reference in New Issue
Block a user