package tezos-protocol-016-PtMumbai

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

Source file zk_rollup_storage.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type error +=
  | Zk_rollup_does_not_exist of Zk_rollup_repr.t
  | Zk_rollup_invalid_op_code of int
  | Zk_rollup_pending_list_too_short
  | Zk_rollup_negative_length

let () =
  register_error_kind
    `Temporary
    ~id:"Zk_rollup_does_not_exist"
    ~title:"ZK Rollup does not exist"
    ~description:"Attempted to use a ZK rollup that has not been originated."
    ~pp:(fun ppf x ->
      Format.fprintf ppf "Rollup %a does not exist" Zk_rollup_repr.Address.pp x)
    Data_encoding.(obj1 (req "rollup" Zk_rollup_repr.Address.encoding))
    (function Zk_rollup_does_not_exist x -> Some x | _ -> None)
    (fun x -> Zk_rollup_does_not_exist x) ;
  register_error_kind
    `Permanent
    ~id:"Zk_rollup_invalid_op code"
    ~title:"Invalid op code in append"
    ~description:"Invalid op code in append"
    ~pp:(fun ppf oc ->
      Format.fprintf ppf "Op code %d is not valid for this ZK Rollup" oc)
    Data_encoding.(obj1 (req "op_code" int31))
    (function Zk_rollup_invalid_op_code oc -> Some oc | _ -> None)
    (fun oc -> Zk_rollup_invalid_op_code oc) ;
  register_error_kind
    `Temporary
    ~id:"Zk_rollup_pending_list_too_short"
    ~title:"Pending list is too short"
    ~description:"Pending list is too short"
    Data_encoding.unit
    (function Zk_rollup_pending_list_too_short -> Some () | _ -> None)
    (fun () -> Zk_rollup_pending_list_too_short) ;
  register_error_kind
    `Permanent
    ~id:"Zk_rollup_negative_length"
    ~title:"Negative length for pending list prefix"
    ~description:"Negative length for pending list prefix"
    Data_encoding.unit
    (function Zk_rollup_negative_length -> Some () | _ -> None)
    (fun () -> Zk_rollup_negative_length)

let account = Storage.Zk_rollup.Account.get

let pending_list = Storage.Zk_rollup.Pending_list.get

let pending_op ctxt id = Storage.Zk_rollup.Pending_operation.get (ctxt, id)

let originate ctxt static ~init_state =
  let open Lwt_result_syntax in
  let*? ctxt, nonce = Raw_context.increment_origination_nonce ctxt in
  let*? address = Zk_rollup_repr.Address.from_nonce nonce in
  let origination_size = Constants_storage.zk_rollup_origination_size ctxt in
  let initial_account =
    Zk_rollup_account_repr.
      {
        static;
        dynamic =
          {
            state = init_state;
            paid_l2_operations_storage_space = Z.of_int origination_size;
            used_l2_operations_storage_space = Z.zero;
          };
      }
  in
  let* ctxt, account_size =
    Storage.Zk_rollup.Account.init ctxt address initial_account
  in
  let init_pl = Zk_rollup_repr.(Empty {next_index = 0L}) in
  let* ctxt, pl_size =
    Storage.Zk_rollup.Pending_list.init ctxt address init_pl
  in
  let address_size = Zk_rollup_repr.Address.size in
  let size =
    Z.of_int (origination_size + address_size + account_size + pl_size)
  in
  return (ctxt, address, size)

let add_to_pending ctxt rollup ops =
  let open Lwt_result_syntax in
  let open Zk_rollup_repr in
  let open Zk_rollup_operation_repr in
  let* ctxt, acc = account ctxt rollup in
  let*? () =
    List.iter_e
      (fun (op, _ticket_hash_opt) ->
        if Compare.Int.(op.op_code >= acc.static.nb_ops || op.op_code < 0) then
          error @@ Zk_rollup_invalid_op_code op.op_code
        else ok ())
      ops
  in
  let* ctxt, pl = Storage.Zk_rollup.Pending_list.get ctxt rollup in
  let next_index, length =
    match pl with
    | Empty {next_index} -> (next_index, 0)
    | Pending {next_index; length} -> (next_index, length)
  in
  let* ctxt, next_index, length, storage_diff =
    List.fold_left_es
      (fun (ctxt, next_index, length, storage_diff) op ->
        let* ctxt, new_storage_diff, _was_bound =
          Storage.Zk_rollup.Pending_operation.add (ctxt, rollup) next_index op
        in
        return
          ( ctxt,
            Int64.succ next_index,
            length + 1,
            new_storage_diff + storage_diff ))
      (ctxt, next_index, length, 0)
      ops
  in
  let used_l2_operations_storage_space =
    Z.(add acc.dynamic.used_l2_operations_storage_space (Z.of_int storage_diff))
  in
  let l2_operations_storage_space_to_pay =
    Z.(
      max
        zero
        (sub
           used_l2_operations_storage_space
           acc.dynamic.paid_l2_operations_storage_space))
  in
  let paid_l2_operations_storage_space =
    Z.(
      add
        acc.dynamic.paid_l2_operations_storage_space
        l2_operations_storage_space_to_pay)
  in
  let acc =
    {
      acc with
      dynamic =
        {
          acc.dynamic with
          paid_l2_operations_storage_space;
          used_l2_operations_storage_space;
        };
    }
  in

  let pl =
    if Compare.Int.(length = 0) then Empty {next_index}
    else Pending {next_index; length}
  in
  (* Users aren't charged for storage diff in the account or pending list
     description of a ZKRU.
     When updating a ZKRU account, the storage diff can only come from the
     dynamically sized [Z.t] used for the watermark. These changes
     in storage size will not be accounted for.
     As for the pending list description, the storage size is fixed for
     each of the two cases (empty / non-empty). Then, there will be a storage
     diff when switching between these two, which won't be accounted for
     either.
  *)
  let* ctxt, _diff_acc = Storage.Zk_rollup.Account.update ctxt rollup acc in
  let* ctxt, _diff_pl = Storage.Zk_rollup.Pending_list.update ctxt rollup pl in
  return (ctxt, l2_operations_storage_space_to_pay)

