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

MinaProtocol / mina / 3409

26 Feb 2025 01:10PM UTC coverage: 32.353% (-28.4%) from 60.756%
3409

push

buildkite

web-flow
Merge pull request #16687 from MinaProtocol/dw/merge-compatible-into-develop-20250225

Merge compatible into develop [20250224]

23144 of 71535 relevant lines covered (32.35%)

16324.05 hits per line

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

46.55
/src/lib/mina_net2/validation_callback.ml
1
open Core
16✔
2
open Async
3
module Timeout = Timeout_lib.Core_time_ns
4

5
type validation_result = [ `Accept | `Reject | `Ignore ] [@@deriving equal]
×
6

7
type t =
8
  { expiration : Time_ns.t option
9
  ; created_at : Time_ns.t
10
  ; signal : validation_result Ivar.t
11
  ; mutable message_type : [ `Unknown | `Block | `Snark_work | `Transaction ]
12
  }
13

14
let create expiration =
15
  { expiration = Some expiration
6✔
16
  ; created_at = Time_ns.now ()
6✔
17
  ; signal = Ivar.create ()
6✔
18
  ; message_type = `Unknown
19
  }
20

21
let create_without_expiration () =
22
  { expiration = None
×
23
  ; created_at = Time_ns.now ()
×
24
  ; signal = Ivar.create ()
×
25
  ; message_type = `Unknown
26
  }
27

28
let is_expired cb =
29
  match cb.expiration with
12✔
30
  | None ->
×
31
      false
32
  | Some expires_at ->
12✔
33
      Time_ns.(now () >= expires_at)
12✔
34

35
module type Metric_intf = sig
36
  val validations_timed_out : Mina_metrics.Counter.t
37

38
  val rejected : Mina_metrics.Counter.t
39

40
  val ignored : Mina_metrics.Counter.t
41

42
  module Validation_time : sig
43
    val update : Time.Span.t -> unit
44
  end
45

46
  module Processing_time : sig
47
    val update : Time.Span.t -> unit
48
  end
49

50
  module Rejection_time : sig
51
    val update : Time.Span.t -> unit
52
  end
53
end
54

55
let metrics_of_message_type m : (module Metric_intf) option =
56
  match m with
6✔
57
  | `Unknown ->
6✔
58
      None
59
  | `Block ->
×
60
      Some (module Mina_metrics.Network.Block)
61
  | `Snark_work ->
×
62
      Some (module Mina_metrics.Network.Snark_work)
63
  | `Transaction ->
×
64
      Some (module Mina_metrics.Network.Transaction)
65

66
let record_timeout_metrics cb =
67
  Mina_metrics.(Counter.inc_one Network.validations_timed_out) ;
×
68
  match metrics_of_message_type cb.message_type with
69
  | None ->
×
70
      ()
71
  | Some (module M) ->
×
72
      Mina_metrics.Counter.inc_one M.validations_timed_out
73

74
let record_validation_metrics message_type (result : validation_result)
75
    validation_time processing_time =
76
  match metrics_of_message_type message_type with
6✔
77
  | None ->
6✔
78
      ()
79
  | Some (module M) -> (
×
80
      match result with
81
      | `Ignore ->
×
82
          Mina_metrics.Counter.inc_one M.ignored
83
      | `Accept ->
×
84
          M.Validation_time.update validation_time ;
85
          M.Processing_time.update processing_time
×
86
      | `Reject ->
×
87
          Mina_metrics.Counter.inc_one M.rejected ;
88
          M.Rejection_time.update processing_time )
×
89

90
let await_timeout cb =
91
  if is_expired cb then Deferred.return ()
×
92
  else
93
    match cb.expiration with
×
94
    | None ->
×
95
        Deferred.never ()
96
    | Some expires_at ->
×
97
        after
98
          ( Time_ns.Span.to_span_float_round_nearest
×
99
          @@ Time_ns.diff expires_at (Time_ns.now ()) )
×
100

101
let await cb =
102
  if is_expired cb then (record_timeout_metrics cb ; Deferred.return None)
×
103
  else
104
    match cb.expiration with
6✔
105
    | None ->
×
106
        Ivar.read cb.signal >>| Option.some
×
107
    | Some expires_at -> (
6✔
108
        match%map
109
          Timeout.await ()
6✔
110
            ~timeout_duration:(Time_ns.diff expires_at (Time_ns.now ()))
6✔
111
            (Ivar.read cb.signal)
6✔
112
        with
113
        | `Ok result ->
6✔
114
            let validation_time =
115
              Time_ns.abs_diff expires_at (Time_ns.now ())
6✔
116
              |> Time_ns.Span.to_ms |> Time.Span.of_ms
6✔
117
            in
118
            let processing_time =
6✔
119
              Time_ns.abs_diff (Time_ns.now ()) cb.created_at
6✔
120
              |> Time_ns.Span.to_ms |> Time.Span.of_ms
6✔
121
            in
122
            record_validation_metrics cb.message_type result validation_time
6✔
123
              processing_time ;
124
            Some result
6✔
125
        | `Timeout ->
×
126
            record_timeout_metrics cb ; None )
×
127

128
let await_exn cb =
129
  match%map await cb with None -> failwith "timeout" | Some result -> result
×
130

131
let fire_if_not_already_fired cb result =
132
  if not (is_expired cb) then
6✔
133
    if Ivar.is_full cb.signal then
6✔
134
      [%log' error (Logger.create ())] "Ivar.fill bug is here!"
×
135
    else Ivar.fill cb.signal result
6✔
136

137
let set_message_type t x = t.message_type <- x
×
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