• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2863

05 Nov 2024 06:20PM UTC coverage: 30.754% (-16.6%) from 47.311%
2863

push

buildkite

web-flow
Merge pull request #16296 from MinaProtocol/dkijania/more_multi_jobs

more multi jobs in CI

20276 of 65930 relevant lines covered (30.75%)

8631.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.41
/src/lib/network_peer/peer.ml
1
(* peer.ml -- peer with libp2p port and peer id *)
2

7✔
3
open Core
4

5
(** A libp2p PeerID is more or less a hash of a public key. *)
6
module Id = struct
7
  [%%versioned
8
  module Stable = struct
9
    module V1 = struct
10
      type t = Bounded_types.String.Stable.V1.t
7✔
11
      [@@deriving compare, hash, equal, sexp]
35✔
12

13
      let to_latest = Fn.id
14
    end
15
  end]
16

17
  (** Convert to the libp2p-defined base58 string *)
18
  let to_string (x : t) = x
5✔
19

20
  (** Create a Peer ID from a string, without checking if it is well-formed. *)
21
  let unsafe_of_string (s : string) : t = s
112✔
22
end
23

24
module Inet_addr = struct
25
  [%%versioned_binable
26
  module Stable = struct
27
    module V1 = struct
28
      type t = Unix.Inet_addr.t [@@deriving sexp, compare, hash]
7✔
29

30
      let to_latest = Fn.id
31

32
      let of_yojson = function
33
        | `String s ->
×
34
            Ok (Unix.Inet_addr.of_string s)
×
35
        | _ ->
×
36
            Error "expected string"
37

38
      let to_yojson ip_addr = `String (Unix.Inet_addr.to_string ip_addr)
×
39

40
      include Bounded_types.String.Of_stringable (struct
41
        type nonrec t = t
42

43
        [%%define_locally Unix.Inet_addr.(to_string, of_string)]
44
      end)
45
    end
46
  end]
47

48
  [%%define_locally Stable.V1.(to_yojson, of_yojson)]
49
end
50

51
[%%versioned
52
module Stable = struct
53
  [@@@no_toplevel_latest_type]
54

55
  module V1 = struct
56
    type t =
14✔
57
      { host : Inet_addr.Stable.V1.t (* IPv4 or IPv6 address *)
×
58
      ; libp2p_port : int (* TCP *)
×
59
      ; peer_id : Id.Stable.V1.t
×
60
      }
61
    [@@deriving compare, sexp]
35✔
62

63
    let to_latest = Fn.id
64

65
    let equal t t' = compare t t' = 0
×
66

67
    (* these hash functions come from the implementation of Inet_addr,
68
         though they're not exposed *)
69
    let hash_fold_t hash t = hash_fold_int hash (Hashtbl.hash t)
×
70

71
    let hash : t -> int = Ppx_hash_lib.Std.Hash.of_fold hash_fold_t
7✔
72

73
    let to_yojson { host; peer_id; libp2p_port } =
74
      `Assoc
×
75
        [ ("host", `String (Unix.Inet_addr.to_string host))
×
76
        ; ("peer_id", `String peer_id)
77
        ; ("libp2p_port", `Int libp2p_port)
78
        ]
79

80
    let of_yojson =
81
      let lift_string = function `String s -> Some s | _ -> None in
×
82
      let lift_int = function `Int n -> Some n | _ -> None in
×
83
      function
84
      | `Assoc ls ->
×
85
          let open Option.Let_syntax in
86
          Result.of_option ~error:"missing keys"
87
            (let%bind host_str =
88
               List.Assoc.find ls "host" ~equal:String.equal >>= lift_string
×
89
             in
90
             let%bind peer_id =
91
               List.Assoc.find ls "peer_id" ~equal:String.equal >>= lift_string
×
92
             in
93
             let%map libp2p_port =
94
               List.Assoc.find ls "libp2p_port" ~equal:String.equal >>= lift_int
×
95
             in
96
             let host = Unix.Inet_addr.of_string host_str in
×
97
             { host; peer_id; libp2p_port } )
×
98
      | _ ->
×
99
          Error "expected object"
100
  end
101
end]
102

103
type t = Stable.Latest.t =
×
104
  { host : Unix.Inet_addr.Blocking_sexp.t; libp2p_port : int; peer_id : string }
×
105
[@@deriving compare, sexp]
106

107
[%%define_locally Stable.Latest.(of_yojson, to_yojson)]
108

109
include Hashable.Make (Stable.Latest)
110
include Comparable.Make_binable (Stable.Latest)
111

112
let create host ~libp2p_port ~peer_id = { host; libp2p_port; peer_id }
20✔
113

114
let to_discovery_host_and_port t =
115
  Host_and_port.create
×
116
    ~host:(Unix.Inet_addr.to_string t.host)
×
117
    ~port:t.libp2p_port
118

119
let to_string { host; libp2p_port; peer_id } =
120
  sprintf
×
121
    !"[host : %s, libp2p_port : %s, peer_id : %s]"
122
    (Unix.Inet_addr.to_string host)
×
123
    (Int.to_string libp2p_port)
×
124
    peer_id
125

126
let to_multiaddr_string { host; libp2p_port; peer_id } =
127
  sprintf "/ip4/%s/tcp/%d/p2p/%s"
×
128
    (Unix.Inet_addr.to_string host)
×
129
    libp2p_port peer_id
130

131
let pretty_list peers = String.concat ~sep:"," @@ List.map peers ~f:to_string
×
132

133
module Event = struct
134
  type t =
×
135
    | Connect of Stable.Latest.t list
×
136
    | Disconnect of Stable.Latest.t list
×
137
  [@@deriving sexp]
138
end
139

140
module Display = struct
141
  [%%versioned
142
  module Stable = struct
143
    [@@@no_toplevel_latest_type]
144

145
    module V1 = struct
146
      type t =
14✔
147
        { host : Bounded_types.String.Stable.V1.t
×
148
        ; libp2p_port : int
×
149
        ; peer_id : Bounded_types.String.Stable.V1.t
×
150
        }
151
      [@@deriving yojson, version, sexp, fields]
35✔
152

153
      let to_latest = Fn.id
154
    end
155
  end]
156

157
  type t = Stable.Latest.t =
×
158
    { host : string; libp2p_port : int; peer_id : string }
×
159
  [@@deriving yojson, sexp]
×
160

161
  module Fields = Stable.Latest.Fields
162
end
163

164
let ip { host; _ } = host
×
165

166
let to_display { host; libp2p_port; peer_id } =
167
  Display.
×
168
    { host = Unix.Inet_addr.to_string host
×
169
    ; libp2p_port
170
    ; peer_id = Id.to_string peer_id
×
171
    }
172

173
let of_display { Display.host; libp2p_port; peer_id } =
174
  { host = Unix.Inet_addr.of_string host
×
175
  ; libp2p_port
176
  ; peer_id = Id.unsafe_of_string peer_id
×
177
  }
14✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc