Skip to content

Commit debdaf9

Browse files
committed
Snark Work Lib: remove data from partitioned spec; factor out a common
Id.Any type
1 parent 3787b67 commit debdaf9

File tree

11 files changed

+114
-181
lines changed

11 files changed

+114
-181
lines changed

src/app/cli/src/init/mina_run.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ let setup_local_server ?(client_trustlist = []) ?rest_server_port
387387
[%log debug] "responding to a Get_work request with some new work"
388388
~metadata:
389389
[ ( "work_id"
390-
, Snark_work_lib.Spec.Partitioned.Poly.id_to_json spec )
390+
, Snark_work_lib.(
391+
Spec.Partitioned.Poly.get_id spec |> Id.Any.to_yojson)
392+
)
391393
] ;
392394

393395
Mina_metrics.(Counter.inc_one Snark_work.snark_work_assigned_rpc) ;
@@ -407,11 +409,7 @@ let setup_local_server ?(client_trustlist = []) ?rest_server_port
407409
; implement Snark_worker.Rpcs_versioned.Submit_work.Latest.rpc
408410
(fun () (result : Snark_work_lib.Result.Partitioned.Stable.Latest.t) ->
409411
[%log debug] "received completed work from a snark worker"
410-
~metadata:
411-
[ ( "work_id"
412-
, Snark_work_lib.Result.Partitioned.Stable.Latest.id_to_json
413-
result )
414-
] ;
412+
~metadata:[ ("work_id", Snark_work_lib.Id.Any.to_yojson result.id) ] ;
415413
Mina_metrics.(
416414
Counter.inc_one Snark_work.completed_snark_work_received_rpc) ;
417415
Deferred.return @@ Mina_lib.add_work mina result )

src/lib/snark_work_lib/id.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ module Sub_zkapp = struct
5353
let to_single ({ which_one; pairing_id; _ } : t) : Single.t =
5454
{ which_one; pairing_id }
5555
end
56+
57+
module Any = struct
58+
[%%versioned
59+
module Stable = struct
60+
module V1 = struct
61+
type t =
62+
| Single of Single.Stable.V1.t
63+
| Sub_zkapp of Sub_zkapp.Stable.V1.t
64+
[@@deriving compare, hash, sexp, yojson, equal]
65+
66+
let to_latest = Fn.id
67+
end
68+
end]
69+
end

src/lib/snark_work_lib/id.mli

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@ module Sub_zkapp : sig
5151

5252
val to_single : t -> Single.t
5353
end
54+
55+
module Any : sig
56+
[%%versioned:
57+
module Stable : sig
58+
module V1 : sig
59+
type t =
60+
| Single of Single.Stable.V1.t
61+
| Sub_zkapp of Sub_zkapp.Stable.V1.t
62+
[@@deriving compare, hash, sexp, yojson, equal]
63+
64+
val to_latest : t -> t
65+
end
66+
end]
67+
end

src/lib/snark_work_lib/partitioned_result.ml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module Stable = struct
66

77
module V1 = struct
88
type t =
9-
{ id :
10-
[ `Single of Id.Single.Stable.V1.t
11-
| `Sub_zkapp of Id.Sub_zkapp.Stable.V1.t ]
9+
{ id : Id.Any.Stable.V1.t
1210
; data :
1311
(* NOTE: the time here correspond to time elapsed for creating the
1412
proof by a worker *)
@@ -18,18 +16,12 @@ module Stable = struct
1816
}
1917
[@@deriving to_yojson]
2018

21-
let id_to_json : t -> Yojson.Safe.t = function
22-
| { id = `Single id; _ } ->
23-
`List [ `String "single"; Id.Single.to_yojson id ]
24-
| { id = `Sub_zkapp id; _ } ->
25-
`List [ `String "sub_zkapp"; Id.Sub_zkapp.to_yojson id ]
26-
2719
let to_latest = Fn.id
2820
end
2921
end]
3022

