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

MinaProtocol / mina / 3505

20 Mar 2025 10:29PM UTC coverage: 30.437% (-2.1%) from 32.553%
3505

push

buildkite

web-flow
Merge pull request #16755 from MinaProtocol/dkijania/prune_dockers_before_build

[CI] Prune dockers before build

11323 of 37202 relevant lines covered (30.44%)

9247.05 hits per line

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

21.84
/src/app/cli/src/cli_entrypoint/mina_cli_entrypoint.ml
1
open Core
2
open Async
3
open Mina_base
4
open Cli_lib
5
open Signature_lib
6
open Init
7
module YJ = Yojson.Safe
8

9
type mina_initialization =
10
  { mina : Mina_lib.t
11
  ; client_trustlist : Unix.Cidr.t list option
12
  ; rest_server_port : int
13
  ; limited_graphql_port : int option
14
  ; itn_graphql_port : int option
15
  }
16

17
(* keep this code in sync with Client.chain_id_inputs, Mina_commands.chain_id_inputs, and
18
   Daemon_rpcs.Chain_id_inputs
19
*)
20
let chain_id ~constraint_system_digests ~genesis_state_hash ~genesis_constants
21
    ~protocol_transaction_version ~protocol_network_version =
22
  (* if this changes, also change Mina_commands.chain_id_inputs *)
23
  let genesis_state_hash = State_hash.to_base58_check genesis_state_hash in
×
24
  let genesis_constants_hash = Genesis_constants.hash genesis_constants in
×
25
  let all_snark_keys =
×
26
    List.map constraint_system_digests ~f:(fun (_, digest) -> Md5.to_hex digest)
×
27
    |> String.concat ~sep:""
×
28
  in
29
  let version_digest v = Int.to_string v |> Md5.digest_string |> Md5.to_hex in
×
30
  let protocol_transaction_version_digest =
31
    version_digest protocol_transaction_version
32
  in
33
  let protocol_network_version_digest =
×
34
    version_digest protocol_network_version
35
  in
36
  let b2 =
×
37
    Blake2.digest_string
38
      ( genesis_state_hash ^ all_snark_keys ^ genesis_constants_hash
39
      ^ protocol_transaction_version_digest ^ protocol_network_version_digest )
40
  in
41
  Blake2.to_hex b2
×
42

43
let plugin_flag =
44
  if Node_config.plugins then
45
    let open Command.Param in
×
46
    flag "--load-plugin" ~aliases:[ "load-plugin" ] (listed string)
×
47
      ~doc:
48
        "PATH The path to load a .cmxs plugin from. May be passed multiple \
49
         times"
50
  else Command.Param.return []
3✔
51

52
let load_config_files ~logger ~genesis_constants ~constraint_constants ~conf_dir
53
    ~genesis_dir ~cli_proof_level ~proof_level config_files =
54
  let%bind config_jsons =
55
    let config_files_paths =
56
      List.map config_files ~f:(fun (config_file, _) -> `String config_file)
×
57
    in
58
    [%log info] "Reading configuration files $config_files"
×
59
      ~metadata:[ ("config_files", `List config_files_paths) ] ;
60
    Deferred.List.filter_map config_files
