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

MinaProtocol / mina / 3570

03 Apr 2025 11:24PM UTC coverage: 36.683% (-0.5%) from 37.198%
3570

push

buildkite

web-flow
Merge pull request #16846 from MinaProtocol/feature/remove-unused-argument

Pickles: Remove unused function argument

0 of 3 new or added lines in 3 files covered. (0.0%)

3181 existing lines in 31 files now uncovered.

24829 of 67686 relevant lines covered (36.68%)

19962.8 hits per line

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

24.29
/src/lib/mina_base/user_command.ml
1
open Core_kernel
22✔
2

3
module Poly = struct
4
  [%%versioned
5
  module Stable = struct
6
    module V2 = struct
7
      type ('u, 's) t =
47✔
8
            ('u, 's) Mina_wire_types.Mina_base.User_command.Poly.V2.t =
9
        | Signed_command of 'u
×
10
        | Zkapp_command of 's
×
11
      [@@deriving sexp, compare, equal, hash, yojson]
110✔
12

13
      let to_latest = Fn.id
14
    end
15

16
    module V1 = struct
17
      type ('u, 's) t = Signed_command of 'u | Snapp_command of 's
×
18
      [@@deriving sexp, compare, equal, hash, yojson]
22✔
19

20
      let to_latest : _ t -> _ V2.t = function
21
        | Signed_command x ->
×
22
            Signed_command x
23
        | Snapp_command _ ->
×
24
            failwith "Snapp_command"
25
    end
26
  end]
27
end
28

29
type ('u, 's) t_ = ('u, 's) Poly.Stable.Latest.t =
30
  | Signed_command of 'u
31
  | Zkapp_command of 's
32

33
module Gen_make (C : Signed_command_intf.Gen_intf) = struct
34
  let to_signed_command f =
35
    Quickcheck.Generator.map f ~f:(fun c -> Signed_command c)
2✔
36

37
  open C.Gen
38

39
  let payment ?sign_type ~key_gen ?nonce ~max_amount ~fee_range () =
40
    to_signed_command
×
41
      (payment ?sign_type ~key_gen ?nonce ~max_amount ~fee_range ())
×
42

43
  let payment_with_random_participants ?sign_type ~keys ?nonce ~max_amount
44
      ~fee_range () =
45
    to_signed_command
1✔
46
      (payment_with_random_participants ?sign_type ~keys ?nonce ~max_amount
1✔
47
         ~fee_range () )
48

49
  let stake_delegation ~key_gen ?nonce ~fee_range () =
50
    to_signed_command (stake_delegation ~key_gen ?nonce ~fee_range ())
×
51

52
  let stake_delegation_with_random_participants ~keys ?nonce ~fee_range () =
53
    to_signed_command
×
54
      (stake_delegation_with_random_participants ~keys ?nonce ~fee_range ())
×
55

56
  let sequence ?length ?sign_type a =
57
    Quickcheck.Generator.map
×
58
      (sequence ?length ?sign_type a)
×
59
      ~f:(List.map ~f:(fun c -> Signed_command c))
×
60
end
61

62
module Gen = Gen_make (Signed_command)
63

64
let gen_signed =
65
  let module G = Signed_command.Gen in
66
  let open Quickcheck.Let_syntax in
67
  let%bind keys =
68
    Quickcheck.Generator.list_with_length 2
22✔
69
      Mina_base_import.Signature_keypair.gen
70
  in
