Rename JustificationManager

This commit is contained in:
Paul Hauner
2020-01-17 12:48:29 +11:00
parent 272c16c2f2
commit 991223db1e
3 changed files with 16 additions and 42 deletions

View File

@@ -52,13 +52,13 @@ struct FFGCheckpoints {
} }
#[derive(PartialEq, Clone, Encode, Decode)] #[derive(PartialEq, Clone, Encode, Decode)]
struct JustificationManager { struct CheckpointManager {
current: FFGCheckpoints, current: FFGCheckpoints,
best: FFGCheckpoints, best: FFGCheckpoints,
update_at: Option<Epoch>, update_at: Option<Epoch>,
} }
impl JustificationManager { impl CheckpointManager {
pub fn new(genesis_checkpoint: CheckpointBalances) -> Self { pub fn new(genesis_checkpoint: CheckpointBalances) -> Self {
let ffg_checkpoint = FFGCheckpoints { let ffg_checkpoint = FFGCheckpoints {
justified: genesis_checkpoint.clone(), justified: genesis_checkpoint.clone(),
@@ -192,7 +192,7 @@ pub struct ForkChoice<T: BeaconChainTypes> {
/// Does not necessarily need to be the _actual_ genesis, it suffices to be the finalized root /// Does not necessarily need to be the _actual_ genesis, it suffices to be the finalized root
/// whenever the struct was instantiated. /// whenever the struct was instantiated.
genesis_block_root: Hash256, genesis_block_root: Hash256,
justification_manager: RwLock<JustificationManager>, checkpoint_manager: RwLock<CheckpointManager>,
_phantom: PhantomData<T>, _phantom: PhantomData<T>,
} }
@@ -201,7 +201,7 @@ impl<T: BeaconChainTypes> PartialEq for ForkChoice<T> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.backend == other.backend self.backend == other.backend
&& self.genesis_block_root == other.genesis_block_root && self.genesis_block_root == other.genesis_block_root
&& *self.justification_manager.read() == *other.justification_manager.read() && *self.checkpoint_manager.read() == *other.checkpoint_manager.read()
} }
} }
@@ -224,9 +224,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
Self { Self {
backend, backend,
genesis_block_root, genesis_block_root,
justification_manager: RwLock::new(JustificationManager::new( checkpoint_manager: RwLock::new(CheckpointManager::new(genesis_checkpoint.clone())),
genesis_checkpoint.clone(),
)),
_phantom: PhantomData, _phantom: PhantomData,
} }
} }
@@ -244,7 +242,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
}; };
let (justified_checkpoint, finalized_checkpoint) = { let (justified_checkpoint, finalized_checkpoint) = {
let mut jm = self.justification_manager.write(); let mut jm = self.checkpoint_manager.write();
jm.update(chain)?; jm.update(chain)?;
(jm.current.justified.clone(), jm.current.finalized.clone()) (jm.current.justified.clone(), jm.current.finalized.clone())
@@ -292,10 +290,10 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
) -> Result<()> { ) -> Result<()> {
let timer = metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_BLOCK_TIMES); let timer = metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_BLOCK_TIMES);
self.justification_manager self.checkpoint_manager
.write() .write()
.process_state(state, chain, &self.backend)?; .process_state(state, chain, &self.backend)?;
self.justification_manager.write().update(chain)?; self.checkpoint_manager.write().update(chain)?;
// Note: we never count the block as a latest message, only attestations. // Note: we never count the block as a latest message, only attestations.
for attestation in &block.body.attestations { for attestation in &block.body.attestations {
@@ -378,10 +376,10 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
/// Trigger a prune on the underlying fork choice backend. /// Trigger a prune on the underlying fork choice backend.
pub fn prune(&self) -> Result<()> { pub fn prune(&self) -> Result<()> {
let finalized_checkpoint = self.justification_manager.read().current.finalized.clone(); let finalized_checkpoint = self.checkpoint_manager.read().current.finalized.clone();
self.backend self.backend
.update_finalized_root(finalized_checkpoint.epoch, finalized_checkpoint.root) .maybe_prune(finalized_checkpoint.root)
.map_err(Into::into) .map_err(Into::into)
} }
@@ -389,7 +387,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
pub fn as_ssz_container(&self) -> SszForkChoice { pub fn as_ssz_container(&self) -> SszForkChoice {
SszForkChoice { SszForkChoice {
genesis_block_root: self.genesis_block_root.clone(), genesis_block_root: self.genesis_block_root.clone(),
justification_manager: self.justification_manager.read().clone(), checkpoint_manager: self.checkpoint_manager.read().clone(),
backend_bytes: self.backend.as_bytes(), backend_bytes: self.backend.as_bytes(),
} }
} }
@@ -403,7 +401,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
Ok(Self { Ok(Self {
backend, backend,
genesis_block_root: ssz_container.genesis_block_root, genesis_block_root: ssz_container.genesis_block_root,
justification_manager: RwLock::new(ssz_container.justification_manager), checkpoint_manager: RwLock::new(ssz_container.checkpoint_manager),
_phantom: PhantomData, _phantom: PhantomData,
}) })
} }
@@ -415,7 +413,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
#[derive(Encode, Decode, Clone)] #[derive(Encode, Decode, Clone)]
pub struct SszForkChoice { pub struct SszForkChoice {
genesis_block_root: Hash256, genesis_block_root: Hash256,
justification_manager: JustificationManager, checkpoint_manager: CheckpointManager,
backend_bytes: Vec<u8>, backend_bytes: Vec<u8>,
} }

