• 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

20.0
/src/lib/network_pool/intf.ml
1
open Async_kernel
1✔
2
open Core_kernel
3
open Mina_base
4
open Mina_transaction
5
open Pipe_lib
6
open Network_peer
7

8
(** A [Resource_pool_base_intf] is a mutable pool of resources that supports
9
 *  mutation via some [Resource_pool_diff_intf]. A [Resource_pool_base_intf]
10
 *  can only be initialized, and any interaction with it must go through
11
 *  its [Resource_pool_diff_intf] *)
12
module type Resource_pool_base_intf = sig
13
  type t [@@deriving sexp_of]
14

15
  val label : string
16

17
  type transition_frontier_diff
18

19
  type transition_frontier
20

21
  module Config : sig
22
    type t [@@deriving sexp_of]
23
  end
24

25
  (** Diff from a transition frontier extension that would update the resource pool*)
26
  val handle_transition_frontier_diff : transition_frontier_diff -> t -> unit
27

28
  val create :
29
       constraint_constants:Genesis_constants.Constraint_constants.t
30
    -> consensus_constants:Consensus.Constants.t
31
    -> time_controller:Block_time.Controller.t
32
    -> frontier_broadcast_pipe:
33
         transition_frontier Option.t Broadcast_pipe.Reader.t
34
    -> config:Config.t
35
    -> logger:Logger.t
36
    -> tf_diff_writer:
37
         ( transition_frontier_diff
38
         , Strict_pipe.synchronous
39
         , unit Deferred.t )
40
         Strict_pipe.Writer.t
41
    -> t
42
end
43

44
module Verification_error = struct
45
  type t = Fee_higher | Fee_equal | Invalid of Error.t | Failure of Error.t
46

47
  let to_error = function
48
    | Fee_equal ->
×
49
        Error.of_string "fee equal to cheapest work we have"
50
    | Fee_higher ->
×
51
        Error.of_string "fee higher than cheapest work we have"
52
    | Invalid err ->
×
53
        Error.tag err ~tag:"invalid"
54
    | Failure err ->
×
55
        Error.tag err ~tag:"failure"
56

57
  let to_short_string = function
58
    | Fee_equal ->
×
59
        "fee_equal"
60
    | Fee_higher ->
×
61
        "fee_higher"
62
    | Invalid _ ->
×
63
        "invalid"
64
    | Failure _ ->
×
65
        "failure"
66
end
67

68
(** A [Resource_pool_diff_intf] is a representation of a mutation to
69
 *  perform on a [Resource_pool_base_intf]. It includes the logic for
70
 *  processing this mutation and applying it to an underlying
71
 *  [Resource_pool_base_intf]. *)
72
module type Resource_pool_diff_intf = sig
73
  type pool
74

75
  val label : string
76

77
  type t [@@deriving sexp, to_yojson]
78

79
  type verified [@@deriving sexp, to_yojson]
80

81
  (** Part of the diff that was not added to the resource pool*)
82
  type rejected [@@deriving sexp, to_yojson]
83

84
  val empty : t
85

86
  val reject_overloaded_diff : verified -> rejected
87

88
  (** Used to check whether or not information was filtered out of diffs
89
   *  during diff application. Assumes that diff size will be the equal or
90
   *  smaller after application is completed. *)
91
  val size : t -> int
92

93
  (* TODO
94
     val verified_size : verified -> int
95
  *)
96

97
  (** How big to consider this diff for purposes of metering. *)
98
  val score : t -> int
99

100
  (** The maximum "diff score" permitted per IP/peer-id per 15 seconds. *)
101
  val max_per_15_seconds : int
102

103
  val summary : t -> string
104

105
  (** Warning: It must be safe to call this function asynchronously! *)
106
  val verify :
107
       pool
108
    -> t Envelope.Incoming.t
109
    -> (verified Envelope.Incoming.t, Verification_error.t) Deferred.Result.t
110

111
  (** Warning: Using this directly could corrupt the resource pool if it
112
      conincides with applying locally generated diffs or diffs from the network
113
      or diffs from transition frontier extensions.*)
114
  val unsafe_apply :
115
       pool
116
    -> verified Envelope.Incoming.t
117
    -> ( [ `Accept | `Reject ] * t * rejected
118
       , [ `Locally_generated of t * rejected | `Other of Error.t ] )
119
       Result.t
120

121
  val is_empty : t -> bool
122

123
  val update_metrics :
124
       logger:Logger.t
125
    -> log_gossip_heard:bool
126
    -> t Envelope.Incoming.t
127
    -> Mina_net2.Validation_callback.t
128
    -> unit
129

130
  val log_internal :
131
    ?reason:string -> logger:Logger.t -> string -> t Envelope.Incoming.t -> unit
132

133
  val t_of_verified : verified -> t
134
end
135

136
(** A [Resource_pool_intf] ties together an associated pair of
137
 *  [Resource_pool_base_intf] and [Resource_pool_diff_intf]. *)
138
module type Resource_pool_intf = sig
139
  include Resource_pool_base_intf
140

