package tezos-protocol-020-PsParisC

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

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

type error += (* Permanent *) Sc_rollup_invalid_destination

let () =
  let open Data_encoding in
  let msg = "Invalid destination" in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_management_protocol_invalid_destination"
    ~title:msg
    ~pp:(fun fmt () -> Format.fprintf fmt "%s" msg)
    ~description:msg
    unit
    (function Sc_rollup_invalid_destination -> Some () | _ -> None)
    (fun () -> Sc_rollup_invalid_destination)

type transaction =
  | Transaction : {
      destination : Contract_hash.t;
      entrypoint : Entrypoint.t;
      parameters_ty : ('a, _) Script_typed_ir.ty;
      parameters : 'a;
      unparsed_parameters : Script.expr;
    }
      -> transaction

type atomic_transaction_batch = {transactions : transaction list}

type outbox_message =
  | Atomic_transaction_batch of atomic_transaction_batch
  | Whitelist_update of Sc_rollup.Whitelist.t option

let make_internal_transfer ctxt ty ~payload ~sender ~source ~destination =
  let open Lwt_result_syntax in
  let+ payload, ctxt =
    Script_ir_translator.unparse_data
      ctxt
      Script_ir_unparser.Optimized
      ty
      payload
  in
  ( Sc_rollup.Inbox_message.Internal
      (Transfer {payload; sender; source; destination}),
    ctxt )

let make_transaction ctxt ~parameters_ty ~unparsed_parameters ~destination
    ~entrypoint =
  let open Lwt_result_syntax in
  (* Parse the parameters according to the given type. *)
  let+ parameters, ctxt =
    Script_ir_translator.parse_data
      ctxt
      ~elab_conf:Script_ir_translator_config.(make ~legacy:false ())
      ~allow_forged_tickets:true
      ~allow_forged_lazy_storage_id:false
      parameters_ty
      (Micheline.root unparsed_parameters)
  in
  ( ctxt,
    Transaction
      {destination; entrypoint; parameters_ty; parameters; unparsed_parameters}
  )

let internal_untyped_transaction ctxt
    ({unparsed_parameters; destination; entrypoint} :
      Sc_rollup.Outbox.Message.transaction) =
  let open Lwt_result_syntax in
  let* Script_ir_translator.Ex_script (Script {arg_type; entrypoints; _}), ctxt
      =
    let* ctxt, _cache_key, cached = Script_cache.find ctxt destination in
    match cached with
    | Some (_script, ex_script) -> return (ex_script, ctxt)
    | None -> tzfail Sc_rollup_invalid_destination
  in
  (* Find the entrypoint type for the given entrypoint. *)
  let*? res, ctxt =
    Gas_monad.run
      ctxt
      (Script_ir_translator.find_entrypoint
         ~error_details:(Informative ())
         arg_type
         entrypoints
         entrypoint)
  in
  let*? (Ex_ty_cstr {ty = parameters_ty; _}) = res in
  make_transaction
    ctxt
    ~parameters_ty
    ~unparsed_parameters
    ~destination
    ~entrypoint

let internal_typed_transaction ctxt
    ({unparsed_parameters; unparsed_ty; destination; entrypoint} :
      Sc_rollup.Outbox.Message.typed_transaction) =
  let open Lwt_result_syntax in
  (* Parse the parameters type according to the type. *)
  let*? Ex_ty parameters_ty, ctxt =
    Script_ir_translator.parse_any_ty
      ctxt
      ~legacy:false
      (Micheline.root unparsed_ty)
  in
  make_transaction
    ctxt
    ~parameters_ty
    ~unparsed_parameters
    ~destination
    ~entrypoint

let outbox_message_of_outbox_message_repr ctxt transactions =
  let open Lwt_result_syntax in
  match transactions with
  | Sc_rollup.Outbox.Message.Atomic_transaction_batch {transactions} ->
      let* ctxt, transactions =
        List.fold_left_map_es internal_untyped_transaction ctxt transactions
      in
      return (Atomic_transaction_batch {transactions}, ctxt)
  | Sc_rollup.Outbox.Message.Atomic_transaction_batch_typed {transactions} ->
      let* ctxt, transactions =
        List.fold_left_map_es internal_typed_transaction ctxt transactions
      in
      return (Atomic_transaction_batch {transactions}, ctxt)
  | Sc_rollup.Outbox.Message.Whitelist_update whitelist_opt ->
      return (Whitelist_update whitelist_opt, ctxt)

module Internal_for_tests = struct
  let make_transaction ctxt parameters_ty ~parameters ~destination ~entrypoint =
    let open Lwt_result_syntax in
    let* unparsed_parameters, ctxt =
      Script_ir_translator.unparse_data ctxt Optimized parameters_ty parameters
    in
    return
      ( Transaction
          {
            destination;
            entrypoint;
            parameters_ty;
            parameters;
            unparsed_parameters;
          },
        ctxt )

  let make_atomic_batch transactions = Atomic_transaction_batch {transactions}

  let serialize_outbox_transactions_untyped transactions =
    let open Result_syntax in
    let of_internal_transaction
        (Transaction
          {
            destination;
            entrypoint;
            parameters_ty = _;
            parameters = _;
            unparsed_parameters;
          }) =
      return
        {Sc_rollup.Outbox.Message.unparsed_parameters; destination; entrypoint}
    in
    let* transactions = List.map_e of_internal_transaction transactions in
    let output_message_internal =
      Sc_rollup.Outbox.Message.Atomic_transaction_batch {transactions}
    in
    Sc_rollup.Outbox.Message.serialize output_message_internal

  let serialize_outbox_transactions_typed transactions =
    let open Result_syntax in
    let of_internal_transaction
        (Transaction
          {
            destination;
            entrypoint;
            parameters_ty;
            parameters = _;
            unparsed_parameters;
          }) =
      let unparsed_ty =
        Script_ir_unparser.serialize_ty_for_error parameters_ty
      in
      return
        {
          Sc_rollup.Outbox.Message.unparsed_parameters;
          unparsed_ty;
          destination;
          entrypoint;
        }
    in
    let* transactions = List.map_e of_internal_transaction transactions in
    let output_message_internal =
      Sc_rollup.Outbox.Message.Atomic_transaction_batch_typed {transactions}
    in
    Sc_rollup.Outbox.Message.serialize output_message_internal

  let deserialize_inbox_message = Sc_rollup.Inbox_message.deserialize
end
OCaml

Innovation. Community. Security.