package tezos-tx-rollup-015-PtLimaPt

  1. Overview
  2. Docs

Source file inbox.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2022 Marigold, <contact@marigold.dev>                       *)
(* Copyright (c) 2022 Oxhead Alpha <info@oxhead-alpha.com>                   *)
(*                                                                           *)
(* 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 Protocol
open Alpha_context

type message_result =
  | Interpreted of Tx_rollup_l2_apply.Message_result.t
  | Discarded of tztrace

type l2_context_hash = {
  irmin_hash : Tx_rollup_l2_context_hash.t;
  tree_hash : Context_hash.t;
}

type message = {
  message : Tx_rollup_message.t;
  result : message_result;
  l2_context_hash : l2_context_hash;
}

type t = message list

let message_result_encoding =
  let open Data_encoding in
  union
    [
      case
        ~title:"interpreted"
        (Tag 0)
        Tx_rollup_l2_apply.Message_result.encoding
        (function Interpreted r -> Some r | _ -> None)
        (fun r -> Interpreted r);
      case
        ~title:"discarded"
        (Tag 1)
        (obj1
           (req "discarded" (obj1 (req "reason" Error_monad.trace_encoding))))
        (function Discarded e -> Some e | _ -> None)
        (fun e -> Discarded e);
    ]

let l2_context_hash_encoding =
  let open Data_encoding in
  conv
    (fun {irmin_hash; tree_hash} -> (irmin_hash, tree_hash))
    (fun (irmin_hash, tree_hash) -> {irmin_hash; tree_hash})
    (obj2
       (req "irmin_hash" Tx_rollup_l2_context_hash.encoding)
       (req "tree_hash" Context_hash.encoding))

let message_encoding =
  let open Data_encoding in
  conv
    (fun {message; result; l2_context_hash} ->
      (message, result, l2_context_hash))
    (fun (message, result, l2_context_hash) ->
      {message; result; l2_context_hash})
    (obj3
       (req "message" Tx_rollup_message.encoding)
       (req "result" message_result_encoding)
       (req "l2_context_hash" l2_context_hash_encoding))

let encoding =
  let open Data_encoding in
  list message_encoding

let merkle_root inbox =
  let message_hashes =
    List.map
      (fun msg -> Tx_rollup_message_hash.hash_uncarbonated msg.message)
      inbox
  in
  Tx_rollup_inbox.Merkle.merklize_list message_hashes

let to_proto inbox =
  let inbox_length = List.length inbox in
  let cumulated_size =
    List.fold_left
      (fun acc {message; _} ->
        (* TORU/FIXME: https://gitlab.com/tezos/tezos/-/issues/2957
           We need to expose [Alpha_context.Tx_rollup_message.size]. *)
        acc + Tx_rollup_message_repr.size (Obj.magic message))
      0
      inbox
  in
  let merkle_root = merkle_root inbox in
  Tx_rollup_inbox.{inbox_length; cumulated_size; merkle_root}

let proto_message_results inbox =
  List.map
    (fun msg ->
      let withdrawals =
        match msg.result with
        | Discarded _ -> []
        | Interpreted (_result, withdrawals) -> withdrawals
      in
      Tx_rollup_message_result.
        {
          context_hash = msg.l2_context_hash.tree_hash;
          withdraw_list_hash =
            Tx_rollup_withdraw_list_hash.hash_uncarbonated withdrawals;
        })
    inbox
OCaml

Innovation. Community. Security.