package tezos-dal-node-lib

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

Source file transport_layer_interface.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2023 Functori,     <contact@functori.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 Types = Tezos_dal_node_services.Types

(* FIXME: https://gitlab.com/tezos/tezos/-/issues/5583

   Version this type to ease future migrations. *)
module P2p_message_V1 = struct
  type px_peer = {point : P2p_point.Id.t; peer : P2p_peer.Id.t}

  type p2p_message =
    | Graft of {topic : Types.Topic.t}
    | Prune of {
        topic : Types.Topic.t;
        px : px_peer Seq.t;
        backoff : Types.Span.t;
      }
    | IHave of {topic : Types.Topic.t; message_ids : Types.Message_id.t list}
    | IWant of {message_ids : Types.Message_id.t list}
    | Subscribe of {topic : Types.Topic.t}
    | Unsubscribe of {topic : Types.Topic.t}
    | Message_with_header of {
        message : Types.Message.t;
        topic : Types.Topic.t;
        message_id : Types.Message_id.t;
      }

  let px_peer_encoding =
    let open Data_encoding in
    conv
      (fun {point; peer} -> (point, peer))
      (fun (point, peer) -> {point; peer})
      (obj2
         (req "point" P2p_point.Id.encoding)
         (req "peer" P2p_peer.Id.encoding))

  (* FIXME: https://gitlab.com/tezos/tezos/-/issues/5564

     DAL/GS: bound the lists/seqs in exchanged p2p messages. *)
  let p2p_message_app_encoding =
    let open Data_encoding in
    let case ?max_length ~tag ~title encoding unwrap wrap =
      P2p_params.Encoding {tag; title; encoding; wrap; unwrap; max_length}
    in
    [
      case
        ~tag:0x10
        ~title:"Graft"
        (obj2
           (req "kind" (constant "graft"))
           (req "topic" Types.Topic.encoding))
        (function Graft {topic} -> Some ((), topic) | _ -> None)
        (fun ((), topic) -> Graft {topic});
      case
        ~tag:0x11
        ~title:"Prune"
        (obj4
           (req "kind" (constant "prune"))
           (req "topic" Types.Topic.encoding)
           (req "px" (list px_peer_encoding))
           (req "backoff" Types.Span.encoding))
        (function
          | Prune {topic; px; backoff} ->
              Some ((), topic, List.of_seq px, backoff)
          | _ -> None)
        (fun ((), topic, px, backoff) ->
          Prune {topic; px = List.to_seq px; backoff});
      case
        ~tag:0x12
        ~title:"IHave"
        (obj3
           (req "kind" (constant "ihave"))
           (req "topic" Types.Topic.encoding)
           (req "message_ids" (list Types.Message_id.encoding)))
        (function
          | IHave {topic; message_ids} -> Some ((), topic, message_ids)
          | _ -> None)
        (fun ((), topic, message_ids) -> IHave {topic; message_ids});
      case
        ~tag:0x13
        ~title:"IWant"
        (obj2
           (req "kind" (constant "iwant"))
           (req "message_ids" (list Types.Message_id.encoding)))
        (function IWant {message_ids} -> Some ((), message_ids) | _ -> None)
        (fun ((), message_ids) -> IWant {message_ids});
      case
        ~tag:0x14
        ~title:"Subscribe"
        (obj2
           (req "kind" (constant "subscribe"))
           (req "topic" Types.Topic.encoding))
        (function Subscribe {topic} -> Some ((), topic) | _ -> None)
        (fun ((), topic) -> Subscribe {topic});
      case
        ~tag:0x15
        ~title:"Unsubscribe"
        (obj2
           (req "kind" (constant "unsubscribe"))
           (req "topic" Types.Topic.encoding))
        (function Unsubscribe {topic} -> Some ((), topic) | _ -> None)
        (fun ((), topic) -> Unsubscribe {topic});
      case
        ~tag:0x16
        ~title:"Message_with_header"
        (obj4
           (req "kind" (constant "message_with_header"))
           (req "message" Types.Message.encoding)
           (req "topic" Types.Topic.encoding)
           (req "message_id" Types.Message_id.encoding))
        (function
          | Message_with_header {message; topic; message_id} ->
              Some ((), message, topic, message_id)
          | _ -> None)
        (fun ((), message, topic, message_id) ->
          Message_with_header {message; topic; message_id});
    ]

  let p2p_message_encoding =
    let open Data_encoding in
    List.map
      (fun (P2p_params.Encoding
             {tag; title; encoding; wrap; unwrap; max_length = _}) ->
        case (Tag tag) ~title encoding unwrap wrap)
      p2p_message_app_encoding
    |> union

  let pp_list pp_elt =
    Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt "; ") pp_elt

  let pp_p2p_message fmt = function
    | Graft {topic} -> Format.fprintf fmt "Graft{topic=%a}" Types.Topic.pp topic
    | Prune {topic; px; backoff} ->
        Format.fprintf
          fmt
          "Prune{topic=%a, px=%a, backoff=%a}"
          Types.Topic.pp
          topic
          (pp_list Types.Peer.pp)
          (List.of_seq px |> List.map (fun px_peer -> px_peer.peer))
          Types.Span.pp
          backoff
    | IHave {topic; message_ids} ->
        Format.fprintf
          fmt
          "IHave{topic=%a, message_ids=%a}"
          Types.Topic.pp
          topic
          (pp_list Types.Message_id.pp)
          message_ids
    | IWant {message_ids} ->
        Format.fprintf
          fmt
          "IWant{message_ids=%a}"
          (pp_list Types.Message_id.pp)
          message_ids
    | Subscribe {topic} ->
        Format.fprintf fmt "Subscribe{topic=%a}" Types.Topic.pp topic
    | Unsubscribe {topic} ->
        Format.fprintf fmt "Unsubscribe{topic=%a}" Types.Topic.pp topic
    | Message_with_header {message = _; message_id; topic = _} ->
        Format.fprintf
          fmt
          "FullMessage{message_id=%a}"
          Types.Message_id.pp
          message_id

  let distributed_db_version = Distributed_db_version.zero

  (* FIXME: https://gitlab.com/tezos/tezos/-/issues/5638

     Decide how to safely choose the node db version. *)
  let distributed_db_versions = [distributed_db_version]

  let message_config ~network_name : p2p_message P2p_params.message_config =
    let chain_name = Distributed_db_version.Name.of_string network_name in
    {encoding = p2p_message_app_encoding; chain_name; distributed_db_versions}
end

let version ~network_name =
  Network_version.
    {
      chain_name = Distributed_db_version.Name.of_string network_name;
      distributed_db_version = P2p_message_V1.distributed_db_version;
      p2p_version = P2p_version.one;
    }

(* Exposed interface *)

include P2p_message_V1
OCaml

Innovation. Community. Security.