From d5f2ebab7ee0c463bdb30feebd504b8a116016ce Mon Sep 17 00:00:00 2001 From: lykimq Date: Wed, 19 Oct 2022 16:28:05 +0700 Subject: [PATCH 1/3] Michelson/Tezt: add hash origination test --- .../non_regression/dummy_contract.tz | 5 ++ tezt/lib_tezos/client.ml | 10 +++ tezt/lib_tezos/client.mli | 8 ++ tezt/tests/contract_hash_with_origination.ml | 80 +++++++++++++++++++ tezt/tests/main.ml | 1 + 5 files changed, 104 insertions(+) create mode 100644 tests_python/contracts_alpha/non_regression/dummy_contract.tz create mode 100644 tezt/tests/contract_hash_with_origination.ml diff --git a/tests_python/contracts_alpha/non_regression/dummy_contract.tz b/tests_python/contracts_alpha/non_regression/dummy_contract.tz new file mode 100644 index 000000000000..c7d34bc31f9c --- /dev/null +++ b/tests_python/contracts_alpha/non_regression/dummy_contract.tz @@ -0,0 +1,5 @@ +parameter unit; +storage unit; +code { CAR; + NIL operation; + PAIR; } diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index aed3c3a7f053..331174539fea 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -1423,6 +1423,16 @@ let spawn_hash_script ?hooks ~script client = let hash_script ?hooks ~script client = spawn_hash_script ?hooks ~script client |> Process.check_and_read_stdout +let spawn_get_contract_hash ?hooks ~contract client = + spawn_command + ?hooks + client + ["get"; "contract"; "script"; "hash"; "for"; contract] + +let get_contract_hash ?hooks ~contract client = + spawn_get_contract_hash ?hooks ~contract client + |> Process.check_and_read_stdout + let spawn_hash_data ?hooks ~data ~typ client = let cmd = ["hash"; "data"; data; "of"; "type"; typ] in spawn_command ?hooks client cmd diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index ae16f99f73ec..2bffa6a64e65 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -1165,6 +1165,14 @@ val hash_script : ?hooks:Process_hooks.t -> script:string -> t -> string Lwt.t val spawn_hash_script : ?hooks:Process_hooks.t -> script:string -> t -> Process.t +(** Run [octez-client get contract script hash for ..]*) +val get_contract_hash : + ?hooks:Process_hooks.t -> contract:string -> t -> string Lwt.t + +(** Same as [get_contract_hash], but do not wait for the process to exit. *) +val spawn_get_contract_hash : + ?hooks:Process_hooks.t -> contract:string -> t -> Process.t + (** Run [octez-client normalize data .. of type ...]*) val normalize_data : ?mode:normalize_mode -> diff --git a/tezt/tests/contract_hash_with_origination.ml b/tezt/tests/contract_hash_with_origination.ml new file mode 100644 index 000000000000..eb7597ece1f6 --- /dev/null +++ b/tezt/tests/contract_hash_with_origination.ml @@ -0,0 +1,80 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2020 Nomadic Labs *) +(* Copyright (c) 2022 Marigold *) +(* *) +(* 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: Michelson + Invocation: dune exec tezt/tests/main.exe -- --file contract_hash_with_origination.ml + Subject: Tests that `get contract script hash for ` return the + same value as directly hashing the script. +*) + +let contract_path protocol contract = + sf + "tests_python/contracts_%s/opcodes/%s" + (match protocol with + | Protocol.Alpha -> "alpha" + | _ -> sf "%03d" @@ Protocol.number protocol) + contract + +let test_contract_hash_with_origination ~protocol () = + let* client = Client.init_mockup ~protocol () in + let prg = contract_path protocol "noop.tz" in + let script = read_file prg in + let* contract = + Client.originate_contract + ~alias:"noop" + ~amount:(Tez.of_int 1000) + ~src:Constant.bootstrap1.alias + ~prg + ~init:"Unit" + ~burn_cap:Tez.one + client + in + let* received_script_hash = Client.hash_script client ~script in + let returned_script_hash = String.trim received_script_hash in + let* received_contract_hash = Client.get_contract_hash client ~contract in + let returned_contract_hash = String.trim received_contract_hash in + Check.( + (returned_script_hash = returned_contract_hash) + string + ~__LOC__ + ~error_msg:"Expected contract hash %R, got %L") ; + unit + +let register ~protocols = + List.iter + (fun (title, test_function) -> + Protocol.register_test + ~__FILE__ + ~title + ~tags:["client"; "michelson"] + (fun protocol -> test_function ~protocol ()) + protocols) + [ + ( "Test `get contract hash with origination for`", + test_contract_hash_with_origination ); + ] diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index 577624899e9c..d80b6ba8aad5 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -106,6 +106,7 @@ let register_protocol_tests_that_use_supports_correctly () = Client_config.register ~protocols ; Client_run_view.register ~protocols ; Contract_hash_fun.register ~protocols ; + Contract_hash_with_origination.register ~protocols ; Create_contract.register ~protocols ; Deposits_limit.register ~protocols ; Double_bake.register ~protocols ; -- GitLab From 5f79fe543152601f9536479f4176ded0fe8e05bb Mon Sep 17 00:00:00 2001 From: lykimq Date: Wed, 19 Oct 2022 16:30:38 +0700 Subject: [PATCH 2/3] Michelson/Python: remove hash origination test --- tests_python/tests_014/test_contract.py | 19 ------------------- tests_python/tests_015/test_contract.py | 19 ------------------- tests_python/tests_alpha/test_contract.py | 19 ------------------- 3 files changed, 57 deletions(-) diff --git a/tests_python/tests_014/test_contract.py b/tests_python/tests_014/test_contract.py index fadefc087f92..607934590b3d 100644 --- a/tests_python/tests_014/test_contract.py +++ b/tests_python/tests_014/test_contract.py @@ -1872,25 +1872,6 @@ class TestScriptHashRegression: ) -@pytest.mark.contract -class TestScriptHashOrigination: - def test_contract_hash_with_origination( - self, client: Client, session: dict - ): - script = ID_SCRIPT_LITERAL - originate( - client, - session, - contract=script, - init_storage='Unit', - amount=1000, - contract_name='dummy_contract', - ) - [(hash1, _)] = client.hash_script([script]) - hash2 = client.get_script_hash('dummy_contract') - assert hash1 == hash2 - - @pytest.mark.contract class TestScriptHashMultiple: """Test octez-client hash script with diffent number and type of diff --git a/tests_python/tests_015/test_contract.py b/tests_python/tests_015/test_contract.py index fadefc087f92..607934590b3d 100644 --- a/tests_python/tests_015/test_contract.py +++ b/tests_python/tests_015/test_contract.py @@ -1872,25 +1872,6 @@ class TestScriptHashRegression: ) -@pytest.mark.contract -class TestScriptHashOrigination: - def test_contract_hash_with_origination( - self, client: Client, session: dict - ): - script = ID_SCRIPT_LITERAL - originate( - client, - session, - contract=script, - init_storage='Unit', - amount=1000, - contract_name='dummy_contract', - ) - [(hash1, _)] = client.hash_script([script]) - hash2 = client.get_script_hash('dummy_contract') - assert hash1 == hash2 - - @pytest.mark.contract class TestScriptHashMultiple: """Test octez-client hash script with diffent number and type of diff --git a/tests_python/tests_alpha/test_contract.py b/tests_python/tests_alpha/test_contract.py index fadefc087f92..607934590b3d 100644 --- a/tests_python/tests_alpha/test_contract.py +++ b/tests_python/tests_alpha/test_contract.py @@ -1872,25 +1872,6 @@ class TestScriptHashRegression: ) -@pytest.mark.contract -class TestScriptHashOrigination: - def test_contract_hash_with_origination( - self, client: Client, session: dict - ): - script = ID_SCRIPT_LITERAL - originate( - client, - session, - contract=script, - init_storage='Unit', - amount=1000, - contract_name='dummy_contract', - ) - [(hash1, _)] = client.hash_script([script]) - hash2 = client.get_script_hash('dummy_contract') - assert hash1 == hash2 - - @pytest.mark.contract class TestScriptHashMultiple: """Test octez-client hash script with diffent number and type of -- GitLab From 40333c86c613155f35aee7c6fc30d7839da6e740 Mon Sep 17 00:00:00 2001 From: lykimq Date: Thu, 20 Oct 2022 11:21:11 +0700 Subject: [PATCH 3/3] Michelson/Python: regression output because add the dummy_contract.tz --- .../contracts_alpha/non_regression/dummy_contract.tz | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 tests_python/contracts_alpha/non_regression/dummy_contract.tz diff --git a/tests_python/contracts_alpha/non_regression/dummy_contract.tz b/tests_python/contracts_alpha/non_regression/dummy_contract.tz deleted file mode 100644 index c7d34bc31f9c..000000000000 --- a/tests_python/contracts_alpha/non_regression/dummy_contract.tz +++ /dev/null @@ -1,5 +0,0 @@ -parameter unit; -storage unit; -code { CAR; - NIL operation; - PAIR; } -- GitLab