141
  module Diff : Resource_pool_diff_intf with type pool := t
142

143
  (** Locally generated items (user commands and snarks) should be periodically
144
      rebroadcast, to ensure network unreliability doesn't mean they're never
145
      included in a block. This function gets the locally generated items that
146
      are currently rebroadcastable. [has_timed_out] is a function that returns
147
      true if an item that was added at a given time should not be rebroadcast
148
      anymore. If it does, the implementation should not return that item, and
149
      remove it from the set of potentially-rebroadcastable item.
150
  *)
151
  val get_rebroadcastable :
152
    t -> has_timed_out:(Time.t -> [ `Timed_out | `Ok ]) -> Diff.t list
153
end
154

155
module type Broadcast_callback = sig
156
  type resource_pool_diff
157

158
  type rejected_diff
159

160
  type t =
161
    | Local of
162
        (   ( [ `Broadcasted | `Not_broadcasted ]
163
            * resource_pool_diff
164
            * rejected_diff )
165
            Or_error.t
166
         -> unit )
167
    | External of Mina_net2.Validation_callback.t
168

169
  val drop : resource_pool_diff -> rejected_diff -> t -> unit
170
end
171

172
(** A [Network_pool_base_intf] is the core implementation of a
173
 *  network pool on top of a [Resource_pool_intf]. It wraps
174
 *  some [Resource_pool_intf] and provides a generic interface
175
 *  for interacting with the [Resource_pool_intf] using the
176
 *  network. A [Network_pool_base_intf] wires the [Resource_pool_intf]
177
 *  into the network using pipes of diffs and transition frontiers.
178
 *  It also provides a way to apply new diffs and rebroadcast them
179
 *  to the network if necessary. *)
180
module type Network_pool_base_intf = sig
181
  type t
182

183
  type resource_pool
184

185
  type resource_pool_diff
186

187
  type resource_pool_diff_verified
188

189
  type rejected_diff
190

191
  type transition_frontier_diff
192

193
  type config
194

195
  type transition_frontier
196

197
  module Local_sink :
198
    Mina_net2.Sink.S_with_void
199
      with type msg :=
200
        resource_pool_diff
201
        * (   ( [ `Broadcasted | `Not_broadcasted ]
202
              * resource_pool_diff
203
              * rejected_diff )
204
              Or_error.t
205
           -> unit )
206

207
  module Remote_sink :
208
    Mina_net2.Sink.S_with_void
209
      with type msg :=
210
        resource_pool_diff Envelope.Incoming.t * Mina_net2.Validation_callback.t
211

212
  module Broadcast_callback :
213
    Broadcast_callback
214
      with type resource_pool_diff := resource_pool_diff
215
       and type rejected_diff := rejected_diff
216

217
  val create :
218
       config:config
219
    -> constraint_constants:Genesis_constants.Constraint_constants.t
220
    -> consensus_constants:Consensus.Constants.t
221
    -> time_controller:Block_time.Controller.t
222
    -> frontier_broadcast_pipe:
223
         transition_frontier Option.t Broadcast_pipe.Reader.t
224
    -> logger:Logger.t
225
    -> log_gossip_heard:bool
226
    -> on_remote_push:(unit -> unit Deferred.t)
227
    -> block_window_duration:Time.Span.t
228
    -> t * Remote_sink.t * Local_sink.t
229

230
  val of_resource_pool_and_diffs :
231
       resource_pool
232
    -> logger:Logger.t
233
    -> constraint_constants:Genesis_constants.Constraint_constants.t
234
    -> tf_diffs:transition_frontier_diff Strict_pipe.Reader.t
235
    -> log_gossip_heard:bool
236
    -> on_remote_push:(unit -> unit Deferred.t)
237
    -> block_window_duration:Time.Span.t
238
    -> t * Remote_sink.t * Local_sink.t
239

240
  val resource_pool : t -> resource_pool
241

242
  val broadcasts : t -> resource_pool_diff With_nonce.t Linear_pipe.Reader.t
243

244
  val create_rate_limiter : unit -> Rate_limiter.t
245

246
  val apply_and_broadcast :
247
       t
248
    -> resource_pool_diff_verified Envelope.Incoming.t
249
    -> Broadcast_callback.t
250
    -> unit
251
end
252

253
(** A [Snark_resource_pool_intf] is a superset of a
254
 *  [Resource_pool_intf] specifically for handling snarks. *)
255
module type Snark_resource_pool_intf = sig
256
  include Resource_pool_base_intf
257

258
  val make_config :
259
       trust_system:Trust_system.t
260
    -> verifier:Verifier.t
261
    -> disk_location:string
262
    -> Config.t
263

264
  val add_snark :
265
       ?is_local:bool
266
    -> t
267
    -> work:Transaction_snark_work.Statement.t
268
    -> proof:Ledger_proof.t One_or_two.t
269
    -> fee:Fee_with_prover.t
270
    -> [ `Added | `Statement_not_referenced ]
271

272
  val request_proof :
273
       t
274
    -> Transaction_snark_work.Statement.t
