diff --git a/beacon_node/rest_api/src/beacon.rs b/beacon_node/rest_api/src/beacon.rs index 0ac95f7ca9..ad2688bb0f 100644 --- a/beacon_node/rest_api/src/beacon.rs +++ b/beacon_node/rest_api/src/beacon.rs @@ -192,12 +192,11 @@ pub fn get_all_validators( }; let mut state = get_state_from_root_opt(&ctx.beacon_chain, state_root_opt)?; - state.update_pubkey_cache()?; - state - .validators + let validators = state.validators.clone(); + validators .iter() - .map(|validator| validator_response_by_pubkey(&state, validator.pubkey.clone())) + .map(|validator| validator_response_by_pubkey(&mut state, validator.pubkey.clone())) .collect::, _>>() } @@ -215,13 +214,14 @@ pub fn get_active_validators( }; let mut state = get_state_from_root_opt(&ctx.beacon_chain, state_root_opt)?; - state.update_pubkey_cache()?; - state - .validators + let validators = state.validators.clone(); + let current_epoch = state.current_epoch(); + + validators .iter() - .filter(|validator| validator.is_active_at(state.current_epoch())) - .map(|validator| validator_response_by_pubkey(&state, validator.pubkey.clone())) + .filter(|validator| validator.is_active_at(current_epoch)) + .map(|validator| validator_response_by_pubkey(&mut state, validator.pubkey.clone())) .collect::, _>>() } @@ -279,11 +279,10 @@ fn validator_responses_by_pubkey( validator_pubkeys: Vec, ) -> Result, ApiError> { let mut state = get_state_from_root_opt(beacon_chain, state_root_opt)?; - state.update_pubkey_cache()?; validator_pubkeys .into_iter() - .map(|validator_pubkey| validator_response_by_pubkey(&state, validator_pubkey)) + .map(|validator_pubkey| validator_response_by_pubkey(&mut state, validator_pubkey)) .collect::, ApiError>>() } @@ -291,7 +290,7 @@ fn validator_responses_by_pubkey( /// /// The provided `state` must have a fully up-to-date pubkey cache. fn validator_response_by_pubkey( - state: &BeaconState, + state: &mut BeaconState, validator_pubkey: PublicKeyBytes, ) -> Result { let validator_index_opt = state diff --git a/beacon_node/rest_api/src/consensus.rs b/beacon_node/rest_api/src/consensus.rs index d82b05b7a7..9df57f0552 100644 --- a/beacon_node/rest_api/src/consensus.rs +++ b/beacon_node/rest_api/src/consensus.rs @@ -92,10 +92,6 @@ pub fn post_individual_votes( let mut validator_statuses = ValidatorStatuses::new(&state, spec)?; validator_statuses.process_attestations(&state, spec)?; - state.update_pubkey_cache().map_err(|e| { - ApiError::ServerError(format!("Unable to build pubkey cache: {:?}", e)) - })?; - body.pubkeys .into_iter() .map(|pubkey| { diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index e1c3c37dbf..49342ddaa3 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -156,9 +156,6 @@ fn return_validator_duties( state .build_committee_cache(relative_epoch, &beacon_chain.spec) .map_err(|e| ApiError::ServerError(format!("Unable to build committee cache: {:?}", e)))?; - state - .update_pubkey_cache() - .map_err(|e| ApiError::ServerError(format!("Unable to build pubkey cache: {:?}", e)))?; // Get a list of all validators for this epoch. // diff --git a/consensus/state_processing/src/per_block_processing.rs b/consensus/state_processing/src/per_block_processing.rs index d6747b65e5..9bedba23e2 100644 --- a/consensus/state_processing/src/per_block_processing.rs +++ b/consensus/state_processing/src/per_block_processing.rs @@ -446,10 +446,6 @@ pub fn process_deposit( state.eth1_deposit_index.increment()?; - // Ensure the state's pubkey cache is fully up-to-date, it will be used to check to see if the - // depositing validator already exists in the registry. - state.update_pubkey_cache()?; - // Get an `Option` where `u64` is the validator index if this deposit public key // already exists in the beacon_state. let validator_index = get_existing_validator_index(state, &deposit.data.pubkey) diff --git a/consensus/state_processing/src/per_block_processing/verify_deposit.rs b/consensus/state_processing/src/per_block_processing/verify_deposit.rs index 510b73b2a8..5e7e6f1ad1 100644 --- a/consensus/state_processing/src/per_block_processing/verify_deposit.rs +++ b/consensus/state_processing/src/per_block_processing/verify_deposit.rs @@ -35,7 +35,7 @@ pub fn verify_deposit_signature(deposit_data: &DepositData, spec: &ChainSpec) -> /// /// Errors if the state's `pubkey_cache` is not current. pub fn get_existing_validator_index( - state: &BeaconState, + state: &mut BeaconState, pub_key: &PublicKeyBytes, ) -> Result> { let validator_index = state.get_validator_index(pub_key)?; diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index 9594a22175..a335d04926 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -300,19 +300,12 @@ impl BeaconState { } } - /// If a validator pubkey exists in the validator registry, returns `Some(i)`, otherwise - /// returns `None`. - /// - /// Requires a fully up-to-date `pubkey_cache`, returns an error if this is not the case. - pub fn get_validator_index(&self, pubkey: &PublicKeyBytes) -> Result, Error> { - if self.pubkey_cache.len() == self.validators.len() { - Ok(self.pubkey_cache.get(pubkey)) - } else { - Err(Error::PubkeyCacheIncomplete { - cache_len: self.pubkey_cache.len(), - registry_len: self.validators.len(), - }) - } + /// This method ensures the state's pubkey cache is fully up-to-date before checking if the validator + /// exists in the registry. If a validator pubkey exists in the validator registry, returns `Some(i)`, + /// otherwise returns `None`. + pub fn get_validator_index(&mut self, pubkey: &PublicKeyBytes) -> Result, Error> { + self.update_pubkey_cache()?; + Ok(self.pubkey_cache.get(pubkey)) } /// The epoch corresponding to `self.slot`.