• 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

69.44
/src/lib/mina_generators/user_command_generators.ml
1
(* user_command_generators.ml *)
2

3
(* generate User_command.t's, that is, either Signed_commands or
4
   Zkapp_command
5
*)
6

3✔
7
open Core_kernel
8
open Mina_base
9
module Ledger = Mina_ledger.Ledger
10
include User_command.Gen
11

12
let zkapp_command_with_ledger ?(ledger_init_state : Ledger.init_state option)
13
    ?num_keypairs ?max_account_updates ?max_token_updates ?account_state_tbl ?vk
14
    ?failure ~(genesis_constants : Genesis_constants.t)
15
    ~(constraint_constants : Genesis_constants.Constraint_constants.t) () =
16
  let ledger_depth = constraint_constants.ledger_depth in
1✔
17
  let open Quickcheck.Let_syntax in
18
  let open Signature_lib in
19
  (* Need a fee payer keypair, a keypair for the "balancing" account (so that the balance changes
20
     sum to zero), and max_account_updates * 2 keypairs, because all the other zkapp_command
21
     might be new and their accounts not in the ledger; or they might all be old and in the ledger
22

23
     We'll put the fee payer account and max_account_updates accounts in the
24
     ledger, and have max_account_updates keypairs available for new accounts
25
  *)
26
  let max_account_updates =
27
    Option.value max_account_updates
28
      ~default:Zkapp_command_generators.max_account_updates
29
  in
30
  let max_token_updates =
1✔
31
    Option.value max_token_updates
32
      ~default:Zkapp_command_generators.max_token_updates
33
  in
34
  let num_keypairs =
1✔
35
    Option.value num_keypairs
36
      ~default:((max_account_updates * 2) + (max_token_updates * 3) + 2)
37
  in
38
  let%bind new_keypairs =
39
    let existing_keypairs =
40
      let tbl = Public_key.Compressed.Hash_set.create () in
41
      Option.iter ledger_init_state ~f:(fun l ->
1✔
42
          Array.iter l ~f:(fun (kp, _balance, _nonce, _timing) ->
×
43
              Hash_set.add tbl (Public_key.compress kp.public_key) ) ) ;
×
44
      tbl
1✔
45
    in
46
    let rec go acc n =
47
      if n = 0 then return acc
20✔
48
      else
49
        let%bind kp =
50
          Quickcheck.Generator.filter Keypair.gen ~f:(fun kp ->
221✔
51
              not
240✔
52
                (Hash_set.mem existing_keypairs
240✔
53
                   (Public_key.compress kp.public_key) ) )
240✔
54
        in
55
        Hash_set.add existing_keypairs (Public_key.compress kp.public_key) ;
240✔
56
        go (kp :: acc) (n - 1)
240✔
57
    in
58
    go [] num_keypairs
1✔
59
  in
60
  let keymap =
20✔
61
    List.fold new_keypairs ~init:Public_key.Compressed.Map.empty
62
      ~f:(fun map { public_key; private_key } ->
63
        let key = Public_key.compress public_key in
240✔
64
        Public_key.Compressed.Map.add_exn map ~key ~data:private_key )
240✔
65
  in
66
  let num_keypairs_in_ledger = num_keypairs / 2 in
20✔
67
  let keypairs_in_ledger = List.take new_keypairs num_keypairs_in_ledger in
68
  let account_ids =
20✔
69
    List.map keypairs_in_ledger ~f:(fun { public_key; _ } ->
70
        Account_id.create (Public_key.compress public_key) Token_id.default )
120✔
71
  in
72
  let verification_key =
20✔
73
    match vk with
74
    | None ->
20✔
75
        With_hash.
76
          { data = Side_loaded_verification_key.dummy
77
          ; hash = Zkapp_account.dummy_vk_hash ()
20✔
78
          }
79
    | Some vk ->
×
80
        vk
81
  in
82
  let%bind balances =
83
    let min_cmd_fee = genesis_constants.minimum_user_command_fee in
84
    let min_balance =
85
      Currency.Fee.to_nanomina_int min_cmd_fee
86
      |> Int.( + ) 100_000_000_000_000_000
20✔
87
      |> Currency.Balance.of_nanomina_int_exn
20✔
88
    in
89
    (* max balance to avoid overflow when adding deltas *)
90
    let max_balance =
20✔
91
      let max_bal = Currency.Balance.of_mina_string_exn "2000000000.0" in
92
      match
20✔
93
        Currency.Balance.add_amount min_balance
94
          (Currency.Balance.to_amount max_bal)
20✔
95
      with
96
      | None ->
×
97
          failwith "zkapp_command_with_ledger: overflow for max_balance"
98
      | Some _ ->
20✔
99
          max_bal
100
    in
101
    Quickcheck.Generator.list_with_length num_keypairs_in_ledger
20✔
102
      (Currency.Balance.gen_incl min_balance max_balance)
20✔
103
  in
104
  let account_ids_and_balances = List.zip_exn account_ids balances in
20✔
105
  let snappify_account (account : Account.t) : Account.t =
20✔
106
    (* TODO: use real keys *)
107
    let permissions =