View File

@@ -211,22 +211,7 @@ impl ProtoArray {
/// - The finalized epoch is less than the current one. /// - The finalized epoch is less than the current one.
/// - The finalized epoch is equal to the current one, but the finalized root is different. /// - The finalized epoch is equal to the current one, but the finalized root is different.
/// - There is some internal error relating to invalid indices inside `self`. /// - There is some internal error relating to invalid indices inside `self`.
pub fn maybe_prune( pub fn maybe_prune(&mut self, finalized_root: Hash256) -> Result<(), Error> {
&mut self,
finalized_epoch: Epoch,
finalized_root: Hash256,
) -> Result<(), Error> {
if finalized_epoch < self.finalized_epoch {
// It's illegal to swap to an earlier finalized root (this is assumed to be reverting a
// finalized block).
return Err(Error::RevertedFinalizedEpoch {
current_finalized_epoch: self.finalized_epoch,
new_finalized_epoch: finalized_epoch,
});
} else if finalized_epoch != self.finalized_epoch {
self.finalized_epoch = finalized_epoch;
}
let finalized_index = *self let finalized_index = *self
.indices .indices
.get(&finalized_root) .get(&finalized_root)

View File

@@ -34,11 +34,6 @@ where
} }
} }
pub fn get(&mut self, i: usize) -> &T {
self.ensure(i);
&self.0[i]
}
pub fn get_mut(&mut self, i: usize) -> &mut T { pub fn get_mut(&mut self, i: usize) -> &mut T {
self.ensure(i); self.ensure(i);
&mut self.0[i] &mut self.0[i]
@@ -164,14 +159,10 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("find_head failed: {:?}", e)) .map_err(|e| format!("find_head failed: {:?}", e))
} }
pub fn update_finalized_root( pub fn maybe_prune(&self, finalized_root: Hash256) -> Result<(), String> {
&self,
finalized_epoch: Epoch,
finalized_root: Hash256,
) -> Result<(), String> {
self.proto_array self.proto_array
.write() .write()
.maybe_prune(finalized_epoch, finalized_root) .maybe_prune(finalized_root)
.map_err(|e| format!("find_head maybe_prune failed: {:?}", e)) .map_err(|e| format!("find_head maybe_prune failed: {:?}", e))
} }