package tezos-protocol-017-PtNairob

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

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

module El = struct
  type t = Tx_rollup_message_hash_repr.t

  let to_bytes =
    Data_encoding.Binary.to_bytes_exn Tx_rollup_message_hash_repr.encoding
end

module Prefix = struct
  let name = "Inbox_list_hash"

  let title = "A merkle root hash for inboxes"

  let b58check_prefix = Tx_rollup_prefixes.inbox_list_hash.b58check_prefix

  let size = Some Tx_rollup_prefixes.inbox_list_hash.hash_size
end

module H = Blake2B.Make (Base58) (Prefix)
module Merkle_list = Merkle_list.Make (El) (H)

module Merkle = struct
  type tree = Merkle_list.t

  type root = Merkle_list.h

  type path = Merkle_list.path

  let empty = Merkle_list.nil

  let root = Merkle_list.root

  let ( = ) = H.( = )

  let compare = H.compare

  let root_encoding = H.encoding

  let root_of_b58check_opt = H.of_b58check_opt

  let pp_root = H.pp

  let path_encoding = Merkle_list.path_encoding

  let add_message = Merkle_list.snoc

  let tree_of_messages = List.fold_left Merkle_list.snoc Merkle_list.nil

  let compute_path messages position =
    let tree = tree_of_messages messages in
    Merkle_list.compute_path tree position

  let check_path = Merkle_list.check_path

  let path_depth = Merkle_list.path_depth

  let merklize_list messages =
    let tree = tree_of_messages messages in
    root tree
end

type t = {inbox_length : int; cumulated_size : int; merkle_root : Merkle.root}

let ( = )
    {
      inbox_length = inbox_length_left;
      cumulated_size = cumulated_size_left;
      merkle_root = merkle_root_left;
    }
    {
      inbox_length = inbox_length_right;
      cumulated_size = cumulated_size_right;
      merkle_root = merkle_root_right;
    } =
  Compare.Int.(inbox_length_left = inbox_length_right)
  && Compare.Int.(cumulated_size_left = cumulated_size_right)
  && Merkle.(merkle_root_left = merkle_root_right)

let encoding =
  let open Data_encoding in
  conv
    (fun {inbox_length; cumulated_size; merkle_root} ->
      (inbox_length, cumulated_size, merkle_root))
    (fun (inbox_length, cumulated_size, merkle_root) ->
      {inbox_length; cumulated_size; merkle_root})
    (obj3
       (req "inbox_length" int31)
       (req "cumulated_size" int31)
       (req "merkle_root" Merkle.root_encoding))

let empty =
  {inbox_length = 0; cumulated_size = 0; merkle_root = Merkle_list.empty}

let size = Z.of_int @@ Data_encoding.Binary.length encoding empty

let pp fmt {inbox_length; cumulated_size; merkle_root} =
  Format.fprintf
    fmt
    "Inbox with length %d, size %d, merkle root %a"
    inbox_length
    cumulated_size
    Merkle.pp_root
    merkle_root
OCaml

Innovation. Community. Security.