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