71
  G.payment_with_random_participants ~sign_type:`Real ~keys:(Array.of_list keys)
×
72
    ~max_amount:10000 ~fee_range:1000 ()
73

74
let gen = Gen.to_signed_command gen_signed
22✔
75

76
[%%versioned
77
module Stable = struct
78
  module V2 = struct
79
    type t =
22✔
80
      (Signed_command.Stable.V2.t, Zkapp_command.Stable.V1.t) Poly.Stable.V2.t
×
81
    [@@deriving sexp, compare, equal, hash, yojson]
110✔
82

83
    let to_latest = Fn.id
84
  end
85
end]
86

87
let to_base64 : t -> string = function
88
  | Signed_command sc ->
×
89
      Signed_command.to_base64 sc
90
  | Zkapp_command zc ->
×
91
      Zkapp_command.to_base64 zc
92

93
let of_base64 s : t Or_error.t =
94
  match Signed_command.of_base64 s with
×
95
  | Ok sc ->
×
96
      Ok (Signed_command sc)
97
  | Error err1 -> (
×
98
      match Zkapp_command.of_base64 s with
99
      | Ok zc ->
×
100
          Ok (Zkapp_command zc)
101
      | Error err2 ->
×
102
          Error
103
            (Error.of_string
×
104
               (sprintf
×
105
                  "Could decode Base64 neither to signed command (%s), nor to \
106
                   zkApp (%s)"
107
                  (Error.to_string_hum err1) (Error.to_string_hum err2) ) ) )
×
108

109
(*
110
include Allocation_functor.Make.Versioned_v1.Full_compare_eq_hash (struct
111
  let id = "user_command"
112

113
  [%%versioned
114
  module Stable = struct
115
    module V1 = struct
116
      type t =
117
        (Signed_command.Stable.V1.t, Snapp_command.Stable.V1.t) Poly.Stable.V1.t
118
      [@@deriving sexp, compare, equal, hash, yojson]
119

120
      let to_latest = Fn.id
121

122
      type 'a creator : Signed_command.t -> Snapp_command.t -> 'a
123

124
      let create cmd1 cmd2 = (cmd1, cmd2)
125
    end
126
  end]
127
end)
128
*)
129

130
module Zero_one_or_two = struct
131
  [%%versioned
132
  module Stable = struct
133
    module V1 = struct
134
      type 'a t = [ `Zero | `One of 'a | `Two of 'a * 'a ]
×
135
      [@@deriving sexp, compare, equal, hash, yojson]
22✔
136
    end
137
  end]
138
end
139

140
module Verifiable = struct
141
  type t =
22✔
142
    ( Signed_command.Stable.Latest.t
×
143
    , Zkapp_command.Verifiable.t )
×
144
    Poly.Stable.Latest.t
×
145
  [@@deriving sexp, compare, equal, hash, yojson, bin_io_unversioned]
88✔
146

147
  let fee_payer (t : t) =
148
    match t with
×
149
    | Signed_command x ->
×
150
        Signed_command.fee_payer x
151
    | Zkapp_command p ->
×
152
        Account_update.Fee_payer.account_id p.fee_payer
153
end
154

155
let to_verifiable (t : t) ~failed ~find_vk : Verifiable.t Or_error.t =
156
  match t with
×
157
  | Signed_command c ->
×
158
      Ok (Signed_command c)
159
  | Zkapp_command cmd ->
×
160
      Zkapp_command.Verifiable.create ~failed ~find_vk cmd
×
161
      |> Or_error.map ~f:(fun cmd -> Zkapp_command cmd)
×
162

163
module Make_to_all_verifiable
164
    (Strategy : Zkapp_command.Verifiable.Create_all_intf) =
165
struct
166
  let to_all_verifiable (ts : t Strategy.Command_wrapper.t list) ~load_vk_cache
167
      : Verifiable.t Strategy.Command_wrapper.t list Or_error.t =
168
    let open Or_error.Let_syntax in
200✔
169
    (* First we tag everything with its index *)
170
    let its = List.mapi ts ~f:(fun i x -> (i, x)) in
480✔
171
    (* then we partition out the zkapp commands *)
172
    let izk_cmds, is_cmds =
200✔
173
      List.partition_map its ~f:(fun (i, cmd) ->
174
          match Strategy.Command_wrapper.unwrap cmd with
480✔
175
          | Zkapp_command c ->
×
176
              First (i, Strategy.Command_wrapper.map cmd ~f:(Fn.const c))
×
177
          | Signed_command c ->
480✔
178
              Second (i, Strategy.Command_wrapper.map cmd ~f:(Fn.const c)) )
480✔
179
    in
180
    (* then unzip the indices *)
181
    let ixs, zk_cmds = List.unzip izk_cmds in
200✔
182
    (* then we verify the zkapp commands *)
183
    (* TODO: we could optimize this by skipping the fee payer and non-proof authorizations *)
184
    let accounts_referenced =
200✔
185
      List.fold_left zk_cmds ~init:Account_id.Set.empty ~f:(fun set zk_cmd ->
186
          Strategy.Command_wrapper.unwrap zk_cmd
×
187
          |> Zkapp_command.accounts_referenced |> Account_id.Set.of_list
×
188
          |> Set.union set )
