|
| 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