use crate::{Error, NodeCodec};
use hash_db::Hasher;
use hashbrown::HashSet;
use nohash_hasher::BuildNoHashHasher;
use parking_lot::{Mutex, MutexGuard, RwLockReadGuard};
use shared_cache::{SharedValueCache, ValueCacheKey};
use std::{
collections::{hash_map::Entry as MapEntry, HashMap},
sync::Arc,
};
use trie_db::{node::NodeOwned, CachedValue};
mod shared_cache;
pub use shared_cache::SharedTrieCache;
use self::shared_cache::{SharedTrieCacheInner, ValueCacheKeyHash};
const LOG_TARGET: &str = "trie-cache";
#[derive(Debug, Clone, Copy)]
pub enum CacheSize {
Unlimited,
Maximum(usize),
}
impl CacheSize {
fn exceeds(&self, current_size: usize) -> bool {
match self {
Self::Unlimited => false,
Self::Maximum(max) => *max < current_size,
}
}
}
pub struct LocalTrieCache<H: Hasher> {
shared: SharedTrieCache<H>,
node_cache: Mutex<HashMap<H::Out, NodeOwned<H::Out>>>,
shared_node_cache_access: Mutex<HashSet<H::Out>>,
value_cache: Mutex<
HashMap<
ValueCacheKey<'static, H::Out>,
CachedValue<H::Out>,
BuildNoHashHasher<ValueCacheKey<'static, H::Out>>,
>,
>,
shared_value_cache_access:
Mutex<HashSet<ValueCacheKeyHash, BuildNoHashHasher<ValueCacheKeyHash>>>,
}
impl<H: Hasher> LocalTrieCache<H> {
pub fn as_trie_db_cache(&self, storage_root: H::Out) -> TrieCache<'_, H> {
let shared_inner = self.shared.read_lock_inner();
let value_cache = ValueCache::ForStorageRoot {
storage_root,
local_value_cache: self.value_cache.lock(),
shared_value_cache_access: self.shared_value_cache_access.lock(),
};
TrieCache {
shared_inner,
local_cache: self.node_cache.lock(),
value_cache,
shared_node_cache_access: self.shared_node_cache_access.lock(),
}
}
pub fn as_trie_db_mut_cache(&self) -> TrieCache<'_, H> {
TrieCache {
shared_inner: self.shared.read_lock_inner(),
local_cache: self.node_cache.lock(),
value_cache: ValueCache::Fresh(Default::default()),
shared_node_cache_access: self.shared_node_cache_access.lock(),
}
}
}
impl<H: Hasher> Drop for LocalTrieCache<H> {
fn drop(&mut self) {
let mut shared_inner = self.shared.write_lock_inner();
shared_inner
.node_cache_mut()
.update(self.node_cache.lock().drain(), self.shared_node_cache_access.lock().drain());
shared_inner
.value_cache_mut()
.update(self.value_cache.lock().drain(), self.shared_value_cache_access.lock().drain());
}
}
enum ValueCache<'a, H> {
Fresh(HashMap<Arc<[u8]>, CachedValue<H>>),
ForStorageRoot {
shared_value_cache_access: MutexGuard<
'a,
HashSet<ValueCacheKeyHash, nohash_hasher::BuildNoHashHasher<ValueCacheKeyHash>>,
>,
local_value_cache: MutexGuard<
'a,
HashMap<
ValueCacheKey<'static, H>,
CachedValue<H>,
nohash_hasher::BuildNoHashHasher<ValueCacheKey<'static, H>>,
>,
>,
storage_root: H,
},
}
impl<H: AsRef<[u8]> + std::hash::Hash + Eq + Clone + Copy> ValueCache<'_, H> {
fn get<'a>(
&'a mut self,
key: &[u8],
shared_value_cache: &'a SharedValueCache<H>,
) -> Option<&CachedValue<H>> {
match self {
Self::Fresh(map) => map.get(key),
Self::ForStorageRoot { local_value_cache, shared_value_cache_access, storage_root } => {
let key = ValueCacheKey::new_ref(key, *storage_root);
local_value_cache
.get(unsafe {
std::mem::transmute::<&ValueCacheKey<'_, H>, &ValueCacheKey<'static, H>>(
&key,
)
})
.or_else(|| {
shared_value_cache.get(&key).map(|v| {
shared_value_cache_access.insert(key.get_hash());
v
})
})
},
}
}
fn insert(&mut self, key: &[u8], value: CachedValue<H>) {
match self {
Self::Fresh(map) => {
map.insert(key.into(), value);
},
Self::ForStorageRoot { local_value_cache, storage_root, .. } => {
local_value_cache.insert(ValueCacheKey::new_value(key, *storage_root), value);
},
}
}
}
pub struct TrieCache<'a, H: Hasher> {
shared_inner: RwLockReadGuard<'a, SharedTrieCacheInner<H>>,
shared_node_cache_access: MutexGuard<'a, HashSet<H::Out>>,
local_cache: MutexGuard<'a, HashMap<H::Out, NodeOwned<H::Out>>>,
value_cache: ValueCache<'a, H::Out>,
}
impl<'a, H: Hasher> TrieCache<'a, H> {
pub fn merge_into(self, local: &LocalTrieCache<H>, storage_root: H::Out) {
let cache = if let ValueCache::Fresh(cache) = self.value_cache { cache } else { return };
if !cache.is_empty() {
let mut value_cache = local.value_cache.lock();
let partial_hash = ValueCacheKey::hash_partial_data(&storage_root);
cache
.into_iter()
.map(|(k, v)| {
let hash =
ValueCacheKeyHash::from_hasher_and_storage_key(partial_hash.clone(), &k);
(ValueCacheKey::Value { storage_key: k, storage_root, hash }, v)
})
.for_each(|(k, v)| {
value_cache.insert(k, v);
});
}
}
}
impl<'a, H: Hasher> trie_db::TrieCache<NodeCodec<H>> for TrieCache<'a, H> {
fn get_or_insert_node(
&mut self,
hash: H::Out,
fetch_node: &mut dyn FnMut() -> trie_db::Result<NodeOwned<H::Out>, H::Out, Error<H::Out>>,
) -> trie_db::Result<&NodeOwned<H::Out>, H::Out, Error<H::Out>> {
if let Some(res) = self.shared_inner.node_cache().get(&hash) {
tracing::trace!(target: LOG_TARGET, ?hash, "Serving node from shared cache");
self.shared_node_cache_access.insert(hash);
return Ok(res)
}
match self.local_cache.entry(hash) {
MapEntry::Occupied(res) => {
tracing::trace!(target: LOG_TARGET, ?hash, "Serving node from local cache");
Ok(res.into_mut())
},
MapEntry::Vacant(vacant) => {
let node = (*fetch_node)();
tracing::trace!(
target: LOG_TARGET,
?hash,
fetch_successful = node.is_ok(),
"Node not found, needed to fetch it."
);
Ok(vacant.insert(node?))
},
}
}
fn get_node(&mut self, hash: &H::Out) -> Option<&NodeOwned<H::Out>> {
if let Some(node) = self.shared_inner.node_cache().get(hash) {
tracing::trace!(target: LOG_TARGET, ?hash, "Getting node from shared cache");
self.shared_node_cache_access.insert(*hash);
return Some(node)
}
let res = self.local_cache.get(hash);
tracing::trace!(
target: LOG_TARGET,
?hash,
found = res.is_some(),
"Getting node from local cache"
);
res
}
fn lookup_value_for_key(&mut self, key: &[u8]) -> Option<&CachedValue<H::Out>> {
let res = self.value_cache.get(key, self.shared_inner.value_cache());
tracing::trace!(
target: LOG_TARGET,
key = ?sp_core::hexdisplay::HexDisplay::from(&key),
found = res.is_some(),
"Looked up value for key",
);
res
}
fn cache_value_for_key(&mut self, key: &[u8], data: CachedValue<H::Out>) {
tracing::trace!(
target: LOG_TARGET,
key = ?sp_core::hexdisplay::HexDisplay::from(&key),
"Caching value for key",
);
self.value_cache.insert(key.into(), data);
}
}
#[cfg(test)]
mod tests {
use super::*;
use trie_db::{Bytes, Trie, TrieDBBuilder, TrieDBMutBuilder, TrieHash, TrieMut};
type MemoryDB = crate::MemoryDB<sp_core::Blake2Hasher>;
type Layout = crate::LayoutV1<sp_core::Blake2Hasher>;
type Cache = super::SharedTrieCache<sp_core::Blake2Hasher>;
type Recorder = crate::recorder::Recorder<sp_core::Blake2Hasher>;
const TEST_DATA: &[(&[u8], &[u8])] =
&[(b"key1", b"val1"), (b"key2", &[2; 64]), (b"key3", b"val3"), (b"key4", &[4; 64])];
const CACHE_SIZE_RAW: usize = 1024 * 10;
const CACHE_SIZE: CacheSize = CacheSize::Maximum(CACHE_SIZE_RAW);
fn create_trie() -> (MemoryDB, TrieHash<Layout>) {
let mut db = MemoryDB::default();
let mut root = Default::default();
{
let mut trie = TrieDBMutBuilder::<Layout>::new(&mut db, &mut root).build();
for (k, v) in TEST_DATA {
trie.insert(k, v).expect("Inserts data");
}
}
(db, root)
}
#[test]
fn basic_cache_works() {
let (db, root) = create_trie();
let shared_cache = Cache::new(CACHE_SIZE);
let local_cache = shared_cache.local_cache();
{
let mut cache = local_cache.as_trie_db_cache(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root).with_cache(&mut cache).build();
assert_eq!(TEST_DATA[0].1.to_vec(), trie.get(TEST_DATA[0].0).unwrap().unwrap());
}
assert!(shared_cache.read_lock_inner().value_cache().lru.is_empty());
assert!(shared_cache.read_lock_inner().node_cache().lru.is_empty());
drop(local_cache);
assert!(shared_cache.read_lock_inner().node_cache().lru.len() >= 1);
let cached_data = shared_cache
.read_lock_inner()
.value_cache()
.lru
.peek(&ValueCacheKey::new_value(TEST_DATA[0].0, root))
.unwrap()
.clone();
assert_eq!(Bytes::from(TEST_DATA[0].1.to_vec()), cached_data.data().flatten().unwrap());
let fake_data = Bytes::from(&b"fake_data"[..]);
let local_cache = shared_cache.local_cache();
shared_cache.write_lock_inner().value_cache_mut().lru.put(
ValueCacheKey::new_value(TEST_DATA[1].0, root),
(fake_data.clone(), Default::default()).into(),
);
{
let mut cache = local_cache.as_trie_db_cache(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root).with_cache(&mut cache).build();
assert_eq!(b"fake_data".to_vec(), trie.get(TEST_DATA[1].0).unwrap().unwrap());
}
}
#[test]
fn trie_db_mut_cache_works() {
let (mut db, root) = create_trie();
let new_key = b"new_key".to_vec();
let new_value = vec![23; 64];
let shared_cache = Cache::new(CACHE_SIZE);
let mut new_root = root;
{
let local_cache = shared_cache.local_cache();
let mut cache = local_cache.as_trie_db_mut_cache();
{
let mut trie = TrieDBMutBuilder::<Layout>::from_existing(&mut db, &mut new_root)
.with_cache(&mut cache)
.build();
trie.insert(&new_key, &new_value).unwrap();
}
cache.merge_into(&local_cache, new_root);
}
let cached_data = shared_cache
.read_lock_inner()
.value_cache()
.lru
.peek(&ValueCacheKey::new_value(new_key, new_root))
.unwrap()
.clone();
assert_eq!(Bytes::from(new_value), cached_data.data().flatten().unwrap());
}
#[test]
fn trie_db_cache_and_recorder_work_together() {
let (db, root) = create_trie();
let shared_cache = Cache::new(CACHE_SIZE);
for i in 0..5 {
if i == 2 {
shared_cache.reset_node_cache();
} else if i == 3 {
shared_cache.reset_value_cache();
}
let local_cache = shared_cache.local_cache();
let recorder = Recorder::default();
{
let mut cache = local_cache.as_trie_db_cache(root);
let mut recorder = recorder.as_trie_recorder(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root)
.with_cache(&mut cache)
.with_recorder(&mut recorder)
.build();
for (key, value) in TEST_DATA {
assert_eq!(*value, trie.get(&key).unwrap().unwrap());
}
}
let storage_proof = recorder.drain_storage_proof();
let memory_db: MemoryDB = storage_proof.into_memory_db();
{
let trie = TrieDBBuilder::<Layout>::new(&memory_db, &root).build();
for (key, value) in TEST_DATA {
assert_eq!(*value, trie.get(&key).unwrap().unwrap());
}
}
}
}
#[test]
fn trie_db_mut_cache_and_recorder_work_together() {
const DATA_TO_ADD: &[(&[u8], &[u8])] = &[(b"key11", &[45; 78]), (b"key33", &[78; 89])];
let (db, root) = create_trie();
let shared_cache = Cache::new(CACHE_SIZE);
for i in 0..5 {
if i == 2 {
shared_cache.reset_node_cache();
} else if i == 3 {
shared_cache.reset_value_cache();
}
let recorder = Recorder::default();
let local_cache = shared_cache.local_cache();
let mut new_root = root;
{
let mut db = db.clone();
let mut cache = local_cache.as_trie_db_cache(root);
let mut recorder = recorder.as_trie_recorder(root);
let mut trie = TrieDBMutBuilder::<Layout>::from_existing(&mut db, &mut new_root)
.with_cache(&mut cache)
.with_recorder(&mut recorder)
.build();
for (key, value) in DATA_TO_ADD {
trie.insert(key, value).unwrap();
}
}
let storage_proof = recorder.drain_storage_proof();
let mut memory_db: MemoryDB = storage_proof.into_memory_db();
let mut proof_root = root;
{
let mut trie =
TrieDBMutBuilder::<Layout>::from_existing(&mut memory_db, &mut proof_root)
.build();
for (key, value) in DATA_TO_ADD {
trie.insert(key, value).unwrap();
}
}
assert_eq!(new_root, proof_root)
}
}
#[test]
fn cache_lru_works() {
let (db, root) = create_trie();
let shared_cache = Cache::new(CACHE_SIZE);
{
let local_cache = shared_cache.local_cache();
let mut cache = local_cache.as_trie_db_cache(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root).with_cache(&mut cache).build();
for (k, _) in TEST_DATA {
trie.get(k).unwrap().unwrap();
}
}
assert!(shared_cache
.read_lock_inner()
.value_cache()
.lru
.iter()
.map(|d| d.0)
.all(|l| TEST_DATA.iter().any(|d| l.storage_key().unwrap() == d.0)));
for _ in 0..2 {
{
let local_cache = shared_cache.local_cache();
let mut cache = local_cache.as_trie_db_cache(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root).with_cache(&mut cache).build();
for (k, _) in TEST_DATA.iter().take(2) {
trie.get(k).unwrap().unwrap();
}
}
assert!(shared_cache
.read_lock_inner()
.value_cache()
.lru
.iter()
.take(2)
.map(|d| d.0)
.all(|l| { TEST_DATA.iter().take(2).any(|d| l.storage_key().unwrap() == d.0) }));
shared_cache.reset_value_cache();
}
let most_recently_used_nodes = shared_cache
.read_lock_inner()
.node_cache()
.lru
.iter()
.map(|d| *d.0)
.collect::<Vec<_>>();
{
let local_cache = shared_cache.local_cache();
let mut cache = local_cache.as_trie_db_cache(root);
let trie = TrieDBBuilder::<Layout>::new(&db, &root).with_cache(&mut cache).build();
for (k, _) in TEST_DATA.iter().skip(2) {
trie.get(k).unwrap().unwrap();
}
}
assert_ne!(
most_recently_used_nodes,
shared_cache
.read_lock_inner()
.node_cache()
.lru
.iter()
.map(|d| *d.0)
.collect::<Vec<_>>()
);
}
#[test]
fn cache_respects_bounds() {
let (mut db, root) = create_trie();
let shared_cache = Cache::new(CACHE_SIZE);
{
let local_cache = shared_cache.local_cache();
let mut new_root = root;
{
let mut cache = local_cache.as_trie_db_cache(root);
{
let mut trie =
TrieDBMutBuilder::<Layout>::from_existing(&mut db, &mut new_root)
.with_cache(&mut cache)
.build();
let value = vec![10u8; 100];
for i in 0..CACHE_SIZE_RAW / 100 * 2 {
trie.insert(format!("key{}", i).as_bytes(), &value).unwrap();
}
}
cache.merge_into(&local_cache, new_root);
}
}
let node_cache_size = shared_cache.read_lock_inner().node_cache().size_in_bytes;
let value_cache_size = shared_cache.read_lock_inner().value_cache().size_in_bytes;
assert!(node_cache_size + value_cache_size < CACHE_SIZE_RAW);
}
}