From 2b896e129e3d49192d64ec54cc7b9788f510edba Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Thu, 16 Jun 2022 10:09:10 +0200 Subject: [PATCH 1/4] Test: export [test_roundtrip] to helpers --- src/lib_test/qcheck2_helpers.ml | 27 +++++++++++++ src/lib_test/qcheck2_helpers.mli | 9 +++++ .../test/pbt/test_tx_rollup_l2_encoding.ml | 40 +++---------------- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/lib_test/qcheck2_helpers.ml b/src/lib_test/qcheck2_helpers.ml index 2eb6389d34f9..6fbc40e38dea 100644 --- a/src/lib_test/qcheck2_helpers.ml +++ b/src/lib_test/qcheck2_helpers.ml @@ -229,3 +229,30 @@ struct let gen (key_gen : Map.key Gen.t) (val_gen : 'v Gen.t) : 'v Map.t Gen.t = gen_of_size Gen.small_nat key_gen val_gen end + +let test_roundtrip ~count ~title ~gen ~eq encoding = + let pp fmt x = + Data_encoding.Json.construct encoding x + |> Data_encoding.Json.to_string |> Format.pp_print_string fmt + in + let test rdt input = + let output = Roundtrip.make encoding rdt input in + let success = eq input output in + if not success then + QCheck2.Test.fail_reportf + "%s %s roundtrip error: %a became %a" + title + (Roundtrip.target rdt) + pp + input + pp + output + in + QCheck2.Test.make + ~count + ~name:(Format.asprintf "roundtrip %s" title) + gen + (fun input -> + test Roundtrip.binary input ; + test Roundtrip.json input ; + true) diff --git a/src/lib_test/qcheck2_helpers.mli b/src/lib_test/qcheck2_helpers.mli index f00d0410c0d5..c4c376370076 100644 --- a/src/lib_test/qcheck2_helpers.mli +++ b/src/lib_test/qcheck2_helpers.mli @@ -179,3 +179,12 @@ end) : sig are generated with [key_gen] and the values with [val_gen]. *) val gen : Map.key QCheck2.Gen.t -> 'v QCheck2.Gen.t -> 'v Map.t QCheck2.Gen.t end + +(** Test the roundtripness of an encoding both in JSON and binary formats. *) +val test_roundtrip : + count:int -> + title:string -> + gen:'a QCheck2.Gen.t -> + eq:('a -> 'a -> bool) -> + 'a Data_encoding.t -> + QCheck2.Test.t diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_tx_rollup_l2_encoding.ml b/src/proto_alpha/lib_protocol/test/pbt/test_tx_rollup_l2_encoding.ml index f087ae148dee..99e3a4eaedcd 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_tx_rollup_l2_encoding.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_tx_rollup_l2_encoding.ml @@ -31,7 +31,6 @@ Subject: Tx rollup l2 encoding *) -open Lib_test open Lib_test.Qcheck2_helpers open Protocol.Indexable open Protocol.Tx_rollup_l2_batch @@ -274,33 +273,6 @@ let test_quantity ~count = in QCheck2.Test.make ~count ~print ~name:"quantity operation" test_gen test -let test_roundtrip ~count title arb equ encoding = - let pp fmt x = - Data_encoding.Json.construct encoding x - |> Data_encoding.Json.to_string |> Format.pp_print_string fmt - in - let test rdt input = - let output = Roundtrip.make encoding rdt input in - let success = equ input output in - if not success then - QCheck2.Test.fail_reportf - "%s %s roundtrip error: %a became %a" - title - (Roundtrip.target rdt) - pp - input - pp - output - in - QCheck2.Test.make - ~count - ~name:(Format.asprintf "roundtrip %s" title) - arb - (fun input -> - test Roundtrip.binary input ; - test Roundtrip.json input ; - true) - let () = let qcheck_wrap = qcheck_wrap ~rand:(Random.State.make_self_init ()) in Alcotest.run @@ -312,15 +284,15 @@ let () = [ test_roundtrip ~count:100 - "batch" - batch - ( = ) + ~title:"batch" + ~gen:batch + ~eq:( = ) Protocol.Tx_rollup_l2_batch.encoding; test_roundtrip ~count:100 - "message_result" - message_result_withdrawal - ( = ) + ~title:"message_result" + ~gen:message_result_withdrawal + ~eq:( = ) Protocol.Tx_rollup_l2_apply.Message_result.encoding; ] ); ] -- GitLab From 77f8d38ba7b5583a905379d58d5e26de59792726 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Thu, 16 Jun 2022 10:42:04 +0200 Subject: [PATCH 2/4] Test: fix typo --- .../lib_protocol/test/pbt/test_sc_rollup_tick_repr.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_tick_repr.ml b/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_tick_repr.ml index d9d7a5851dc6..1d9424887ebb 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_tick_repr.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_tick_repr.ml @@ -55,7 +55,7 @@ let test_distance_from_self = Z.(equal (Tick.distance x x) zero)) (** Distance from non-self is non-zero. *) -let tets_distance_from_non_self = +let test_distance_from_non_self = Test.make ~name:"distance from non-self is non-zero" (Gen.pair tick tick) @@ -96,7 +96,7 @@ let tests = test_next_is_monotonic; test_initial_is_bottom; test_distance_from_self; - tets_distance_from_non_self; + test_distance_from_non_self; test_distance_symmetry; test_distance_triangle_inequality; test_of_int; -- GitLab From ba296ab7bad96381ad5837ee55fb8b95c601fb55 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Thu, 16 Jun 2022 10:58:23 +0200 Subject: [PATCH 3/4] Lib_test: [int32_range_gen] --- src/lib_test/qcheck2_helpers.ml | 28 +++++++++++++++++++++++----- src/lib_test/qcheck2_helpers.mli | 11 ++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/lib_test/qcheck2_helpers.ml b/src/lib_test/qcheck2_helpers.ml index 6fbc40e38dea..285f9d595563 100644 --- a/src/lib_test/qcheck2_helpers.ml +++ b/src/lib_test/qcheck2_helpers.ml @@ -123,17 +123,35 @@ let qcheck_cond ?pp ~cond e () = | Some pp -> QCheck.Test.fail_reportf "@[The condition check failed!@,%a@]" pp e -let int64_range_gen a b = +let intX_range_gen ~sub ~add ~gen ~shrink a b = let gen a b st = - let range = Int64.sub b a in - let raw_val = Random.State.int64 st range in - let res = Int64.add a raw_val in + let range = sub b a in + let raw_val = gen st range in + let res = add a raw_val in assert (a <= res && res <= b) ; res in - let shrink b () = QCheck2.Shrink.int64_towards a b () in + let shrink b () = shrink a b () in QCheck2.Gen.make_primitive ~gen:(gen a b) ~shrink +let int64_range_gen a b = + intX_range_gen + ~sub:Int64.sub + ~add:Int64.add + ~gen:Random.State.int64 + ~shrink:QCheck2.Shrink.int64_towards + a + b + +let int32_range_gen a b = + intX_range_gen + ~sub:Int32.sub + ~add:Int32.add + ~gen:Random.State.int32 + ~shrink:QCheck2.Shrink.int32_towards + a + b + let int64_strictly_positive_gen = int64_range_gen 1L let int_strictly_positive_gen = QCheck2.Gen.int_range 1 diff --git a/src/lib_test/qcheck2_helpers.mli b/src/lib_test/qcheck2_helpers.mli index c4c376370076..879c13337ccd 100644 --- a/src/lib_test/qcheck2_helpers.mli +++ b/src/lib_test/qcheck2_helpers.mli @@ -96,11 +96,16 @@ val qcheck_cond : and [b] inclusive. Poorman's implementation until - https://github.com/c-cube/qcheck/issues/105 is done. - - This probably spectacularly crashes if [(b - a) > Int64.max_int]. *) + https://github.com/c-cube/qcheck/issues/105 is done. *) val int64_range_gen : int64 -> int64 -> int64 QCheck2.Gen.t +(** [int32_range_gen a b] generates an [int32] between [a] inclusive + and [b] inclusive. + + Poorman's implementation until + https://github.com/c-cube/qcheck/issues/105 is done. *) +val int32_range_gen : int32 -> int32 -> int32 QCheck2.Gen.t + (** [int64_strictly_positive_gen x] generates an [int64] between [1] inclusive and [x] inclusive. -- GitLab From ab87fb311b982a64d779451004cd87c0bf538f54 Mon Sep 17 00:00:00 2001 From: Valentin Chaboche Date: Thu, 16 Jun 2022 10:58:55 +0200 Subject: [PATCH 4/4] Scoru,Test: test roundtripness of commitment's encoding --- manifest/main.ml | 1 + src/proto_alpha/lib_protocol/test/pbt/dune | 6 + .../test/pbt/test_sc_rollup_encoding.ml | 109 ++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_encoding.ml diff --git a/manifest/main.ml b/manifest/main.ml index 8261e2717172..36a6189813bf 100644 --- a/manifest/main.ml +++ b/manifest/main.ml @@ -3247,6 +3247,7 @@ end = struct ("test_tx_rollup_l2_withdraw_storage", N.(number <= 010)); ("test_bitset", N.(number >= 013)); ("test_sc_rollup_tick_repr", N.(number >= 013)); + ("test_sc_rollup_encoding", N.(number >= 014)); ("refutation_game_pbt", N.(number == 013)); ("test_refutation_game", N.(number >= 014)); ("test_carbonated_map", N.(number >= 013)); diff --git a/src/proto_alpha/lib_protocol/test/pbt/dune b/src/proto_alpha/lib_protocol/test/pbt/dune index c1d653c63878..8a661a531a3b 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/dune +++ b/src/proto_alpha/lib_protocol/test/pbt/dune @@ -13,6 +13,7 @@ test_tx_rollup_l2_encoding test_bitset test_sc_rollup_tick_repr + test_sc_rollup_encoding test_refutation_game test_carbonated_map) (libraries @@ -88,6 +89,11 @@ (package tezos-protocol-alpha-tests) (action (run %{dep:./test_sc_rollup_tick_repr.exe}))) +(rule + (alias runtest) + (package tezos-protocol-alpha-tests) + (action (run %{dep:./test_sc_rollup_encoding.exe}))) + (rule (alias runtest) (package tezos-protocol-alpha-tests) diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_encoding.ml b/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_encoding.ml new file mode 100644 index 000000000000..3324faa5b7e9 --- /dev/null +++ b/src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_encoding.ml @@ -0,0 +1,109 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** Testing + ------- + Component: Protocol Library + Invocation: dune exec \ + src/proto_alpha/lib_protocol/test/pbt/test_sc_rollup_encoding.exe + Subject: SC rollup encoding +*) + +open Protocol +open QCheck2 +open Lib_test.Qcheck2_helpers + +(** {2 Generators} *) + +let gen_state_hash = + let open Gen in + let* bytes = bytes_fixed_gen Sc_rollup_repr.State_hash.size in + return (Sc_rollup_repr.State_hash.of_bytes_exn bytes) + +let gen_raw_level = + let open Gen in + let* level = map Int32.abs int32 in + return (Raw_level_repr.of_int32_exn level) + +let gen_commitment_hash = + let open Gen in + let* bytes = bytes_fixed_gen Sc_rollup_commitment_repr.Hash.size in + return (Sc_rollup_commitment_repr.Hash.of_bytes_exn bytes) + +let gen_number_of_messages = + let open Gen in + let open Sc_rollup_repr.Number_of_messages in + let* v = int32_range_gen min_int max_int in + return (WithExceptions.Option.get ~loc:__LOC__ (of_int32 v)) + +let gen_number_of_ticks = + let open Gen in + let open Sc_rollup_repr.Number_of_ticks in + let* v = int32_range_gen min_int max_int in + return (WithExceptions.Option.get ~loc:__LOC__ (of_int32 v)) + +let gen_commitment = + let open Gen in + let* compressed_state = gen_state_hash + and* inbox_level = gen_raw_level + and* predecessor = gen_commitment_hash + and* number_of_messages = gen_number_of_messages + and* number_of_ticks = gen_number_of_ticks in + return + Sc_rollup_commitment_repr. + { + compressed_state; + inbox_level; + predecessor; + number_of_messages; + number_of_ticks; + } + +let gen_versioned_commitment = + let open Gen in + let* commitment = gen_commitment in + return (Sc_rollup_commitment_repr.to_versioned commitment) + +(** {2 Tests} *) + +let test_commitment = + test_roundtrip + ~count:1_000 + ~title:"Sc_rollup_commitment.t" + ~gen:gen_commitment + ~eq:( = ) + Sc_rollup_commitment_repr.encoding + +let test_versioned_commitment = + test_roundtrip + ~count:1_000 + ~title:"Sc_rollup_commitment.versioned" + ~gen:gen_versioned_commitment + ~eq:( = ) + Sc_rollup_commitment_repr.versioned_encoding + +let tests = [test_commitment; test_versioned_commitment] + +let () = Alcotest.run "SC rollup encoding" [("roundtrip", qcheck_wrap tests)] -- GitLab