package tezos-context

  1. Overview
  2. Docs

Source file context.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018-2021 Tarides <contact@tarides.com>                     *)
(* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Tezos_context_encoding.Context
module Store =
  Irmin_pack_mem.Make (Node) (Commit) (Conf) (Metadata) (Contents) (Path)
    (Branch)
    (Hash)

type index = Store.Repo.t

type context = {repo : index; parents : Store.Commit.t list; tree : Store.tree}

type t = context

type tree = Store.tree

type key = string list

type value = bytes

module Tree = Tezos_context_helpers.Context.Make_tree (Store)
include Tree

let index {repo; _} = repo

let exists index key =
  Store.Commit.of_hash index (Hash.of_context_hash key) >|= function
  | None -> false
  | Some _ -> true

let checkout index key =
  Store.Commit.of_hash index (Hash.of_context_hash key) >>= function
  | None -> Lwt.return_none
  | Some commit ->
      let tree = Store.Commit.tree commit in
      let ctxt = {repo = index; tree; parents = [commit]} in
      Lwt.return_some ctxt

let checkout_exn index key =
  checkout index key >>= function
  | None -> Lwt.fail Not_found
  | Some p -> Lwt.return p

(* unshallow possible 1-st level objects from previous partial
   checkouts ; might be better to pass directly the list of shallow
   objects. *)
let unshallow context =
  Store.Tree.list context.tree [] >>= fun children ->
  Store.Private.Repo.batch context.repo (fun x y _ ->
      List.iter_s
        (fun (s, k) ->
          match Store.Tree.destruct k with
          | `Contents _ -> Lwt.return ()
          | `Node _ ->
              Store.Tree.get_tree context.tree [s] >>= fun tree ->
              Store.save_tree ~clear:true context.repo x y tree >|= fun _ -> ())
        children)

let raw_commit ~time ?(message = "") context =
  let info =
    Irmin.Info.v ~date:(Time.Protocol.to_seconds time) ~author:"Tezos" message
  in
  let parents = List.map Store.Commit.hash context.parents in
  unshallow context >>= fun () ->
  Store.Commit.v context.repo ~info ~parents context.tree >|= fun h ->
  Store.Tree.clear context.tree ;
  h

let hash ~time ?(message = "") context =
  let info =
    Irmin.Info.v ~date:(Time.Protocol.to_seconds time) ~author:"Tezos" message
  in
  let parents = List.map (fun c -> Store.Commit.hash c) context.parents in
  let node = Store.Tree.hash context.tree in
  let commit = Store.Private.Commit.Val.v ~parents ~node ~info in
  let x = Store.Private.Commit.Key.hash commit in
  Hash.to_context_hash x

let commit ~time ?message context =
  raw_commit ~time ?message context >|= fun commit ->
  Hash.to_context_hash (Store.Commit.hash commit)

(*-- Generic Store Primitives ------------------------------------------------*)

let data_key key = "data" :: key

let mem ctxt key = Tree.mem ctxt.tree (data_key key)

let mem_tree ctxt key = Tree.mem_tree ctxt.tree (data_key key)

let list ctxt ?offset ?length key =
  Tree.list ctxt.tree ?offset ?length (data_key key)

let find ctxt key = Tree.find ctxt.tree (data_key key)

let raw_add ctxt key data =
  Tree.add ctxt.tree key data >|= fun tree -> {ctxt with tree}

let add ctxt key data = raw_add ctxt (data_key key) data

let raw_remove ctxt k = Tree.remove ctxt.tree k >|= fun tree -> {ctxt with tree}

let remove ctxt key = raw_remove ctxt (data_key key)

let find_tree ctxt key = Tree.find_tree ctxt.tree (data_key key)

let add_tree ctxt key tree =
  Tree.add_tree ctxt.tree (data_key key) tree >|= fun tree -> {ctxt with tree}

let fold ?depth ctxt key ~order ~init ~f =
  Tree.fold ?depth ctxt.tree (data_key key) ~order ~init ~f

let current_protocol_key = ["protocol"]

let current_predecessor_block_metadata_hash_key =
  ["predecessor_block_metadata_hash"]

let current_predecessor_ops_metadata_hash_key =
  ["predecessor_ops_metadata_hash"]

let get_protocol ctxt =
  Tree.find ctxt.tree current_protocol_key >>= function
  | None -> assert false
  | Some data -> Lwt.return (Protocol_hash.of_bytes_exn data)

let add_protocol ctxt key =
  let key = Protocol_hash.to_bytes key in
  raw_add ctxt current_protocol_key key

let get_hash_version _c = Context_hash.Version.of_int 0

let set_hash_version c v =
  if Context_hash.Version.(of_int 0 = v) then return c
  else fail (Tezos_context_helpers.Context.Unsupported_context_hash_version v)

let add_predecessor_block_metadata_hash v hash =
  let data =
    Data_encoding.Binary.to_bytes_exn Block_metadata_hash.encoding hash
  in
  raw_add v current_predecessor_block_metadata_hash_key data

let add_predecessor_ops_metadata_hash v hash =
  let data =
    Data_encoding.Binary.to_bytes_exn
      Operation_metadata_list_list_hash.encoding
      hash
  in
  raw_add v current_predecessor_ops_metadata_hash_key data

let create () =
  let cfg = Irmin_pack.config "/tmp" in
  let promise =
    Store.Repo.v cfg >>= fun repo ->
    Lwt.return {repo; parents = []; tree = Store.Tree.empty}
  in
  match Lwt.state promise with
  | Lwt.Return result -> result
  | Lwt.Fail exn -> raise exn
  | Lwt.Sleep ->
      (* The in-memory context should never block *)
      assert false

let empty = create ()

let concrete_encoding : Store.Tree.concrete Data_encoding.t =
  let open Data_encoding in
  mu "memory_context" (fun encoding ->
      let map_encoding = list (tup2 string encoding) in
      union
        [
          case
            ~title:"tree"
            (Tag 0)
            map_encoding
            (function `Tree map -> Some map | `Contents _ -> None)
            (fun map -> `Tree map);
          case
            ~title:"value"
            (Tag 1)
            bytes
            (function `Contents (v, _) -> Some v | `Tree _ -> None)
            (fun v -> `Contents (v, ()));
        ])

let encoding : t Data_encoding.t =
  Data_encoding.conv
    (fun t ->
      let tree = Store.Tree.to_concrete t.tree in
      let tree =
        (* This is safe as store.Tree will never call any blocking
           functions. *)
        match Lwt.state tree with Return t -> t | _ -> assert false
      in
      tree)
    (fun t ->
      let tree = Store.Tree.of_concrete t in
      let ctxt = create () in
      {ctxt with tree})
    concrete_encoding

let current_test_chain_key = ["test_chain"]

let get_test_chain v =
  Tree.find v.tree current_test_chain_key >>= function
  | None -> Lwt.fail (Failure "Unexpected error (Context.get_test_chain)")
  | Some data -> (
      match Data_encoding.Binary.of_bytes Test_chain_status.encoding data with
      | Error re ->
          Format.kasprintf
            (fun s -> Lwt.fail (Failure s))
            "Error in Context.get_test_chain: %a"
            Data_encoding.Binary.pp_read_error
            re
      | Ok r -> Lwt.return r)

let add_test_chain v id =
  let id = Data_encoding.Binary.to_bytes_exn Test_chain_status.encoding id in
  raw_add v current_test_chain_key id
OCaml

Innovation. Community. Security.