Skip to content

Commit 236d4b7

Browse files
committed
adding missing daemon functionalitites
1 parent c2f8f3c commit 236d4b7

File tree

3 files changed

+202
-30
lines changed

3 files changed

+202
-30
lines changed

src/test/mina_automation/daemon.ml

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,35 @@ end
1313

1414
module Executor = Executor.Make (Paths)
1515

16-
type t = Executor.t
17-
1816
let logger = Logger.create ()
1917

18+
let path () =
19+
Deferred.map Executor.PathFinder.standalone_path ~f:(fun opt ->
20+
Option.value_exn opt
21+
~message:
22+
"Could not find released mina daemon environment. App is not \
23+
executable outside the dune" )
24+
2025
(**
2126
Module [Client] provides functions to interact with a Mina daemon.
2227
*)
2328
module Client = struct
2429
type t = { port : int; executor : Executor.t }
2530

26-
let create ?(port = 3085) ?(executor = Executor.AutoDetect) () =
31+
let create ?(port = 8031) ?(executor = Executor.AutoDetect) () =
2732
{ port; executor }
2833

2934
(** [stop_daemon t] stops the daemon running on the specified port.
3035
@param t The daemon instance containing the executor and port information.
3136
@return Unit. Executes the command to stop the daemon using the executor.
3237
*)
33-
let stop_daemon t =
34-
Executor.run t.executor
35-
~args:[ "client"; "stop-daemon"; "-daemon-port"; sprintf "%d" t.port ]
36-
()
38+
let stop_daemon t : unit Deferred.t =
39+
let%map _ =
40+
Executor.run t.executor
41+
~args:[ "client"; "stop-daemon"; "-daemon-port"; sprintf "%d" t.port ]
42+
()
43+
in
44+
()
3745

3846
(** [daemon_status t] retrieves the status of the daemon running on the specified port.
3947
It executes the command `client status -daemon-port <port>` using the provided executor.
@@ -53,8 +61,8 @@ module Client = struct
5361
@param retry_attempts The number of retries.
5462
@return A deferred result indicating the success or failure of the operation.
5563
*)
56-
let wait_for_bootstrap t ?(client_delay = 40.) ?(retry_delay = 60.)
57-
?(retry_attempts = 10) () =
64+
let wait_for_bootstrap t ?(client_delay = 60.) ?(retry_delay = 60.)
65+
?(retry_attempts = 40) () =
5866
Async.printf "Waiting initial %d s. before connecting\n"
5967
(int_of_float client_delay) ;
6068
let%bind _ =
@@ -74,6 +82,33 @@ module Client = struct
7482
else Deferred.Or_error.error_string output
7583
in
7684
go retry_attempts
85+
86+
let ledger_hash t ~ledger_file =
87+
Executor.run t.executor
88+
~args:[ "ledger"; "hash"; "--ledger-file"; ledger_file ]
89+
()
90+
91+
let ledger_currency t ~ledger_file =
92+
Executor.run t.executor
93+
~args:[ "ledger"; "currency"; "--ledger-file"; ledger_file ]
94+
()
95+
96+
let test_ledger t ~(n : int) =
97+
Executor.run t.executor
98+
~args:[ "ledger"; "test"; "generate-accounts"; "-n"; string_of_int n ]
99+
()
100+
101+
let advanced_print_signature_kind t =
102+
Executor.run t.executor ~args:[ "advanced"; "print-signature-kind" ] ()
103+
104+
let advanced_compile_time_constants t ~config_file =
105+
Executor.run t.executor
106+
~env:(`Extend [ ("MINA_CONFIG_FILE", config_file) ])
107+
~args:[ "advanced"; "compile-time-constants" ]
108+
()
109+
110+
let advanced_constraint_system_digests t =
111+
Executor.run t.executor ~args:[ "advanced"; "constraint-system-digests" ] ()
77112
end
78113

79114
module Config = struct
@@ -90,6 +125,8 @@ module Config = struct
90125
let create ?(root_path = "/tmp") ?(config_dir = "mina_spun_test")
91126
?(genesis_dir = "mina_genesis_state")
92127
?(p2p_dir = "mina_test_libp2p_keypair") () =
128+
Unix.putenv ~key:"MINA_LIBP2P_PASS" ~data:"naughty blue worm" ;
129+
Unix.putenv ~key:"MINA_PRIVKEY_PASS" ~data:"naughty blue worm" ;
93130
(* create empty config dir to avoid any issues with the default config dir *)
94131
let conf = Filename.temp_dir ~in_dir:root_path config_dir "" in
95132
let genesis = Filename.temp_dir ~in_dir:root_path genesis_dir "" in
@@ -103,9 +140,9 @@ module Config = struct
103140

104141
type t = { port : int; dirs : ConfigDirs.t }
105142

106-
let default ?dirs =
143+
let default ?dirs () =
107144
let root_path = Filename.temp_dir ~in_dir:"/tmp" "mina_automation" "" in
108-
{ port = 3085
145+
{ port = 8031
109146
; dirs =
110147
( match dirs with
111148
| Some dirs ->
@@ -117,11 +154,7 @@ module Config = struct
117154
let libp2p_keypair_folder t = ConfigDirs.libp2p_keypair_folder t.dirs
118155

119156
let generate_keys t =
120-
let open Deferred.Let_syntax in
121-
let%map () =
122-
Init.Client.generate_libp2p_keypair_do (libp2p_keypair_folder t) ()
123-
in
124-
()
157+
Init.Client.generate_libp2p_keypair_do (libp2p_keypair_folder t) ()
125158
end
126159

127160
(**
@@ -137,53 +170,67 @@ module Process = struct
137170
let force_kill t = Utils.force_kill t.process
138171
end
139172

140-
let archive_blocks t ~archive_address ~(format : Archive_blocks.format) blocks =
173+
let archive_blocks t ~archive_address ~format blocks =
174+
let format_arg =
175+
match format with
176+
| `Precomputed ->
177+
"--precomputed"
178+
| `Extensional ->
179+
"--extensional"
180+
in
141181
Executor.run t
142182
~args:
143183
( [ "advanced"
144184
; "archive-blocks"
145185
; "--archive-addres"
146186
; string_of_int archive_address
147-
; "-" ^ Archive_blocks.format_to_string format
187+
; format_arg
148188
]
149189
@ blocks )
150190

151-
let archive_blocks_from_files t ~archive_address
152-
~(format : Archive_blocks.format) ?(sleep = 5) blocks =
191+
let archive_blocks_from_files t ~archive_address ~format ?(sleep = 5) blocks =
153192
Deferred.List.iter blocks ~f:(fun block ->
154193
let%bind _ = archive_blocks t ~archive_address ~format [ block ] () in
155194
after (Time.Span.of_sec (Float.of_int sleep)) )
156195

157-
let default = Executor.default
196+
type t = { config : Config.t; executor : Executor.t }
158197

159-
let client t ~(config : Config.t) = Client.create ~port:config.port t
198+
let of_config config = { config; executor = Executor.AutoDetect }
160199

161-
let start t (config : Config.t) =
200+
let default () = { config = Config.default (); executor = Executor.AutoDetect }
201+
202+
let client t = Client.create ~port:t.config.port ~executor:t.executor ()
203+
204+
let start t =
205+
let open Deferred.Let_syntax in
162206
let args =
163207
[ "daemon"
164208
; "--seed"
165209
; "--demo-mode"
166-
; "-background"
210+
; "--background"
167211
; "--working-dir"
168212
; "."
169213
; "--client-port"
170-
; sprintf "%d" config.port
214+
; string_of_int t.config.port
171215
; "--config-directory"
172-
; config.dirs.conf
216+
; t.config.dirs.conf
173217
; "--genesis-ledger-dir"
174-
; config.dirs.genesis
218+
; t.config.dirs.genesis
175219
; "--external-ip"
176220
; "0.0.0.0"
177221
; "--libp2p-keypair"
178-
; Config.libp2p_keypair_folder config
222+
; Config.libp2p_keypair_folder t.config
179223
]
180224
in
181225

182226
[%log debug] "Starting daemon" ;
183227

184-
let%bind _, process = Executor.run_in_background t ~args () in
228+
let%bind _, process = Executor.run_in_background t.executor ~args () in
185229

186230
let mina_process : Process.t =
187-
{ config; process; client = Client.create ~port:config.port ~executor:t () }
231+
{ config = t.config
232+
; process
233+
; client = Client.create ~port:t.config.port ~executor:t.executor ()
234+
}
188235
in
189236
Deferred.return mina_process
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
open Async
2+
open Core
3+
open Mina_automation
4+
open Intf
5+
6+
let logger = Logger.create ()
7+
8+
type after_bootstrap = { daemon : Daemon.Process.t; temp_dir : string }
9+
10+
type before_bootstrap = { config : Daemon.Config.t; temp_dir : string }
11+
12+
let generate_random_accounts t output =
13+
let client = Daemon.client t in
14+
let%map ledger_content = Daemon.Client.test_ledger client ~n:10 in
15+
let accounts =
16+
Yojson.Safe.from_string ledger_content
17+
|> Runtime_config.Accounts.of_yojson |> Result.ok_or_failwith
18+
in
19+
Runtime_config.Accounts.to_yojson accounts |> Yojson.Safe.to_file output ;
20+
accounts
21+
22+
let generate_random_config t output =
23+
let client = Daemon.client t in
24+
let%map ledger_content = Daemon.Client.test_ledger client ~n:10 in
25+
let accounts =
26+
Yojson.Safe.from_string ledger_content
27+
|> Runtime_config.Accounts.of_yojson |> Result.ok_or_failwith
28+
in
29+
let ledger : Runtime_config.Ledger.t =
30+
{ base = Accounts accounts
31+
; num_accounts = None
32+
; balances = []
33+
; hash = None
34+
; s3_data_hash = None
35+
; name = None
36+
; add_genesis_winner = Some true
37+
}
38+
in
39+
let runtime_config = Runtime_config.make ~ledger () in
40+
Runtime_config.to_yojson runtime_config |> Yojson.Safe.to_file output
41+
42+
module type TestCaseWithBootstrap = TestCase with type t = after_bootstrap
43+
44+
module type TestCaseWithoutBootstrap = TestCase with type t = before_bootstrap
45+
46+
module Make_FixtureWithBootstrap (M : TestCaseWithBootstrap) :
47+
Fixture with type t = after_bootstrap = struct
48+
type t = after_bootstrap
49+
50+
let test_case = M.test_case
51+
52+
(**
53+
Sets up the daemon by performing the following steps:
54+
1. Retrieves the network data folder path from the environment variable "MINA_TEST_NETWORK_DATA".
55+
(This variable must be set before running the test.)
56+
2. Creates network data using the retrieved folder path.
57+
3. Sets up a connection using the created network data.
58+
59+
@return A record containing the started daemon and the network data.
60+
*)
61+
let setup () =
62+
let config = Daemon.Config.default () in
63+
let executor = Daemon.of_config config in
64+
let ledger_file = config.dirs.conf ^/ "daemon.json" in
65+
let%bind () = generate_random_config executor ledger_file in
66+
[%log info] "Starting daemon" ;
67+
let%bind daemon = Daemon.start executor in
68+
[%log info] "Daemon started successfully" ;
69+
Deferred.Or_error.return
70+
{ daemon; temp_dir = Filename.temp_dir "daemon_test" "" }
71+
72+
let teardown t =
73+
let open Deferred.Or_error.Let_syntax in
74+
[%log info] "Tearing down daemon" ;
75+
let%bind _ = Daemon.Process.force_kill t.daemon in
76+
let%bind.Deferred () =
77+
Mina_stdlib_unix.File_system.remove_dir @@ t.temp_dir
78+
in
79+
[%log info] "Daemon teardown completed" ;
80+
Deferred.Or_error.ok_unit
81+
82+
let on_test_fail (t : t) =
83+
let%map contents = Process.stdout t.daemon.process |> Reader.contents in
84+
[%log debug] "Daemon process output: %s" contents ;
85+
()
86+
end
87+
88+
module Make_FixtureWithoutBootstrap (M : TestCaseWithoutBootstrap) :
89+
Fixture with type t = before_bootstrap = struct
90+
type t = before_bootstrap
91+
92+
let test_case = M.test_case
93+
94+
let setup () =
95+
Deferred.Or_error.return
96+
{ config = Daemon.Config.default ()
97+
; temp_dir = Filename.temp_dir "daemon_test" ""
98+
}
99+
100+
let teardown _t = Deferred.Or_error.ok_unit
101+
102+
let on_test_fail _t = Deferred.unit
103+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
open Mina_automation
2+
open Intf
3+
open Async
4+
5+
type after_bootstrap = { daemon : Daemon.Process.t; temp_dir : string }
6+
7+
type before_bootstrap = { config : Daemon.Config.t; temp_dir : string }
8+
9+
val generate_random_accounts :
10+
Daemon.t -> string -> Runtime_config.Accounts.t Deferred.t
11+
12+
val generate_random_config : Daemon.t -> string -> unit Deferred.t
13+
14+
module type TestCaseWithBootstrap = TestCase with type t = after_bootstrap
15+
16+
module type TestCaseWithoutBootstrap = TestCase with type t = before_bootstrap
17+
18+
module Make_FixtureWithBootstrap (M : TestCaseWithBootstrap) :
19+
Fixture with type t = after_bootstrap
20+
21+
module Make_FixtureWithoutBootstrap (M : TestCaseWithoutBootstrap) :
22+
Fixture with type t = before_bootstrap

0 commit comments

Comments
 (0)