3123
type t =
32-
{ id : [ `Single of Id.Single.t | `Sub_zkapp of Id.Sub_zkapp.t ]
24+
{ id : Id.Any.t
3325
; data : (Core.Time.Span.t, Ledger_proof.Cached.t) Proof_carrying_data.t
3426
}
3527

src/lib/snark_work_lib/partitioned_result.mli

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,20 @@ module Stable : sig
66

77
module V1 : sig
88
type t =
9-
{ id :
10-
[ `Single of Id.Single.Stable.V1.t
11-
| `Sub_zkapp of Id.Sub_zkapp.Stable.V1.t ]
9+
{ id : Id.Any.Stable.V1.t
1210
; data :
1311
( Mina_stdlib.Time.Span.Stable.V1.t
1412
, Ledger_proof.Stable.V2.t )
1513
Proof_carrying_data.Stable.V1.t
1614
}
1715
[@@deriving to_yojson]
1816

19-
val id_to_json : t -> Yojson.Safe.t
20-
2117
val to_latest : t -> t
2218
end
2319
end]
2420

2521
type t =
26-
{ id : [ `Single of Id.Single.t | `Sub_zkapp of Id.Sub_zkapp.t ]
22+
{ id : Id.Any.t
2723
; data : (Core.Time.Span.t, Ledger_proof.Cached.t) Proof_carrying_data.t
2824
}
2925

src/lib/snark_work_lib/partitioned_spec.ml

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,34 @@ module Poly = struct
44
[%%versioned
55
module Stable = struct
66
module V1 = struct
7-
type ('single_spec, 'sub_zkapp_spec, 'data) t =
7+
type ('single_spec, 'sub_zkapp_spec) t =
88
| Single of
9-
{ job :
10-
('single_spec, Id.Single.Stable.V1.t) With_job_meta.Stable.V1.t
11-
; data : 'data
12-
}
9+
('single_spec, Id.Single.Stable.V1.t) With_job_meta.Stable.V1.t
1310
| Sub_zkapp_command of
14-
{ job :
15-
( 'sub_zkapp_spec
16-
, Id.Sub_zkapp.Stable.V1.t )
17-
With_job_meta.Stable.V1.t
18-
; data : 'data
19-
}
11+
( 'sub_zkapp_spec
12+
, Id.Sub_zkapp.Stable.V1.t )
13+
With_job_meta.Stable.V1.t
2014
[@@deriving sexp, yojson]
2115
end
2216
end]
2317

24-
let drop_data : _ t -> _ t = function
25-
| Single { job; _ } ->
26-
Single { job; data = () }
27-
| Sub_zkapp_command { job; _ } ->
28-
Sub_zkapp_command { job; data = () }
29-
30-
let map ~f_single_spec ~f_subzkapp_spec ~f_data = function
31-
| Single { job; data } ->
32-
Single
33-
{ job = With_job_meta.map ~f_spec:f_single_spec job
34-
; data = f_data data
35-
}
36-
| Sub_zkapp_command { job; data } ->
37-
Sub_zkapp_command
38-
{ job = With_job_meta.map ~f_spec:f_subzkapp_spec job
39-
; data = f_data data
40-
}
18+
let map ~f_single_spec ~f_subzkapp_spec = function
19+
| Single job ->
20+
Single (With_job_meta.map ~f_spec:f_single_spec job)
21+
| Sub_zkapp_command job ->
22+
Sub_zkapp_command (With_job_meta.map ~f_spec:f_subzkapp_spec job)
4123

4224
let sok_message : _ t -> Mina_base.Sok_message.t = function
43-
| Single { job; _ } ->
25+
| Single job ->
4426
job.sok_message
45-
| Sub_zkapp_command { job; _ } ->
27+
| Sub_zkapp_command job ->
4628
job.sok_message
4729

48-
let id_to_json : _ t -> Yojson.Safe.t = function
49-
| Single { job = { job_id; _ }; _ } ->
50-
`List [ `String "single"; Id.Single.to_yojson job_id ]
51-
| Sub_zkapp_command { job = { job_id; _ }; _ } ->
52-
`List [ `String "sub_zkapp"; Id.Sub_zkapp.to_yojson job_id ]
30+
let get_id : _ t -> Id.Any.t = function
31+
| Single { job_id; _ } ->
32+
Single job_id
33+
| Sub_zkapp_command { job_id; _ } ->
34+
Sub_zkapp job_id
5335
end
5436

5537
[%%versioned
@@ -58,55 +40,42 @@ module Stable = struct
5840

5941
module V1 = struct
6042
type t =
61-
( Single_spec.Stable.V1.t
62-
, Sub_zkapp_spec.Stable.V1.t
63-
, unit )
64-
Poly.Stable.V1.t
43+
(Single_spec.Stable.V1.t, Sub_zkapp_spec.Stable.V1.t) Poly.Stable.V1.t
6544
[@@deriving sexp, yojson]
6645

6746
let to_latest = Fn.id
6847

6948
let statement : t -> Transaction_snark.Statement.t = function
70-
| Single { job = { spec; _ }; _ } ->
49+
| Single { spec; _ } ->
7150
Single_spec.Poly.statement spec
72-
| Sub_zkapp_command { job = { spec; _ }; _ } ->
51+
| Sub_zkapp_command { spec; _ } ->
7352
Sub_zkapp_spec.Stable.Latest.statement spec
7453

7554
let sok_message : t -> Mina_base.Sok_message.t = function
76-
| Single { job = { sok_message; _ }; _ } ->
77-
sok_message
78-
| Sub_zkapp_command { job = { sok_message; _ }; _ } ->
55+
| Single { sok_message; _ } | Sub_zkapp_command { sok_message; _ } ->
7956
sok_message
8057
end
8158
end]
8259

83-
type t = (Single_spec.t, Sub_zkapp_spec.t, unit) Poly.t
60+
type t = (Single_spec.t, Sub_zkapp_spec.t) Poly.t
8461

8562
let read_all_proofs_from_disk : t -> Stable.Latest.t = function
86-
| Single { job; data } ->
87-
let job =
88-
With_job_meta.map ~f_spec:Single_spec.read_all_proofs_from_disk job
89-
in
90-
Single { job; data }
91-
| Sub_zkapp_command { job; data } ->
92-
let job =
93-
With_job_meta.map ~f_spec:Sub_zkapp_spec.read_all_proofs_from_disk job
94-
in
95-
Sub_zkapp_command { job; data }
63+
| Single job ->
64+
Single
65+
(With_job_meta.map ~f_spec:Single_spec.read_all_proofs_from_disk job)
66+
| Sub_zkapp_command job ->
67+
Sub_zkapp_command
68+
(With_job_meta.map ~f_spec:Sub_zkapp_spec.read_all_proofs_from_disk job)
9669

9770
let write_all_proofs_to_disk ~(proof_cache_db : Proof_cache_tag.cache_db) :
9871
Stable.Latest.t -> t = function
99-
| Single { job; data } ->
100-
let job =
101-
With_job_meta.map
102-
~f_spec:(Single_spec.write_all_proofs_to_disk ~proof_cache_db)
103-
job
104-
in
105-
Single { job; data }
106-
| Sub_zkapp_command { job; data } ->
107-
let job =
108-
With_job_meta.map
109-
~f_spec:(Sub_zkapp_spec.write_all_proofs_to_disk ~proof_cache_db)
110-
job
111-
in
112-
Sub_zkapp_command { job; data }
72+
| Single job ->
73+
Single
74+
(With_job_meta.map
75+
~f_spec:(Single_spec.write_all_proofs_to_disk ~proof_cache_db)
76+
job )
77+
| Sub_zkapp_command job ->
78+
Sub_zkapp_command
79+
(With_job_meta.map
80+
~f_spec:(Sub_zkapp_spec.write_all_proofs_to_disk ~proof_cache_db)
81+
job )

src/lib/snark_work_lib/partitioned_spec.mli

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,26 @@ module Poly : sig
44
[%%versioned:
55
module Stable : sig
66
module V1 : sig
7-
type ('single_spec, 'sub_zkapp_spec, 'data) t =
7+
type ('single_spec, 'sub_zkapp_spec) t =
88
| Single of
9-
{ job :
10-
('single_spec, Id.Single.Stable.V1.t) With_job_meta.Stable.V1.t
11-
; data : 'data
12-
}
9+
('single_spec, Id.Single.Stable.V1.t) With_job_meta.Stable.V1.t
1310
| Sub_zkapp_command of
14-
{ job :
15-
( 'sub_zkapp_spec
16-
, Id.Sub_zkapp.Stable.V1.t )
17-
With_job_meta.Stable.V1.t
18-
; data : 'data
19-
}
11+
( 'sub_zkapp_spec
12+
, Id.Sub_zkapp.Stable.V1.t )
13+
With_job_meta.Stable.V1.t
2014
[@@deriving sexp, yojson]
2115
end
2216
end]
2317

24-
val drop_data : ('a, 'b, 'c) t -> ('a, 'b, unit) t
25-
2618
val map :
2719
f_single_spec:('a -> 'b)
2820
-> f_subzkapp_spec:('c -> 'd)
29-
-> f_data:('e -> 'f)
30-
-> ('a, 'c, 'e) t
31-
-> ('b, 'd, 'f) t
21+
-> ('a, 'c) t
22+
-> ('b, 'd) t
3223

3324
val sok_message : _ t -> Mina_base.Sok_message.t
3425

35-
val id_to_json : _ t -> Yojson.Safe.t
26+
val get_id : _ t -> Id.Any.t
3627
end
3728

3829
[%%versioned:
@@ -41,10 +32,7 @@ module Stable : sig
4132

4233
module V1 : sig
4334
type t =
44-
( Single_spec.Stable.V1.t
45-
, Sub_zkapp_spec.Stable.V1.t
46-
, unit )
47-
Poly.Stable.V1.t
35+
(Single_spec.Stable.V1.t, Sub_zkapp_spec.Stable.V1.t) Poly.Stable.V1.t
4836
[@@deriving sexp, yojson]
4937

5038
val to_latest : t -> t
@@ -55,7 +43,7 @@ module Stable : sig
5543
end
5644
end]
5745

58-
type t = (Single_spec.t, Sub_zkapp_spec.t, unit) Poly.t
46+
type t = (Single_spec.t, Sub_zkapp_spec.t) Poly.t
5947

6048
val read_all_proofs_from_disk : t -> Stable.Latest.t
6149

src/lib/snark_worker/entry.ml

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,15 @@ let main ~logger ~proof_level ~constraint_constants ~signature_kind
110110
~metadata:[ address_json; work_ids_json ] ;
111111
let%bind () = wait () in
112112
(* Pause to wait for stdout to flush *)
113+
let id = Spec.Partitioned.Poly.get_id partitioned_spec in
113114
match%bind
114115
Prod.Impl.perform_partitioned ~state ~spec:partitioned_spec
115116
with
116117
| Error e ->
117-
let partitioned_id =
118-
Spec.Partitioned.Poly.map ~f_single_spec:ignore
119-
~f_subzkapp_spec:ignore ~f_data:ignore partitioned_spec
120-
in
121118
let%bind () =
122119
match%map
123120
dispatch Rpc_failed_to_generate_snark.Stable.Latest.rpc
124-
shutdown_on_disconnect (e, partitioned_id) daemon_address
121+
shutdown_on_disconnect (e, id) daemon_address
125122
with
126123
| Error e ->
127124
[%log error]
@@ -131,26 +128,13 @@ let main ~logger ~proof_level ~constraint_constants ~signature_kind
131128
()
132129
in
133130
log_and_retry "performing work" e (retry_pause 10.) go
134-
| Ok result ->
135-
let wire_result =
136-
match result with
137-
| Spec.Partitioned.Poly.Single { job; data } ->
138-
Result.Partitioned.Stable.Latest.
139-
{ id = `Single job.job_id; data }
140-
| Sub_zkapp_command { job; data } ->
141-
Result.Partitioned.Stable.Latest.
142-
{ id = `Sub_zkapp job.job_id; data }
143-
in
144-
( match result with
145-
| Spec.Partitioned.Poly.Single
146-
{ job = { spec = single_spec; _ }
147-
; data = { data = elapsed; _ }
148-
} ->
131+
| Ok ({ data = elapsed; _ } as data) ->
132+
let wire_result = Result.Partitioned.Stable.Latest.{ id; data } in
133+
( match partitioned_spec with
134+
| Spec.Partitioned.Poly.Single { spec = single_spec; _ } ->
149135
Metrics.emit_single_metrics_stable ~logger ~single_spec ~elapsed
150136
| Spec.Partitioned.Poly.Sub_zkapp_command
151-
{ job = { spec = sub_zkapp_spec; _ }
152-
; data = { data = elapsed; _ }
153-
} ->
137+
{ spec = sub_zkapp_spec; _ } ->
154138
Metrics.emit_subzkapp_metrics ~logger ~sub_zkapp_spec ~elapsed
155139
) ;
156140
let rec submit_work () =

0 commit comments

Comments
 (0)