Skip to content

Commit 9c29c98

Browse files
authored
Merge pull request #16604 from MinaProtocol/georgeee/precomputed-block-json-comprehensive-test
Comprehensive json roundtrip test for precomputed blocks
2 parents 5a7facb + 728a013 commit 9c29c98

File tree

6 files changed

+137
-44
lines changed

6 files changed

+137
-44
lines changed

nix/ocaml.nix

+18
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ let
195195
PLONK_WASM_NODEJS = "${pkgs.plonk_wasm}/nodejs";
196196
PLONK_WASM_WEB = "${pkgs.plonk_wasm}/web";
197197
};
198+
pkgs.__src-lib-mina_block-tests__ = let
199+
gzipped = pkgs.fetchurl {
200+
url =
201+
"https://storage.googleapis.com/o1labs-ci-test-data/precomputed-blocks/hetzner-itn-1-1795-3NL9Vn7Rg1mz8cS1gVxFkVPsjESG1Zu1XRpMLRQAz3W24hctRoD6.json.gz";
202+
sha256 = "sha256-vEmtOe6vKANbKHyAK2n59bjWvwFwVICo7wiBwasRzT0=";
203+
};
204+
ungzipped = pkgs.stdenv.mkDerivation {
205+
name = "unpacked-precomputed-block-json";
206+
src = gzipped;
207+
buildInputs = [ pkgs.gzip ];
208+
phases = [ "installPhase" ];
209+
installPhase = ''
210+
gunzip -c $src > $out
211+
'';
212+
};
213+
in super.pkgs.__src-lib-mina_block-tests__.overrideAttrs {
214+
TEST_PRECOMPUTED_BLOCK_JSON_PATH = "${ungzipped}";
215+
};
198216
files.src-lib-crypto-kimchi_bindings-js-node_js =
199217
super.files.src-lib-crypto-kimchi_bindings-js-node_js.overrideAttrs {
200218
PLONK_WASM_NODEJS = "${pkgs.plonk_wasm}/nodejs";

src/lib/mina_block/dune

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(library
22
(name mina_block)
33
(public_name mina_block)
4-
(inline_tests (flags -verbose -show-counts))
54
(libraries
65
;; opam libraries
76
integers
@@ -12,7 +11,6 @@
1211
sexplib0
1312
base64
1413
result
15-
ppx_inline_test.config
1614
base
1715
core
1816
;; local libraries

src/lib/mina_block/precomputed_block.ml

-42
Original file line numberDiff line numberDiff line change
@@ -188,45 +188,3 @@ let of_block ~logger
188188
; accounts_created
189189
; tokens_used
190190
}
191-
192-
(* NOTE: This serialization is used externally and MUST NOT change.
193-
If the underlying types change, you should write a conversion, or add
194-
optional fields and handle them appropriately.
195-
*)
196-
(* But if you really need to update it, you can generate new samples using:
197-
`dune exec dump_blocks 1> block.txt` *)
198-
let%test_unit "Sexp serialization is stable" =
199-
let serialized_block = Sample_precomputed_block.sample_block_sexp in
200-
ignore @@ t_of_sexp @@ Sexp.of_string serialized_block
201-
202-
let%test_unit "Sexp serialization roundtrips" =
203-
let serialized_block = Sample_precomputed_block.sample_block_sexp in
204-
let sexp = Sexp.of_string serialized_block in
205-
let sexp_roundtrip = sexp_of_t @@ t_of_sexp sexp in
206-
[%test_eq: Sexp.t] sexp sexp_roundtrip
207-
208-
(* NOTE: This serialization is used externally and MUST NOT change.
209-
If the underlying types change, you should write a conversion, or add
210-
optional fields and handle them appropriately.
211-
*)
212-
(* But if you really need to update it, see output of CLI command:
213-
`dune exec dump_blocks 1> block.txt` *)
214-
let%test_unit "JSON serialization is stable" =
215-
let serialized_block = Sample_precomputed_block.sample_block_json in
216-
match of_yojson @@ Yojson.Safe.from_string serialized_block with
217-
| Ok _ ->
218-
()
219-
| Error err ->
220-
failwith err
221-
222-
let%test_unit "JSON serialization roundtrips" =
223-
let serialized_block = Sample_precomputed_block.sample_block_json in
224-
let json = Yojson.Safe.from_string serialized_block in
225-
let json_roundtrip =
226-
match Result.map ~f:to_yojson @@ of_yojson json with
227-
| Ok json ->
228-
json
229-
| Error err ->
230-
failwith err
231-
in
232-
assert (Yojson.Safe.equal json json_roundtrip)

src/lib/mina_block/tests/dune

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(rule
2+
(alias precomputed_block)
3+
(target hetzner-itn-1-1795.json)
4+
(deps
5+
(env_var TEST_PRECOMPUTED_BLOCK_JSON_PATH))
6+
(enabled_if (= %{env:TEST_PRECOMPUTED_BLOCK_JSON_PATH=n} n))
7+
(action
8+
(progn
9+
(run curl -L -o %{target}.gz https://storage.googleapis.com/o1labs-ci-test-data/precomputed-blocks/hetzner-itn-1-1795-3NL9Vn7Rg1mz8cS1gVxFkVPsjESG1Zu1XRpMLRQAz3W24hctRoD6.json.gz)
10+
(run gzip -d %{target}.gz)
11+
)))
12+
13+
(rule
14+
(enabled_if
15+
(<> %{env:TEST_PRECOMPUTED_BLOCK_JSON_PATH=n} n))
16+
(target hetzner-itn-1-1795.json)
17+
(deps
18+
(env_var TEST_PRECOMPUTED_BLOCK_JSON_PATH))
19+
(action
20+
(progn
21+
(copy
22+
%{env:TEST_PRECOMPUTED_BLOCK_JSON_PATH=n}
23+
%{target}))))
24+
25+
(test
26+
(name main)
27+
(flags
28+
(:standard -warn-error +a))
29+
(libraries alcotest mina_block)
30+
(deps hetzner-itn-1-1795.json)
31+
(action
32+
(run %{test}))
33+
(instrumentation (backend bisect_ppx))
34+
(preprocess (pps ppx_version)))

src/lib/mina_block/tests/main.ml

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
open Alcotest
2+
open Core_kernel
3+
4+
(* NOTE: This serialization is used externally and MUST NOT change.
5+
If the underlying types change, you should write a conversion, or add
6+
optional fields and handle them appropriately.
7+
*)
8+
(* But if you really need to update it, you can generate new samples using:
9+
`dune exec dump_blocks 1> block.txt` *)
10+
let sexp_serialization_is_stable () =
11+
let serialized_block = Sample_precomputed_block.sample_block_sexp in
12+
ignore @@ Mina_block.Precomputed.t_of_sexp @@ Sexp.of_string serialized_block
13+
14+
let sexp_serialization_roundtrips () =
15+
let serialized_block = Sample_precomputed_block.sample_block_sexp in
16+
let sexp = Sexp.of_string serialized_block in
17+
let sexp_roundtrip = Mina_block.Precomputed.(sexp_of_t @@ t_of_sexp sexp) in
18+
if Sexp.equal sexp sexp_roundtrip then ()
19+
else failwith "sexp roundtrip failed"
20+
21+
let json_serialization_is_stable_impl serialized_block =
22+
match
23+
Mina_block.Precomputed.of_yojson @@ Yojson.Safe.from_string serialized_block
24+
with
25+
| Ok _ ->
26+
()
27+
| Error err ->
28+
failwith err
29+
30+
(* NOTE: This serialization is used externally and MUST NOT change.
31+
If the underlying types change, you should write a conversion, or add
32+
optional fields and handle them appropriately.
33+
*)
34+
(* But if you really need to update it, see output of CLI command:
35+
`dune exec dump_blocks 1> block.txt` *)
36+
let json_serialization_is_stable () =
37+
json_serialization_is_stable_impl
38+
@@ Sample_precomputed_block.sample_block_json
39+
40+
let json_serialization_roundtrips_impl serialized_block =
41+
let json = Yojson.Safe.from_string serialized_block in
42+
let json_roundtrip =
43+
match
44+
Mina_block.Precomputed.(Result.map ~f:to_yojson @@ of_yojson json)
45+
with
46+
| Ok json ->
47+
json
48+
| Error err ->
49+
failwith err
50+
in
51+
assert (Yojson.Safe.equal json json_roundtrip)
52+
53+
let json_serialization_roundtrips () =
54+
json_serialization_roundtrips_impl
55+
@@ Sample_precomputed_block.sample_block_json
56+
57+
let large_precomputed_json_file = "hetzner-itn-1-1795.json"
58+
59+
let json_serialization_is_stable_from_file () =
60+
json_serialization_is_stable_impl
61+
@@ In_channel.read_all large_precomputed_json_file
62+
63+
let json_serialization_roundtrips_from_file () =
64+
json_serialization_roundtrips_impl
65+
@@ In_channel.read_all large_precomputed_json_file
66+
67+
let () =
68+
run "Precomputed block serialization tests"
69+
[ ( "sexp"
70+
, [ test_case "serialization is stable" `Quick
71+
sexp_serialization_is_stable
72+
; test_case "serialization roundtrips" `Quick
73+
sexp_serialization_roundtrips
74+
] )
75+
; ( "json"
76+
, [ test_case "serialization is stable" `Quick
77+
json_serialization_is_stable
78+
; test_case "serialization roundtrips" `Quick
79+
json_serialization_roundtrips
80+
; test_case "serialization is stable from file" `Quick
81+
json_serialization_is_stable_from_file
82+
; test_case "serialization roundtrips from file" `Quick
83+
json_serialization_roundtrips_from_file
84+
] )
85+
]

0 commit comments

Comments
 (0)