×
61
      ~f:(fun (config_file, handle_missing) ->
62
        match%bind Genesis_ledger_helper.load_config_json config_file with
×
63
        | Ok config_json ->
×
64
            let%map config_json =
65
              Genesis_ledger_helper.upgrade_old_config ~logger config_file
×
66
                config_json
67
            in
68
            Some (config_file, config_json)
×
69
        | Error err -> (
×
70
            match handle_missing with
71
            | `Must_exist ->
×
72
                Mina_user_error.raisef ~where:"reading configuration file"
73
                  "The configuration file %s could not be read:\n%s" config_file
74
                  (Error.to_string_hum err)
×
75
            | `May_be_missing ->
×
76
                [%log warn] "Could not read configuration from $config_file"
×
77
                  ~metadata:
78
                    [ ("config_file", `String config_file)
79
                    ; ("error", Error_json.error_to_yojson err)
×
80
                    ] ;
81
                return None ) )
×
82
  in
83
  let config =
×
84
    List.fold ~init:Runtime_config.default config_jsons
85
      ~f:(fun config (config_file, config_json) ->
86
        match Runtime_config.of_yojson config_json with
×
87
        | Ok loaded_config ->
×
88
            Runtime_config.combine config loaded_config
89
        | Error err ->
×
90
            [%log fatal]
×
91
              "Could not parse configuration from $config_file: $error"
92
              ~metadata:
93
                [ ("config_file", `String config_file)
94
                ; ("config_json", config_json)
95
                ; ("error", `String err)
96
                ] ;
97
            failwithf "Could not parse configuration file: %s" err () )
×
98
  in
99
  let genesis_dir = Option.value ~default:(conf_dir ^/ "genesis") genesis_dir in
×
100
  let%bind precomputed_values =
101
    match%map
102
      Genesis_ledger_helper.init_from_config_file ~cli_proof_level ~genesis_dir
×
103
        ~logger ~genesis_constants ~constraint_constants ~proof_level config
104
    with
105
    | Ok (precomputed_values, _) ->
×
106
        precomputed_values
107
    | Error err ->
×
108
        let ( json_config
109
            , `Accounts_omitted
110
                ( `Genesis genesis_accounts_omitted
111
                , `Staking staking_accounts_omitted
112
                , `Next next_accounts_omitted ) ) =
113
          Runtime_config.to_yojson_without_accounts config
114
        in
115
        let append_accounts_omitted s =
×
116
          Option.value_map
×
117
            ~f:(fun i -> List.cons (s ^ "_accounts_omitted", `Int i))
×
118
            ~default:Fn.id
119
        in
120
        let metadata =
121
          append_accounts_omitted "genesis" genesis_accounts_omitted
122
          @@ append_accounts_omitted "staking" staking_accounts_omitted
×
123
          @@ append_accounts_omitted "next" next_accounts_omitted []
×
124
          @ [ ("config", json_config)
125
            ; ( "name"
126
              , `String
127
                  (Option.value ~default:"not provided"
×
128
                     (let%bind.Option ledger = config.ledger in
129
                      Option.first_some ledger.name ledger.hash ) ) )
×
130
            ; ("error", Error_json.error_to_yojson err)
×
131
            ]
132
        in
133
        [%log info]
×
134
          "Initializing with runtime configuration. Ledger source: $name"
135
          ~metadata ;
136
        Error.raise err
×
137
  in
138
  return (precomputed_values, config_jsons, config)
×
139

140
let setup_daemon logger ~itn_features ~default_snark_worker_fee =
141
  let open Command.Let_syntax in
6✔
142
  let open Cli_lib.Arg_type in
143
  let receiver_key_warning = Cli_lib.Default.receiver_key_warning in
144
  let%map_open conf_dir = Cli_lib.Flag.conf_dir
145
  and block_production_key =
146
    flag "--block-producer-key" ~aliases:[ "block-producer-key" ]
6✔
147
      ~doc:
148
        (sprintf
6✔
149
           "DEPRECATED: Use environment variable `MINA_BP_PRIVKEY` instead. \
150
            Private key file for the block producer. Providing this flag or \
151
            the environment variable will enable block production. You cannot \
152
            provide both `block-producer-key` and `block-producer-pubkey`. \
153
            (default: use environment variable `MINA_BP_PRIVKEY`, if provided, \
154
            or else don't produce any blocks) %s"
155
           receiver_key_warning )
156
      (optional string)
6✔
157
  and block_production_pubkey =
158
    flag "--block-producer-pubkey"
6✔
159
      ~aliases:[ "block-producer-pubkey" ]
160
      ~doc:
161
        (sprintf
6✔
162
           "PUBLICKEY Public key for the associated private key that is being \
163
            tracked by this daemon. You cannot provide both \
164
            `block-producer-key` (or `MINA_BP_PRIVKEY`) and \
165
            `block-producer-pubkey`. (default: don't produce blocks) %s"
166
           receiver_key_warning )
167
      (optional public_key_compressed)
6✔
168
  and block_production_password =
169
    flag "--block-producer-password"
6✔
170
      ~aliases:[ "block-producer-password" ]
171
      ~doc:
172
        "PASSWORD Password associated with the block-producer key. Setting \
173
         this is equivalent to setting the MINA_PRIVKEY_PASS environment \
174
         variable. Be careful when setting it in the commandline as it will \
175
         likely get tracked in your history. Mainly to be used from the \
176
         daemon.json config file"
177
      (optional string)
6✔
178
  and itn_keys =
179
    if itn_features then
180
      flag "--itn-keys" ~aliases:[ "itn-keys" ] (optional string)
×
181
        ~doc:
182
          "PUBLICKEYS A comma-delimited list of Ed25519 public keys that are \
183
           permitted to send signed requests to the incentivized testnet \
184
           GraphQL server"
185
    else Command.Param.return None
6✔
186
  and itn_max_logs =
187
    if itn_features then
188
      flag "--itn-max-logs" ~aliases:[ "itn-max-logs" ] (optional int)
×
189
        ~doc:
190
          "NN Maximum number of logs to store to be made available via GraphQL \
191
           for incentivized testnet"
192
    else Command.Param.return None
6✔
193
  and demo_mode =
194
    flag "--demo-mode" ~aliases:[ "demo-mode" ] no_arg
6✔
195
      ~doc:
196
        "Run the daemon in demo-mode -- assume we're \"synced\" to the network \
197
         instantly"
198
  and coinbase_receiver_flag =
199
    flag "--coinbase-receiver" ~aliases:[ "coinbase-receiver" ]
6✔
200
      ~doc:
201
        (sprintf
6✔
202
           "PUBLICKEY Address to send coinbase rewards to (if this node is \
203
            producing blocks). If not provided, coinbase rewards will be sent \
204
            to the producer of a block. %s"
205
           receiver_key_warning )
206
      (optional public_key_compressed)
6✔
207
  and genesis_dir =
208
    flag "--genesis-ledger-dir" ~aliases:[ "genesis-ledger-dir" ]
6✔
209
      ~doc:
210
        "DIR Directory that contains the genesis ledger and the genesis \
211
         blockchain proof (default: <config-dir>)"
212
      (optional string)
6✔
213
  and run_snark_worker_flag =
214
    flag "--run-snark-worker" ~aliases:[ "run-snark-worker" ]
6✔
215
      ~doc:
216
        (sprintf "PUBLICKEY Run the SNARK worker with this public key. %s"
6✔
217
           receiver_key_warning )
218
      (optional public_key_compressed)
6✔
219
  and run_snark_coordinator_flag =
220
    flag "--run-snark-coordinator"
6✔
221
      ~aliases:[ "run-snark-coordinator" ]
222
      ~doc:
223
        (sprintf
6✔
224
           "PUBLICKEY Run a SNARK coordinator with this public key (ignored if \
225
            the run-snark-worker is set). %s"
226
           receiver_key_warning )
227
      (optional public_key_compressed)
6✔
228
  and snark_worker_parallelism_flag =
229
    flag "--snark-worker-parallelism"
6✔
230
      ~aliases:[ "snark-worker-parallelism" ]
231
      ~doc:
232
        "NUM Run the SNARK worker using this many threads. Equivalent to \
233
         setting OMP_NUM_THREADS, but doesn't affect block production."
234
      (optional int)
6✔
235
  and work_selection_method_flag =
236
    flag "--work-selection" ~aliases:[ "work-selection" ]
6✔
237
      ~doc:
238
        "seq|rand|roffset Choose work sequentially (seq), randomly (rand), or \
239
         sequentially with a random offset (roffset) (default: rand)"
240
      (optional work_selection_method)
6✔
241
  and libp2p_port = Flag.Port.Daemon.external_
242
  and client_port = Flag.Port.Daemon.client
243
  and rest_server_port = Flag.Port.Daemon.rest_server
244
  and limited_graphql_port = Flag.Port.Daemon.limited_graphql_server
245
  and itn_graphql_port =
246
    if itn_features then
247
      flag "--itn-graphql-port" ~aliases:[ "itn-graphql-port" ]
×
248
        ~doc:"PORT GraphQL-server for incentivized testnet interaction"
249
        (optional int)
×
250
    else Command.Param.return None
6✔
251
  and open_limited_graphql_port =
252
    flag "--open-limited-graphql-port"
6✔
253
      ~aliases:[ "open-limited-graphql-port" ]
254
      no_arg
255
      ~doc:
256
        "Have the limited GraphQL server listen on all addresses, not just \
257
         localhost (this is INSECURE, make sure your firewall is configured \
258
         correctly!)"
259
  and archive_process_location = Flag.Host_and_port.Daemon.archive
260
  and metrics_server_port =
261
    flag "--metrics-port" ~aliases:[ "metrics-port" ]
6✔
262
      ~doc:
263
        "PORT metrics server for scraping via Prometheus (default no \
264
         metrics-server)"
265
      (optional int16)
6✔
266
  and gc_stat_interval =
267
    flag "--gc-stat-interval" ~aliases:[ "gc-stat-interval" ] (optional float)
6✔
268
      ~doc:
269
        (sprintf
6✔
270
           "INTERVAL in mins for collecting GC stats for metrics (Default: %f)"
271
           !Mina_metrics.Runtime.gc_stat_interval_mins )
272
  and libp2p_metrics_port =
273
    flag "--libp2p-metrics-port" ~aliases:[ "libp2p-metrics-port" ]
6✔
274
      ~doc:
275
        "PORT libp2p metrics server for scraping via Prometheus (default no \
276
         libp2p-metrics-server)"
277
      (optional int16)
6✔
278
  and external_ip_opt =
279
    flag "--external-ip" ~aliases:[ "external-ip" ]
6✔
280
      ~doc:
281
        "IP External IP address for other nodes to connect to. You only need \
282
         to set this if auto-discovery fails for some reason."
283
      (optional string)
6✔
284
  and bind_ip_opt =
285
    flag "--bind-ip" ~aliases:[ "bind-ip" ]
6✔
286
      ~doc:"IP IP of network interface to use for peer connections"
287
      (optional string)
6✔
288
  and working_dir =
289
    flag "--working-dir" ~aliases:[ "working-dir" ]
6✔
290
      ~doc:
291
        "PATH path to chdir into before starting (useful for background mode, \
292
         defaults to cwd, or / if -background)"
293
      (optional string)
6✔
294
  and is_background =
295
    flag "--background" ~aliases:[ "background" ] no_arg
6✔
296
      ~doc:"Run process on the background"
297
  and is_archive_rocksdb =
298
    flag "--archive-rocksdb" ~aliases:[ "archive-rocksdb" ] no_arg
6✔
299
      ~doc:"Stores all the blocks heard in RocksDB"
300
  and log_json = Flag.Log.json
301
  and log_level = Flag.Log.level
302
  and file_log_level = Flag.Log.file_log_level
303
  and file_log_rotations = Flag.Log.file_log_rotations
304
  and snark_work_fee =
305
    flag "--snark-worker-fee" ~aliases:[ "snark-worker-fee" ]
6✔
306
      ~doc:
307
        (sprintf
6✔
308
           "FEE Amount a worker wants to get compensated for generating a \
309
            snark proof (default: %d)"
310
           (Currency.Fee.to_nanomina_int default_snark_worker_fee) )
6✔
311
      (optional txn_fee)
6✔
312
  and work_reassignment_wait =
313
    flag "--work-reassignment-wait"
6✔
314
      ~aliases:[ "work-reassignment-wait" ]
315
      (optional int)
6✔
316
      ~doc:
317
        (sprintf
6✔
318
           "WAIT-TIME in ms before a snark-work is reassigned (default: %dms)"
319
           Cli_lib.Default.work_reassignment_wait )
320
  and enable_tracing =
321
    flag "--tracing" ~aliases:[ "tracing" ] no_arg
6✔
322
      ~doc:"Trace into $config-directory/trace/$pid.trace"
323
  and enable_internal_tracing =
324
    flag "--internal-tracing" ~aliases:[ "internal-tracing" ] no_arg
6✔
325
      ~doc:
326
        "Enables internal tracing into \
327
         $config-directory/internal-tracing/internal-trace.jsonl"
328
  and insecure_rest_server =
329
    flag "--insecure-rest-server" ~aliases:[ "insecure-rest-server" ] no_arg
6✔
330
      ~doc:
331
        "Have REST server listen on all addresses, not just localhost (this is \
332
         INSECURE, make sure your firewall is configured correctly!)"
333
  (* FIXME #4095
334
     and limit_connections =
335
       flag "--limit-concurrent-connections"
336
         ~aliases:[ "limit-concurrent-connections"]
337
         ~doc:
338
           "true|false Limit the number of concurrent connections per IP \
339
            address (default: true)"
340
         (optional bool)*)
341
  (*TODO: This is being added to log all the snark works received for the
342
     beta-testnet challenge. We might want to remove this later?*)
343
  and log_received_snark_pool_diff =
344
    flag "--log-snark-work-gossip"
6✔
345
      ~aliases:[ "log-snark-work-gossip" ]
346
      ~doc:"true|false Log snark-pool diff received from peers (default: false)"
347
      (optional bool)
6✔
348
  and log_transaction_pool_diff =
349
    flag "--log-txn-pool-gossip" ~aliases:[ "log-txn-pool-gossip" ]
6✔
350
      ~doc:
351
        "true|false Log transaction-pool diff received from peers (default: \
352
         false)"
353
      (optional bool)
6✔
354
  and log_block_creation =
355
    flag "--log-block-creation" ~aliases:[ "log-block-creation" ]
6✔
356
      ~doc:
357
        "true|false Log the steps involved in including transactions and snark \
358
         work in a block (default: true)"
359
      (optional bool)
6✔
360
  and libp2p_keypair =
361
    flag "--libp2p-keypair" ~aliases:[ "libp2p-keypair" ] (optional string)
6✔
362
      ~doc:
363
        "KEYFILE Keypair (generated from `mina libp2p generate-keypair`) to \
364
         use with libp2p discovery"
365
  and is_seed =
366
    flag "--seed" ~aliases:[ "seed" ] ~doc:"Start the node as a seed node"
6✔
367
      no_arg
368
  and no_super_catchup =
369
    flag "--no-super-catchup" ~aliases:[ "no-super-catchup" ]
6✔
370
      ~doc:"Don't use super-catchup" no_arg
371
  and enable_flooding =
372
    flag "--enable-flooding" ~aliases:[ "enable-flooding" ]
6✔
373
      ~doc:
374
        "true|false Publish our own blocks/transactions to every peer we can \
375
         find (default: false)"
376
      (optional bool)
6✔
377
  and peer_exchange =
378
    flag "--enable-peer-exchange" ~aliases:[ "enable-peer-exchange" ]
6✔
379
      ~doc:
380
        "true|false Help keep the mesh connected when closing connections \
381
         (default: false)"
382
      (optional bool)
6✔
383
  and peer_protection_ratio =
384
    flag "--peer-protection-rate" ~aliases:[ "peer-protection-rate" ]
6✔
385
      ~doc:"float Proportion of peers to be marked as protected (default: 0.2)"
386
      (optional_with_default 0.2 float)
6✔
387
  and min_connections =
388
    flag "--min-connections" ~aliases:[ "min-connections" ]
6✔
389
      ~doc:
390
        (Printf.sprintf
6✔
391
           "NN min number of connections that this peer will have to neighbors \
392
            in the gossip network (default: %d)"
393
           Cli_lib.Default.min_connections )
394
      (optional int)
6✔
395
  and max_connections =
396
    flag "--max-connections" ~aliases:[ "max-connections" ]
6✔
397
      ~doc:
398
        (Printf.sprintf
6✔
399
           "NN max number of connections that this peer will have to neighbors \
400
            in the gossip network. Tuning this higher will strengthen your \
401
            connection to the network in exchange for using more RAM (default: \
402
            %d)"
403
           Cli_lib.Default.max_connections )
404
      (optional int)
6✔
405
  and validation_queue_size =
406
    flag "--validation-queue-size"
6✔
407
      ~aliases:[ "validation-queue-size" ]
408
      ~doc:
409
        (Printf.sprintf
6✔
410
           "NN size of the validation queue in the p2p network used to buffer \
411
            messages (like blocks and transactions received on the gossip \
412
            network) while validation is pending. If a transaction, for \
413
            example, is invalid, we don't forward the message on the gossip \
414
            net. If this queue is too small, we will drop messages without \
415
            validating them. If it is too large, we are susceptible to DoS \
416
            attacks on memory. (default: %d)"
417
           Cli_lib.Default.validation_queue_size )
418
      (optional int)
6✔
419
  and direct_peers_raw =
420
    flag "--direct-peer" ~aliases:[ "direct-peer" ]
6✔
421
      ~doc:
422
        "/ip4/IPADDR/tcp/PORT/p2p/PEERID Peers to always send new messages \
423
         to/from. These peers should also have you configured as a direct \
424
         peer, the relationship is intended to be symmetric"
425
      (listed string)
6✔
426
  and isolate =
427
    flag "--isolate-network" ~aliases:[ "isolate-network" ]
6✔
428
      ~doc:
429
        "true|false Only allow connections to the peers passed on the command \
430
         line or configured through GraphQL. (default: false)"
431
      (optional bool)
6✔
432
  and libp2p_peers_raw =
433
    flag "--peer" ~aliases:[ "peer" ]
6✔
434
      ~doc:
435
        "/ip4/IPADDR/tcp/PORT/p2p/PEERID initial \"bootstrap\" peers for \
436
         discovery"
437
      (listed string)
6✔
438
  and libp2p_peer_list_file =
439
    flag "--peer-list-file" ~aliases:[ "peer-list-file" ]
6✔
440
      ~doc:
441
        "PATH path to a file containing \"bootstrap\" peers for discovery, one \
442
         multiaddress per line"
443
      (optional string)
6✔
444
  and seed_peer_list_url =
445
    flag "--peer-list-url" ~aliases:[ "peer-list-url" ]
6✔
446
      ~doc:"URL URL of seed peer list file. Will be polled periodically."
447
      (optional string)
6✔
448
  and proposed_protocol_version =
449
    flag "--proposed-protocol-version"
6✔
450
      ~aliases:[ "proposed-protocol-version" ]
451
      (optional string)
6✔
452
      ~doc:"NN.NN.NN Proposed protocol version to signal other nodes"
453
  and config_files =
454
    flag "--config-file" ~aliases:[ "config-file" ]
6✔
455
      ~doc:
456
        "PATH path to a configuration file (overrides MINA_CONFIG_FILE, \
457
         default: <config_dir>/daemon.json). Pass multiple times to override \
458
         fields from earlier config files"
459
      (listed string)
6✔
460
  and _may_generate =
461
    flag "--generate-genesis-proof"
6✔
462
      ~aliases:[ "generate-genesis-proof" ]
463
      ~doc:"true|false Deprecated. Passing this flag has no effect"
464
      (optional bool)
6✔
465
  and disable_node_status =
466
    flag "--disable-node-status" ~aliases:[ "disable-node-status" ] no_arg
6✔
467
      ~doc:"Disable reporting node status to other nodes (default: enabled)"
468
  and cli_proof_level =
469
    flag "--proof-level" ~aliases:[ "proof-level" ]
6✔
470
      (optional (Arg_type.create Genesis_constants.Proof_level.of_string))
6✔
471
      ~doc:
472
        "full|check|none Internal, for testing. Start or connect to a network \
473
         with full proving (full), snark-testing with dummy proofs (check), or \
474
         dummy proofs (none)"
475
  and plugins = plugin_flag
476
  and precomputed_blocks_path =
477
    flag "--precomputed-blocks-file"
6✔
478
      ~aliases:[ "precomputed-blocks-file" ]
479
      (optional string)
6✔
480
      ~doc:"PATH Path to write precomputed blocks to, for replay or archiving"
481
  and log_precomputed_blocks =
482
    flag "--log-precomputed-blocks"
6✔
483
      ~aliases:[ "log-precomputed-blocks" ]
484
      (optional_with_default false bool)
6✔
485
      ~doc:"true|false Include precomputed blocks in the log (default: false)"
486
  and start_filtered_logs =
487
    flag "--start-filtered-logs" (listed string)
6✔
488
      ~doc:
489
        "LOG-FILTER Include filtered logs for the given filter. May be passed \
490
         multiple times"
491
  and block_reward_threshold =
492
    flag "--minimum-block-reward" ~aliases:[ "minimum-block-reward" ]
6✔
493
      ~doc:
494
        "AMOUNT Minimum reward a block produced by the node should have. Empty \
495
         blocks are created if the rewards are lower than the specified \
496
         threshold (default: No threshold, transactions and coinbase will be \
497
         included as long as the required snark work is available and can be \
498
         paid for)"
499
      (optional txn_amount)
6✔
500
  and stop_time =
501
    flag "--stop-time" ~aliases:[ "stop-time" ] (optional int)
6✔
502
      ~doc:
503
        (sprintf
6✔
504
           "UPTIME in hours after which the daemon stops itself (only if there \
505
            were no slots won within an hour after the stop time) (Default: \
506
            %d)"
507
           Cli_lib.Default.stop_time )
508
  and upload_blocks_to_gcloud =
509
    flag "--upload-blocks-to-gcloud"
6✔
510
      ~aliases:[ "upload-blocks-to-gcloud" ]
511
      (optional_with_default false bool)
6✔
512
      ~doc:
513
        "true|false upload blocks to gcloud storage. Requires the environment \
514
         variables GCLOUD_KEYFILE, NETWORK_NAME, and \
515
         GCLOUD_BLOCK_UPLOAD_BUCKET"
516
  and all_peers_seen_metric =
517
    flag "--all-peers-seen-metric"
6✔
518
      ~aliases:[ "all-peers-seen-metric" ]
519
      (optional_with_default false bool)
6✔
520
      ~doc:
521
        "true|false whether to track the set of all peers ever seen for the \
522
         all_peers metric (default: false)"
523
  and node_status_url =
524
    flag "--node-status-url" ~aliases:[ "node-status-url" ] (optional string)
6✔
525
      ~doc:"URL of the node status collection service"
526
  and node_error_url =
527
    flag "--node-error-url" ~aliases:[ "node-error-url" ] (optional string)
6✔
528
      ~doc:"URL of the node error collection service"
529
  and simplified_node_stats =
530
    flag "--simplified-node-stats"
6✔
531
      ~aliases:[ "simplified-node-stats" ]
532
      (optional_with_default true bool)
6✔
533
      ~doc:"whether to report simplified node stats (default: true)"
534
  and contact_info =
535
    flag "--contact-info" ~aliases:[ "contact-info" ] (optional string)
6✔
536
      ~doc:
537
        "contact info used in node error report service (it could be either \
538
         email address or discord username), it should be less than 200 \
539
         characters"
540
    |> Command.Param.map ~f:(fun opt ->
6✔
541
           Option.value_map opt ~default:None ~f:(fun s ->
×
542
               if String.length s < 200 then Some s
×
543
               else
544
                 Mina_user_error.raisef
×
545
                   "The length of contact info exceeds 200 characters:\n %s" s ) )
546
  and uptime_url_string =
547
    flag "--uptime-url" ~aliases:[ "uptime-url" ] (optional string)
6✔
548
      ~doc:"URL URL of the uptime service of the Mina delegation program"
549
  and uptime_submitter_key =
550
    flag "--uptime-submitter-key" ~aliases:[ "uptime-submitter-key" ]
6✔
551
      ~doc:
552
        "KEYFILE Private key file for the uptime submitter. You cannot provide \
553
         both `uptime-submitter-key` and `uptime-submitter-pubkey`."
554
      (optional string)
6✔
555
  and uptime_submitter_pubkey =
556
    flag "--uptime-submitter-pubkey"
6✔
557
      ~aliases:[ "uptime-submitter-pubkey" ]
558
      (optional string)
6✔
559
      ~doc:
560
        "PUBLICKEY Public key of the submitter to the Mina delegation program, \
561
         for the associated private key that is being tracked by this daemon. \
562
         You cannot provide both `uptime-submitter-key` and \
563
         `uptime-submitter-pubkey`."
564
  and uptime_send_node_commit =
565
    flag "--uptime-send-node-commit-sha"
6✔
566
      ~aliases:[ "uptime-send-node-commit-sha" ]
567
      ~doc:
568
        "true|false Whether to send the commit SHA used to build the node to \
569
         the uptime service. (default: false)"
570
      no_arg
571
  in
572
  let to_pubsub_topic_mode_option =
×
573
    let open Gossip_net.Libp2p in
574
    function
575
    | `String "ro" ->
×
576
        Some RO
577
    | `String "rw" ->
×
578
        Some RW
579
    | `String "none" ->
×
580
        Some N
581
    | `Null ->
×
582
        None
583
    | _ ->
×
584
        raise (Error.to_exn (Error.of_string "Invalid pubsub topic mode"))
×
585
  in
586
  fun () ->
587
    O1trace.thread "mina" (fun () ->
×
588
        let open Deferred.Let_syntax in
×
589
        let conf_dir = Mina_lib.Conf_dir.compute_conf_dir conf_dir in
590
        let%bind () = File_system.create_dir conf_dir in
×
591
        let () =
×
592
          if is_background then (
×
593
            Core.printf "Starting background mina daemon. (Log Dir: %s)\n%!"
594
              conf_dir ;
595
            Daemon.daemonize ~allow_threads_to_have_been_created:true
×
596
              ~redirect_stdout:`Dev_null ?cd:working_dir
597
              ~redirect_stderr:`Dev_null () )
598
          else Option.iter working_dir ~f:Caml.Sys.chdir
×
599
        in
600
        Stdout_log.setup log_json log_level ;
601
        (* 512MB logrotate max size = 1GB max filesystem usage *)
602
        let logrotate_max_size = 1024 * 1024 * 10 in
×
603
        Logger.Consumer_registry.register ~commit_id:Mina_version.commit_id
604
          ~id:Logger.Logger_id.mina
605
          ~processor:(Logger.Processor.raw ~log_level:file_log_level ())
×
606
          ~transport:
607
            (Logger_file_system.dumb_logrotate ~directory:conf_dir
608
               ~log_filename:"mina.log" ~max_size:logrotate_max_size
609
               ~num_rotate:file_log_rotations )
610
          () ;
611
        let best_tip_diff_log_size = 1024 * 1024 * 5 in
×
612
        Logger.Consumer_registry.register ~commit_id:Mina_version.commit_id
613
          ~id:Logger.Logger_id.best_tip_diff
614
          ~processor:(Logger.Processor.raw ())
×
615
          ~transport:
616
            (Logger_file_system.dumb_logrotate ~directory:conf_dir
617
               ~log_filename:"mina-best-tip.log"
618
               ~max_size:best_tip_diff_log_size ~num_rotate:1 )
619
          () ;
620
        let rejected_blocks_log_size = 1024 * 1024 * 5 in
×
621
        Logger.Consumer_registry.register ~commit_id:Mina_version.commit_id
622
          ~id:Logger.Logger_id.rejected_blocks
623
          ~processor:(Logger.Processor.raw ())
×
624
          ~transport:
625
            (Logger_file_system.dumb_logrotate ~directory:conf_dir
626
               ~log_filename:"mina-rejected-blocks.log"
627
               ~max_size:rejected_blocks_log_size ~num_rotate:50 )
628
          () ;
629
        Logger.Consumer_registry.register ~commit_id:Mina_version.commit_id
×
630
          ~id:Logger.Logger_id.oversized_logs
631
          ~processor:(Logger.Processor.raw ())
×
632
          ~transport:
633
            (Logger_file_system.dumb_logrotate ~directory:conf_dir
634
               ~log_filename:"mina-oversized-logs.log"
635
               ~max_size:logrotate_max_size ~num_rotate:20 )
636
          () ;
637
        (* Consumer for `[%log internal]` logging used for internal tracing *)
638
        Itn_logger.set_message_postprocessor
×
639
          Internal_tracing.For_itn_logger.post_process_message ;
640
        Logger.Consumer_registry.register ~commit_id:Mina_version.commit_id
×
641
          ~id:Logger.Logger_id.mina
642
          ~processor:Internal_tracing.For_logger.processor
643
          ~transport:
644
            (Internal_tracing.For_logger.json_lines_rotate_transport
×
645
               ~directory:(conf_dir ^ "/internal-tracing")
646
               () )
647
          () ;
648
        let version_metadata = [ ("commit", `String Mina_version.commit_id) ] in
×
649
        [%log info] "Mina daemon is booting up; built with commit $commit"
×
650
          ~metadata:version_metadata ;
651
        let%bind () =
652
          Mina_lib.Conf_dir.check_and_set_lockfile ~logger conf_dir
×
653
        in
654
        [%log info] "Booting may take several seconds, please wait" ;
×
655
        let wallets_disk_location = conf_dir ^/ "wallets" in
×
656
        let%bind wallets =
657
          (* Load wallets early, to give user errors before expensive
658
             initialization starts.
659
          *)
660
          Secrets.Wallets.load ~logger ~disk_location:wallets_disk_location
661
        in
662
        let%bind libp2p_keypair =
663
          let libp2p_keypair_old_format =
664
            Option.bind libp2p_keypair ~f:(fun libp2p_keypair ->
665
                match Mina_net2.Keypair.of_string libp2p_keypair with
×
666
                | Ok kp ->
×
667
                    Some kp
668
                | Error _ ->
×
669
                    if String.contains libp2p_keypair ',' then
670
                      [%log warn]
×
671
                        "I think -libp2p-keypair is in the old format, but I \
672
                         failed to parse it! Using it as a path..." ;
673
                    None )
×
674
          in
675
          match libp2p_keypair_old_format with
×
676
          | Some kp ->
×
677
              return (Some kp)
×
678
          | None -> (
×
679
              match libp2p_keypair with
680
              | None ->
×
681
                  return None
×
682
              | Some s ->
×
683
                  Secrets.Libp2p_keypair.Terminal_stdin.read_exn
684
                    ~should_prompt_user:false ~which:"libp2p keypair" s
685
                  |> Deferred.map ~f:Option.some )
×
686
        in
687
        let%bind () =
688
          let version_filename = conf_dir ^/ "mina.version" in
689
          let make_version () =
×
690
            let%map () =
691
              (*Delete any trace files if version changes. TODO: Implement rotate logic similar to log files*)
692
              File_system.remove_dir (conf_dir ^/ "trace")
×
693
            in
694
            Yojson.Safe.to_file version_filename (`Assoc version_metadata)
×
695
          in
696
          match
697
            Or_error.try_with_join (fun () ->
698
                match Yojson.Safe.from_file version_filename with
×
699
                | `Assoc list -> (
×
700
                    match String.Map.(find (of_alist_exn list) "commit") with
×
701
                    | Some (`String commit) ->
×
702
                        Ok commit
703
                    | _ ->
×
704
                        Or_error.errorf "commit not found in version file %s"
705
                          version_filename )
706
                | _ ->
×
707
                    Or_error.errorf "Unexpected value in %s" version_filename )
708
          with
709
          | Ok c ->
×
710
              if String.equal c Mina_version.commit_id then return ()
×
711
              else (
×
712
                [%log warn]
×
713
                  "Different version of Mina detected in config directory \
714
                   $config_directory, removing existing configuration"
715
                  ~metadata:[ ("config_directory", `String conf_dir) ] ;
716
                make_version () )
×
717
          | Error e ->
×
718
              [%log debug]
×
719
                "Error reading $file: $error. Cleaning up the config directory \
720
                 $config_directory"
721
                ~metadata:
722
                  [ ("error", `String (Error.to_string_mach e))
×
723
                  ; ("config_directory", `String conf_dir)
724
                  ; ("file", `String version_filename)
725
                  ] ;
726
              make_version ()
×
727
        in
728
        Parallel.init_master () ;
×
729
        let monitor = Async.Monitor.create ~name:"coda" () in
×
730
        let time_controller =
×
731
          Block_time.Controller.create @@ Block_time.Controller.basic ~logger
732
        in
733
        let pids = Child_processes.Termination.create_pid_table () in
×
734
        let mina_initialization_deferred () =
×
735
          let config_file_installed =
×
736
            (* Search for config files installed as part of a deb/brew package.
737
               These files are commit-dependent, to ensure that we don't clobber
738
               configuration for dev builds or use incompatible configs.
739
            *)
740
            let config_file_installed =
741
              let json = "config_" ^ Mina_version.commit_id_short ^ ".json" in
742
              List.fold_until ~init:None
×
743
                (Cache_dir.possible_paths json)
×
744
                ~f:(fun _acc f ->
745
                  match Core.Sys.file_exists f with
×
746
                  | `Yes ->
×
747
                      Stop (Some f)
748
                  | _ ->
×
749
                      Continue None )
750
                ~finish:Fn.id
751
            in
752
            match config_file_installed with
753
            | Some config_file ->
×
754
                Some (config_file, `Must_exist)
755
            | None ->
×
756
                None
757
          in
758
          let config_file_configdir =
759
            (conf_dir ^/ "daemon.json", `May_be_missing)
×
760
          in
761
          let config_file_envvar =
762
            match Sys.getenv "MINA_CONFIG_FILE" with
763
            | Some config_file ->
×
764
                Some (config_file, `Must_exist)
765
            | None ->
×
766
                None
767
          in
768
          let config_files =
769
            Option.to_list config_file_installed
×
770
            @ (config_file_configdir :: Option.to_list config_file_envvar)
×
771
            @ List.map config_files ~f:(fun config_file ->
×
772
                  (config_file, `Must_exist) )
×
773
          in
774
          let genesis_constants =
775
            Genesis_constants.Compiled.genesis_constants
776
          in
777
          let constraint_constants =
778
            Genesis_constants.Compiled.constraint_constants
779
          in
780
          let compile_config = Mina_compile_config.Compiled.t in
781
          let%bind precomputed_values, config_jsons, config =
782
            load_config_files ~logger ~conf_dir ~genesis_dir
×
783
              ~proof_level:Genesis_constants.Compiled.proof_level config_files
784
              ~genesis_constants ~constraint_constants ~cli_proof_level
785
          in
786

787
          constraint_constants.block_window_duration_ms |> Float.of_int
×
788
          |> Time.Span.of_ms |> Mina_metrics.initialize_all ;
×
789

790
          let rev_daemon_configs =
×
791
            List.rev_filter_map config_jsons
792
              ~f:(fun (config_file, config_json) ->
793
                Option.map
×
794
                  YJ.Util.(
795
                    to_option Fn.id (YJ.Util.member "daemon" config_json))
×
796
                  ~f:(fun daemon_config -> (config_file, daemon_config)) )
×
797
          in
798
          let maybe_from_config (type a) (f : YJ.t -> a option)
×
799
              (keyname : string) (actual_value : a option) : a option =
800
            let open Option.Let_syntax in
×
801
            let open YJ.Util in
802
            match actual_value with
803
            | Some v ->
×
804
                Some v
805
            | None ->
×
806
                (* Load value from the latest config file that both
807
                   * has the key we are looking for, and
808
                   * has the key in a format that [f] can parse.
809
                *)
810
                let%map config_file, data =
811
                  List.find_map rev_daemon_configs
×
812
                    ~f:(fun (config_file, daemon_config) ->
813
                      let%bind json_val =
814
                        to_option Fn.id (member keyname daemon_config)
×
815
                      in
816
                      let%map data = f json_val in
×
817
                      (config_file, data) )
×
818
                in
819
                [%log debug] "Key $key being used from config file $config_file"
×
820
                  ~metadata:
821
                    [ ("key", `String keyname)
822
                    ; ("config_file", `String config_file)
823
                    ] ;
824
                data
×
825
          in
826
          let or_from_config map keyname actual_value ~default =
827
            match maybe_from_config map keyname actual_value with
×
828
            | Some x ->
×
829
                x
830
            | None ->
×
831
                [%log trace]
×
832
                  "Key '$key' not found in the config file, using default"
833
                  ~metadata:[ ("key", `String keyname) ] ;
834
                default
×
835
          in
836
          let get_port { Flag.Types.value; default; name } =
837
            or_from_config YJ.Util.to_int_option name ~default value
×
838
          in
839
          let libp2p_port = get_port libp2p_port in
840
          let rest_server_port = get_port rest_server_port in
×
841
          let limited_graphql_port =
×
842
            let ({ value; name } : int option Flag.Types.with_name) =
843
              limited_graphql_port
844
            in
845
            maybe_from_config YJ.Util.to_int_option name value
×
846
          in
847
          let client_port = get_port client_port in
848
          let snark_work_fee_flag =
×
849
            let json_to_currency_fee_option json =
850
              YJ.Util.to_int_option json
×
851
              |> Option.map ~f:Currency.Fee.of_nanomina_int_exn
×
852
            in
853
            or_from_config json_to_currency_fee_option "snark-worker-fee"
×
854
              ~default:compile_config.default_snark_worker_fee snark_work_fee
855
          in
856
          let node_status_url =
857
            maybe_from_config YJ.Util.to_string_option "node-status-url"
858
              node_status_url
859
          in
860
          (* FIXME #4095: pass this through to Gossip_net.Libp2p *)
861
          let _max_concurrent_connections =
×
862
            (*if
863
                 or_from_config YJ.Util.to_bool_option "max-concurrent-connections"
864
                   ~default:true limit_connections
865
               then Some 40
866
               else *)
867
            None
868
          in
869
          let work_selection_method =
870
            or_from_config
871
              (Fn.compose Option.return
×
872
                 (Fn.compose work_selection_method_val YJ.Util.to_string) )
×
873
              "work-selection"
874
              ~default:Cli_lib.Arg_type.Work_selection_method.Random
875
              work_selection_method_flag
876
          in
877
          let work_reassignment_wait =
×
878
            or_from_config YJ.Util.to_int_option "work-reassignment-wait"
879
              ~default:Cli_lib.Default.work_reassignment_wait
880
              work_reassignment_wait
881
          in
882
          let log_received_snark_pool_diff =
×
883
            or_from_config YJ.Util.to_bool_option "log-snark-work-gossip"
884
              ~default:false log_received_snark_pool_diff
885
          in
886
          let log_transaction_pool_diff =
×
887
            or_from_config YJ.Util.to_bool_option "log-txn-pool-gossip"
888
              ~default:false log_transaction_pool_diff
889
          in
890
          let log_block_creation =
×
891
            or_from_config YJ.Util.to_bool_option "log-block-creation"
892
              ~default:true log_block_creation
893
          in
894
          let log_gossip_heard =
×
895
            { Mina_networking.Config.snark_pool_diff =
896
                log_received_snark_pool_diff
897
            ; transaction_pool_diff = log_transaction_pool_diff
898
            ; new_state = true
899
            }
900
          in
901
          let json_to_publickey_compressed_option which json =
902
            YJ.Util.to_string_option json
×
903
            |> Option.bind ~f:(fun pk_str ->
×
904
                   match Public_key.Compressed.of_base58_check pk_str with
×
905
                   | Ok key -> (
×
906
                       match Public_key.decompress key with
907
                       | None ->
×
908
                           Mina_user_error.raisef
909
                             ~where:"decompressing a public key"
910
                             "The %s public key %s could not be decompressed."
911
                             which pk_str
912
                       | Some _ ->
×
913
                           Some key )
914
                   | Error _e ->
×
915
                       Mina_user_error.raisef ~where:"decoding a public key"
916
                         "The %s public key %s could not be decoded." which
917
                         pk_str )
918
          in
919
          let run_snark_worker_flag =
920
            maybe_from_config
921
              (json_to_publickey_compressed_option "snark worker")
×
922
              "run-snark-worker" run_snark_worker_flag
923
          in
924
          let run_snark_coordinator_flag =
×
925
            maybe_from_config
926
              (json_to_publickey_compressed_option "snark coordinator")
×
927
              "run-snark-coordinator" run_snark_coordinator_flag
928
          in
929
          let snark_worker_parallelism_flag =
×
930
            maybe_from_config YJ.Util.to_int_option "snark-worker-parallelism"
931
              snark_worker_parallelism_flag
932
          in
933
          let coinbase_receiver_flag =
×
934
            maybe_from_config
935
              (json_to_publickey_compressed_option "coinbase receiver")
×
936
              "coinbase-receiver" coinbase_receiver_flag
937
          in
938
          let%bind external_ip =
939
            match external_ip_opt with
940
            | None ->
×
941
                Find_ip.find ~logger
942
            | Some ip ->
×
943
                return @@ Unix.Inet_addr.of_string ip
×
944
          in
945
          let bind_ip =
×
946
            Option.value bind_ip_opt ~default:"0.0.0.0"
947
            |> Unix.Inet_addr.of_string
×
948
          in
949
          let addrs_and_ports : Node_addrs_and_ports.t =
×
950
            { external_ip; bind_ip; peer = None; client_port; libp2p_port }
951
          in
952
          let block_production_key =
953
            maybe_from_config YJ.Util.to_string_option "block-producer-key"
954
              block_production_key
955
          in
956
          let block_production_pubkey =
×
957
            maybe_from_config
958
              (json_to_publickey_compressed_option "block producer")
×
959
              "block-producer-pubkey" block_production_pubkey
960
          in
961
          let block_production_password =
×
962
            maybe_from_config YJ.Util.to_string_option "block-producer-password"
963
              block_production_password
964
          in
965
          Option.iter
×
966
            ~f:(fun password ->
967
              match Sys.getenv Secrets.Keypair.env with
×
968
              | Some env_pass when not (String.equal env_pass password) ->
×
969
                  [%log warn]
×
970
                    "$envkey environment variable doesn't match value provided \
971
                     on command-line or daemon.json. Using value from $envkey"
972
                    ~metadata:[ ("envkey", `String Secrets.Keypair.env) ]
973
              | _ ->
×
974
                  Unix.putenv ~key:Secrets.Keypair.env ~data:password )
975
            block_production_password ;
976
          let%bind block_production_keypair =
977
            match
978
              ( block_production_key
979
              , block_production_pubkey
980
              , Sys.getenv "MINA_BP_PRIVKEY" )
×
981
            with
982
            | Some _, Some _, _ ->
×
983
                Mina_user_error.raise
×
984
                  "You cannot provide both `block-producer-key` and \
985
                   `block_production_pubkey`"
986
            | None, Some _, Some _ ->
×
987
                Mina_user_error.raise
×
988
                  "You cannot provide both `MINA_BP_PRIVKEY` and \
989
                   `block_production_pubkey`"
990
            | None, None, None ->
×
991
                Deferred.return None
×
992
            | None, None, Some base58_privkey ->
×
993
                let kp =
994
                  Private_key.of_base58_check_exn base58_privkey
995
                  |> Keypair.of_private_key_exn
×
996
                in
997
                Deferred.return (Some kp)
×
998
            (* CLI argument takes precedence over env variable *)
999
            | Some sk_file, None, (Some _ | None) ->
×
1000
                [%log warn]
×
1001
                  "`block-producer-key` is deprecated. Please set \
1002
                   `MINA_BP_PRIVKEY` environment variable instead." ;
1003
                let%map kp =
1004
                  Secrets.Keypair.Terminal_stdin.read_exn
×
1005
                    ~should_prompt_user:false ~which:"block producer keypair"
1006
                    sk_file
1007
                in
1008
                Some kp
×
1009
            | None, Some tracked_pubkey, None ->
×
1010
                let%map kp =
1011
                  Secrets.Wallets.get_tracked_keypair ~logger
×
1012
                    ~which:"block producer keypair"
1013
                    ~read_from_env_exn:
1014
                      (Secrets.Keypair.Terminal_stdin.read_exn
1015
                         ~should_prompt_user:false ~should_reask:false )
1016
                    ~conf_dir tracked_pubkey
1017
                in
1018
                Some kp
×
1019
          in
1020
          let%bind client_trustlist =
1021
            Reader.load_sexp
×
1022
              (conf_dir ^/ "client_trustlist")
×
1023
              [%of_sexp: Unix.Cidr.t list]
1024
            >>| Or_error.ok
×
1025
          in
1026
          let client_trustlist =
×
1027
            let mina_client_trustlist = "MINA_CLIENT_TRUSTLIST" in
1028
            let cidrs_of_env_str env_str env_var =
1029
              let cidrs =
×
1030
                String.split ~on:',' env_str
1031
                |> List.filter_map ~f:(fun str ->
×
1032
                       try Some (Unix.Cidr.of_string str)
×
1033
                       with _ ->
×
1034
                         [%log warn] "Could not parse address $address in %s"
×
1035
                           env_var
1036
                           ~metadata:[ ("address", `String str) ] ;
1037
                         None )
×
1038
              in
1039
              Some
×
1040
                (List.append cidrs (Option.value ~default:[] client_trustlist))
×
1041
            in
1042
            match Unix.getenv mina_client_trustlist with
1043
            | Some env_str ->
×
1044
                cidrs_of_env_str env_str mina_client_trustlist
×
1045
            | None ->
×
1046
                client_trustlist
1047
          in
1048
          let get_monitor_infos monitor =
1049
            let rec get_monitors accum monitor =
×
1050
              match Async_kernel.Monitor.parent monitor with
×
1051
              | None ->
×
1052
                  List.rev accum
1053
              | Some parent ->
×
1054
                  get_monitors (parent :: accum) parent
1055
            in
1056
            let monitors = get_monitors [ monitor ] monitor in
1057
            List.map monitors ~f:(fun monitor ->
×
1058
                match Async_kernel.Monitor.sexp_of_t monitor with
×
1059
                | Sexp.List sexps ->
×
1060
                    `List (List.map ~f:Error_json.sexp_record_to_yojson sexps)
×
1061
                | Sexp.Atom _ ->
×
1062
                    failwith "Expected a sexp list" )
1063
          in
1064
          let o1trace context =
1065
            Execution_context.find_local context O1trace.local_storage_id
×
1066
            |> Option.value ~default:[]
×
1067
            |> List.map ~f:(fun x -> `String x)
×
1068
          in
1069
          Stream.iter
1070
            (Async_kernel.Async_kernel_scheduler.long_cycles_with_context
1071
               ~at_least:(sec 0.5 |> Time_ns.Span.of_span_float_round_nearest) )
×
1072
            ~f:(fun (span, context) ->
1073
              let secs = Time_ns.Span.to_sec span in
×
1074
              let monitor_infos = get_monitor_infos context.monitor in
×
1075
              let o1trace = o1trace context in
×
1076
              [%log internal] "Long_async_cycle"
×
1077
                ~metadata:
1078
                  [ ("duration", `Float secs); ("trace", `List o1trace) ] ;
1079
              [%log debug]
×
1080
                ~metadata:
1081
                  [ ("long_async_cycle", `Float secs)
1082
                  ; ("monitors", `List monitor_infos)
1083
                  ; ("o1trace", `List o1trace)
1084
                  ]
1085
                "Long async cycle, $long_async_cycle seconds, $monitors, \
1086
                 $o1trace" ;
1087
              Mina_metrics.(
×
1088
                Runtime.Long_async_histogram.observe Runtime.long_async_cycle
1089
                  secs) ) ;
1090
          Stream.iter Async_kernel.Async_kernel_scheduler.long_jobs_with_context
×
1091
            ~f:(fun (context, span) ->
1092
              let secs = Time_ns.Span.to_sec span in
×
1093
              let monitor_infos = get_monitor_infos context.monitor in
×
1094
              let o1trace = o1trace context in
×
1095
              [%log internal] "Long_async_job"
×
1096
                ~metadata:
1097
                  [ ("duration", `Float secs); ("trace", `List o1trace) ] ;
1098
              [%log debug]
×
1099
                ~metadata:
1100
                  [ ("long_async_job", `Float secs)
1101
                  ; ("monitors", `List monitor_infos)
1102
                  ; ("o1trace", `List o1trace)
1103
                  ; ( "most_recent_2_backtrace"
1104
                    , `String
1105
                        (String.concat ~sep:"␤"
×
1106
                           (List.map ~f:Backtrace.to_string
×
1107
                              (List.take
×
1108
                                 (Execution_context.backtrace_history context)
×
1109
                                 2 ) ) ) )
1110
                  ]
1111
                "Long async job, $long_async_job seconds, $monitors, $o1trace" ;
1112
              Mina_metrics.(
×
1113
                Runtime.Long_job_histogram.observe Runtime.long_async_job secs) ) ;
1114
          let trace_database_initialization typ location =
×
1115
            (* can't use %log ppx here, because we're using the passed-in location *)
1116
            Logger.trace logger ~module_:__MODULE__ "Creating %s at %s"
×
1117
              ~location typ
1118
          in
1119
          let trust_dir = conf_dir ^/ "trust" in
1120
          let%bind () = Async.Unix.mkdir ~p:() trust_dir in
×
1121
          let%bind trust_system = Trust_system.create trust_dir in
×
1122
          trace_database_initialization "trust_system" __LOC__ trust_dir ;
×
1123
          let genesis_state_hash =
×
1124
            (Precomputed_values.genesis_state_hashes precomputed_values)
×
1125
              .state_hash
1126
          in
1127
          let genesis_ledger_hash =
1128
            Precomputed_values.genesis_ledger precomputed_values
1129
            |> Lazy.force |> Mina_ledger.Ledger.merkle_root
×
1130
          in
1131
          let block_production_keypairs =
×
1132
            block_production_keypair
1133
            |> Option.map ~f:(fun kp ->
1134
                   (kp, Public_key.compress kp.Keypair.public_key) )
×
1135
            |> Option.to_list |> Keypair.And_compressed_pk.Set.of_list
×
1136
          in
1137
          let epoch_ledger_location = conf_dir ^/ "epoch_ledger" in
×
1138
          let module Context = struct
×
1139
            let logger = logger
1140

1141
            let constraint_constants = precomputed_values.constraint_constants
1142

1143
            let consensus_constants = precomputed_values.consensus_constants
1144
          end in
1145
          let consensus_local_state =
1146
            Consensus.Data.Local_state.create
1147
              ~context:(module Context)
1148
              ~genesis_ledger:
1149
                (Precomputed_values.genesis_ledger precomputed_values)
×
1150
              ~genesis_epoch_data:precomputed_values.genesis_epoch_data
1151
              ~epoch_ledger_location
1152
              ( Option.map block_production_keypair ~f:(fun keypair ->
1153
                    let open Keypair in
×
1154
                    Public_key.compress keypair.public_key )
1155
              |> Option.to_list |> Public_key.Compressed.Set.of_list )
×
1156
              ~genesis_state_hash:
1157
                precomputed_values.protocol_state_with_hashes.hash.state_hash
1158
          in
1159
          trace_database_initialization "epoch ledger" __LOC__
×
1160
            epoch_ledger_location ;
1161
          let%bind peer_list_file_contents_or_empty =
1162
            match libp2p_peer_list_file with
1163
            | None ->
×
1164
                return []
×
1165
            | Some file -> (
×
1166
                match%bind
1167
                  Monitor.try_with_or_error ~here:[%here] (fun () ->
×
1168
                      Reader.file_contents file )
×
1169
                with
1170
                | Ok contents ->
×
1171
                    return (Mina_net2.Multiaddr.of_file_contents contents)
×
1172
                | Error _ ->
×
1173
                    Mina_user_error.raisef
1174
                      ~where:"reading libp2p peer address file"
1175
                      "The file %s could not be read.\n\n\
1176
                       It must be a newline-separated list of libp2p \
1177
                       multiaddrs (ex: /ip4/IPADDR/tcp/PORT/p2p/PEERID)"
1178
                      file )
1179
          in
1180
          List.iter libp2p_peers_raw ~f:(fun raw_peer ->
×
1181
              if not Mina_net2.Multiaddr.(valid_as_peer @@ of_string raw_peer)
×
1182
              then
1183
                Mina_user_error.raisef ~where:"decoding peer as a multiaddress"
×
1184
                  "The given peer \"%s\" is not a valid multiaddress (ex: \
1185
                   /ip4/IPADDR/tcp/PORT/p2p/PEERID)"
1186
                  raw_peer ) ;
1187
          let initial_peers =
×
1188
            List.concat
1189
              [ List.map ~f:Mina_net2.Multiaddr.of_string libp2p_peers_raw
×
1190
              ; peer_list_file_contents_or_empty
1191
              ; List.map ~f:Mina_net2.Multiaddr.of_string
×
1192
                @@ or_from_config
×
1193
                     (Fn.compose Option.some
×
1194
                        (YJ.Util.convert_each YJ.Util.to_string) )
×
1195
                     "peers" None ~default:[]
1196
              ]
1197
          in
1198
          let direct_peers =
×
1199
            List.map ~f:Mina_net2.Multiaddr.of_string direct_peers_raw
1200
          in
1201
          let min_connections =
×
1202
            or_from_config YJ.Util.to_int_option "min-connections"
1203
              ~default:Cli_lib.Default.min_connections min_connections
1204
          in
1205
          let max_connections =
×
1206
            or_from_config YJ.Util.to_int_option "max-connections"
1207
              ~default:Cli_lib.Default.max_connections max_connections
1208
          in
1209
          let pubsub_v1 = Gossip_net.Libp2p.N in
×
1210
          (* TODO uncomment after introducing Bitswap-based block retrieval *)
1211
          (* let pubsub_v1 =
1212
               or_from_config to_pubsub_topic_mode_option "pubsub-v1"
1213
                 ~default:Cli_lib.Default.pubsub_v1 pubsub_v1
1214
             in *)
1215
          let pubsub_v0 =
1216
            or_from_config to_pubsub_topic_mode_option "pubsub-v0"
1217
              ~default:Cli_lib.Default.pubsub_v0 None
1218
          in
1219
          let validation_queue_size =
×
1220
            or_from_config YJ.Util.to_int_option "validation-queue-size"
1221
              ~default:Cli_lib.Default.validation_queue_size
1222
              validation_queue_size
1223
          in
1224
          let stop_time =
×
1225
            or_from_config YJ.Util.to_int_option "stop-time"
1226
              ~default:Cli_lib.Default.stop_time stop_time
1227
          in
1228
          if enable_tracing then Mina_tracing.start conf_dir |> don't_wait_for ;
×
1229
          let%bind () =
1230
            if enable_internal_tracing then
1231
              Internal_tracing.toggle ~commit_id:Mina_version.commit_id ~logger
×
1232
                `Enabled
1233
            else Deferred.unit
×
1234
          in
1235
          let seed_peer_list_url =
×
1236
            Option.value_map seed_peer_list_url ~f:Option.some
1237
              ~default:
1238
                (Option.bind config.daemon
×
1239
                   ~f:(fun { Runtime_config.Daemon.peer_list_url; _ } ->
1240
                     peer_list_url ) )
×
1241
          in
1242
          if is_seed then [%log info] "Starting node as a seed node"
×
1243
          else if demo_mode then [%log info] "Starting node in demo mode"
×
1244
          else if
×
1245
            List.is_empty initial_peers && Option.is_none seed_peer_list_url
×
1246
          then
1247
            Mina_user_error.raise
×
1248
              {|No peers were given.
1249

1250
Pass one of -peer, -peer-list-file, -seed, -peer-list-url.|} ;
1251
          let chain_id =
1252
            let protocol_transaction_version =
1253
              Protocol_version.(transaction current)
×
1254
            in
1255
            let protocol_network_version =
1256
              Protocol_version.(transaction current)
×
1257
            in
1258
            chain_id ~genesis_state_hash
1259
              ~genesis_constants:precomputed_values.genesis_constants
1260
              ~constraint_system_digests:
1261
                (Lazy.force precomputed_values.constraint_system_digests)
×
1262
              ~protocol_transaction_version ~protocol_network_version
1263
          in
1264
          [%log info] "Daemon will use chain id %s" chain_id ;
×
1265
          [%log info] "Daemon running protocol version %s"
×
1266
            Protocol_version.(to_string current) ;
×
1267
          let gossip_net_params =
×
1268
            Gossip_net.Libp2p.Config.
1269
              { timeout = Time.Span.of_sec 3.
×
1270
              ; logger
1271
              ; conf_dir
1272
              ; chain_id
1273
              ; unsafe_no_trust_ip = false
1274
              ; seed_peer_list_url =
1275
                  Option.map seed_peer_list_url ~f:Uri.of_string
×
1276
              ; initial_peers
1277
              ; addrs_and_ports
1278
              ; metrics_port = libp2p_metrics_port
1279
              ; trust_system
1280
              ; flooding = Option.value ~default:false enable_flooding
×
1281
              ; direct_peers
1282
              ; peer_protection_ratio
1283
              ; peer_exchange = Option.value ~default:false peer_exchange
×
1284
              ; min_connections
1285
              ; max_connections
1286
              ; validation_queue_size
1287
              ; isolate = Option.value ~default:false isolate
×
1288
              ; keypair = libp2p_keypair
1289
              ; all_peers_seen_metric
1290
              ; known_private_ip_nets =
1291
                  Option.value ~default:[] client_trustlist
×
1292
              ; time_controller
1293
              ; pubsub_v1
1294
              ; pubsub_v0
1295
              }
1296
          in
1297
          let net_config =
1298
            { Mina_networking.Config.genesis_ledger_hash
1299
            ; log_gossip_heard
1300
            ; is_seed
1301
            ; creatable_gossip_net =
1302
                Mina_networking.Gossip_net.(
1303
                  Any.Creatable
1304
                    ((module Libp2p), Libp2p.create ~pids gossip_net_params))
×
1305
            }
1306
          in
1307
          let coinbase_receiver : Consensus.Coinbase_receiver.t =
1308
            Option.value_map coinbase_receiver_flag ~default:`Producer
×
1309
              ~f:(fun pk -> `Other pk)
×
1310
          in
1311
          let proposed_protocol_version_opt =
1312
            Mina_run.get_proposed_protocol_version_opt ~conf_dir ~logger
1313
              proposed_protocol_version
1314
          in
1315
          ( match
×
1316
              (uptime_url_string, uptime_submitter_key, uptime_submitter_pubkey)
1317
            with
1318
          | Some _, Some _, None | Some _, None, Some _ | None, None, None ->
×
1319
              ()
1320
          | _ ->
×
1321
              Mina_user_error.raise
×
1322
                "Must provide both --uptime-url and exactly one of \
1323
                 --uptime-submitter-key or --uptime-submitter-pubkey" ) ;
1324
          let uptime_url =
1325
            Option.map uptime_url_string ~f:(fun s -> Uri.of_string s)
×
1326
          in
1327
          let uptime_submitter_opt =
×
1328
            Option.map uptime_submitter_pubkey ~f:(fun s ->
1329
                match Public_key.Compressed.of_base58_check s with
×
1330
                | Ok pk -> (
×
1331
                    match Public_key.decompress pk with
1332
                    | Some _ ->
×
1333
                        pk
1334
                    | None ->
×
1335
                        failwithf
1336
                          "Invalid public key %s for uptime submitter (could \
1337
                           not decompress)"
1338
                          s () )
1339
                | Error err ->
×
1340
                    Mina_user_error.raisef
1341
                      "Invalid public key %s for uptime submitter, %s" s
1342
                      (Error.to_string_hum err) () )
×
1343
          in
1344
          let%bind uptime_submitter_keypair =
1345
            match (uptime_submitter_key, uptime_submitter_opt) with
1346
            | None, None ->
×
1347
                return None
×
1348
            | None, Some pk ->
×
1349
                let%map kp =
1350
                  Secrets.Wallets.get_tracked_keypair ~logger
×
1351
                    ~which:"uptime submitter keypair"
1352
                    ~read_from_env_exn:
1353
                      (Secrets.Uptime_keypair.Terminal_stdin.read_exn
1354
                         ~should_prompt_user:false ~should_reask:false )
1355
                    ~conf_dir pk
1356
                in
1357
                Some kp
×
1358
            | Some sk_file, None ->
×
1359
                let%map kp =
1360
                  Secrets.Uptime_keypair.Terminal_stdin.read_exn
×
1361
                    ~should_prompt_user:false ~should_reask:false
1362
                    ~which:"uptime submitter keypair" sk_file
1363
                in
1364
                Some kp
×
1365
            | _ ->
×
1366
                (* unreachable, because of earlier check *)
1367
                failwith
1368
                  "Cannot provide both uptime submitter public key and uptime \
1369
                   submitter keyfile"
1370
          in
1371
          if compile_config.itn_features then
×
1372
            (* set queue bound directly in Itn_logger
1373
               adding bound to Mina_lib config introduces cycle
1374
            *)
1375
            Option.iter itn_max_logs ~f:Itn_logger.set_queue_bound ;
×
1376
          let start_time = Time.now () in
×
1377
          let%map mina =
1378
            Mina_lib.create ~commit_id:Mina_version.commit_id ~wallets
×
1379
              (Mina_lib.Config.make ~logger ~pids ~trust_system ~conf_dir
×
1380
                 ~chain_id ~is_seed ~super_catchup:(not no_super_catchup)
1381
                 ~disable_node_status ~demo_mode ~coinbase_receiver ~net_config
1382
                 ~gossip_net_params ~proposed_protocol_version_opt
1383
                 ~work_selection_method:
1384
                   (Cli_lib.Arg_type.work_selection_method_to_module
×
1385
                      work_selection_method )
1386
                 ~snark_worker_config:
1387
                   { Mina_lib.Config.Snark_worker_config
1388
                     .initial_snark_worker_key = run_snark_worker_flag
1389
                   ; shutdown_on_disconnect = true
1390
                   ; num_threads = snark_worker_parallelism_flag
1391
                   }
1392
                 ~snark_coordinator_key:run_snark_coordinator_flag
1393
                 ~snark_pool_disk_location:(conf_dir ^/ "snark_pool")
×
1394
                 ~wallets_disk_location:(conf_dir ^/ "wallets")
×
1395
                 ~persistent_root_location:(conf_dir ^/ "root")
×
1396
                 ~persistent_frontier_location:(conf_dir ^/ "frontier")
×
1397
                 ~epoch_ledger_location ~snark_work_fee:snark_work_fee_flag
1398
                 ~time_controller ~block_production_keypairs ~monitor
1399
                 ~consensus_local_state ~is_archive_rocksdb
1400
                 ~work_reassignment_wait ~archive_process_location
1401
                 ~log_block_creation ~precomputed_values ~start_time
1402
                 ?precomputed_blocks_path ~log_precomputed_blocks
1403
                 ~start_filtered_logs ~upload_blocks_to_gcloud
1404
                 ~block_reward_threshold ~uptime_url ~uptime_submitter_keypair
1405
                 ~uptime_send_node_commit ~stop_time ~node_status_url
1406
                 ~graphql_control_port:itn_graphql_port ~simplified_node_stats
1407
                 ~zkapp_cmd_limit:(ref compile_config.zkapp_cmd_limit)
1408
                 ~compile_config () )
1409
          in
1410
          { mina
×
1411
          ; client_trustlist
1412
          ; rest_server_port
1413
          ; limited_graphql_port
1414
          ; itn_graphql_port
1415
          }
1416
        in
1417
        (* Breaks a dependency cycle with monitor initilization and coda *)
1418
        let mina_ref : Mina_lib.t option ref = ref None in
1419
        Option.iter node_error_url ~f:(fun url ->
1420
            let get_node_state () =
×
1421
              match !mina_ref with
×
1422
              | None ->
×
1423
                  Deferred.return None
1424
              | Some mina ->
×
1425
                  let%map node_state = Mina_lib.get_node_state mina in
×
1426
                  Some node_state
×
1427
            in
1428
            Node_error_service.set_config ~get_node_state
1429
              ~node_error_url:(Uri.of_string url) ~contact_info ) ;
×
1430
        Mina_run.handle_shutdown ~monitor ~time_controller ~conf_dir
×
1431
          ~child_pids:pids ~top_logger:logger mina_ref ;
1432
        Async.Scheduler.within' ~monitor
×
1433
        @@ fun () ->
1434
        let%bind { mina
1435
                 ; client_trustlist
1436
                 ; rest_server_port
1437
                 ; limited_graphql_port
1438
                 ; itn_graphql_port
1439
                 } =
1440
          mina_initialization_deferred ()
×
1441
        in
1442
        mina_ref := Some mina ;
×
1443
        (*This pipe is consumed only by integration tests*)
1444
        don't_wait_for
1445
          (Pipe_lib.Strict_pipe.Reader.iter_without_pushback
×
1446
             (Mina_lib.validated_transitions mina)
×
1447
             ~f:ignore ) ;
1448
        Mina_run.setup_local_server ?client_trustlist ~rest_server_port
×
1449
          ~insecure_rest_server ~open_limited_graphql_port ?limited_graphql_port
1450
          ?itn_graphql_port ?auth_keys:itn_keys mina ;
1451
        let%bind () =
1452
          Option.map metrics_server_port ~f:(fun port ->
1453
              let forward_uri =
×
1454
                Option.map libp2p_metrics_port ~f:(fun port ->
1455
                    Uri.with_uri ~scheme:(Some "http") ~host:(Some "127.0.0.1")
×
1456
                      ~port:(Some port) ~path:(Some "/metrics") Uri.empty )
1457
              in
1458
              Mina_metrics.Runtime.(
×
1459
                gc_stat_interval_mins :=
1460
                  Option.value ~default:!gc_stat_interval_mins gc_stat_interval) ;
×
1461
              Mina_metrics.server ?forward_uri ~port ~logger () >>| ignore )
×
1462
          |> Option.value ~default:Deferred.unit
×
1463
        in
1464
        let () = Mina_plugins.init_plugins ~logger mina plugins in
×
1465
        return mina )
×
1466

1467
let daemon logger =
1468
  let compile_config = Mina_compile_config.Compiled.t in
3✔
1469
  Command.async ~summary:"Mina daemon"
1470
    (Command.Param.map
3✔
1471
       (setup_daemon logger ~itn_features:compile_config.itn_features
3✔
1472
          ~default_snark_worker_fee:compile_config.default_snark_worker_fee )
1473
       ~f:(fun setup_daemon () ->
1474
         (* Immediately disable updating the time offset. *)
1475
         Block_time.Controller.disable_setting_offset () ;
×
1476
         let%bind mina = setup_daemon () in
×
1477
         let%bind () = Mina_lib.start mina in
×
1478
         [%log info] "Daemon ready. Clients can now connect" ;
×
1479
         Async.never () ) )
×
1480

1481
let replay_blocks logger =
1482
  let replay_flag =
3✔
1483
    let open Command.Param in
1484
    flag "--blocks-filename" ~aliases:[ "-blocks-filename" ] (required string)
3✔
1485
      ~doc:"PATH The file to read the precomputed blocks from"
1486
  in
1487
  let read_kind =
1488
    let open Command.Param in
1489
    flag "--format" ~aliases:[ "-format" ] (optional string)
3✔
1490
      ~doc:"json|sexp The format to read lines of the file in (default: json)"
1491
  in
1492
  let compile_config = Mina_compile_config.Compiled.t in
1493
  Command.async ~summary:"Start mina daemon with blocks replayed from a file"
1494
    (Command.Param.map3 replay_flag read_kind
3✔
1495
       (setup_daemon logger ~itn_features:compile_config.itn_features
3✔
1496
          ~default_snark_worker_fee:compile_config.default_snark_worker_fee )
1497
       ~f:(fun blocks_filename read_kind setup_daemon () ->
1498
         (* Enable updating the time offset. *)
1499
         Block_time.Controller.enable_setting_offset () ;
×
1500
         let read_block_line =
×
1501
           match Option.map ~f:String.lowercase read_kind with
1502
           | Some "json" | None -> (
×
1503
               fun line ->
1504
                 match
×
1505
                   Yojson.Safe.from_string line
1506
                   |> Mina_block.Precomputed.of_yojson
×
1507
                 with
1508
                 | Ok block ->
×
1509
                     block
1510
                 | Error err ->
×
1511
                     failwithf "Could not read block: %s" err () )
1512
           | Some "sexp" ->
×
1513
               fun line ->
1514
                 Sexp.of_string_conv_exn line Mina_block.Precomputed.t_of_sexp
×
1515
           | _ ->
×
1516
               failwith "Expected one of 'json', 'sexp' for -format flag"
1517
         in
1518
         let blocks =
1519
           Sequence.unfold ~init:(In_channel.create blocks_filename)
×
1520
             ~f:(fun blocks_file ->
1521
               match In_channel.input_line blocks_file with
×
1522
               | Some line ->
×
1523
                   Some (read_block_line line, blocks_file)
×
1524
               | None ->
×
1525
                   In_channel.close blocks_file ;
1526
                   None )
×
1527
         in
1528
         let%bind mina = setup_daemon () in
×
1529
         let%bind () = Mina_lib.start_with_precomputed_blocks mina blocks in
×
1530
         [%log info]
×
1531
           "Daemon is ready, replayed precomputed blocks. Clients can now \
1532
            connect" ;
1533
         Async.never () ) )
×
1534

1535
let dump_type_shapes =
1536
  let max_depth_flag =
1537
    let open Command.Param in
1538
    flag "--max-depth" ~aliases:[ "-max-depth" ] (optional int)
3✔
1539
      ~doc:"NN Maximum depth of shape S-expressions"
1540
  in
1541
  Command.basic ~summary:"Print serialization shapes of versioned types"
3✔
1542
    (Command.Param.map max_depth_flag ~f:(fun max_depth () ->
3✔
1543
         Ppx_version_runtime.Shapes.iteri
×
1544
           ~f:(fun ~key:path ~data:(shape, ty_decl) ->
1545
             let open Bin_prot.Shape in
×
1546
             let canonical = eval shape in
1547
             let digest = Canonical.to_digest canonical |> Digest.to_hex in
×
1548
             let shape_summary =
×
1549
               let shape_sexp =
1550
                 Canonical.to_string_hum canonical |> Sexp.of_string
×
1551
               in
1552
               (* elide the shape below specified depth, so that changes to
1553
                  contained types aren't considered a change to the containing
1554
                  type, even though the shape digests differ
1555
               *)
1556
               let summary_sexp =
×
1557
                 match max_depth with
1558
                 | None ->
×
1559
                     shape_sexp
1560
                 | Some n ->
×
1561
                     let rec go sexp depth =
1562
                       if depth > n then Sexp.Atom "."
×
1563
                       else
1564
                         match sexp with
×
1565
                         | Sexp.Atom _ ->
×
1566
                             sexp
1567
                         | Sexp.List items ->
×
1568
                             Sexp.List
1569
                               (List.map items ~f:(fun item ->
×
1570
                                    go item (depth + 1) ) )
×
1571
                     in
1572
                     go shape_sexp 0
×
1573
               in
1574
               Sexp.to_string summary_sexp
×
1575
             in
1576
             Core_kernel.printf "%s, %s, %s, %s\n" path digest shape_summary
1577
               ty_decl ) ) )
1578

1579
let primitive_ok = function
1580
  | "array" | "bytes" | "string" | "bigstring" ->
×
1581
      false
1582
  | "int" | "int32" | "int64" | "nativeint" | "char" | "bool" | "float" ->
×
1583
      true
1584
  | "unit" | "option" | "list" ->
×
1585
      true
1586
  | "kimchi_backend_bigint_32_V1" ->
×
1587
      true
1588
  | "Bounded_types.String.t"
×
1589
  | "Bounded_types.String.Tagged.t"
×
1590
  | "Bounded_types.Array.t" ->
×
1591
      true
1592
  | "8fabab0a-4992-11e6-8cca-9ba2c4686d9e" ->
×
1593
      true (* hashtbl *)
1594
  | "ac8a9ff4-4994-11e6-9a1b-9fb4e933bd9d" ->
×
1595
      true (* Make_iterable_binable *)
1596
  | s ->
×
1597
      failwithf "unknown primitive %s" s ()
1598

1599
let audit_type_shapes : Command.t =
1600
  let rec shape_ok (shape : Sexp.t) : bool =
1601
    match shape with
×
1602
    | List [ Atom "Exp"; exp ] ->
×
1603
        exp_ok exp
1604
    | List [] ->
×
1605
        true
1606
    | _ ->
×
1607
        failwithf "bad shape: %s" (Sexp.to_string shape) ()
×
1608
  and exp_ok (exp : Sexp.t) : bool =
1609
    match exp with
×
1610
    | List [ Atom "Base"; Atom tyname; List exps ] ->
×
1611
        primitive_ok tyname && List.for_all exps ~f:shape_ok
×
1612
    | List [ Atom "Record"; List fields ] ->
×
1613
        List.for_all fields ~f:(fun field ->
1614
            match field with
×
1615
            | List [ Atom _; sh ] ->
×
1616
                shape_ok sh
1617
            | _ ->
×
1618
                failwithf "unhandled rec field: %s" (Sexp.to_string_hum field)
×
1619
                  () )
1620
    | List [ Atom "Tuple"; List exps ] ->
×
1621
        List.for_all exps ~f:shape_ok
1622
    | List [ Atom "Variant"; List ctors ] ->
×
1623
        List.for_all ctors ~f:(fun ctor ->
1624
            match ctor with
×
1625
            | List [ Atom _ctr; List exps ] ->
×
1626
                List.for_all exps ~f:shape_ok
1627
            | _ ->
×
1628
                failwithf "unhandled variant: %s" (Sexp.to_string_hum ctor) () )
×
1629
    | List [ Atom "Poly_variant"; List [ List [ Atom "sorted"; List ctors ] ] ]
×
1630
      ->
1631
        List.for_all ctors ~f:(fun ctor ->
1632
            match ctor with
×
1633
            | List [ Atom _ctr ] ->
×
1634
                true
1635
            | List [ Atom _ctr; List fields ] ->
×
1636
                List.for_all fields ~f:shape_ok
1637
            | _ ->
×
1638
                failwithf "unhandled poly variant: %s" (Sexp.to_string_hum ctor)
×
1639
                  () )
1640
    | List [ Atom "Application"; sh; List args ] ->
×
1641
        shape_ok sh && List.for_all args ~f:shape_ok
×
1642
    | List [ Atom "Rec_app"; Atom _; List args ] ->
×
1643
        List.for_all args ~f:shape_ok
1644
    | List [ Atom "Var"; Atom _ ] ->
×
1645
        true
1646
    | List (Atom ctr :: _) ->
×
1647
        failwithf "unhandled ctor (%s) in exp_ok: %s" ctr
1648
          (Sexp.to_string_hum exp) ()
×
1649
    | List [] | List _ | Atom _ ->
×
1650
        failwithf "bad format: %s" (Sexp.to_string_hum exp) ()
×
1651
  in
1652
  let handle_shape (path : string) (shape : Bin_prot.Shape.t) (ty_decl : string)
1653
      (good : int ref) (bad : int ref) =
1654
    let open Bin_prot.Shape in
×
1655
    let path, file = String.lsplit2_exn ~on:':' path in
1656
    let canonical = eval shape in
×
1657
    let shape_sexp = Canonical.to_string_hum canonical |> Sexp.of_string in
×
1658
    if not @@ shape_ok shape_sexp then (
×
1659
      incr bad ;
1660
      Core.eprintf "%s has a bad shape in %s (%s):\n%s\n" path file ty_decl
×
1661
        (Canonical.to_string_hum canonical) )
×
1662
    else incr good
×
1663
  in
1664
  Command.basic ~summary:"Audit shapes of versioned types"
3✔
1665
    (Command.Param.return (fun () ->
3✔
1666
         let bad, good = (ref 0, ref 0) in
×
1667
         Ppx_version_runtime.Shapes.iteri
1668
           ~f:(fun ~key:path ~data:(shape, ty_decl) ->
1669
             handle_shape path shape ty_decl good bad ) ;
×
1670
         Core.printf "good shapes:\n\t%d\nbad shapes:\n\t%d\n%!" !good !bad ;
1671
         if !bad > 0 then Core.exit 1 ) )
×
1672

1673
(*NOTE A previous version of this function included compile time ppx that didn't compile, and was never
1674
  evaluated under any build profile
1675
*)
1676
let ensure_testnet_id_still_good _ = Deferred.unit
3✔
1677

1678
let snark_hashes =
1679
  let module Hashes = struct
1680
    type t = string list [@@deriving to_yojson]
×
1681
  end in
1682
  let open Command.Let_syntax in
1683
  Command.basic ~summary:"List hashes of proving and verification keys"
3✔
1684
    [%map_open
1685
      let json = Cli_lib.Flag.json in
1686
      fun () -> if json then Core.printf "[]\n%!"]
×
1687

1688
let internal_commands logger =
1689
  [ ( Snark_worker.Intf.command_name
3✔
1690
    , Snark_worker.command ~proof_level:Genesis_constants.Compiled.proof_level
1691
        ~constraint_constants:Genesis_constants.Compiled.constraint_constants
1692
        ~commit_id:Mina_version.commit_id )
1693
  ; ("snark-hashes", snark_hashes)
1694
  ; ( "run-prover"
1695
    , Command.async
3✔
1696
        ~summary:"Run prover on a sexp provided on a single line of stdin"
1697
        (Command.Param.return (fun () ->
3✔
1698
             let logger = Logger.create () in
×
1699
             let constraint_constants =
×
1700
               Genesis_constants.Compiled.constraint_constants
1701
             in
1702
             let proof_level = Genesis_constants.Compiled.proof_level in
1703
             Parallel.init_master () ;
1704
             match%bind Reader.read_sexp (Lazy.force Reader.stdin) with
×
1705
             | `Ok sexp ->
×
1706
                 let%bind conf_dir = Unix.mkdtemp "/tmp/mina-prover" in
×
1707
                 [%log info] "Prover state being logged to %s" conf_dir ;
×
1708
                 let%bind prover =
1709
                   Prover.create ~commit_id:Mina_version.commit_id ~logger
×
1710
                     ~proof_level ~constraint_constants
1711
                     ~pids:(Pid.Table.create ()) ~conf_dir ()
×
1712
                 in
1713
                 Prover.prove_from_input_sexp prover sexp >>| ignore
×
1714
             | `Eof ->
×
1715
                 failwith "early EOF while reading sexp" ) ) )
1716
  ; ( "run-snark-worker-single"
1717
    , Command.async
3✔
1718
        ~summary:"Run snark-worker on a sexp provided on a single line of stdin"
1719
        (let open Command.Let_syntax in
1720
        let%map_open filename =
1721
          flag "--file" (required string)
3✔
1722
            ~doc:"File containing the s-expression of the snark work to execute"
1723
        in
1724
        fun () ->
1725
          let open Deferred.Let_syntax in
×
1726
          let logger = Logger.create () in
1727
          let constraint_constants =
×
1728
            Genesis_constants.Compiled.constraint_constants
1729
          in
1730
          let proof_level = Genesis_constants.Compiled.proof_level in
1731
          Parallel.init_master () ;
1732
          match%bind
1733
            Reader.with_file filename ~f:(fun reader ->
×
1734
                [%log info] "Created reader for %s" filename ;
×
1735
                Reader.read_sexp reader )
×
1736
          with
1737
          | `Ok sexp -> (
×
1738
              let%bind worker_state =
1739
                Snark_worker.Prod.Inputs.Worker_state.create ~proof_level
×
1740
                  ~constraint_constants ()
1741
              in
1742
              let sok_message =
×
1743
                { Mina_base.Sok_message.fee = Currency.Fee.of_mina_int_exn 0
×
1744
                ; prover = Quickcheck.random_value Public_key.Compressed.gen
×
1745
                }
1746
              in
1747
              let spec =
1748
                [%of_sexp:
1749
                  ( Transaction_witness.t
1750
                  , Ledger_proof.t )
1751
                  Snark_work_lib.Work.Single.Spec.t] sexp
1752
              in
1753
              match%map
1754
                Snark_worker.Prod.Inputs.perform_single worker_state
×
1755
                  ~message:sok_message spec
1756
              with
1757
              | Ok _ ->
×
1758
                  [%log info] "Successfully worked"
×
1759
              | Error err ->
×
1760
                  [%log error] "Work didn't work: $err"
×
1761
                    ~metadata:[ ("err", Error_json.error_to_yojson err) ] )
×
1762
          | `Eof ->
×
1763
              failwith "early EOF while reading sexp") )
1764
  ; ( "run-verifier"
1765
    , Command.async
3✔
1766
        ~summary:"Run verifier on a proof provided on a single line of stdin"
1767
        (let open Command.Let_syntax in
1768
        let%map_open mode =
1769
          flag "--mode" ~aliases:[ "-mode" ] (required string)
3✔
1770
            ~doc:"transaction/blockchain the snark to verify. Defaults to json"
1771
        and format =
1772
          flag "--format" ~aliases:[ "-format" ] (optional string)
3✔
1773
            ~doc:"sexp/json the format to parse input in"
1774
        and limit =
1775
          flag "--limit" ~aliases:[ "-limit" ] (optional int)
3✔
1776
            ~doc:"limit the number of proofs taken from the file"
1777
        in
1778
        fun () ->
1779
          let open Async in
×
1780
          let logger = Logger.create () in
1781
          let constraint_constants =
×
1782
            Genesis_constants.Compiled.constraint_constants
1783
          in
1784
          let proof_level = Genesis_constants.Compiled.proof_level in
1785
          Parallel.init_master () ;
1786
          let%bind conf_dir = Unix.mkdtemp "/tmp/mina-verifier" in
×
1787
          let mode =
×
1788
            match mode with
1789
            | "transaction" ->
×
1790
                `Transaction
1791
            | "blockchain" ->
×
1792
                `Blockchain
1793
            | mode ->
×
1794
                failwithf
×
1795
                  "Expected mode flag to be one of transaction, blockchain, \
1796
                   got '%s'"
1797
                  mode ()
1798
          in
1799
          let format =
1800
            match format with
1801
            | Some "sexp" ->
×
1802
                `Sexp
1803
            | Some "json" | None ->
×
1804
                `Json
1805
            | Some format ->
×
1806
                failwithf
×
1807
                  "Expected format flag to be one of sexp, json, got '%s'"
1808
                  format ()
1809
          in
1810
          let%bind input =
1811
            match format with
1812
            | `Sexp -> (
×
1813
                let%map input_sexp =
1814
                  match%map Reader.read_sexp (Lazy.force Reader.stdin) with
×
1815
                  | `Ok input_sexp ->
×
1816
                      input_sexp
1817
                  | `Eof ->
×
1818
                      failwith "early EOF while reading sexp"
1819
                in
1820
                match mode with
×
1821
                | `Transaction ->
×
1822
                    `Transaction
1823
                      (List.t_of_sexp
×
1824
                         (Tuple2.t_of_sexp Ledger_proof.t_of_sexp
×
1825
                            Sok_message.t_of_sexp )
1826
                         input_sexp )
1827
                | `Blockchain ->
×
1828
                    `Blockchain
1829
                      (List.t_of_sexp Blockchain_snark.Blockchain.t_of_sexp
×
1830
                         input_sexp ) )
1831
            | `Json -> (
×
1832
                let%map input_line =
1833
                  match%map Reader.read_line (Lazy.force Reader.stdin) with
×
1834
                  | `Ok input_line ->
×
1835
                      input_line
1836
                  | `Eof ->
×
1837
                      failwith "early EOF while reading json"
1838
                in
1839
                match mode with
×
1840
                | `Transaction -> (
×
1841
                    match
1842
                      [%derive.of_yojson: (Ledger_proof.t * Sok_message.t) list]
×
1843
                        (Yojson.Safe.from_string input_line)
×
1844
                    with
1845
                    | Ok input ->
×
1846
                        `Transaction input
1847
                    | Error err ->
×
1848
                        failwithf "Could not parse JSON: %s" err () )
1849
                | `Blockchain -> (
×
1850
                    match
1851
                      [%derive.of_yojson: Blockchain_snark.Blockchain.t list]
×
1852
                        (Yojson.Safe.from_string input_line)
×
1853
                    with
1854
                    | Ok input ->
×
1855
                        `Blockchain input
1856
                    | Error err ->
×
1857
                        failwithf "Could not parse JSON: %s" err () ) )
1858
          in
1859

1860
          let%bind verifier =
1861
            Verifier.For_tests.default ~constraint_constants ~proof_level
×
1862
              ~commit_id:Mina_version.commit_id ~logger
1863
              ~pids:(Pid.Table.create ()) ~conf_dir:(Some conf_dir) ()
×
1864
          in
1865
          let%bind result =
1866
            let cap lst =
1867
              Option.value_map ~default:Fn.id ~f:(Fn.flip List.take) limit lst
×
1868
            in
1869
            match input with
1870
            | `Transaction input ->
×
1871
                input |> cap |> Verifier.verify_transaction_snarks verifier
×
1872
            | `Blockchain input ->
×
1873
                input |> cap |> Verifier.verify_blockchain_snarks verifier
×
1874
          in
1875
          match result with
×
1876
          | Ok (Ok ()) ->
×
1877
              printf "Proofs verified successfully" ;
1878
              exit 0
×
1879
          | Ok (Error err) ->
×
1880
              printf "Proofs failed to verify:\n%s\n"
1881
                (Yojson.Safe.pretty_to_string (Error_json.error_to_yojson err)) ;
×
1882
              exit 1
×
1883
          | Error err ->
×
1884
              printf "Failed while verifying proofs:\n%s"
1885
                (Error.to_string_hum err) ;
×
1886
              exit 2) )
×
1887
  ; ( "dump-structured-events"
1888
    , Command.async ~summary:"Dump the registered structured events"
3✔
1889
        (let open Command.Let_syntax in
1890
        let%map outfile =
1891
          Core_kernel.Command.Param.flag "--out-file" ~aliases:[ "-out-file" ]
3✔
1892
            (Core_kernel.Command.Flag.optional Core_kernel.Command.Param.string)
3✔
1893
            ~doc:"FILENAME File to output to. Defaults to stdout"
1894
        and pretty =
1895
          Core_kernel.Command.Param.flag "--pretty" ~aliases:[ "-pretty" ]
3✔
1896
            Core_kernel.Command.Param.no_arg
1897
            ~doc:"  Set to output 'pretty' JSON"
1898
        in
1899
        fun () ->
1900
          let out_channel =
×
1901
            match outfile with
1902
            | Some outfile ->
×
1903
                Core_kernel.Out_channel.create outfile
×
1904
            | None ->
×
1905
                Core_kernel.Out_channel.stdout
1906
          in
1907
          let json =
1908
            Structured_log_events.dump_registered_events ()
1909
            |> [%derive.to_yojson:
1910
                 (string * Structured_log_events.id * string list) list]
×
1911
          in
1912
          if pretty then Yojson.Safe.pretty_to_channel out_channel json
×
1913
          else Yojson.Safe.to_channel out_channel json ;
×
1914
          ( match outfile with
1915
          | Some _ ->
×
1916
              Core_kernel.Out_channel.close out_channel
×
1917
          | None ->
×
1918
              () ) ;
1919
          Deferred.return ()) )
1920
  ; ("dump-type-shapes", dump_type_shapes)
1921
  ; ("replay-blocks", replay_blocks logger)
3✔
1922
  ; ("audit-type-shapes", audit_type_shapes)
1923
  ; ( "test-genesis-block-generation"
1924
    , Command.async ~summary:"Generate a genesis proof"
3✔
1925
        (let open Command.Let_syntax in
1926
        let%map_open config_files =
1927
          flag "--config-file" ~aliases:[ "config-file" ]
3✔
1928
            ~doc:
1929
              "PATH path to a configuration file (overrides MINA_CONFIG_FILE, \
1930
               default: <config_dir>/daemon.json). Pass multiple times to \
1931
               override fields from earlier config files"
1932
            (listed string)
3✔
1933
        and conf_dir = Cli_lib.Flag.conf_dir
1934
        and genesis_dir =
1935
          flag "--genesis-ledger-dir" ~aliases:[ "genesis-ledger-dir" ]
3✔
1936
            ~doc:
1937
              "DIR Directory that contains the genesis ledger and the genesis \
1938
               blockchain proof (default: <config-dir>)"
1939
            (optional string)
3✔
1940
        in
1941
        fun () ->
1942
          let open Deferred.Let_syntax in
×
1943
          Parallel.init_master () ;
1944
          let logger = Logger.create () in
×
1945
          let conf_dir = Mina_lib.Conf_dir.compute_conf_dir conf_dir in
×
1946
          let genesis_constants =
×
1947
            Genesis_constants.Compiled.genesis_constants
1948
          in
1949
          let constraint_constants =
1950
            Genesis_constants.Compiled.constraint_constants
1951
          in
1952
          let proof_level = Genesis_constants.Proof_level.Full in
1953
          let config_files =
1954
            List.map config_files ~f:(fun config_file ->
1955
                (config_file, `Must_exist) )
×
1956
          in
1957
          let%bind precomputed_values, _config_jsons, _config =
1958
            load_config_files ~logger ~conf_dir ~genesis_dir ~genesis_constants
×
1959
              ~constraint_constants ~proof_level config_files
1960
              ~cli_proof_level:None
1961
          in
1962
          let pids = Child_processes.Termination.create_pid_table () in
×
1963
          let%bind prover =
1964
            (* We create a prover process (unnecessarily) here, to have a more
1965
               realistic test.
1966
            *)
1967
            Prover.create ~commit_id:Mina_version.commit_id ~logger ~pids
×
1968
              ~conf_dir ~proof_level
1969
              ~constraint_constants:precomputed_values.constraint_constants ()
1970
          in
1971
          match%bind
1972
            Prover.create_genesis_block prover
×
1973
              (Genesis_proof.to_inputs precomputed_values)
×
1974
          with
1975
          | Ok block ->
×
1976
              Format.eprintf "Generated block@.%s@."
1977
                ( Yojson.Safe.to_string
×
1978
                @@ Blockchain_snark.Blockchain.to_yojson block ) ;
×
1979
              exit 0
×
1980
          | Error err ->
×
1981
              Format.eprintf "Failed to generate block@.%s@."
1982
                (Yojson.Safe.to_string @@ Error_json.error_to_yojson err) ;
×
1983
              exit 1) )
