package octez-l2-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

let err_implementation_mismatch ~expected ~got =
  Format.kasprintf
    invalid_arg
    "Context implementation mismatch: expecting %s, got %s"
    expected
    got

open Context_sigs

type ('repo, 'tree) pvm_context_impl =
  (module Context_sigs.S with type repo = 'repo and type tree = 'tree)

let equiv (a, b) (c, d) = (Equality_witness.eq a c, Equality_witness.eq b d)

type hash = Context_hash.t

type 'a t =
  | Context : {
      index : ('a, 'repo) index;
      pvm_context_impl : ('repo, 'tree) pvm_context_impl;
      impl_name : string;
      tree : 'tree;
      equality_witness : ('repo, 'tree) equality_witness;
    }
      -> 'a t

type ro = [`Read] t

type rw = [`Read | `Write] t

let make ~index ~tree ~pvm_context_impl ~equality_witness ~impl_name =
  Context {index; tree; pvm_context_impl; equality_witness; impl_name}

let load :
    type tree repo.
    (repo, tree) pvm_context_impl ->
    cache_size:int ->
    'a Store_sigs.mode ->
    string ->
    'a t tzresult Lwt.t =
 fun (module Pvm_Context_Impl) ~cache_size mode path ->
  let open Lwt_result_syntax in
  let* index = Pvm_Context_Impl.load ~cache_size mode path in
  let equality_witness = Pvm_Context_Impl.equality_witness in
  let impl_name = Pvm_Context_Impl.impl_name in
  return
  @@ make
       ~index
       ~tree:(Pvm_Context_Impl.PVMState.empty ())
       ~pvm_context_impl:(module Pvm_Context_Impl)
       ~equality_witness
       ~impl_name

let index c = c

let close (type a)
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} : a t) :
    unit Lwt.t =
  Pvm_Context_Impl.close index

let readonly (type a)
    (Context ({pvm_context_impl = (module Pvm_Context_Impl); index; _} as o) :
      a t) : ro =
  Context {o with index = Pvm_Context_Impl.readonly index}

let checkout (type a)
    (Context ({pvm_context_impl = (module Pvm_Context_Impl); index; _} as o) :
      a t) hash : a t option Lwt.t =
  let open Lwt_syntax in
  let+ ctx = Pvm_Context_Impl.checkout index hash in
  match ctx with
  | None -> None
  | Some {index; tree} -> Some (Context {o with index; tree})

let empty (type a)
    (Context ({pvm_context_impl = (module Pvm_Context_Impl); index; _} as o) :
      a t) : a t =
  let {index; tree} = Pvm_Context_Impl.empty index in
  Context {o with index; tree}

let commit ?message
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; tree; _} :
      [> `Write] t) =
  Pvm_Context_Impl.commit ?message {index; tree}

let is_gc_finished
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} :
      [> `Write] t) =
  Pvm_Context_Impl.is_gc_finished index

let split (type a)
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} : a t) =
  Pvm_Context_Impl.split index

let gc
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} :
      [> `Write] t) ?callback hash =
  Pvm_Context_Impl.gc index ?callback hash

let wait_gc_completion
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} :
      [> `Write] t) =
  Pvm_Context_Impl.wait_gc_completion index

let export_snapshot (type a)
    (Context {pvm_context_impl = (module Pvm_Context_Impl); index; _} : a t) =
  Pvm_Context_Impl.export_snapshot index

type pvmstate =
  | PVMState : {
      pvm_context_impl : ('repo, 'tree) pvm_context_impl;
      impl_name : string;
      pvmstate : 'tree;
      equality_witness : ('repo, 'tree) equality_witness;
    }
      -> pvmstate

let make_pvmstate ~pvm_context_impl ~equality_witness ~impl_name ~pvmstate =
  PVMState {pvm_context_impl; impl_name; pvmstate; equality_witness}

(** State of the PVM that this rollup node deals with *)
module PVMState = struct
  type value = pvmstate

  let empty : type a. a t -> value =
   fun (Context
         {
           pvm_context_impl = (module Pvm_Context_Impl);
           equality_witness;
           impl_name;
           _;
         }) ->
    make_pvmstate
      ~pvm_context_impl:(module Pvm_Context_Impl)
      ~equality_witness
      ~pvmstate:(Pvm_Context_Impl.PVMState.empty ())
      ~impl_name

  let find : type a. a t -> value option Lwt.t =
   fun (Context
         {
           pvm_context_impl = (module Pvm_Context_Impl);
           index;
           tree;
           equality_witness;
           impl_name;
           _;
         }) ->
    let open Lwt_syntax in
    let+ pvmstate = Pvm_Context_Impl.PVMState.find {index; tree} in
    match pvmstate with
    | None -> None
    | Some pvmstate ->
        Some
          (make_pvmstate
             ~pvm_context_impl:(module Pvm_Context_Impl)
             ~equality_witness
             ~pvmstate
             ~impl_name)

  let lookup : value -> string list -> bytes option Lwt.t =
   fun (PVMState {pvm_context_impl = (module Pvm_Context_Impl); pvmstate; _})
       path ->
    Pvm_Context_Impl.PVMState.lookup pvmstate path

  let set : type a. a t -> value -> a t Lwt.t =
   fun (Context
         ({pvm_context_impl = (module Pvm_Context_Impl); index; tree; _} as o1))
       (PVMState o2) ->
    let open Lwt_syntax in
    match equiv o1.equality_witness o2.equality_witness with
    | Some Refl, Some Refl ->
        let+ ctxt = Pvm_Context_Impl.PVMState.set {index; tree} o2.pvmstate in
        Context {o1 with index = ctxt.index; tree = ctxt.tree}
    | _ -> err_implementation_mismatch ~expected:o1.impl_name ~got:o2.impl_name
end

module Internal_for_tests = struct
  let get_a_tree : (module Context_sigs.S) -> string -> pvmstate Lwt.t =
   fun (module Pvm_Context_Impl) key ->
    let open Lwt_syntax in
    let+ tree = Pvm_Context_Impl.Internal_for_tests.get_a_tree key in
    make_pvmstate
      ~pvm_context_impl:(module Pvm_Context_Impl)
      ~equality_witness:Pvm_Context_Impl.equality_witness
      ~impl_name:Pvm_Context_Impl.impl_name
      ~pvmstate:tree
end

module Version = struct
  type t = V0

  let version = V0

  let encoding =
    let open Data_encoding in
    conv_with_guard
      (fun V0 -> 0)
      (function
        | 0 -> Ok V0
        | v -> Error ("Unsupported context version " ^ string_of_int v))
      int31

  let check = function V0 -> Result.return_unit

  let to_string = function V0 -> "0"
end
OCaml

Innovation. Community. Security.