Move CheckPoint into file, tidy canonical_head

This commit is contained in:
Paul Hauner 2019-02-01 16:55:37 +11:00
parent 942ef4b002
commit 4d062d77f9
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 55 additions and 37 deletions

View File

@ -12,6 +12,7 @@ where
T: ClientDB, T: ClientDB,
U: SlotClock, U: SlotClock,
{ {
/// Update the canonical head to some new values.
pub fn update_canonical_head( pub fn update_canonical_head(
&self, &self,
new_beacon_block: BeaconBlock, new_beacon_block: BeaconBlock,
@ -28,6 +29,12 @@ where
); );
} }
/// Returns a read-lock guarded `CheckPoint` struct for reading the head (as chosen by the
/// fork-choice rule).
///
/// It is important to note that the `beacon_state` returned may not match the present slot. It
/// is the state as it was when the head block was recieved, which could be some slots prior to
/// now.
pub fn head(&self) -> RwLockReadGuard<CheckPoint> { pub fn head(&self) -> RwLockReadGuard<CheckPoint> {
self.canonical_head.read() self.canonical_head.read()
} }

View File

@ -0,0 +1,41 @@
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.
pub struct CheckPoint {
pub beacon_block: BeaconBlock,
pub beacon_block_root: Hash256,
pub beacon_state: BeaconState,
pub beacon_state_root: Hash256,
}
impl CheckPoint {
/// Create a new checkpoint.
pub fn new(
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state_root: Hash256,
) -> Self {
Self {
beacon_block,
beacon_block_root,
beacon_state,
beacon_state_root,
}
}
/// Update all fields of the checkpoint.
pub fn update(
&mut self,
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state_root: Hash256,
) {
self.beacon_block = beacon_block;
self.beacon_block_root = beacon_block_root;
self.beacon_state = beacon_state;
self.beacon_state_root = beacon_state_root;
}
}

View File

@ -6,6 +6,7 @@ mod block_graph;
pub mod block_processing; pub mod block_processing;
pub mod block_production; pub mod block_production;
mod canonical_head; mod canonical_head;
mod checkpoint;
pub mod dump; pub mod dump;
mod finalized_head; mod finalized_head;
mod info; mod info;
@ -14,6 +15,7 @@ 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},
@ -24,7 +26,7 @@ use parking_lot::RwLock;
use slot_clock::SlotClock; use slot_clock::SlotClock;
use ssz::ssz_encode; use ssz::ssz_encode;
use std::sync::Arc; use std::sync::Arc;
use types::{BeaconBlock, BeaconState, ChainSpec, Hash256}; use types::{BeaconState, ChainSpec, Hash256};
pub use self::block_processing::Outcome as BlockProcessingOutcome; pub use self::block_processing::Outcome as BlockProcessingOutcome;
@ -35,42 +37,6 @@ pub enum BeaconChainError {
DBError(String), DBError(String),
} }
pub struct CheckPoint {
pub beacon_block: BeaconBlock,
pub beacon_block_root: Hash256,
pub beacon_state: BeaconState,
pub beacon_state_root: Hash256,
}
impl CheckPoint {
pub fn new(
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state_root: Hash256,
) -> Self {
Self {
beacon_block,
beacon_block_root,
beacon_state,
beacon_state_root,
}
}
pub fn update(
&mut self,
beacon_block: BeaconBlock,
beacon_block_root: Hash256,
beacon_state: BeaconState,
beacon_state_root: Hash256,
) {
self.beacon_block = beacon_block;
self.beacon_block_root = beacon_block_root;
self.beacon_state = beacon_state;
self.beacon_state_root = beacon_state_root;
}
}
pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock> { pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock> {
pub block_store: Arc<BeaconBlockStore<T>>, pub block_store: Arc<BeaconBlockStore<T>>,
pub state_store: Arc<BeaconStateStore<T>>, pub state_store: Arc<BeaconStateStore<T>>,

View File

@ -12,6 +12,10 @@ where
/// ///
/// The `previous_block_root` will be set to the root of the current head block (as determined /// The `previous_block_root` will be set to the root of the current head block (as determined
/// by the fork-choice rule). /// by the fork-choice rule).
///
/// It is important to note that this is _not_ the state corresponding to the canonical head
/// block, instead it is that state which may or may not have had additional per slot/epoch
/// processing applied to it.
pub fn advance_state(&self, slot: u64) -> Result<(), SlotProcessingError> { pub fn advance_state(&self, slot: u64) -> Result<(), SlotProcessingError> {
let state_slot = self.state.read().slot; let state_slot = self.state.read().slot;
let head_block_root = self.head().beacon_block_root; let head_block_root = self.head().beacon_block_root;