package tezos-protocol-014-PtKathma

  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
(*****************************************************************************)
(*                                                                           *)
(* 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:"sc_rollup_management_protocol.sc_rollup_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

let make_internal_inbox_message ctxt ty ~payload ~sender ~source =
  let open Lwt_tzresult_syntax in
  let+ payload, ctxt =
    Script_ir_translator.unparse_data
      ctxt
      Script_ir_translator.Optimized
      ty
      payload
  in
  let payload = Micheline.strip_locations payload in
  (Sc_rollup.Inbox.Message.Internal {payload; sender; source}, ctxt)

let transactions_batch_of_internal ctxt transactions =
  let open Lwt_tzresult_syntax in
  let or_internal_transaction ctxt
      {Sc_rollup.Outbox.Message.unparsed_parameters; destination; entrypoint} =
    (* Lookup the contract-hash. *)
    (* Load the type and entrypoints of the script. *)
    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 -> fail 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
    (* Parse the parameters according to the entrypoint type. *)
    let* parameters, ctxt =
      Script_ir_translator.parse_data
        ctxt
        ~legacy:false
        ~allow_forged:true
        parameters_ty
        (Micheline.root unparsed_parameters)
    in
    return
      ( Transaction
          {
            destination;
            entrypoint;
            parameters_ty;
            parameters;
            unparsed_parameters;
          },
        ctxt )
  in
  let+ ctxt, transactions =
    List.fold_left_map_es
      (fun ctxt msg ->
        let+ t, ctxt = or_internal_transaction ctxt msg in
        (ctxt, t))
      ctxt
      transactions
  in
  ({transactions}, ctxt)

(** TODO: #2951
    Carbonate [of_bytes] step.
    Gas for decoding the binary values should be be accounted for.
  *)
let outbox_message_of_bytes ctxt bytes =
  let open Lwt_tzresult_syntax in
  let*? (Sc_rollup.Outbox.Message.Atomic_transaction_batch {transactions}) =
    Sc_rollup.Outbox.Message.of_bytes bytes
  in
  let+ ts, ctxt = transactions_batch_of_internal ctxt transactions in
  (Atomic_transaction_batch ts, ctxt)

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

  let make_atomic_batch transactions = Atomic_transaction_batch {transactions}

  let bytes_of_outbox_message (Atomic_transaction_batch {transactions}) =
    let open Tzresult_syntax in
    let to_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 to_internal_transaction transactions in
    let output_message_internal =
      Sc_rollup.Outbox.Message.Atomic_transaction_batch {transactions}
    in
    Sc_rollup.Outbox.Message.Internal_for_tests.to_bytes output_message_internal

  let inbox_message_of_bytes =
    Sc_rollup.Inbox.Message.Internal_for_tests.of_bytes
end
OCaml

Innovation. Community. Security.