Skip to content

Commit

Permalink
Remove pubkey_bytes from ValidatorPubkeyCache
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit a0b3490
Author: Michael Sproul <michael@sigmaprime.io>
Date:   Mon Oct 28 12:03:58 2024 +1100

    Remove pubkey_bytes from ValidatorPubkeyCache
  • Loading branch information
michaelsproul committed Oct 28, 2024
1 parent ff9c459 commit 5dac510
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
16 changes: 10 additions & 6 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,9 +1558,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self,
validator_index: usize,
) -> Result<Option<PublicKeyBytes>, 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
Expand All @@ -1572,12 +1576,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&self,
validator_indices: &[usize],
) -> Result<HashMap<usize, PublicKeyBytes>, 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)
Expand Down
12 changes: 0 additions & 12 deletions beacon_node/beacon_chain/src/validator_pubkey_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use types::{BeaconState, FixedBytesExtended, Hash256, PublicKey, PublicKeyBytes}
pub struct ValidatorPubkeyCache<T: BeaconChainTypes> {
pubkeys: Vec<PublicKey>,
indices: HashMap<PublicKeyBytes, usize>,
pubkey_bytes: Vec<PublicKeyBytes>,
_phantom: PhantomData<T>,
}

Expand All @@ -35,7 +34,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
let mut cache = Self {
pubkeys: vec![],
indices: HashMap::new(),
pubkey_bytes: vec![],
_phantom: PhantomData,
};

Expand All @@ -49,7 +47,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
pub fn load_from_store(store: BeaconStore<T>) -> Result<Self, BeaconChainError> {
let mut pubkeys = vec![];
let mut indices = HashMap::new();
let mut pubkey_bytes = vec![];

for validator_index in 0.. {
if let Some(db_pubkey) =
Expand All @@ -58,7 +55,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
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;
}
Expand All @@ -67,7 +63,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
Ok(ValidatorPubkeyCache {
pubkeys,
indices,
pubkey_bytes,
_phantom: PhantomData,
})
}
Expand Down Expand Up @@ -101,7 +96,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
where
I: Iterator<Item = PublicKeyBytes> + ExactSizeIterator,
{
self.pubkey_bytes.reserve(validator_keys.len());
self.pubkeys.reserve(validator_keys.len());
self.indices.reserve(validator_keys.len());

Expand All @@ -127,7 +121,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
));

self.pubkeys.push(pubkey);
self.pubkey_bytes.push(pubkey_bytes);
self.indices.insert(pubkey_bytes, i);
}

Expand All @@ -144,11 +137,6 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {
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<usize> {
self.indices.get(pubkey).copied()
Expand Down

0 comments on commit 5dac510

Please sign in to comment.