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

MinaProtocol / mina / 71

10 Apr 2025 08:34PM UTC coverage: 32.444% (-28.3%) from 60.793%
71

push

buildkite

web-flow
Merge pull request #16865 from MinaProtocol/georgeee/use-stable-zkapp_command-in-graphql

Update graphql code to use stable zkapp_command

2 of 20 new or added lines in 4 files covered. (10.0%)

19665 existing lines in 384 files now uncovered.

23267 of 71714 relevant lines covered (32.44%)

24457.65 hits per line

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

21.23
/src/lib/mina_base/user_command.ml
1
open Core_kernel
27✔
2
open Mina_stdlib
3

4
module Poly = struct
5
  [%%versioned
6
  module Stable = struct
7
    module V2 = struct
8
      type ('u, 's) t =
57✔
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]
135✔
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]
27✔
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
27✔
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
27✔
76

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

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

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

90
type t = (Signed_command.t, Zkapp_command.t) Poly.t
×
UNCOV
91
[@@deriving sexp, compare, equal, hash, yojson]
×
92

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

99
let read_all_proofs_from_disk : t -> Stable.Latest.t = function
100
  | Signed_command sc ->
10,000✔
101
      Signed_command sc
102
  | Zkapp_command zc ->
20✔
103
      Zkapp_command (Zkapp_command.read_all_proofs_from_disk zc)
20✔
104

105
type ('a, 'b) with_forest =
106
  (Signed_command.t, ('a, 'b) Zkapp_command.with_forest) Poly.t
107

108
let to_base64 : Stable.Latest.t -> string = function
UNCOV
109
  | Signed_command sc ->
×
110
      Signed_command.to_base64 sc
111
  | Zkapp_command zc ->
×
112
      Zkapp_command.to_base64 zc
113

114
let of_base64 s : Stable.Latest.t Or_error.t =
UNCOV
115
  match Signed_command.of_base64 s with
×
UNCOV
116
  | Ok sc ->
×
117
      Ok (Signed_command sc)
118
  | Error err1 -> (
×
119
      match Zkapp_command.of_base64 s with
120
      | Ok zc ->
×
121
          Ok (Zkapp_command zc)
122
      | Error err2 ->
×
123
          Error
124
            (Error.of_string
×
125
               (sprintf
×
126
                  "Could decode Base64 neither to signed command (%s), nor to \
127
                   zkApp (%s)"
128
                  (Error.to_string_hum err1) (Error.to_string_hum err2) ) ) )
×
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]
27✔
136
    end
137
  end]
138
end
139

140
module Verifiable = struct
141
  type t =
27✔
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]
108✔
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
    (* TODO: it seems we're just doing noop with the Strategy.Command_wrapper,
169
       need double-check.
170
    *)
171
    let open Or_error.Let_syntax in
200✔
172
    let partitioner (cmd : t Strategy.Command_wrapper.t) =
173
      match Strategy.Command_wrapper.unwrap cmd with
480✔
UNCOV
174
      | Zkapp_command c ->
×
UNCOV
175
          First (Strategy.Command_wrapper.map cmd ~f:(Fn.const c))
×
176
      | Signed_command c ->
480✔
177
          Second (Strategy.Command_wrapper.map cmd ~f:(Fn.const c))
480✔
178
    in
179
    let process_left zk_cmds =
180
      (* TODO: we could optimize this by skipping the fee payer and non-proof authorizations *)
181
      let accounts_referenced =
200✔
182
        List.fold_left zk_cmds ~init:Account_id.Set.empty ~f:(fun set zk_cmd ->
UNCOV
183
            Strategy.Command_wrapper.unwrap zk_cmd
×
UNCOV
184
            |> Zkapp_command.accounts_referenced |> Account_id.Set.of_list
×
185
            |> Set.union set )
186
      in
187
      let vk_cache = load_vk_cache accounts_referenced in
200✔
188
      Strategy.create_all zk_cmds vk_cache
200✔
189
    in
190
    let process_right =
191
      List.map ~f:(fun cmd ->
192
          Strategy.Command_wrapper.map cmd ~f:(fun c -> Signed_command c) )