189
    in
190
    let vk_cache = load_vk_cache accounts_referenced in
200✔
191
    let%map vzk_cmds = Strategy.create_all zk_cmds vk_cache in
200✔
192
    (* rezip indices *)
193
    let ivzk_cmds = List.zip_exn ixs vzk_cmds in
200✔
194
    (* Put them back in with a sort by index (un-partition) *)
195
    let ivs =
200✔
196
      List.map is_cmds ~f:(fun (i, cmd) ->
200✔
197
          (i, Strategy.Command_wrapper.map cmd ~f:(fun c -> Signed_command c)) )
480✔
198
      @ List.map ivzk_cmds ~f:(fun (i, cmd) ->
200✔
199
            (i, Strategy.Command_wrapper.map cmd ~f:(fun c -> Zkapp_command c)) )
×
200
      |> List.sort ~compare:(fun (i, _) (j, _) -> i - j)
700✔
201
    in
202
    (* Drop the indices *)
203
    List.unzip ivs |> snd
200✔
204
end
205

206
module Unapplied_sequence =
207
  Make_to_all_verifiable (Zkapp_command.Verifiable.From_unapplied_sequence)
208
module Applied_sequence =
209
  Make_to_all_verifiable (Zkapp_command.Verifiable.From_applied_sequence)
210

211
let of_verifiable (t : Verifiable.t) : t =
212
  match t with
480✔
213
  | Signed_command x ->
480✔
214
      Signed_command x
215
  | Zkapp_command p ->
×
216
      Zkapp_command (Zkapp_command.of_verifiable p)
×
217

218
let fee : t -> Currency.Fee.t = function
219
  | Signed_command x ->
20,000✔
220
      Signed_command.fee x
221
  | Zkapp_command p ->
×
222
      Zkapp_command.fee p
223

224
let has_insufficient_fee ~minimum_fee t = Currency.Fee.(fee t < minimum_fee)
×
225

226
let is_disabled = function
227
  | Zkapp_command _ ->
×
228
      Node_config_unconfigurable_constants.zkapps_disabled
229
  | _ ->
×
230
      false
231

232
(* always `Accessed` for fee payer *)
233
let accounts_accessed (t : t) (status : Transaction_status.t) :
234
    (Account_id.t * [ `Accessed | `Not_accessed ]) list =
235
  match t with
×
236
  | Signed_command x ->
×
237
      Signed_command.account_access_statuses x status
238
  | Zkapp_command ps ->
×
239
      Zkapp_command.account_access_statuses ps status
240

241
let accounts_referenced (t : t) =
242
  List.map (accounts_accessed t Applied) ~f:(fun (acct_id, _status) -> acct_id)
×
243

244
let fee_payer (t : t) =
245
  match t with
480✔
246
  | Signed_command x ->
480✔
247
      Signed_command.fee_payer x
248
  | Zkapp_command p ->
×
249
      Zkapp_command.fee_payer p
250

251
(** The application nonce is the nonce of the fee payer at which a user command can be applied. *)
252
let applicable_at_nonce (t : t) =
253
  match t with
×
254
  | Signed_command x ->
×
255
      Signed_command.nonce x
256
  | Zkapp_command p ->
×
257
      Zkapp_command.applicable_at_nonce p
258

259
let expected_target_nonce t = Account.Nonce.succ (applicable_at_nonce t)
×
260

261
let extract_vks : t -> (Account_id.t * Verification_key_wire.t) List.t =
262
  function
263
  | Signed_command _ ->
×
264
      []
265
  | Zkapp_command cmd ->
×
266
      Zkapp_command.extract_vks cmd
267

268
(** The target nonce is what the nonce of the fee payer will be after a user command is successfully applied. *)
269
let target_nonce_on_success (t : t) =
270
  match t with
×
271
  | Signed_command x ->
×
272
      Account.Nonce.succ (Signed_command.nonce x)
×
273
  | Zkapp_command p ->
×
274
      Zkapp_command.target_nonce_on_success p
275

276
let fee_token (t : t) =
277
  match t with
×
278
  | Signed_command x ->
×
279
      Signed_command.fee_token x
280
  | Zkapp_command x ->
