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

MinaProtocol / mina / 176

13 May 2025 07:34PM UTC coverage: 30.854% (-31.0%) from 61.819%
176

push

buildkite

web-flow
Merge pull request #17171 from MinaProtocol/compiled-signature-kind-schnorr

Move deprecated value out of schnorr modules

12 of 40 new or added lines in 9 files covered. (30.0%)

19812 existing lines in 390 files now uncovered.

21939 of 71107 relevant lines covered (30.85%)

25333.08 hits per line

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

19.49
/src/lib/mina_base/user_command.ml
1
open Core_kernel
26✔
2
open Mina_stdlib
3

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

14
      let to_latest = Fn.id
15
    end
16

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

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

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

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

38
  open C.Gen
39

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

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

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

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

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

63
module Gen = Gen_make (Signed_command)
64

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

75
let gen = Gen.to_signed_command gen_signed
26✔
76

77
[%%versioned
78
module Stable = struct
79
  [@@@no_toplevel_latest_type]
80

81
  module V2 = struct
82
    type t =
26✔
UNCOV
83
      (Signed_command.Stable.V2.t, Zkapp_command.Stable.V1.t) Poly.Stable.V2.t
×
84
    [@@deriving sexp, compare, equal, hash, yojson]
130✔
85

86
    let to_latest = Fn.id
87
  end
88
end]
89

90
type t = (Signed_command.t, Zkapp_command.t) Poly.t
×
91
[@@deriving sexp_of, to_yojson]
×
92

93
let write_all_proofs_to_disk ~proof_cache_db : Stable.Latest.t -> t = function
400✔
94
  | Signed_command sc ->
480✔
95
      Signed_command sc
UNCOV
96
  | Zkapp_command zc ->
×
UNCOV
97
      Zkapp_command (Zkapp_command.write_all_proofs_to_disk ~proof_cache_db zc)
×
98

99
let read_all_proofs_from_disk : t -> Stable.Latest.t = function
100
  | Signed_command sc ->
1,440✔
101
      Signed_command sc
UNCOV
102
  | Zkapp_command zc ->
×
UNCOV
103
      Zkapp_command (Zkapp_command.read_all_proofs_from_disk zc)
×
104

