ligo/src/lib_storage/context.ml

268 lines
8.2 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
2018-02-06 00:17:03 +04:00
(* Copyright (c) 2014 - 2018. *)
2016-09-08 21:13:10 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
2016-10-06 14:55:38 +04:00
(** Tezos - Versioned (key x value) store (over Irmin) *)
2016-09-08 21:13:10 +04:00
module IrminPath = Irmin.Path.String_list
module MBytesContent = struct
2017-07-17 17:59:09 +04:00
type t = MBytes.t
let t =
Irmin.Type.(like cstruct)
(fun x -> Cstruct.to_bigarray x)
(fun x -> Cstruct.of_bigarray x)
let merge = Irmin.Merge.default Irmin.Type.(option t)
let pp ppf b = Format.pp_print_string ppf (MBytes.to_string b)
let of_string s = Ok (MBytes.of_string s)
end
2016-09-08 21:13:10 +04:00
module Metadata = struct
type t = unit
let t = Irmin.Type.unit
let default = ()
let merge = Irmin.Merge.default t
end
module IrminBlake2B : Irmin.Hash.S with type t = Context_hash.t = struct
type t = Context_hash.t
let digest_size = Context_hash.size
let to_raw t = Cstruct.of_bigarray (Context_hash.to_bytes t)
let of_raw t =
match Context_hash.of_bytes_opt (Cstruct.to_bigarray t) with
| Some t -> t
| None ->
let str = Cstruct.to_string t in
Format.kasprintf invalid_arg "%s (%d)" str (String.length str)
let t = Irmin.Type.like Irmin.Type.cstruct of_raw to_raw
let digest t x =
Context_hash.hash_bytes
[Cstruct.to_bigarray (Irmin.Type.encode_cstruct t x)]
let pp = Context_hash.pp
let of_string x =
match Context_hash.of_b58check_exn x with
| exception (Invalid_argument s) -> Error (`Msg s)
| h -> Ok h
let has_kind = function
| `SHA1 -> true
| _ -> false
let to_raw_int c =
Int64.to_int @@ MBytes.get_int64 (Context_hash.to_bytes c) 0
end
2017-07-17 17:59:09 +04:00
module GitStore =
Irmin_leveldb.Make
(Metadata)
(MBytesContent)
(Irmin.Path.String_list)
(Irmin.Branch.String)
(IrminBlake2B)
2016-09-08 21:13:10 +04:00
type index = {
path: string ;
repo: GitStore.Repo.t ;
patch_context: context -> context Lwt.t ;
}
2017-07-17 17:59:09 +04:00
and context = {
index: index ;
2017-07-17 17:59:09 +04:00
parents: GitStore.Commit.t list ;
tree: GitStore.tree ;
}
type t = context
2016-09-08 21:13:10 +04:00
(*-- Version Access and Update -----------------------------------------------*)
let current_protocol_key = ["protocol"]
2018-02-16 04:26:24 +04:00
let current_test_chain_key = ["test_chain"]
2016-09-08 21:13:10 +04:00
2017-07-17 17:59:09 +04:00
let exists index key =
GitStore.Commit.of_hash index.repo key >>= function
| None -> Lwt.return_false
| Some _ -> Lwt.return_true
2016-09-08 21:13:10 +04:00
let checkout index key =
2017-07-17 17:59:09 +04:00
GitStore.Commit.of_hash index.repo key >>= function
| None -> Lwt.return_none
| Some commit ->
GitStore.Commit.tree commit >>= fun tree ->
let ctxt = { index ; tree ; parents = [commit] } in
index.patch_context ctxt >>= fun ctxt ->
Lwt.return (Some ctxt)
2016-09-08 21:13:10 +04:00
let checkout_exn index key =
checkout index key >>= function
| None -> Lwt.fail Not_found
| Some p -> Lwt.return p
2016-09-08 21:13:10 +04:00
let raw_commit ~time ?(message = "") context =
2017-07-17 17:59:09 +04:00
let info =
Irmin.Info.v ~date:(Time.to_seconds time) ~author:"Tezos" message in
GitStore.Commit.v
context.index.repo ~info ~parents:context.parents context.tree
let commit ~time ?message context =
raw_commit ~time ?message context >>= fun commit ->
Lwt.return (GitStore.Commit.hash commit)
2016-09-08 21:13:10 +04:00
(*-- Generic Store Primitives ------------------------------------------------*)
let data_key key = "data" :: key
let undata_key = function
| "data" :: key -> key
| _ -> assert false
type key = string list
type value = MBytes.t
let mem ctxt key =
2017-07-17 17:59:09 +04:00
GitStore.Tree.mem ctxt.tree (data_key key) >>= fun v ->
2016-09-08 21:13:10 +04:00
Lwt.return v
let dir_mem ctxt key =
2017-07-17 17:59:09 +04:00
GitStore.Tree.mem_tree ctxt.tree (data_key key) >>= fun v ->
Lwt.return v
2017-07-17 17:59:09 +04:00
let raw_get ctxt key =
GitStore.Tree.find ctxt.tree key
2016-09-08 21:13:10 +04:00
let get t key = raw_get t (data_key key)
let raw_set ctxt key data =
2017-07-17 17:59:09 +04:00
GitStore.Tree.add ctxt.tree key data >>= fun tree ->
Lwt.return { ctxt with tree }
2016-09-08 21:13:10 +04:00
let set t key data = raw_set t (data_key key) data
let raw_del ctxt key =
2017-07-17 17:59:09 +04:00
GitStore.Tree.remove ctxt.tree key >>= fun tree ->
Lwt.return { ctxt with tree }
2016-09-08 21:13:10 +04:00
let del t key = raw_del t (data_key key)
let remove_rec ctxt key =
2017-07-17 17:59:09 +04:00
GitStore.Tree.remove ctxt.tree (data_key key) >>= fun tree ->
Lwt.return { ctxt with tree }
2016-09-08 21:13:10 +04:00
let fold ctxt key ~init ~f =
GitStore.Tree.list ctxt.tree (data_key key) >>= fun keys ->
Lwt_list.fold_left_s
begin fun acc (name, kind) ->
let key =
match kind with
| `Contents -> `Key (key @ [name])
| `Node -> `Dir (key @ [name]) in
f key acc
end
init keys
(*-- Predefined Fields -------------------------------------------------------*)
let get_protocol v =
raw_get v current_protocol_key >>= function
| None -> assert false
| Some data -> Lwt.return (Protocol_hash.of_bytes_exn data)
let set_protocol v key =
raw_set v current_protocol_key (Protocol_hash.to_bytes key)
2018-02-16 04:26:24 +04:00
let get_test_chain v =
raw_get v current_test_chain_key >>= function
| None -> Lwt.fail (Failure "Unexpected error (Context.get_test_chain)")
| Some data ->
2018-02-16 04:26:24 +04:00
match Data_encoding.Binary.of_bytes Test_chain_status.encoding data with
| None -> Lwt.fail (Failure "Unexpected error (Context.get_test_chain)")
| Some r -> Lwt.return r
2018-02-16 04:26:24 +04:00
let set_test_chain v id =
raw_set v current_test_chain_key
(Data_encoding.Binary.to_bytes Test_chain_status.encoding id)
let del_test_chain v = raw_del v current_test_chain_key
2018-02-16 04:26:24 +04:00
let fork_test_chain v ~protocol ~expiration =
set_test_chain v (Forking { protocol ; expiration })
2016-09-08 21:13:10 +04:00
(*-- Initialisation ----------------------------------------------------------*)
let init ?patch_context ~root =
2017-07-17 17:59:09 +04:00
GitStore.Repo.v
(Irmin_leveldb.config root) >>= fun repo ->
Lwt.return {
path = root ;
repo ;
patch_context =
2016-09-08 21:13:10 +04:00
match patch_context with
| None -> (fun ctxt -> Lwt.return ctxt)
| Some patch_context -> patch_context
}
2016-09-08 21:13:10 +04:00
2018-02-16 04:26:24 +04:00
let get_branch chain_id = Format.asprintf "%a" Chain_id.pp chain_id
2017-07-17 17:59:09 +04:00
2018-02-16 04:26:24 +04:00
let commit_genesis index ~chain_id ~time ~protocol =
2017-07-17 17:59:09 +04:00
let tree = GitStore.Tree.empty in
let ctxt = { index ; tree ; parents = [] } in
index.patch_context ctxt >>= fun ctxt ->
set_protocol ctxt protocol >>= fun ctxt ->
2018-02-16 04:26:24 +04:00
set_test_chain ctxt Not_running >>= fun ctxt ->
2017-07-17 17:59:09 +04:00
raw_commit ~time ~message:"Genesis" ctxt >>= fun commit ->
2018-02-16 04:26:24 +04:00
GitStore.Branch.set index.repo (get_branch chain_id) commit >>= fun () ->
2017-07-17 17:59:09 +04:00
Lwt.return (GitStore.Commit.hash commit)
2016-09-08 21:13:10 +04:00
2018-02-16 04:26:24 +04:00
let compute_testchain_genesis forked_block =
let genesis = Block_hash.hash_bytes [Block_hash.to_bytes forked_block] in
2018-02-16 04:26:24 +04:00
let chain_id = Chain_id.of_block_hash genesis in
chain_id, genesis
2016-09-08 21:13:10 +04:00
2018-02-16 04:26:24 +04:00
let commit_test_chain_genesis index forked_block time ctxt =
let chain_id, genesis = compute_testchain_genesis forked_block in
let branch = get_branch chain_id in
let message = Format.asprintf "Forking testchain: %s." branch in
2017-07-17 17:59:09 +04:00
raw_commit ~time ~message ctxt >>= fun commit ->
GitStore.Branch.set index.repo branch commit >>= fun () ->
2018-02-16 04:26:24 +04:00
return (chain_id, genesis, GitStore.Commit.hash commit)
2018-02-16 04:26:24 +04:00
let reset_test_chain ctxt forked_block timestamp =
get_test_chain ctxt >>= function
| Not_running -> Lwt.return ctxt
| Running { expiration } ->
if Time.(expiration <= timestamp) then
2018-02-16 04:26:24 +04:00
set_test_chain ctxt Not_running
else
Lwt.return ctxt
| Forking { protocol ; expiration } ->
2018-02-16 04:26:24 +04:00
let chain_id, genesis = compute_testchain_genesis forked_block in
set_test_chain ctxt
(Running { chain_id ; genesis ;
protocol ; expiration })
2017-07-17 17:59:09 +04:00
2018-02-16 04:26:24 +04:00
let clear_test_chain index chain_id =
2017-07-17 17:59:09 +04:00
(* TODO remove commits... ??? *)
2018-02-16 04:26:24 +04:00
let branch = get_branch chain_id in
2017-07-17 17:59:09 +04:00
GitStore.Branch.remove index.repo branch
2018-02-16 04:26:24 +04:00
let set_head index chain_id commit =
let branch = get_branch chain_id in
2017-07-17 17:59:09 +04:00
GitStore.Commit.of_hash index.repo commit >>= function
| None -> assert false
| Some commit ->
GitStore.Branch.set index.repo branch commit
let set_master index commit =
GitStore.Commit.of_hash index.repo commit >>= function
| None -> assert false
| Some commit ->
GitStore.Branch.set index.repo GitStore.Branch.master commit