×
281
      Zkapp_command.fee_token x
282

283
let valid_until (t : t) =
284
  match t with
×
285
  | Signed_command x ->
×
286
      Signed_command.valid_until x
287
  | Zkapp_command { fee_payer; _ } -> (
×
288
      match fee_payer.Account_update.Fee_payer.body.valid_until with
289
      | Some valid_until ->
×
290
          valid_until
291
      | None ->
×
292
          Mina_numbers.Global_slot_since_genesis.max_value )
293

294
module Valid = struct
295
  type t_ = t
296

297
  type t = (Signed_command.With_valid_signature.t, Zkapp_command.Valid.t) Poly.t
×
298
  [@@deriving sexp, compare, equal, hash, yojson]
×
299

300
  module Gen = Gen_make (Signed_command.With_valid_signature)
301
end
302

303
module For_tests = struct
304
  let check_verifiable (t : Verifiable.t) : Valid.t Or_error.t =
305
    match t with
×
UNCOV
306
    | Signed_command x -> (
×
307
        match Signed_command.check x with
UNCOV
308
        | Some c ->
×
309
            Ok (Signed_command c)
UNCOV
310
        | None ->
×
311
            Or_error.error_string "Invalid signature" )
312
    | Zkapp_command p ->
×
UNCOV
313
        Ok (Zkapp_command (Zkapp_command.Valid.For_tests.of_verifiable p))
×
314
end
315

316
let forget_check (t : Valid.t) : t =
317
  match t with
22,560✔
UNCOV
318
  | Zkapp_command x ->
×
319
      Zkapp_command (Zkapp_command.Valid.forget x)
×
320
  | Signed_command c ->
22,560✔
321
      Signed_command (c :> Signed_command.t)
322

323
let to_valid_unsafe (t : t) =
324
  `If_this_is_used_it_should_have_a_comment_justifying_it
960✔
325
    ( match t with
UNCOV
326
    | Zkapp_command x ->
×
327
        let (`If_this_is_used_it_should_have_a_comment_justifying_it x) =
328
          Zkapp_command.Valid.to_valid_unsafe x
329
        in
UNCOV
330
        Zkapp_command x
×
331
    | Signed_command x ->
960✔
332
        (* This is safe due to being immediately wrapped again. *)
