Persistent PubkeyCache on the state

This commit is contained in:
Michael Sproul
2022-02-11 18:15:34 +11:00
parent c97f6dcc06
commit 42e4675c97

View File

@@ -1,16 +1,15 @@
use crate::*; use crate::*;
use serde_derive::{Deserialize, Serialize}; use rpds::HashTrieMapSync as HashTrieMap;
use std::collections::HashMap;
type ValidatorIndex = usize; type ValidatorIndex = usize;
#[derive(Debug, PartialEq, Clone, Default, Serialize, Deserialize)] #[derive(Debug, PartialEq, Clone, Default)]
pub struct PubkeyCache { pub struct PubkeyCache {
/// Maintain the number of keys added to the map. It is not sufficient to just use the HashMap /// Maintain the number of keys added to the map. It is not sufficient to just use the
/// len, as it does not increase when duplicate keys are added. Duplicate keys are used during /// HashTrieMap len, as it does not increase when duplicate keys are added. Duplicate keys are
/// testing. /// used during testing.
len: usize, len: usize,
map: HashMap<PublicKeyBytes, ValidatorIndex>, map: HashTrieMap<PublicKeyBytes, ValidatorIndex>,
} }
impl PubkeyCache { impl PubkeyCache {
@@ -25,7 +24,7 @@ impl PubkeyCache {
/// that an index is never skipped. /// that an index is never skipped.
pub fn insert(&mut self, pubkey: PublicKeyBytes, index: ValidatorIndex) -> bool { pub fn insert(&mut self, pubkey: PublicKeyBytes, index: ValidatorIndex) -> bool {
if index == self.len { if index == self.len {
self.map.insert(pubkey, index); self.map.insert_mut(pubkey, index);
self.len = self self.len = self
.len .len
.checked_add(1) .checked_add(1)