let pending_length =
  let open Zk_rollup_repr in
  function Empty _ -> 0 | Pending {length; _} -> length

let head =
  let open Zk_rollup_repr in
  function
  | Empty _ -> error Zk_rollup_pending_list_too_short
  | Pending {next_index; length} ->
      Result_syntax.return Int64.(sub next_index (of_int length))

let next_index =
  let open Zk_rollup_repr in
  function
  | Empty {next_index} -> next_index | Pending {next_index; _} -> next_index

let get_pending_length ctxt rollup =
  let open Lwt_result_syntax in
  let* ctxt, pl = pending_list ctxt rollup in
  return (ctxt, pending_length pl)

(** Same as [Tezos_stdlib.Utils.fold_n_times] but with Lwt and Error monad *)
let fold_n_times_es ~when_negative n f e =
  let open Lwt_result_syntax in
  if Compare.Int.(n < 0) then tzfail when_negative
  else
    let rec go acc = function
      | 0 -> return acc
      | n ->
          let* acc = f acc in
          (go [@ocaml.tailcall]) acc (n - 1)
    in
    go e n

let get_prefix ctxt rollup n =
  let open Lwt_result_syntax in
  if Compare.Int.(n = 0) then return (ctxt, [])
  else
    let* ctxt, pl = pending_list ctxt rollup in
    let pl_length = pending_length pl in
    let*? () =
      error_when Compare.Int.(n > pl_length) Zk_rollup_pending_list_too_short
    in
    let*? hd = head pl in
    let* ctxt, ops, _i =
      (* Get the l2 ops corresponding to indeces [hd + n - 1 .. hd],
         so that the accumulated list is in the right order *)
      fold_n_times_es
        ~when_negative:Zk_rollup_negative_length
        n
        (fun (ctxt, ops, i) ->
          let* ctxt, op = pending_op ctxt rollup i in
          return (ctxt, op :: ops, Int64.pred i))
        (ctxt, [], Int64.(sub (add hd (of_int n)) 1L))
    in
    return (ctxt, ops)

let update ctxt rollup ~pending_to_drop ~new_account =
  let open Lwt_result_syntax in
  let open Zk_rollup_repr in
  let open Zk_rollup_account_repr in
  let* ctxt, pl = pending_list ctxt rollup in
  let* ctxt, acc = account ctxt rollup in
  let pl_length = pending_length pl in
  let*? () =
    error_when
      Compare.Int.(pending_to_drop > pl_length)
      Zk_rollup_pending_list_too_short
  in
  let next_index = next_index pl in
  (* Drop the indeces from [head] to [head + pending_to_drop - 1]
     from the storage of L2 operations. *)
  let* ctxt, freed =
    match head pl with
    | Error _e ->
        (* If the pending list is empty, then [pending_to_drop] must be 0. *)
        return (ctxt, 0)
    | Ok head ->
        let* ctxt, freed, _i =
          fold_n_times_es
            ~when_negative:Zk_rollup_negative_length
            pending_to_drop
            (fun (ctxt, freed, i) ->
              let* ctxt, new_freed, _bound =
                Storage.Zk_rollup.Pending_operation.remove (ctxt, rollup) i
              in
              return (ctxt, freed + new_freed, Int64.succ i))
            (ctxt, 0, head)
        in
        return (ctxt, freed)
  in
  (* Subtract the bytes freed by removing pending operations from
     acc.dynamic.used_l2_operations_storage_space, and update
     [new_account].
  *)
  let used_l2_operations_storage_space =
    Z.(sub acc.dynamic.used_l2_operations_storage_space (Z.of_int freed))
  in
  let new_account =
    {
      new_account with
      dynamic =
        {
          state = new_account.dynamic.state;
          paid_l2_operations_storage_space =
            new_account.dynamic.paid_l2_operations_storage_space;
          used_l2_operations_storage_space;
        };
    }
  in
  let* ctxt, _diff_acc =
    Storage.Zk_rollup.Account.update ctxt rollup new_account
  in
  (* Update the pending list descriptor *)
  let pl_length = pl_length - pending_to_drop in
  let pl =
    if Compare.Int.(pl_length = 0) then Empty {next_index}
    else Pending {next_index; length = pl_length}
  in
  let* ctxt, _diff_pl = Storage.Zk_rollup.Pending_list.update ctxt rollup pl in
  return ctxt

let assert_exist ctxt rollup =
  let open Lwt_result_syntax in
  let* ctxt, exists = Storage.Zk_rollup.Account.mem ctxt rollup in
  let*? () = error_unless exists (Zk_rollup_does_not_exist rollup) in
  return ctxt

let exists ctxt rollup = Storage.Zk_rollup.Account.mem ctxt rollup
OCaml

Innovation. Community. Security.