Module Mavryk_store_unix.Store

Store abstraction over the disk storage.

Description

This component handles the on-disk storage of static objects such as blocks, operations, block's metadata, protocols and chain data. The store also handle the chain's current state: current head, invalid blocks, active testchains, ...

Concurrency

This module is designed to handle concurrent accesses to the store. Data-races and deadlocks might still happen through the use of potential dangerous calls that are documented as such. Both a mutex and a lockfile are present to handle concurrent accesses at process levels.

Context handling

The store manages the context and thus handles the context initialization through Lib_context.Context.init. This is done, among others reasons, for a consistency checking when storing a block. The store never commits a context on-disk.

History mode handling

This store handles the three different Mavryk_base.History_mode.t:

Protocol store

The store has a global protocol store which may be shared by different chain stores.

Chain stores and merging

The store has a toplevel chain store which is called the main_chain_store and which can be accessed through the store's interface. It is either created if the associated directory is not present or loaded if it was previously created. This main_chain_store is able to recursively spawn testchains which are also chain stores. Those testchains are also recursively able to spawn their own testchains.

Each chain store possesses its own block store and therefore its own historic. It also maintains a state throughout its run which is composed of:

More details and invariants on these points are provided in the Chain module description below.

When a block is promoted as head of the chain (through Chain.set_head) the following happens:

A merge phase consists of establishing the interval of blocks to cement, which is trivially last_preserved_block_level(new_head) to last_preserved_block_level(prev_head), but also, for Full and Rolling history modes, keep some extra blocks so that we make sure to keep blocks above max_operation_ttl(last_preserved_block_level(checkpoint)). This is done to make sure that we can export snapshots at the checkpoint level later on. This merging operation is asynchronous, the changes will be committed on disk only when the merge succeeds. Before that, we only retain the changes in RAM so we may keep storing new blocks without blocking. If the process crashes while a merge is happening, the state is reloaded before the merging point. More details are given in Chain.set_head.

Files hierarchy

The store directory is organized as follows:

chain_store_dir is a symbol for all chain stores directory hierarchy.

type t

The abstract type to manipulate the global store.

type store = t

The type alias for the global store.

type chain_store

The abstract type for a chain store. Equivalent to Chain.t.

Initialization

val init : ?patch_context: (Mavryk_protocol_environment.Context.t -> Mavryk_protocol_environment.Context.t Mavryk_base.TzPervasives.tzresult Lwt.t) -> ?commit_genesis: (chain_id:Mavryk_base.TzPervasives.Chain_id.t -> Mavryk_base.TzPervasives.Context_hash.t Mavryk_base.TzPervasives.tzresult Lwt.t) -> ?history_mode:Mavryk_shell_services.History_mode.t -> ?readonly:bool -> ?block_cache_limit:int -> store_dir:string -> context_dir:string -> allow_testchains:bool -> Mavryk_base.TzPervasives.Genesis.t -> store Mavryk_base.TzPervasives.tzresult Lwt.t

init ?patch_context ?commit_genesis ?history_mode ?block_cache_limit ~store_dir ~context_dir ~allow_testchains genesis initializes the store and a main chain store. If store_dir (resp. context_dir) does not exist, a fresh store (resp. context) is created. Otherwise, it loads the store (resp. context) from reading the adequate directory. If allow_testchains is passed, the store will be able to fork chains and instantiate testchain's sub chain stores, for all chains contained in the store. The chain store created is based on the genesis provided. Its chain identifier will be computed using the Chain_id.of_block_hash function.

  • parameter patch_context

    the handle called when initializing the context. It usually is passed when creating a sandboxed chain.

  • parameter commit_genesis

    overrides the default initial genesis commit when the node is created. This is used for when the context must be in readonly (e.g. started by an external validator) and we want to prevent writing in the context. Warning passing this argument will initialize the context in readonly. Default: Context.commit_genesis is called with genesis

  • parameter history_mode

    the history mode used throughout the store. If a directory already exists and the given history_mode is different, the initialization will fail. Default: History_mode.default (which should correspond to full with 5 extra preserved cycles.)

  • parameter block_cache_limit

    allows to override the size of the block cache to use. The minimal value is 1.

  • parameter readonly

    a flag that, if set to true, prevent writing throughout the store and context. Default: false

val main_chain_store : store -> chain_store

main_chain_store global_store returns the main chain store.

val close_store : store -> unit Lwt.t

close_store global_store closes the underlying block store and context along with every opened file descriptors for every chain store and testchain present in global_store. If the store is already closed, this function is idempotent.

val may_switch_history_mode : store_dir:string -> context_dir:string -> Mavryk_base.TzPervasives.Genesis.t -> new_history_mode:Mavryk_shell_services.History_mode.t -> unit Mavryk_base.TzPervasives.tzresult Lwt.t

may_switch_history_mode ?patch_context ~store_dir ~context_dir genesis ~new_history_mode tries switching the store located at store_dir (if present) to new_history_mode when possible.

Accessors

val directory : store -> [ `Store_dir ] Mavryk_store_shared.Naming.directory

directory global_store returns the path where global_store is stored. This corresponds to the store_dir argument passed to the init function.

context_index global_store returns the context's index initialized in global_store.

val allow_testchains : store -> bool

allow_testchains global_store returns true if the store is allowed to fork testchains.

val all_chain_stores : store -> chain_store list Lwt.t

all_chain_stores global_store returns every initialized chain store in global_store. The resulting list also comprises the initialized testchains. If allow_testchains is false, the list will only contain a single element.

get_chain_store global_store chain_id returns the initialized chain store in global_store associated to chain_id.

val get_chain_store_opt : store -> Mavryk_base.TzPervasives.Chain_id.t -> chain_store option Lwt.t

get_chain_store_opt global_store chain_id optional version of get_chain_store.

val make_pp_store : store -> (Stdlib.Format.formatter -> unit -> unit) Lwt.t
val make_pp_chain_store : chain_store -> (Stdlib.Format.formatter -> unit -> unit) Lwt.t
module Block : sig ... end

The module for handling block-related operations such as storing and reading data associated to a single block.

module Chain : sig ... end

The module for handling chain-related operations such as setting the head of the chain, updating the chain state, forking testchains, ...

val global_block_watcher : store -> (Chain.chain_store * Block.block) Lwt_stream.t * Lwt_watcher.stopper

global_block_watcher global_store instantiates a new block watcher for every chain store active in global_store.

module Protocol : sig ... end

The module for handling protocol-related operations.

module Chain_traversal : sig ... end

The utility module used to traverse the chain.

val v_3_0_upgrade : store_dir:string -> Mavryk_base.TzPervasives.Genesis.t -> unit Mavryk_base.TzPervasives.tzresult Lwt.t

Upgrade a v_2 to v_3 store by rewriting the block store and the protocol level's table.

Warning Not backward-compatible.