package tezos-protocol-alpha

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

Source file sc_rollup_inbox_message_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
(*****************************************************************************)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type error +=
  | (* `Permanent *) Error_encode_inbox_message
  | (* `Permanent *) Error_decode_inbox_message

let () =
  let open Data_encoding in
  let msg =
    "Failed to encode a rollup management protocol inbox message value"
  in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_inbox_message_encoding"
    ~title:msg
    ~pp:(fun fmt () -> Format.fprintf fmt "%s" msg)
    ~description:msg
    unit
    (function Error_encode_inbox_message -> Some () | _ -> None)
    (fun () -> Error_encode_inbox_message) ;
  let msg =
    "Failed to decode a smart rollup management protocol inbox message value"
  in
  register_error_kind
    `Permanent
    ~id:"smart_rollup_inbox_message_decoding"
    ~title:msg
    ~pp:(fun fmt () -> Format.fprintf fmt "%s" msg)
    ~description:msg
    unit
    (function Error_decode_inbox_message -> Some () | _ -> None)
    (fun () -> Error_decode_inbox_message)

type internal_inbox_message =
  | Transfer of {
      payload : Script_repr.expr;
      sender : Contract_hash.t;
      source : Signature.public_key_hash;
      destination : Sc_rollup_repr.Address.t;
    }
  | Start_of_level
  | End_of_level
  | Info_per_level of {
      predecessor_timestamp : Time.t;
      predecessor : Block_hash.t;
    }
  | Protocol_migration of string

let internal_inbox_message_encoding =
  let open Data_encoding in
  let kind name = req "internal_inbox_message_kind" (constant name) in
  union
    [
      case
        (Tag 0)
        ~title:"Transfer"
        (obj5
           (kind "transfer")
           (req "payload" Script_repr.expr_encoding)
           (req "sender" Contract_hash.encoding)
           (req "source" Signature.Public_key_hash.encoding)
           (req "destination" Sc_rollup_repr.Address.encoding))
        (function
          | Transfer {payload; sender; source; destination} ->
              Some ((), payload, sender, source, destination)
          | _ -> None)
        (fun ((), payload, sender, source, destination) ->
          Transfer {payload; sender; source; destination});
      case
        (Tag 1)
        ~title:"Start_of_level"
        (obj1 (kind "start_of_level"))
        (function Start_of_level -> Some () | _ -> None)
        (fun () -> Start_of_level);
      case
        (Tag 2)
        ~title:"End_of_level"
        (obj1 (kind "end_of_level"))
        (function End_of_level -> Some () | _ -> None)
        (fun () -> End_of_level);
      case
        (Tag 3)
        ~title:"Info_per_level"
        (obj3
           (kind "info_per_level")
           (req "predecessor_timestamp" Time.encoding)
           (req "predecessor" Block_hash.encoding))
        (function
          | Info_per_level {predecessor_timestamp; predecessor} ->
              Some ((), predecessor_timestamp, predecessor)
          | _ -> None)
        (fun ((), predecessor_timestamp, predecessor) ->
          Info_per_level {predecessor_timestamp; predecessor});
      case
        (Tag 4)
        ~title:"Protocol_migration"
        (obj2 (kind "protocol_migration") (req "protocol" (string Hex)))
        (function Protocol_migration proto -> Some ((), proto) | _ -> None)
        (fun ((), proto) -> Protocol_migration proto);
    ]

type t = Internal of internal_inbox_message | External of string

let encoding =
  let open Data_encoding in
  check_size
    Constants_repr.sc_rollup_message_size_limit
    (union
       [
         case
           (Tag 0)
           ~title:"Internal"
           internal_inbox_message_encoding
           (function
             | Internal internal_message -> Some internal_message
             | External _ -> None)
           (fun internal_message -> Internal internal_message);
         case
           (Tag 1)
           ~title:"External"
           Variable.(string Hex)
           (function External msg -> Some msg | Internal _ -> None)
           (fun msg -> External msg);
       ])

type serialized = string

let serialize msg =
  let open Result_syntax in
  match Data_encoding.Binary.to_string_opt encoding msg with
  | None -> tzfail Error_encode_inbox_message
  | Some str -> return str

let deserialize s =
  let open Result_syntax in
  match Data_encoding.Binary.of_string_opt encoding s with
  | None -> tzfail Error_decode_inbox_message
  | Some msg -> return msg

let unsafe_of_string s = s

let unsafe_to_string s = s

(* 32 *)
let hash_prefix = "\003\255\138\145\170" (* srib3(55) *)

module Hash = struct
  let prefix = "srib3"

  let encoded_size = 55

  module H =
    Blake2B.Make
      (Base58)
      (struct
        let name = "Smart_rollup_serialized_message_hash"

        let title =
          "The hash of a serialized message of the smart rollup inbox."

        let b58check_prefix = hash_prefix

        (* defaults to 32 *)
        let size = None
      end)

  include H

  let () = Base58.check_encoded_prefix b58check_encoding prefix encoded_size
end

let hash_serialized_message (payload : serialized) =
  Hash.hash_string [(payload :> string)]

let start_of_level_serialized =
  (* If [Start_of_level] cannot be serialized, this will be detected at
     startup time as we are defining a top-level value. *)
  Data_encoding.Binary.to_string_exn encoding (Internal Start_of_level)

let end_of_level_serialized =
  (* If [End_of_level] cannot be serialized, this will be detected at
     startup time as we are defining a top-level value. *)
  Data_encoding.Binary.to_string_exn encoding (Internal End_of_level)

let info_per_level_serialized ~predecessor_timestamp ~predecessor =
  match
    serialize (Internal (Info_per_level {predecessor_timestamp; predecessor}))
  with
  | Error _ ->
      (* The info per level should always be serializable as the encoding
         functions for this case do not fail. *)
      assert false
  | Ok info -> info

let (_dummy_serialized_info_per_level_serialized : serialized) =
  (* This allows to detect an error, at startup, we might have introduced in the
     encoding of serialization of info per level messages . *)
  info_per_level_serialized
    ~predecessor_timestamp:(Time.of_seconds Int64.min_int)
    ~predecessor:Block_hash.zero
OCaml

Innovation. Community. Security.