package tezos-protocol-016-PtMumbai

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

Source file sc_rollup_inbox_storage.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com>                *)
(* Copyright (c) 2022 TriliTech <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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module Store = Storage.Sc_rollup

let get_inbox ctxt =
  let open Lwt_result_syntax in
  let* inbox = Store.Inbox.get ctxt in
  return (inbox, ctxt)

let add_messages ctxt messages =
  let open Lwt_result_syntax in
  let open Raw_context in
  (* TODO: https://gitlab.com/tezos/tezos/-/issues/3292

     The carbonation needs to be activated again with the new internal inbox's
     design, i.e. the skip list.
  *)
  let current_messages = Sc_rollup_in_memory_inbox.current_messages ctxt in
  let*? ctxt =
    List.fold_left_e
      (fun ctxt (message : Sc_rollup_inbox_message_repr.serialized) ->
        let msg_len = String.length (message :> string) in
        (* The average cost of adding a message with a
           [current_index] from [0] to [1_000_000] is reached after [100_000]
           messages.
           If we use the real index, the simulations of [Sc_rollup_add_messages]
           are always performed on an empty skip list.
        *)
        let cost =
          Sc_rollup_costs.cost_add_message
            ~current_index:Z.(of_int 100_000)
            ~msg_len
        in
        Raw_context.consume_gas ctxt cost)
      ctxt
      messages
  in
  (*
      Notice that the protocol is forgetful: it throws away the inbox
      history. On the contrary, the history is stored by the rollup
      node to produce inclusion proofs when needed.
  *)
  let*? current_messages =
    Sc_rollup_inbox_repr.add_messages_no_history messages current_messages
  in
  let ctxt =
    Sc_rollup_in_memory_inbox.set_current_messages ctxt current_messages
  in
  return ctxt

let serialize_external_messages ctxt external_messages =
  let open Sc_rollup_inbox_message_repr in
  List.fold_left_map_e
    (fun ctxt message ->
      let open Result_syntax in
      (* Pay gas for serializing an external message. *)
      let* ctxt =
        let bytes_len = String.length message in
        Raw_context.consume_gas
          ctxt
          (Sc_rollup_costs.cost_serialize_external_inbox_message ~bytes_len)
      in
      let* serialized_message = serialize @@ External message in
      return (ctxt, serialized_message))
    ctxt
    external_messages

let serialize_internal_message ctxt internal_message =
  let open Result_syntax in
  (* Pay gas for serializing an internal message. *)
  let* ctxt =
    Raw_context.consume_gas
      ctxt
      (Sc_rollup_costs.cost_serialize_internal_inbox_message internal_message)
  in
  let* message =
    Sc_rollup_inbox_message_repr.(serialize @@ Internal internal_message)
  in
  return (message, ctxt)

let add_external_messages ctxt external_messages =
  let open Lwt_result_syntax in
  let*? ctxt, messages = serialize_external_messages ctxt external_messages in
  add_messages ctxt messages

let add_internal_message ctxt internal_message =
  let open Lwt_result_syntax in
  let*? message, ctxt = serialize_internal_message ctxt internal_message in
  add_messages ctxt [message]

let add_deposit ctxt ~payload ~sender ~source ~destination =
  let internal_message : Sc_rollup_inbox_message_repr.internal_inbox_message =
    Transfer {destination; payload; sender; source}
  in
  add_internal_message ctxt internal_message

let finalize_inbox_level ctxt =
  let open Lwt_syntax in
  let* res =
    let open Lwt_result_syntax in
    let* inbox, ctxt = get_inbox ctxt in
    let witness = Raw_context.Sc_rollup_in_memory_inbox.current_messages ctxt in
    let*? inbox =
      Sc_rollup_inbox_repr.finalize_inbox_level_no_history inbox witness
    in
    let* ctxt = Store.Inbox.update ctxt inbox in
    return ctxt
  in
  match res with
  | Ok ctxt -> return ctxt
  | Error err ->
      (* As a protection, we backtrack the [ctxt] if finalizing the inbox level
         failed. This way, we cannot make [finalize_block] fail. *)
      Logging.(
        log
          Fatal
          "Finalizing inbox level failed because of %a, the context is \
           backtracked. Smart rollups inbox failed to finalize this block, \
           this behavior is undefined and its consequence is unexplored."
          pp_trace
          err) ;
      return ctxt

let add_info_per_level ~predecessor ctxt =
  let open Lwt_syntax in
  let* res =
    let open Lwt_result_syntax in
    let predecessor_timestamp = Raw_context.predecessor_timestamp ctxt in
    let witness = Raw_context.Sc_rollup_in_memory_inbox.current_messages ctxt in
    let*? witness =
      Sc_rollup_inbox_repr.add_info_per_level_no_history
        ~predecessor_timestamp
        ~predecessor
        witness
    in
    let ctxt =
      Raw_context.Sc_rollup_in_memory_inbox.set_current_messages ctxt witness
    in
    return ctxt
  in
  match res with
  | Ok ctxt -> return ctxt
  | Error err ->
      (* As a protection, we backtrack the [ctxt] if adding the info per level
         failed. This way, we cannot make [begin_application],
         [begin_partial_application] and [begin_full_construction] fail. *)
      Logging.(
        log
          Fatal
          "Adding [Info_per_level] failed because of %a, the context is \
           backtracked. Smart rollups inbox failed to finalize this block, \
           this behavior is undefined and its consequence is unexplored."
          pp_trace
          err) ;
      return ctxt

let init_inbox ~predecessor ctxt =
  let open Lwt_syntax in
  let* res =
    let open Lwt_result_syntax in
    let ({level; _} : Level_repr.t) = Raw_context.current_level ctxt in
    let predecessor_timestamp = Raw_context.predecessor_timestamp ctxt in
    let*? inbox =
      Sc_rollup_inbox_repr.genesis ~predecessor_timestamp ~predecessor level
    in
    let* ctxt = Store.Inbox.init ctxt inbox in
    return ctxt
  in
  match res with
  | Ok ctxt -> return ctxt
  | Error err ->
      (* As a protection, we backtrack the [ctxt] if initializing the inbox
         failed. This way, we cannot make [prepare_first_block] fail. *)
      Logging.(
        log
          Fatal
          "Initializing inbox failed because of %a, the context is \
           backtracked. Smart rollups inbox failed, this behavior is undefined \
           and its consequence is unexplored."
          pp_trace
          err) ;
      return ctxt

module Internal_for_tests = struct
  let add_start_of_level ctxt =
    add_internal_message ctxt Sc_rollup_inbox_message_repr.Start_of_level

  let add_end_of_level ctxt =
    add_internal_message ctxt Sc_rollup_inbox_message_repr.End_of_level

  let add_info_per_level ctxt predecessor_timestamp predecessor =
    add_internal_message
      ctxt
      (Sc_rollup_inbox_message_repr.Info_per_level
         {predecessor_timestamp; predecessor})
end
OCaml

Innovation. Community. Security.