480✔
193
    in
194
    let finalizer vzk_cmds_m is_cmds_mapped ~f =
195
      let%map vzk_cmds = vzk_cmds_m in
196
      let vzk_cmds_mapped =
200✔
197
        List.map vzk_cmds ~f:(fun cmd ->
UNCOV
198
            Strategy.Command_wrapper.map cmd ~f:(fun c -> Zkapp_command c) )
×
199
      in
200
      f vzk_cmds_mapped is_cmds_mapped
200✔
201
    in
202
    List.process_separately ts ~partitioner ~process_left ~process_right
203
      ~finalizer
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
UNCOV
215
  | Zkapp_command p ->
×
UNCOV
216
      Zkapp_command (Zkapp_command.of_verifiable p)
×
217

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

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

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

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

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

244
let fee_payer (t : (_, _) with_forest) =
245
  match t with
480✔
246
  | Signed_command x ->
480✔
247
      Signed_command.fee_payer x
UNCOV
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 : (_, _) with_forest) =
UNCOV
253
  match t with
×
UNCOV
254
  | Signed_command x ->
×
255
      Signed_command.nonce x
UNCOV
256
  | Zkapp_command p ->
×
257
      Zkapp_command.applicable_at_nonce p
258

UNCOV
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
UNCOV
263
  | Signed_command _ ->
×
264
      []
UNCOV
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 : (_, _) with_forest) =
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 : (_, _) with_forest) =
UNCOV
277
  match t with
×
UNCOV
278
  | Signed_command x ->
×
279
      Signed_command.fee_token x
UNCOV
280
  | Zkapp_command x ->
×
281
      Zkapp_command.fee_token x
282

283
let valid_until (t : (_, _) with_forest) =
UNCOV
284
  match t with
×
UNCOV
285
  | Signed_command x ->
×
286
      Signed_command.valid_until x
UNCOV
287
  | Zkapp_command { fee_payer; _ } -> (
×
288
      match fee_payer.Account_update.Fee_payer.body.valid_until with
UNCOV
289
      | Some valid_until ->
×
290
          valid_until
UNCOV
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
×
UNCOV
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 =
UNCOV
305
    match t with
×
306
    | Signed_command x -> (
×
307
        match Signed_command.check x with
308
        | Some c ->
×
309
            Ok (Signed_command c)
310
        | None ->
×
311
            Or_error.error_string "Invalid signature" )
UNCOV
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 ->
×
UNCOV
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 =
339
  List.filter commands ~f:(fun user_command ->
×
340
      Core_kernel.List.exists
×
341
        (accounts_referenced user_command)
×
342
        ~f:
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 : (_, _) with_forest -> int = function
UNCOV
350
  | Signed_command signed_command ->
×
UNCOV
351
      Signed_command.payload signed_command |> Signed_command_payload.weight
×
UNCOV
352
  | Zkapp_command zkapp_command ->
×
353
      Zkapp_command.weight zkapp_command
354

355
(* Fee per weight unit *)
356
let fee_per_wu (user_command : (_, _) with_forest) : 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 : (_, _) with_forest -> 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
400
    | Insufficient_fee ->
×
401
        "Insufficient fee"
402
    | Zero_vesting_period ->
×
403
        "Zero vesting period"
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)"
409
    | Incompatible_version ->
×
410
        "Set verification-key permission is updated to an incompatible version"
411
    | Transaction_type_disabled ->
×
412
        "Transaction type disabled"
413
end
414

415
let check_well_formedness ~(genesis_constants : Genesis_constants.t)
416
    (t : (_, _) with_forest) : (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
436
    | Error err ->
×
437
        Zkapp_too_big err :: errs0
438
  in
UNCOV
439
  if List.is_empty errs then Ok () else Error errs
×
440

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

444
let fee_payer_summary : (_, _) with_forest -> 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
×
UNCOV
450
      , Body.Fee_payer.public_key body
×
UNCOV
451
      , Body.Fee_payer.nonce body |> Unsigned.UInt32.to_int )
×
UNCOV
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 tx =
UNCOV
457
  fee_payer_summary_t_to_yojson (fee_payer_summary tx)
×
458

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