60✔
108
      { Permissions.user_default with
109
        edit_state = Permissions.Auth_required.Either
110
      ; send = Either
111
      ; set_delegate = Either
112
      ; set_permissions = Either
113
      ; set_verification_key = (Either, Mina_numbers.Txn_version.current)
114
      ; set_zkapp_uri = Either
115
      ; edit_action_state = Either
116
      ; set_token_symbol = Either
117
      ; increment_nonce = Either
118
      ; set_voting_for = Either
119
      ; set_timing = Either
120
      }
121
    in
122
    let verification_key = Some verification_key in
123
    let zkapp = Some { Zkapp_account.default with verification_key } in
124
    { account with permissions; zkapp }
125
  in
126
  (* half zkApp accounts, half non-zkApp accounts *)
127
  let accounts =
128
    List.mapi account_ids_and_balances ~f:(fun ndx (account_id, balance) ->
129
        let account = Account.create account_id balance in
120✔
130
        if ndx mod 2 = 0 then account else snappify_account account )
60✔
131
  in
132
  let fee_payer_keypair = List.hd_exn new_keypairs in
20✔
133
  let ledger = Ledger.create_ephemeral ~depth:ledger_depth () in
20✔
134
  Ledger.set_batch_accounts ledger
20✔
135
    (List.mapi accounts ~f:(fun i account ->
20✔
136
         (Ledger.Addr.of_int_exn ~ledger_depth i, account) ) ) ;
120✔
137
  (* to keep track of account states across transactions *)
138
  let account_state_tbl =
20✔
139
    Option.value account_state_tbl ~default:(Account_id.Table.create ())
20✔
140
  in
141
  let%bind zkapp_command =
142
    Zkapp_command_generators.gen_zkapp_command_from ~max_account_updates
20✔
143
      ~max_token_updates ~fee_payer_keypair ~keymap ~ledger ~account_state_tbl
144
      ?vk ?failure ~genesis_constants ~constraint_constants ()
145
  in
146
  let zkapp_command =
20✔
147
    Or_error.ok_exn
148
      (Zkapp_command.Valid.to_valid ~failed:false
20✔
149
         ~find_vk:
150
           (Zkapp_command.Verifiable.load_vk_from_ledger
151
              ~get:(Ledger.get ledger)
20✔
152
              ~location_of_account:(Ledger.location_of_account ledger) )
20✔
153
         zkapp_command )
154
  in
155
  (* include generated ledger in result *)
156
  return
20✔
157
    (User_command.Zkapp_command zkapp_command, fee_payer_keypair, keymap, ledger)
158

159
let sequence_zkapp_command_with_ledger ?ledger_init_state ?max_account_updates
160
    ?max_token_updates ?length ?vk ?failure ~genesis_constants
161
    ~constraint_constants () =
162
  let open Quickcheck.Let_syntax in
×
163
  let%bind length =
164
    match length with
165
    | Some n ->
×
166
        return n
×
167
    | None ->
×
168
        Quickcheck.Generator.small_non_negative_int
169
  in
170
  let max_account_updates =
×
171
    Option.value max_account_updates
172
      ~default:Zkapp_command_generators.max_account_updates
173
  in
174
  let max_token_updates =
×
175
    Option.value max_token_updates
176
      ~default:Zkapp_command_generators.max_token_updates
177
  in
178
  let num_keypairs = length * max_account_updates * 2 in
×
179
  (* Keep track of account states across multiple zkapp_command transaction *)
180
  let account_state_tbl = Account_id.Table.create () in
181
  let%bind zkapp_command, fee_payer_keypair, keymap, ledger =
182
    zkapp_command_with_ledger ?ledger_init_state ~num_keypairs
×
183
      ~max_account_updates ~max_token_updates ~account_state_tbl ?vk ?failure
184
      ~genesis_constants ~constraint_constants ()
185
  in
186
  let rec go zkapp_command_and_fee_payer_keypairs n =
×
187
    if n <= 1 then
×
188
      return
×
189
        ( (zkapp_command, fee_payer_keypair, keymap)
190
          :: List.rev zkapp_command_and_fee_payer_keypairs
×
191
        , ledger )
192
    else
193
      let%bind zkapp_command =
194
        Zkapp_command_generators.gen_zkapp_command_from ~max_account_updates
×
195
          ~max_token_updates ~fee_payer_keypair ~keymap ~ledger
196
          ~account_state_tbl ?vk ?failure ~genesis_constants
197
          ~constraint_constants ()
198
      in
199
      let valid_zkapp_command =
×
200
        Or_error.ok_exn
201
          (Zkapp_command.Valid.to_valid ~failed:false
×
202
             ~find_vk:
203
               (Zkapp_command.Verifiable.load_vk_from_ledger
204
                  ~get:(Ledger.get ledger)
×
205
                  ~location_of_account:(Ledger.location_of_account ledger) )
×
206
             zkapp_command )
207
      in
208
      let zkapp_command_and_fee_payer_keypairs' =
×
209
        ( User_command.Zkapp_command valid_zkapp_command
210
        , fee_payer_keypair
211
        , keymap )
212
        :: zkapp_command_and_fee_payer_keypairs
213
      in
214
      go zkapp_command_and_fee_payer_keypairs' (n - 1)
215
  in
216
  go [] length
3✔
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