105
type ('p, 'a, 'b) with_forest =
×
UNCOV
106
  (Signed_command.t, ('p, 'a, 'b) Zkapp_command.with_forest) Poly.t
×
107
[@@deriving equal]
108

109
let forget_digests_and_proofs (t : (_, _, _) with_forest) :
110
    (unit, unit, unit) with_forest =
UNCOV
111
  match t with
×
UNCOV
112
  | Signed_command sc ->
×
113
      Signed_command sc
UNCOV
114
  | Zkapp_command zc ->
×
UNCOV
115
      Zkapp_command (Zkapp_command.forget_digests_and_proofs zc)
×
116

117
let equal_ignoring_proofs_and_hashes
118
    (type proof_l account_update_digest_l forest_digest_l proof_r
119
    account_update_digest_r forest_digest_r )
120
    (t1 : (proof_l, account_update_digest_l, forest_digest_l) with_forest)
121
    (t2 : (proof_r, account_update_digest_r, forest_digest_r) with_forest) =
UNCOV
122
  let ignore2 _ _ = true in
×
123
  let t1' = forget_digests_and_proofs t1 in
UNCOV
124
  let t2' = forget_digests_and_proofs t2 in
×
UNCOV
125
  equal_with_forest ignore2 ignore2 ignore2 t1' t2'
×
126

127
let to_base64 : Stable.Latest.t -> string = function
UNCOV
128
  | Signed_command sc ->
×
129
      Signed_command.to_base64 sc
130
  | Zkapp_command zc ->
×
131
      Zkapp_command.to_base64 zc
132

133
let of_base64 s : Stable.Latest.t Or_error.t =
UNCOV
134
  match Signed_command.of_base64 s with
×
UNCOV
135
  | Ok sc ->
×
136
      Ok (Signed_command sc)
137
  | Error err1 -> (
×
138
      match Zkapp_command.of_base64 s with
139
      | Ok zc ->
×
140
          Ok (Zkapp_command zc)
141
      | Error err2 ->
×
142
          Error
143
            (Error.of_string
×
144
               (sprintf
×
145
                  "Could decode Base64 neither to signed command (%s), nor to \
146
                   zkApp (%s)"
147
                  (Error.to_string_hum err1) (Error.to_string_hum err2) ) ) )
×
148

149
module Zero_one_or_two = struct
150
  [%%versioned
151
  module Stable = struct
152
    module V1 = struct
153
      type 'a t = [ `Zero | `One of 'a | `Two of 'a * 'a ]
×
154
      [@@deriving sexp, compare, equal, hash, yojson]
26✔
155
    end
156
  end]
157
end
158

159
module Verifiable = struct
160
  type t =
×
161
    ( Signed_command.Stable.Latest.t
162
    , Zkapp_command.Verifiable.t )
163
    Poly.Stable.Latest.t
164
  [@@deriving sexp_of]
165

166
  let fee_payer (t : t) =
167
    match t with
×
168
    | Signed_command x ->
×
169
        Signed_command.fee_payer x
170
    | Zkapp_command p ->
×
171
        Account_update.Fee_payer.account_id p.fee_payer
172

173
  module Serializable = struct
174
    type t =
26✔
175
      ( Signed_command.Stable.Latest.t
176
      , Zkapp_command.Verifiable.Serializable.t )
177
      Poly.Stable.Latest.t
178
    [@@deriving bin_io_unversioned]
104✔
179
  end
180

181
  let to_serializable (t : t) : Serializable.t =
182
    match t with
×
183
    | Signed_command c ->
×
184
        Signed_command c
185
    | Zkapp_command cmd ->
×
186
        Zkapp_command (Zkapp_command.Verifiable.to_serializable cmd)
×
187

188
  let of_serializable ~proof_cache_db (t : Serializable.t) : t =
189
    match t with
×
190
    | Signed_command c ->
×
191
        Signed_command c
192
    | Zkapp_command cmd ->
×
193
        Zkapp_command
194
          (Zkapp_command.Verifiable.of_serializable ~proof_cache_db cmd)
×
195
end
196

197
let to_verifiable (t : t) ~failed ~find_vk : Verifiable.t Or_error.t =
198
  match t with
×
199
  | Signed_command c ->
×
200
      Ok (Signed_command c)
201
  | Zkapp_command cmd ->
×
202
      Zkapp_command.Verifiable.create ~failed ~find_vk cmd
×
203
      |> Or_error.map ~f:(fun cmd -> Zkapp_command cmd)
×
204

205
module Make_to_all_verifiable
206
    (Strategy : Zkapp_command.Verifiable.Create_all_intf) =
207
struct
208
  let to_all_verifiable (ts : t Strategy.Command_wrapper.t list) ~load_vk_cache
209
      : Verifiable.t Strategy.Command_wrapper.t list Or_error.t =
210
    (* TODO: it seems we're just doing noop with the Strategy.Command_wrapper,
211
       need double-check.
212
    *)
213
    let open Or_error.Let_syntax in
200✔
214
    let partitioner (cmd : t Strategy.Command_wrapper.t) =
215
      match Strategy.Command_wrapper.unwrap cmd with
480✔
UNCOV
216
      | Zkapp_command c ->
×
UNCOV
217
          First (Strategy.Command_wrapper.map cmd ~f:(Fn.const c))
×
218
      | Signed_command c ->
480✔
219
          Second (Strategy.Command_wrapper.map cmd ~f:(Fn.const c))
480✔
220
    in
221
    let process_left zk_cmds =
222
      (* TODO: we could optimize this by skipping the fee payer and non-proof authorizations *)
223
      let accounts_referenced =
200✔
224
        List.fold_left zk_cmds ~init:Account_id.Set.empty ~f:(fun set zk_cmd ->
UNCOV
225
            Strategy.Command_wrapper.unwrap zk_cmd
×
UNCOV
226
            |> Zkapp_command.accounts_referenced |> Account_id.Set.of_list
×
227
            |> Set.union set )
228
      in
229
      let vk_cache = load_vk_cache accounts_referenced in
200✔
230
      Strategy.create_all zk_cmds vk_cache
200✔
231
    in
232
    let process_right =
233
      List.map ~f:(fun cmd ->
234
          Strategy.Command_wrapper.map cmd ~f:(fun c -> Signed_command c) )
480✔
235
    in
236
    let finalizer vzk_cmds_m is_cmds_mapped ~f =
237
      let%map vzk_cmds = vzk_cmds_m in
238
      let vzk_cmds_mapped =
200✔
239
        List.map vzk_cmds ~f:(fun cmd ->
UNCOV
240
            Strategy.Command_wrapper.map cmd ~f:(fun c -> Zkapp_command c) )
×
241
      in
242
      f vzk_cmds_mapped is_cmds_mapped
200✔
243
    in
244
    List.process_separately ts ~partitioner ~process_left ~process_right
245
      ~finalizer
246
end
247

248
module Unapplied_sequence =
249
  Make_to_all_verifiable (Zkapp_command.Verifiable.From_unapplied_sequence)
250
module Applied_sequence =
251
  Make_to_all_verifiable (Zkapp_command.Verifiable.From_applied_sequence)
252

253
let of_verifiable (t : Verifiable.t) : t =
254
  match t with
960✔
255
  | Signed_command x ->
960✔
256
      Signed_command x
UNCOV
257
  | Zkapp_command p ->
×
UNCOV
258
      Zkapp_command (Zkapp_command.of_verifiable p)
×
259

260
let fee : (_, _, _) with_forest -> Currency.Fee.t = function
261
  | Signed_command x ->
20,000✔
262
      Signed_command.fee x
UNCOV
263
  | Zkapp_command p ->
×
264
      Zkapp_command.fee p
265

UNCOV
266
let has_insufficient_fee ~minimum_fee t = Currency.Fee.(fee t < minimum_fee)
×
267

268
let is_disabled = function
UNCOV
269
  | Zkapp_command _ ->
×
270
      Node_config_unconfigurable_constants.zkapps_disabled
UNCOV
271
  | _ ->
×
272
      false
273

274
(* always `Accessed` for fee payer *)
275
let accounts_accessed (t : (_, _, _) with_forest) (status : Transaction_status.t)
276
    : (Account_id.t * [ `Accessed | `Not_accessed ]) list =
UNCOV
277
  match t with
×
UNCOV
278
  | Signed_command x ->
×
279
      Signed_command.account_access_statuses x status
UNCOV
280
  | Zkapp_command ps ->
×
281
      Zkapp_command.account_access_statuses ps status
282

283
let accounts_referenced (t : (_, _, _) with_forest) =
UNCOV
284
  List.map (accounts_accessed t Applied) ~f:(fun (acct_id, _status) -> acct_id)
×
285

286
let fee_payer (t : (_, _, _) with_forest) =
287
  match t with
480✔
288
  | Signed_command x ->
480✔
289
      Signed_command.fee_payer x
UNCOV
290
  | Zkapp_command p ->
×
291
      Zkapp_command.fee_payer p
292

293
(** The application nonce is the nonce of the fee payer at which a user command can be applied. *)
294
let applicable_at_nonce (t : (_, _, _) with_forest) =
UNCOV
295
  match t with
×
UNCOV
296
  | Signed_command x ->
×
297
      Signed_command.nonce x
UNCOV
298
  | Zkapp_command p ->
×
299
      Zkapp_command.applicable_at_nonce p
300

UNCOV
301
let expected_target_nonce t = Account.Nonce.succ (applicable_at_nonce t)
×
302

303
let extract_vks : t -> (Account_id.t * Verification_key_wire.t) List.t =
304
  function
UNCOV
305
  | Signed_command _ ->
×
306
      []
UNCOV
307
  | Zkapp_command cmd ->
×
308
      Zkapp_command.extract_vks cmd
309

310
(** The target nonce is what the nonce of the fee payer will be after a user command is successfully applied. *)
311
let target_nonce_on_success (t : (_, _, _) with_forest) =
312
  match t with
×
313
  | Signed_command x ->
×
314
      Account.Nonce.succ (Signed_command.nonce x)
×
315
  | Zkapp_command p ->
×
316
      Zkapp_command.target_nonce_on_success p
317

318
let fee_token (t : (_, _, _) with_forest) =
UNCOV
319
  match t with
×
UNCOV
320
  | Signed_command x ->
×
321
      Signed_command.fee_token x
UNCOV
322
  | Zkapp_command x ->
×
323
      Zkapp_command.fee_token x
324

325
let valid_until (t : (_, _, _) with_forest) =
UNCOV
326
  match t with
×
UNCOV
327
  | Signed_command x ->
×
328
      Signed_command.valid_until x
UNCOV
329
  | Zkapp_command { fee_payer; _ } -> (
×
330
      match fee_payer.Account_update.Fee_payer.body.valid_until with
UNCOV
331
      | Some valid_until ->
×
332
          valid_until
UNCOV
333
      | None ->
×
334
          Mina_numbers.Global_slot_since_genesis.max_value )
335

336
module Valid = struct
337
  type t_ = t
338

339
  type t = (Signed_command.With_valid_signature.t, Zkapp_command.Valid.t) Poly.t
×
UNCOV
340
  [@@deriving sexp_of, to_yojson]
×
341

342
  module Gen = Gen_make (Signed_command.With_valid_signature)
343
end
344

345
module For_tests = struct
346
  let check_verifiable (t : Verifiable.t) : Valid.t Or_error.t =
UNCOV
347
    match t with
×
348
    | Signed_command x -> (
×
349
        match Signed_command.check x with
350
        | Some c ->
×
351
            Ok (Signed_command c)
352
        | None ->
×
353
            Or_error.error_string "Invalid signature" )
UNCOV
354
    | Zkapp_command p ->
×
UNCOV
355
        Ok (Zkapp_command (Zkapp_command.Valid.For_tests.of_verifiable p))
×
356
end
357

358
let forget_check (t : Valid.t) : t =
359
  match t with
22,560✔
UNCOV
360
  | Zkapp_command x ->
×
UNCOV
361
      Zkapp_command (Zkapp_command.Valid.forget x)
×
362
  | Signed_command c ->
22,560✔
363
      Signed_command (c :> Signed_command.t)
364

365
let to_valid_unsafe (t : t) =
366
  `If_this_is_used_it_should_have_a_comment_justifying_it
1,440✔
367
    ( match t with
UNCOV
368
    | Zkapp_command x ->
×
369
        let (`If_this_is_used_it_should_have_a_comment_justifying_it x) =
370
          Zkapp_command.Valid.to_valid_unsafe x
371
        in
UNCOV
372
        Zkapp_command x
×
373
    | Signed_command x ->
1,440✔
374
        (* This is safe due to being immediately wrapped again. *)
375
        let (`If_this_is_used_it_should_have_a_comment_justifying_it x) =
376
          Signed_command.to_valid_unsafe x
377
        in
378
        Signed_command x )
1,440✔
379

380
let filter_by_participant (commands : t list) public_key =
381
  List.filter commands ~f:(fun user_command ->
×
382
      Core_kernel.List.exists
×
383
        (accounts_referenced user_command)
×
384
        ~f:
385
          (Fn.compose
×
386
             (Signature_lib.Public_key.Compressed.equal public_key)
×
387
             Account_id.public_key ) )
388

389
(* A metric on user commands that should correspond roughly to resource costs
390
   for validation/application *)
391
let weight : (_, _, _) with_forest -> int = function
UNCOV
392
  | Signed_command signed_command ->
×
UNCOV
393
      Signed_command.payload signed_command |> Signed_command_payload.weight
×
UNCOV
394
  | Zkapp_command zkapp_command ->
×
395
      Zkapp_command.weight zkapp_command
396

397
(* Fee per weight unit *)
398
let fee_per_wu (user_command : (_, _, _) with_forest) : Currency.Fee_rate.t =
399
  (*TODO: return Or_error*)
UNCOV
400
  Currency.Fee_rate.make_exn (fee user_command) (weight user_command)
×
401

402
let valid_size ~genesis_constants = function
UNCOV
403
  | Signed_command _ ->
×
404
      Ok ()
UNCOV
405
  | Zkapp_command zkapp_command ->
×
406
      Zkapp_command.valid_size ~genesis_constants zkapp_command
407

408
let has_zero_vesting_period = function
UNCOV
409
  | Signed_command _ ->
×
410
      false
UNCOV
411
  | Zkapp_command p ->
×
412
      Zkapp_command.has_zero_vesting_period p
413

414
let is_incompatible_version = function
UNCOV
415
  | Signed_command _ ->
×
416
      false
UNCOV
417
  | Zkapp_command p ->
×
418
      Zkapp_command.is_incompatible_version p
419

420
let has_invalid_call_forest : (_, _, _) with_forest -> bool = function
UNCOV
421
  | Signed_command _ ->
×
422
      false
UNCOV
423
  | Zkapp_command cmd ->
×
424
      List.exists cmd.account_updates ~f:(fun call_forest ->
UNCOV
425
          let root_may_use_token =
×
426
            call_forest.elt.account_update.body.may_use_token
427
          in
UNCOV
428
          not (Account_update.May_use_token.equal root_may_use_token No) )
×
429

430
module Well_formedness_error = struct
431
  (* syntactically-evident errors such that a user command can never succeed *)
UNCOV
432
  type t =
×
433
    | Insufficient_fee
×
434
    | Zero_vesting_period
×
435
    | Zkapp_too_big of (Error.t[@to_yojson Error_json.error_to_yojson])
×
436
    | Zkapp_invalid_call_forest
×
437
    | Transaction_type_disabled
×
438
    | Incompatible_version
×
439
  [@@deriving compare, to_yojson, sexp]
440

441
  let to_string = function
442
    | Insufficient_fee ->
×
443
        "Insufficient fee"
444
    | Zero_vesting_period ->
×
445
        "Zero vesting period"
446
    | Zkapp_too_big err ->
×
447
        sprintf "Zkapp too big (%s)" (Error.to_string_hum err)
×
448
    | Zkapp_invalid_call_forest ->
×
449
        "Zkapp has an invalid call forest (root account updates may not use \
450
         tokens)"
451
    | Incompatible_version ->
×
452
        "Set verification-key permission is updated to an incompatible version"
453
    | Transaction_type_disabled ->
×
454
        "Transaction type disabled"
455
end
456

457
let check_well_formedness ~(genesis_constants : Genesis_constants.t)
458
    (t : (_, _, _) with_forest) : (unit, Well_formedness_error.t list) result =
UNCOV
459
  let preds =
×
460
    let open Well_formedness_error in
461
    [ ( has_insufficient_fee
462
          ~minimum_fee:genesis_constants.minimum_user_command_fee
463
      , Insufficient_fee )
464
    ; (has_zero_vesting_period, Zero_vesting_period)
465
    ; (is_incompatible_version, Incompatible_version)
466
    ; (is_disabled, Transaction_type_disabled)
467
    ; (has_invalid_call_forest, Zkapp_invalid_call_forest)
468
    ]
469
  in
470
  let errs0 =
471
    List.fold preds ~init:[] ~f:(fun acc (f, err) ->
UNCOV
472
        if f t then err :: acc else acc )
×
473
  in
UNCOV
474
  let errs =
×
475
    match valid_size ~genesis_constants t with
UNCOV
476
    | Ok () ->
×
477
        errs0
478
    | Error err ->
×
479
        Zkapp_too_big err :: errs0
480
  in
UNCOV
481
  if List.is_empty errs then Ok () else Error errs
×
482

483
type fee_payer_summary_t = Signature.t * Account.key * int
×
484
[@@deriving yojson, hash]
×
485

486
let fee_payer_summary : (_, _, _) with_forest -> fee_payer_summary_t = function
UNCOV
487
  | Zkapp_command cmd ->
×
488
      let fp = Zkapp_command.fee_payer_account_update cmd in
UNCOV
489
      let open Account_update in
×
490
      let body = Fee_payer.body fp in
UNCOV
491
      ( Fee_payer.authorization fp
×
UNCOV
492
      , Body.Fee_payer.public_key body
×
UNCOV
493
      , Body.Fee_payer.nonce body |> Unsigned.UInt32.to_int )
×
UNCOV
494
  | Signed_command cmd ->
×
495
      Signed_command.
UNCOV
496
        (signature cmd, fee_payer_pk cmd, nonce cmd |> Unsigned.UInt32.to_int)
×
497

498
let fee_payer_summary_json tx =
UNCOV
499
  fee_payer_summary_t_to_yojson (fee_payer_summary tx)
×
500

501
let fee_payer_summary_string tx =
UNCOV
502
  let signature, pk, nonce = fee_payer_summary tx in
×
UNCOV
503
  sprintf "%s (%s %d)"
×
UNCOV
504
    (Signature.to_base58_check signature)
×
UNCOV
505
    (Signature_lib.Public_key.Compressed.to_base58_check pk)
×
506
    nonce
26✔
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