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)]
struct JustificationManager {
struct CheckpointManager {
current: FFGCheckpoints,
best: FFGCheckpoints,
update_at: Option<Epoch>,
}
impl JustificationManager {
impl CheckpointManager {
pub fn new(genesis_checkpoint: CheckpointBalances) -> Self {
let ffg_checkpoint = FFGCheckpoints {
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
/// whenever the struct was instantiated.
genesis_block_root: Hash256,
justification_manager: RwLock<JustificationManager>,
checkpoint_manager: RwLock<CheckpointManager>,
_phantom: PhantomData<T>,
}
@@ -201,7 +201,7 @@ impl<T: BeaconChainTypes> PartialEq for ForkChoice<T> {
fn eq(&self, other: &Self) -> bool {
self.backend == other.backend
&& 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 {
backend,
genesis_block_root,
justification_manager: RwLock::new(JustificationManager::new(
genesis_checkpoint.clone(),
)),
checkpoint_manager: RwLock::new(CheckpointManager::new(genesis_checkpoint.clone())),
_phantom: PhantomData,
}
}
@@ -244,7 +242,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
};
let (justified_checkpoint, finalized_checkpoint) = {
let mut jm = self.justification_manager.write();
let mut jm = self.checkpoint_manager.write();
jm.update(chain)?;
(jm.current.justified.clone(), jm.current.finalized.clone())
@@ -292,10 +290,10 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
) -> Result<()> {
let timer = metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_BLOCK_TIMES);
self.justification_manager
self.checkpoint_manager
.write()
.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.
for attestation in &block.body.attestations {
@@ -378,10 +376,10 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
/// Trigger a prune on the underlying fork choice backend.
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
.update_finalized_root(finalized_checkpoint.epoch, finalized_checkpoint.root)
.maybe_prune(finalized_checkpoint.root)
.map_err(Into::into)
}
@@ -389,7 +387,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
pub fn as_ssz_container(&self) -> SszForkChoice {
SszForkChoice {
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(),
}
}
@@ -403,7 +401,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
Ok(Self {
backend,
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,
})
}
@@ -415,7 +413,7 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
#[derive(Encode, Decode, Clone)]
pub struct SszForkChoice {
genesis_block_root: Hash256,
justification_manager: JustificationManager,
checkpoint_manager: CheckpointManager,
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 equal to the current one, but the finalized root is different.
/// - There is some internal error relating to invalid indices inside `self`.
pub fn maybe_prune(
&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;
}
pub fn maybe_prune(&mut self, finalized_root: Hash256) -> Result<(), Error> {
let finalized_index = *self
.indices
.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 {
self.ensure(i);
&mut self.0[i]
@@ -164,14 +159,10 @@ impl ProtoArrayForkChoice {
.map_err(|e| format!("find_head failed: {:?}", e))
}
pub fn update_finalized_root(
&self,
finalized_epoch: Epoch,
finalized_root: Hash256,
) -> Result<(), String> {
pub fn maybe_prune(&self, finalized_root: Hash256) -> Result<(), String> {
self.proto_array
.write()
.maybe_prune(finalized_epoch, finalized_root)
.maybe_prune(finalized_root)
.map_err(|e| format!("find_head maybe_prune failed: {:?}", e))
}