333
        let (`If_this_is_used_it_should_have_a_comment_justifying_it x) =
334
          Signed_command.to_valid_unsafe x
335
        in
336
        Signed_command x )
960✔
337

338
let filter_by_participant (commands : t list) public_key =
UNCOV
339
  List.filter commands ~f:(fun user_command ->
×
340
      Core_kernel.List.exists
×
341
        (accounts_referenced user_command)
×
342
        ~f:
UNCOV
343
          (Fn.compose
×
344
             (Signature_lib.Public_key.Compressed.equal public_key)
×
345
             Account_id.public_key ) )
346

347
(* A metric on user commands that should correspond roughly to resource costs
348
   for validation/application *)
349
let weight : t -> int = function
UNCOV
350
  | Signed_command signed_command ->
×
351
      Signed_command.payload signed_command |> Signed_command_payload.weight
×
352
  | Zkapp_command zkapp_command ->
×
353
      Zkapp_command.weight zkapp_command
354

355
(* Fee per weight unit *)
356
let fee_per_wu (user_command : Stable.Latest.t) : Currency.Fee_rate.t =
357
  (*TODO: return Or_error*)
UNCOV
358
  Currency.Fee_rate.make_exn (fee user_command) (weight user_command)
×
359

360
let valid_size ~genesis_constants = function
UNCOV
361
  | Signed_command _ ->
×
362
      Ok ()
UNCOV
363
  | Zkapp_command zkapp_command ->
×
364
      Zkapp_command.valid_size ~genesis_constants zkapp_command
365

366
let has_zero_vesting_period = function
UNCOV
367
  | Signed_command _ ->
×
368
      false
UNCOV
369
  | Zkapp_command p ->
×
370
      Zkapp_command.has_zero_vesting_period p
371

372
let is_incompatible_version = function
UNCOV
373
  | Signed_command _ ->
×
374
      false
UNCOV
375
  | Zkapp_command p ->
×
376
      Zkapp_command.is_incompatible_version p
377

378
let has_invalid_call_forest : t -> bool = function
UNCOV
379
  | Signed_command _ ->
×
380
      false
UNCOV
381
  | Zkapp_command cmd ->
×
382
      List.exists cmd.account_updates ~f:(fun call_forest ->
UNCOV
383
          let root_may_use_token =
×
384
            call_forest.elt.account_update.body.may_use_token
385
          in
UNCOV
386
          not (Account_update.May_use_token.equal root_may_use_token No) )
×
387

388
module Well_formedness_error = struct
389
  (* syntactically-evident errors such that a user command can never succeed *)
UNCOV
390
  type t =
×
391
    | Insufficient_fee
×
392
    | Zero_vesting_period
×
393
    | Zkapp_too_big of (Error.t[@to_yojson Error_json.error_to_yojson])
×
394
    | Zkapp_invalid_call_forest
×
395
    | Transaction_type_disabled
×
396
    | Incompatible_version
×
397
  [@@deriving compare, to_yojson, sexp]
398

399
  let to_string = function
UNCOV
400
    | Insufficient_fee ->
×
401
        "Insufficient fee"
UNCOV
402
    | Zero_vesting_period ->
×
403
        "Zero vesting period"
UNCOV
404
    | Zkapp_too_big err ->
×
405
        sprintf "Zkapp too big (%s)" (Error.to_string_hum err)
×
406
    | Zkapp_invalid_call_forest ->
×
407
        "Zkapp has an invalid call forest (root account updates may not use \
408
         tokens)"
UNCOV
409
    | Incompatible_version ->
×
410
        "Set verification-key permission is updated to an incompatible version"
UNCOV
411
    | Transaction_type_disabled ->
×
412
        "Transaction type disabled"
413
end
414

415
let check_well_formedness ~(genesis_constants : Genesis_constants.t) t :
416
    (unit, Well_formedness_error.t list) result =
UNCOV
417
  let preds =
×
418
    let open Well_formedness_error in
419
    [ ( has_insufficient_fee
420
          ~minimum_fee:genesis_constants.minimum_user_command_fee
421
      , Insufficient_fee )
422
    ; (has_zero_vesting_period, Zero_vesting_period)
423
    ; (is_incompatible_version, Incompatible_version)
424
    ; (is_disabled, Transaction_type_disabled)
425
    ; (has_invalid_call_forest, Zkapp_invalid_call_forest)
426
    ]
427
  in
428
  let errs0 =
429
    List.fold preds ~init:[] ~f:(fun acc (f, err) ->
UNCOV
430
        if f t then err :: acc else acc )
×
431
  in
UNCOV
432
  let errs =
×
433
    match valid_size ~genesis_constants t with
UNCOV
434
    | Ok () ->
×
435
        errs0
UNCOV
436
    | Error err ->
×
437
        Zkapp_too_big err :: errs0
438
  in
UNCOV
439
  if List.is_empty errs then Ok () else Error errs
×
440

UNCOV
441
type fee_payer_summary_t = Signature.t * Account.key * int
×
442
[@@deriving yojson, hash]
×
443

444
let fee_payer_summary : t -> fee_payer_summary_t = function
UNCOV
445
  | Zkapp_command cmd ->
×
446
      let fp = Zkapp_command.fee_payer_account_update cmd in
UNCOV
447
      let open Account_update in
×
448
      let body = Fee_payer.body fp in
UNCOV
449
      ( Fee_payer.authorization fp
×
450
      , Body.Fee_payer.public_key body
×
451
      , Body.Fee_payer.nonce body |> Unsigned.UInt32.to_int )
×
452
  | Signed_command cmd ->
×
453
      Signed_command.
UNCOV
454
        (signature cmd, fee_payer_pk cmd, nonce cmd |> Unsigned.UInt32.to_int)
×
455

456
let fee_payer_summary_json =
457
  Fn.compose fee_payer_summary_t_to_yojson fee_payer_summary
22✔
458

459
let fee_payer_summary_string =
460
  let to_string (signature, pk, nonce) =
UNCOV
461
    sprintf "%s (%s %d)"
×
462
      (Signature.to_base58_check signature)
×
463
      (Signature_lib.Public_key.Compressed.to_base58_check pk)
×
464
      nonce
465
  in
466
  Fn.compose to_string fee_payer_summary
22✔
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