From 5dac51062c506ff15c1dfdd7cb59a69d81a7cbe9 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Mon, 28 Oct 2024 12:16:26 +1100 Subject: [PATCH] Remove pubkey_bytes from ValidatorPubkeyCache Squashed commit of the following: commit a0b3490acc9cf7bd78d085c803cde68047ebb575 Author: Michael Sproul Date: Mon Oct 28 12:03:58 2024 +1100 Remove pubkey_bytes from ValidatorPubkeyCache --- beacon_node/beacon_chain/src/beacon_chain.rs | 16 ++++++++++------ .../beacon_chain/src/validator_pubkey_cache.rs | 12 ------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index f8dfbc55155..9b1235905cb 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1558,9 +1558,13 @@ impl BeaconChain { &self, validator_index: usize, ) -> Result, Error> { - let pubkey_cache = self.validator_pubkey_cache.read(); - - Ok(pubkey_cache.get_pubkey_bytes(validator_index).copied()) + let head = self.canonical_head.cached_head(); + Ok(head + .snapshot + .beacon_state + .get_validator(validator_index) + .ok() + .map(|v| v.pubkey)) } /// As per `Self::validator_pubkey_bytes` but will resolve multiple indices at once to avoid @@ -1572,12 +1576,12 @@ impl BeaconChain { &self, validator_indices: &[usize], ) -> Result, Error> { - let pubkey_cache = self.validator_pubkey_cache.read(); + let head = self.canonical_head.cached_head(); let mut map = HashMap::with_capacity(validator_indices.len()); for &validator_index in validator_indices { - if let Some(pubkey) = pubkey_cache.get_pubkey_bytes(validator_index) { - map.insert(validator_index, *pubkey); + if let Ok(validator) = head.snapshot.beacon_state.get_validator(validator_index) { + map.insert(validator_index, validator.pubkey); } } Ok(map) diff --git a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs index 877c297a3b7..49dd25fd806 100644 --- a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs +++ b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs @@ -20,7 +20,6 @@ use types::{BeaconState, FixedBytesExtended, Hash256, PublicKey, PublicKeyBytes} pub struct ValidatorPubkeyCache { pubkeys: Vec, indices: HashMap, - pubkey_bytes: Vec, _phantom: PhantomData, } @@ -35,7 +34,6 @@ impl ValidatorPubkeyCache { let mut cache = Self { pubkeys: vec![], indices: HashMap::new(), - pubkey_bytes: vec![], _phantom: PhantomData, }; @@ -49,7 +47,6 @@ impl ValidatorPubkeyCache { pub fn load_from_store(store: BeaconStore) -> Result { let mut pubkeys = vec![]; let mut indices = HashMap::new(); - let mut pubkey_bytes = vec![]; for validator_index in 0.. { if let Some(db_pubkey) = @@ -58,7 +55,6 @@ impl ValidatorPubkeyCache { let (pk, pk_bytes) = DatabasePubkey::as_pubkey(&db_pubkey)?; pubkeys.push(pk); indices.insert(pk_bytes, validator_index); - pubkey_bytes.push(pk_bytes); } else { break; } @@ -67,7 +63,6 @@ impl ValidatorPubkeyCache { Ok(ValidatorPubkeyCache { pubkeys, indices, - pubkey_bytes, _phantom: PhantomData, }) } @@ -101,7 +96,6 @@ impl ValidatorPubkeyCache { where I: Iterator + ExactSizeIterator, { - self.pubkey_bytes.reserve(validator_keys.len()); self.pubkeys.reserve(validator_keys.len()); self.indices.reserve(validator_keys.len()); @@ -127,7 +121,6 @@ impl ValidatorPubkeyCache { )); self.pubkeys.push(pubkey); - self.pubkey_bytes.push(pubkey_bytes); self.indices.insert(pubkey_bytes, i); } @@ -144,11 +137,6 @@ impl ValidatorPubkeyCache { self.get_index(pubkey).and_then(|index| self.get(index)) } - /// Get the public key (in bytes form) for a validator with index `i`. - pub fn get_pubkey_bytes(&self, i: usize) -> Option<&PublicKeyBytes> { - self.pubkey_bytes.get(i) - } - /// Get the index of a validator with `pubkey`. pub fn get_index(&self, pubkey: &PublicKeyBytes) -> Option { self.indices.get(pubkey).copied()