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

MinaProtocol / mina / 159

06 May 2025 07:33AM UTC coverage: 36.542% (-24.2%) from 60.761%
159

push

buildkite

web-flow
Merge pull request #17152 from MinaProtocol/dw/replace-codeowners-master

CODEOWNERS: add Deepthi, George and Danny

26216 of 71743 relevant lines covered (36.54%)

26600.54 hits per line

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

23.03
/src/lib/mina_base/user_command.ml
1
open Core_kernel
25✔
2

3
module Poly = struct
4
  [%%versioned
5
  module Stable = struct
6
    module V2 = struct
7
      type ('u, 's) t =
53✔
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]
125✔
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]
25✔
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
25✔
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
25✔
75

76
[%%versioned
77
module Stable = struct
78
  module V2 = struct
79
    type t =
25✔
80
      (Signed_command.Stable.V2.t, Zkapp_command.Stable.V1.t) Poly.Stable.V2.t
×
81
    [@@deriving sexp, compare, equal, hash, yojson]
125✔
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]
25✔
136
    end
137
  end]
138
end
139

140
module Verifiable = struct
141
  type t =
25✔
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]
100✔
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
×
213
  | Signed_command x ->
×
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
let check_verifiable (t : Verifiable.t) : Valid.t Or_error.t =
304
  match t with
×
305
  | Signed_command x -> (
×
306
      match Signed_command.check x with
307
      | Some c ->
×
308
          Ok (Signed_command c)
309
      | None ->
×
310
          Or_error.error_string "Invalid signature" )
311
  | Zkapp_command p ->
×
312
      Ok (Zkapp_command (Zkapp_command.Valid.of_verifiable p))
×
313

314
let check ~failed ~find_vk (t : t) : Valid.t Or_error.t =
315
  to_verifiable ~failed ~find_vk t |> Or_error.bind ~f:check_verifiable
×
316

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

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

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

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

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

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

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

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

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

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

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

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

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

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

457
let fee_payer_summary_json =
458
  Fn.compose fee_payer_summary_t_to_yojson fee_payer_summary
25✔
459

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