275
    -> Ledger_proof.t One_or_two.t Priced_proof.t option
276

277
  val verify_and_act :
278
       t
279
    -> work:
280
         Transaction_snark_work.Statement.t
281
         * Ledger_proof.t One_or_two.t Priced_proof.t
282
    -> sender:Envelope.Sender.t
283
    -> (unit, Verification_error.t) Deferred.Result.t
284

285
  val snark_pool_json : t -> Yojson.Safe.t
286

287
  val all_completed_work : t -> Transaction_snark_work.Info.t list
288

289
  val get_logger : t -> Logger.t
290
end
291

292
(** A [Snark_pool_diff_intf] is the resource pool diff for
293
 *  a [Snark_resource_pool_intf]. *)
294
module type Snark_pool_diff_intf = sig
295
  type resource_pool
296

297
  type t = Mina_wire_types.Network_pool.Snark_pool.Diff_versioned.V2.t =
298
    | Add_solved_work of
299
        Transaction_snark_work.Statement.t
300
        * Ledger_proof.t One_or_two.t Priced_proof.t
301
    | Empty
302
  [@@deriving compare, sexp]
303

304
  type verified = t [@@deriving compare, sexp]
305

306
  type compact =
307
    { work_ids : int One_or_two.t
308
    ; fee : Currency.Fee.t
309
    ; prover : Signature_lib.Public_key.Compressed.t
310
    }
311
  [@@deriving yojson, hash]
312

313
  type Structured_log_events.t +=
314
    | Snark_work_received of { work : compact; sender : Envelope.Sender.t }
315
    [@@deriving register_event]
316

317
  include
318
    Resource_pool_diff_intf
319
      with type t := t
320
       and type verified := t
321
       and type pool := resource_pool
322

323
  val to_compact : t -> compact option
324

325
  val compact_json : t -> Yojson.Safe.t option
326

327
  val of_result :
328
       ( (_, _) Snark_work_lib.Work.Single.Spec.t Snark_work_lib.Work.Spec.t
329
       , Ledger_proof.t )
330
       Snark_work_lib.Work.Result.t
331
    -> t
332
end
333

334
module type Transaction_pool_diff_intf = sig
335
  type resource_pool
336

337
  type t = User_command.t list [@@deriving sexp, of_yojson]
338

339
  module Diff_error : sig
340
    type t =
341
      | Insufficient_replace_fee
342
      | Duplicate
343
      | Invalid_nonce
344
      | Insufficient_funds
345
      | Overflow
346
      | Bad_token
347
      | Unwanted_fee_token
348
      | Expired
349
      | Overloaded
350
      | Fee_payer_account_not_found
351
      | Fee_payer_not_permitted_to_send
352
      | After_slot_tx_end
353
    [@@deriving sexp, yojson]
354

355
    val to_string_hum : t -> string
356
  end
357

358
  module Rejected : sig
359
    type t = (User_command.t * Diff_error.t) list [@@deriving sexp, yojson]
360
  end
361

362
  type Structured_log_events.t +=
363
    | Transactions_received of
364
        { fee_payer_summaries : User_command.fee_payer_summary_t list
365
        ; sender : Envelope.Sender.t
366
        }
367
    [@@deriving register_event]
368

369
  include
370
    Resource_pool_diff_intf
371
      with type t := t
372
       and type pool := resource_pool
373
       and type rejected = Rejected.t
374
end
375

376
module type Transaction_resource_pool_intf = sig
377
  type t
378

379
  include Resource_pool_base_intf with type t := t
380

381
  val make_config :
382
       trust_system:Trust_system.t
383
    -> pool_max_size:int
384
    -> verifier:Verifier.t
385
    -> genesis_constants:Genesis_constants.t
386
    -> slot_tx_end:Mina_numbers.Global_slot_since_hard_fork.t option
387
    -> Config.t
388

389
  val member : t -> Transaction_hash.User_command_with_valid_signature.t -> bool
390

391
  val transactions :
392
    t -> Transaction_hash.User_command_with_valid_signature.t Sequence.t
393

394
  val all_from_account :
395
       t
396
    -> Account_id.t
397
    -> Transaction_hash.User_command_with_valid_signature.t list
398

399
  val get_all : t -> Transaction_hash.User_command_with_valid_signature.t list
400

401
  val find_by_hash :
402
       t
403
    -> Transaction_hash.t
404
    -> Transaction_hash.User_command_with_valid_signature.t option
405
end
406

407
module type Base_ledger_intf = sig
408
  type t
409

410
  module Location : sig
411
    type t
412
  end
413

414
  val location_of_account : t -> Account_id.t -> Location.t option
415

416
  val location_of_account_batch :
417
    t -> Account_id.t list -> (Account_id.t * Location.t option) list
418

419
  val get : t -> Location.t -> Account.t option
420

421
  val accounts : t -> Account_id.Set.t Deferred.t
422

423
  val get_batch : t -> Location.t list -> (Location.t * Account.t option) list
424

425
  val detached_signal : t -> unit Deferred.t
426
end
1✔
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