From 991223db1e975a2bab41e508d9736bcdaf66017c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 17 Jan 2020 12:48:29 +1100 Subject: [PATCH] Rename JustificationManager --- beacon_node/beacon_chain/src/fork_choice.rs | 28 +++++++++---------- .../src/proto_array.rs | 17 +---------- .../src/proto_array_fork_choice.rs | 13 ++------- 3 files changed, 16 insertions(+), 42 deletions(-) diff --git a/beacon_node/beacon_chain/src/fork_choice.rs b/beacon_node/beacon_chain/src/fork_choice.rs index 312554ad54..5011b381e3 100644 --- a/beacon_node/beacon_chain/src/fork_choice.rs +++ b/beacon_node/beacon_chain/src/fork_choice.rs @@ -52,13 +52,13 @@ struct FFGCheckpoints { } #[derive(PartialEq, Clone, Encode, Decode)] -struct JustificationManager { +struct CheckpointManager { current: FFGCheckpoints, best: FFGCheckpoints, update_at: Option, } -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 { /// 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, + checkpoint_manager: RwLock, _phantom: PhantomData, } @@ -201,7 +201,7 @@ impl PartialEq for ForkChoice { 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 ForkChoice { 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 ForkChoice { }; 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 ForkChoice { ) -> 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 ForkChoice { /// 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 ForkChoice { 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 ForkChoice { 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 ForkChoice { #[derive(Encode, Decode, Clone)] pub struct SszForkChoice { genesis_block_root: Hash256, - justification_manager: JustificationManager, + checkpoint_manager: CheckpointManager, backend_bytes: Vec, } diff --git a/eth2/proto_array_fork_choice/src/proto_array.rs b/eth2/proto_array_fork_choice/src/proto_array.rs index 587818d78d..978a8d6f1e 100644 --- a/eth2/proto_array_fork_choice/src/proto_array.rs +++ b/eth2/proto_array_fork_choice/src/proto_array.rs @@ -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) diff --git a/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs b/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs index b3de443f31..ca2dfd118d 100644 --- a/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs +++ b/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs @@ -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)) }