package tezos-protocol-alpha

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

Source file sc_rollup_dissection_chunk_repr.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
315
316
317
318
319
320
321
322
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com>                *)
(* Copyright (c) 2022 Trili Tech, <contact@trili.tech>                       *)
(*                                                                           *)
(* 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 Sc_rollup_repr

type t = {state_hash : State_hash.t option; tick : Sc_rollup_tick_repr.t}

let equal {state_hash; tick} chunk2 =
  Option.equal State_hash.equal state_hash chunk2.state_hash
  && Sc_rollup_tick_repr.equal tick chunk2.tick

let encoding =
  let open Data_encoding in
  conv
    (fun {state_hash; tick} -> (state_hash, tick))
    (fun (state_hash, tick) -> {state_hash; tick})
    (obj2
       (opt "state" State_hash.encoding)
       (req "tick" Sc_rollup_tick_repr.encoding))

type error +=
  | (* `Temporary *)
      Dissection_number_of_sections_mismatch of {
      expected : Z.t;
      given : Z.t;
    }
  | (* `Permanent *) Dissection_invalid_number_of_sections of Z.t
  | (* `Temporary *)
      Dissection_start_hash_mismatch of {
      expected : Sc_rollup_repr.State_hash.t option;
      given : Sc_rollup_repr.State_hash.t option;
    }
  | (* `Temporary *)
      Dissection_stop_hash_mismatch of
      Sc_rollup_repr.State_hash.t option
  | (* `Temporary *)
      Dissection_edge_ticks_mismatch of {
      dissection_start_tick : Sc_rollup_tick_repr.t;
      dissection_stop_tick : Sc_rollup_tick_repr.t;
      chunk_start_tick : Sc_rollup_tick_repr.t;
      chunk_stop_tick : Sc_rollup_tick_repr.t;
    }
  | (* `Permanent *) Dissection_ticks_not_increasing
  | (* `Permanent *) Dissection_invalid_distribution of Z.t
  | (* `Permanent *) Dissection_invalid_successive_states_shape

let pp_state_hash =
  let open Format in
  pp_print_option ~none:(fun ppf () -> fprintf ppf "None") State_hash.pp

let pp_hash_opt fmt = function
  | None -> Format.fprintf fmt "None"
  | Some x -> Sc_rollup_repr.State_hash.pp fmt x

let pp ppf {state_hash; tick} =
  let open Format in
  fprintf
    ppf
    "State hash:%a@ Tick: %a"
    pp_state_hash
    state_hash
    Sc_rollup_tick_repr.pp
    tick

let default_check_sections_number ~default_number_of_sections
    ~number_of_sections ~dist =
  let open Result_syntax in
  let number_of_sections = Z.of_int number_of_sections in
  let default_number_of_sections = Z.of_int default_number_of_sections in
  let should_be_equal_to expected =
    Dissection_number_of_sections_mismatch
      {expected; given = number_of_sections}
  in
  if Compare.Z.(default_number_of_sections <= dist) then
    error_unless
      Compare.Z.(number_of_sections = default_number_of_sections)
      (should_be_equal_to default_number_of_sections)
  else if Compare.Z.(dist > Z.one) then
    error_unless Compare.Z.(number_of_sections = dist) (should_be_equal_to dist)
  else tzfail (Dissection_invalid_number_of_sections number_of_sections)

let default_check ~section_maximum_size ~check_sections_number
    ~default_number_of_sections ~start_chunk ~stop_chunk dissection =
  let open Result_syntax in
  let number_of_sections = Compare.Int.max 0 (List.length dissection - 1) in
  let dist = Sc_rollup_tick_repr.distance start_chunk.tick stop_chunk.tick in
  let* () =
    check_sections_number ~default_number_of_sections ~number_of_sections ~dist
  in
  let* () =
    match (List.hd dissection, List.last_opt dissection) with
    | Some {state_hash = a; tick = a_tick}, Some {state_hash = b; tick = b_tick}
      ->
        let* () =
          error_unless
            (Option.equal State_hash.equal a start_chunk.state_hash
            && not (Option.is_none a))
            (Dissection_start_hash_mismatch
               {expected = start_chunk.state_hash; given = a})
        in
        let* () =
          error_unless
            (not (Option.equal State_hash.equal b stop_chunk.state_hash))
            ((* If the [b] state is equal to [stop_chunk], that means we
                agree on the after state of the section. But, we're trying
                to dispute it, it doesn't make sense. *)
               Dissection_stop_hash_mismatch
               stop_chunk.state_hash)
        in
        Sc_rollup_tick_repr.(
          error_unless
            (a_tick = start_chunk.tick && b_tick = stop_chunk.tick)
            (Dissection_edge_ticks_mismatch
               {
                 dissection_start_tick = a_tick;
                 dissection_stop_tick = b_tick;
                 chunk_start_tick = start_chunk.tick;
                 chunk_stop_tick = stop_chunk.tick;
               }))
    | _ ->
        (* This case is probably already handled by the
           [Dissection_invalid_number_of_sections] returned above *)
        tzfail
          (Dissection_invalid_number_of_sections (Z.of_int number_of_sections))
  in
  let rec traverse states =
    match states with
    | {state_hash = None; _} :: {state_hash = Some _; _} :: _ ->
        tzfail Dissection_invalid_successive_states_shape
    | {tick; _} :: ({tick = next_tick; state_hash = _} as next) :: others ->
        if Sc_rollup_tick_repr.(tick < next_tick) then
          let incr = Sc_rollup_tick_repr.distance tick next_tick in
          if Z.(leq incr section_maximum_size) then traverse (next :: others)
          else tzfail (Dissection_invalid_distribution section_maximum_size)
        else tzfail Dissection_ticks_not_increasing
    | _ -> return_unit
  in
  traverse dissection

let () =
  let description = "Mismatch in the number of sections in the dissection" in
  register_error_kind
    `Temporary
    ~id:"smart_rollup_dissection_number_of_sections_mismatch"
    ~title:description
    ~description
    ~pp:(fun ppf (expected, given) ->
      Format.fprintf
        ppf
        "The number of sections must be equal to %a instead of %a"
        Z.pp_print
        expected
        Z.pp_print
        given)
    Data_encoding.(obj2 (req "expected" n) (req "given" n))
    (function
      | Dissection_number_of_sections_mismatch {expected; given} ->
          Some (expected, given)
      | _ -> None)
    (fun (expected, given) ->
      Dissection_number_of_sections_mismatch {expected; given}) ;
  let description = "Invalid number of sections in the dissection" in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_dissection_invalid_number_of_sections"
    ~title:description
    ~description
    ~pp:(fun ppf n ->
      Format.fprintf
        ppf
        "A dissection with %a sections can never be valid"
        Z.pp_print
        n)
    Data_encoding.(obj1 (req "value" n))
    (function Dissection_invalid_number_of_sections n -> Some n | _ -> None)
    (fun n -> Dissection_invalid_number_of_sections n) ;
  let description = "Mismatch in the start hash of the dissection" in
  register_error_kind
    `Temporary
    ~id:"smart_rollup_dissection_start_hash_mismatch"
    ~title:description
    ~description
    ~pp:(fun ppf (given, expected) ->
      match given with
      | None -> Format.fprintf ppf "The start hash must not be None"
      | Some _ ->
          Format.fprintf
            ppf
            "The start hash should be equal to %a, but the provided hash is %a"
            pp_hash_opt
            expected
            pp_hash_opt
            given)
    Data_encoding.(
      obj2
        (req "expected" (option Sc_rollup_repr.State_hash.encoding))
        (req "given" (option Sc_rollup_repr.State_hash.encoding)))
    (function
      | Dissection_start_hash_mismatch {expected; given} ->
          Some (expected, given)
      | _ -> None)
    (fun (expected, given) -> Dissection_start_hash_mismatch {expected; given}) ;
  let description = "Mismatch in the stop hash of the dissection" in
  register_error_kind
    `Temporary
    ~id:"smart_rollup_dissection_stop_hash_mismatch"
    ~title:description
    ~description
    ~pp:(fun ppf h ->
      Format.fprintf ppf "The stop hash should not be equal to %a" pp_hash_opt h)
    Data_encoding.(
      obj1 (req "hash" (option Sc_rollup_repr.State_hash.encoding)))
    (function Dissection_stop_hash_mismatch hopt -> Some hopt | _ -> None)
    (fun hopt -> Dissection_stop_hash_mismatch hopt) ;
  let description = "Mismatch in the edge ticks of the dissection" in
  register_error_kind
    `Temporary
    ~id:"smart_rollup_dissection_edge_ticks_mismatch"
    ~title:description
    ~description
    ~pp:
      (fun ppf
           ( dissection_start_tick,
             dissection_stop_tick,
             chunk_start_tick,
             chunk_stop_tick ) ->
      Sc_rollup_tick_repr.(
        Format.fprintf
          ppf
          "We should have dissection_start_tick(%a) = %a and \
           dissection_stop_tick(%a) = %a"
          pp
          dissection_start_tick
          pp
          chunk_start_tick
          pp
          dissection_stop_tick
          pp
          chunk_stop_tick))
    Data_encoding.(
      obj4
        (req "dissection_start_tick" Sc_rollup_tick_repr.encoding)
        (req "dissection_stop_tick" Sc_rollup_tick_repr.encoding)
        (req "chunk_start_tick" Sc_rollup_tick_repr.encoding)
        (req "chunk_stop_tick" Sc_rollup_tick_repr.encoding))
    (function
      | Dissection_edge_ticks_mismatch e ->
          Some
            ( e.dissection_start_tick,
              e.dissection_stop_tick,
              e.chunk_start_tick,
              e.chunk_stop_tick )
      | _ -> None)
    (fun ( dissection_start_tick,
           dissection_stop_tick,
           chunk_start_tick,
           chunk_stop_tick ) ->
      Dissection_edge_ticks_mismatch
        {
          dissection_start_tick;
          dissection_stop_tick;
          chunk_start_tick;
          chunk_stop_tick;
        }) ;
  let description = "Ticks should only increase in dissection" in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_dissection_ticks_not_increasing"
    ~title:description
    ~description
    ~pp:(fun ppf () -> Format.pp_print_string ppf description)
    Data_encoding.empty
    (function Dissection_ticks_not_increasing -> Some () | _ -> None)
    (fun () -> Dissection_ticks_not_increasing) ;
  register_error_kind
    `Permanent
    ~id:"smart_rollup_dissection_invalid_distribution"
    ~title:description
    ~description
    ~pp:(fun ppf max ->
      Format.fprintf
        ppf
        "Maximum tick increment in a section cannot be more than %a ticks"
        Z.pp_print
        max)
    Data_encoding.(obj1 (req "section_max_size" n))
    (function Dissection_invalid_distribution max -> Some max | _ -> None)
    (fun max -> Dissection_invalid_distribution max) ;
  let description = "Cannot recover from a blocked state in a dissection" in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_dissection_invalid_successive_states_shape"
    ~title:description
    ~description
    ~pp:(fun ppf () -> Format.pp_print_string ppf description)
    Data_encoding.empty
    (function
      | Dissection_invalid_successive_states_shape -> Some () | _ -> None)
    (fun () -> Dissection_invalid_successive_states_shape)
OCaml

Innovation. Community. Security.