Module Context.Cache

type key = cache_key

A key uniquely identifies a cached value in some subcache.

type value = cache_value = ..

Cached values inhabit an extensible type.

val key_of_identifier : cache_index:int -> string -> key

key_of_identifier ~cache_index identifier builds a key from the cache_index and the identifier.

No check are made to ensure the validity of the index.

val identifier_of_key : key -> string

identifier_of_key key returns the identifier associated to the key.

val pp : Format.formatter -> t -> unit

pp fmt cache is a pretty printter for a cache.

val find : t -> key -> value option Lwt.t

find ctxt k = Some v if v is the value associated to k in in the cache where k is. Returns None if there is no such value in the cache of k. This function is in the Lwt monad because if the value has not been constructed, it is constructed on the fly.

val set_cache_layout : t -> int list -> t Lwt.t

set_cache_layout ctxt layout sets the caches of ctxt to comply with given layout. If there was already a cache in ctxt, it is erased by the new layout.

Otherwise, a fresh collection of empty caches is reconstructed from the new layout. Notice that cache keys are invalidated in that case, i.e., get t k will return None.

val update : t -> key -> (value * int) option -> t

update ctxt k (Some (e, size)) returns a cache where the value e of size is associated to key k. If k is already in the cache, the cache entry is updated.

update ctxt k None removes k from the cache.

val sync : t -> cache_nonce:Bytes.t -> t Lwt.t

sync ctxt ~cache_nonce updates the context with the domain of the cache computed so far. Such function is expected to be called at the end of the validation of a block, when there is no more accesses to the cache.

cache_nonce identifies the block that introduced new cache entries. The nonce should identify uniquely the block which modifies this value. It cannot be the block hash for circularity reasons: The value of the nonce is stored onto the context and consequently influences the context hash of the very same block. Such nonce cannot be determined by the shell and its computation is delegated to the economic protocol.

val clear : t -> t

clear ctxt removes all cache entries.

Cache introspection

val list_keys : t -> cache_index:int -> (key * int) list option

list_keys ctxt ~cache_index returns the list of cached keys in cache numbered cache_index along with their respective size. The returned list is sorted in terms of their age in the cache, the oldest coming first. If cache_index is invalid, then this function returns None.

val key_rank : t -> key -> int option

key_rank index ctxt key returns the number of cached value older than the given key; or, None if the key is not a cache key.

Cache helpers for RPCs

val future_cache_expectation : t -> time_in_blocks:int -> t

future_cache_expectation ctxt ~time_in_blocks returns ctxt except that the entries of the caches that are presumably too old to still be in the caches in n_blocks are removed.

This function is based on a heuristic. The context maintains the median of the number of removed entries: this number is multipled by `n_blocks` to determine the entries that are likely to be removed in `n_blocks`.

val cache_size : t -> cache_index:int -> int option

cache_size ctxt ~cache_index returns an overapproximation of the size of the cache. Returns None if cache_index is not a valid cache index.

val cache_size_limit : t -> cache_index:int -> int option

cache_size_limit ctxt ~cache_index returns the maximal size of the cache indexed by cache_index. Returns None if cache_index is not a valid cache index.