×
1984
  ]
1985

1986
let mina_commands logger ~itn_features =
1987
  [ ("accounts", Client.accounts)
3✔
1988
  ; ("daemon", daemon logger)
3✔
1989
  ; ("client", Client.client)
1990
  ; ("advanced", Client.advanced ~itn_features)
1991
  ; ("ledger", Client.ledger)
1992
  ; ("libp2p", Client.libp2p)
1993
  ; ( "internal"
1994
    , Command.group ~summary:"Internal commands" (internal_commands logger) )
3✔
1995
  ; (Parallel.worker_command_name, Parallel.worker_command)
1996
  ; ("transaction-snark-profiler", Transaction_snark_profiler.command)
1997
  ]
1998

1999
let print_version_help coda_exe version =
2000
  (* mimic Jane Street command help *)
2001
  let lines =
×
2002
    [ "print version information"
2003
    ; ""
2004
    ; sprintf "  %s %s" (Filename.basename coda_exe) version
×
2005
    ; ""
2006
    ; "=== flags ==="
2007
    ; ""
2008
    ; "  [-help]  print this help text and exit"
2009
    ; "           (alias: -?)"
2010
    ]
2011
  in
2012
  List.iter lines ~f:(Core.printf "%s\n%!")
×
2013

2014
let print_version_info () = Core.printf "Commit %s\n" Mina_version.commit_id
×
2015

2016
let () =
2017
  Random.self_init () ;
2018
  let compile_config = Mina_compile_config.Compiled.t in
3✔
2019
  let logger = Logger.create ~itn_features:compile_config.itn_features () in
2020
  don't_wait_for (ensure_testnet_id_still_good logger) ;
3✔
2021
  (* Turn on snark debugging in prod for now *)
2022
  Snarky_backendless.Snark.set_eval_constraints true ;
3✔
2023
  (* intercept command-line processing for "version", because we don't
2024
     use the Jane Street scripts that generate their version information
2025
  *)
2026
  (let is_version_cmd s =
3✔
2027
     List.mem [ "version"; "-version"; "--version" ] s ~equal:String.equal
×
2028
   in
2029
   match Sys.get_argv () with
2030
   | [| _mina_exe; version |] when is_version_cmd version ->
×
2031
       Mina_version.print_version ()
×
2032
   | _ ->
3✔
2033
       Command.run
×
2034
         (Command.group ~summary:"Mina" ~preserve_subcommand_order:()
3✔
2035
            (mina_commands logger ~itn_features:compile_config.itn_features) )
3✔
2036
  ) ;
2037
  Core.exit 0
×
2038

2039
let linkme = ()
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