package tezos-protocol-014-PtKathma

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

Source file sc_rollup_proof_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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type t = {
  pvm_step : Sc_rollups.wrapped_proof;
  inbox : Sc_rollup_inbox_repr.Proof.t option;
}

let encoding =
  let open Data_encoding in
  conv
    (fun {pvm_step; inbox} -> (pvm_step, inbox))
    (fun (pvm_step, inbox) -> {pvm_step; inbox})
    (obj2
       (req "pvm_step" Sc_rollups.wrapped_proof_encoding)
       (req "inbox" (option Sc_rollup_inbox_repr.Proof.encoding)))

let pp ppf _ = Format.fprintf ppf "Refutation game proof"

let start proof =
  let (module P) = Sc_rollups.wrapped_proof_module proof.pvm_step in
  P.proof_start_state P.proof

let stop proof =
  let (module P) = Sc_rollups.wrapped_proof_module proof.pvm_step in
  P.proof_stop_state P.proof

(* This takes an [input] and checks if it is at or above the given level.
   It returns [None] if this is the case.

   We use this to check that the PVM proof is obeying [commit_level]
   correctly---if the message obtained from the inbox proof is at or
   above [commit_level] the [input_given] in the PVM proof should be
   [None]. *)
let cut_at_level level input =
  let input_level = Sc_rollup_PVM_sem.(input.inbox_level) in
  if Raw_level_repr.(level <= input_level) then None else Some input

type error += Sc_rollup_proof_check of string

let proof_error reason =
  let open Lwt_result_syntax in
  fail (Sc_rollup_proof_check reason)

let check p reason =
  let open Lwt_result_syntax in
  if p then return () else proof_error reason

let valid snapshot commit_level ~pvm_name proof =
  let (module P) = Sc_rollups.wrapped_proof_module proof.pvm_step in
  let open Lwt_result_syntax in
  let* _ = check (String.equal P.name pvm_name) "Incorrect PVM kind" in
  let input_requested = P.proof_input_requested P.proof in
  let input_given = P.proof_input_given P.proof in
  let* input =
    match (input_requested, proof.inbox) with
    | Sc_rollup_PVM_sem.No_input_required, None -> return None
    | Sc_rollup_PVM_sem.Initial, Some inbox_proof ->
        Sc_rollup_inbox_repr.Proof.valid
          {inbox_level = Raw_level_repr.root; message_counter = Z.zero}
          snapshot
          inbox_proof
    | Sc_rollup_PVM_sem.First_after (level, counter), Some inbox_proof ->
        Sc_rollup_inbox_repr.Proof.valid
          {inbox_level = level; message_counter = Z.succ counter}
          snapshot
          inbox_proof
    | _ ->
        proof_error
          (Format.asprintf
             "input_requested is %a, inbox proof is %a"
             Sc_rollup_PVM_sem.pp_input_request
             input_requested
             (Format.pp_print_option Sc_rollup_inbox_repr.Proof.pp)
             proof.inbox)
  in
  let* _ =
    check
      (Option.equal
         Sc_rollup_PVM_sem.input_equal
         (Option.bind input (cut_at_level commit_level))
         input_given)
      "Input given is not what inbox proof expects"
  in
  Lwt.map Result.ok (P.verify_proof P.proof)

module type PVM_with_context_and_state = sig
  include Sc_rollups.PVM.S

  val context : context

  val state : state
end

type error += Proof_cannot_be_wrapped

let produce pvm_and_state inbox commit_level =
  let open Lwt_result_syntax in
  let (module P : PVM_with_context_and_state) = pvm_and_state in
  let*! request = P.is_input_state P.state in
  let* inbox, input_given =
    match request with
    | Sc_rollup_PVM_sem.No_input_required -> return (None, None)
    | Sc_rollup_PVM_sem.Initial ->
        let* p, i =
          Sc_rollup_inbox_repr.Proof.produce_proof
            inbox
            (Raw_level_repr.root, Z.zero)
        in
        return (Some p, i)
    | Sc_rollup_PVM_sem.First_after (l, n) ->
        let* p, i = Sc_rollup_inbox_repr.Proof.produce_proof inbox (l, n) in
        return (Some p, i)
  in
  let input_given = Option.bind input_given (cut_at_level commit_level) in
  let* pvm_step_proof = P.produce_proof P.context input_given P.state in
  let module P_with_proof = struct
    include P

    let proof = pvm_step_proof
  end in
  match Sc_rollups.wrap_proof (module P_with_proof) with
  | Some pvm_step -> return {pvm_step; inbox}
  | None -> fail Proof_cannot_be_wrapped
OCaml

Innovation. Community. Security.