From 0bd415ac607250fc235dff01a093025890344dc0 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Tue, 29 Nov 2022 16:18:32 -0500 Subject: [PATCH] Tezt: translate [test_chain_id_*] of [test_contract.py] --- tests_python/tests_014/test_contract.py | 44 -------- tests_python/tests_015/test_contract.py | 44 -------- tests_python/tests_alpha/test_contract.py | 44 -------- tezt/tests/main.ml | 1 + tezt/tests/script_chain_id.ml | 126 ++++++++++++++++++++++ 5 files changed, 127 insertions(+), 132 deletions(-) create mode 100644 tezt/tests/script_chain_id.ml diff --git a/tests_python/tests_014/test_contract.py b/tests_python/tests_014/test_contract.py index 3de6b39c49b8..09bc5db1c214 100644 --- a/tests_python/tests_014/test_contract.py +++ b/tests_python/tests_014/test_contract.py @@ -874,50 +874,6 @@ class TestView: originate(client, session, path, '4', 0) -@pytest.mark.incremental -@pytest.mark.contract -class TestChainId: - def test_chain_id_opcode(self, client: Client, session: dict): - path = os.path.join(CONTRACT_PATH, 'opcodes', 'chain_id.tz') - originate(client, session, path, 'Unit', 0) - client.call('bootstrap2', "chain_id", []) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_origination(self, client: Client, session): - path = os.path.join( - CONTRACT_PATH, 'mini_scenarios', 'authentication.tz' - ) - pubkey = IDENTITIES['bootstrap1']['public'] - originate(client, session, path, f'Pair 0 "{pubkey}"', 1000) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_first_run( - self, client: Client, session: dict - ): - destination = IDENTITIES['bootstrap2']['identity'] - operation = ( - '{DROP; NIL operation; ' - + f'PUSH address "{destination}"; ' - + 'CONTRACT unit; ASSERT_SOME; PUSH mutez 1000; UNIT; ' - + 'TRANSFER_TOKENS; CONS}' - ) - chain_id = client.rpc('get', 'chains/main/chain_id') - contract_address = session['contract'] - packed = client.pack( - f'Pair (Pair "{chain_id}" "{contract_address}") ' - + f'(Pair {operation} 0)', - 'pair (pair chain_id address)' - + '(pair (lambda unit (list operation)) nat)', - ) - signature = client.sign_bytes_of_string(packed, "bootstrap1") - client.call( - 'bootstrap2', - 'authentication', - ['--arg', f'Pair {operation} \"{signature}\"'], - ) - utils.bake(client, bake_for='bootstrap5') - - @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 3de6b39c49b8..09bc5db1c214 100644 --- a/tests_python/tests_015/test_contract.py +++ b/tests_python/tests_015/test_contract.py @@ -874,50 +874,6 @@ class TestView: originate(client, session, path, '4', 0) -@pytest.mark.incremental -@pytest.mark.contract -class TestChainId: - def test_chain_id_opcode(self, client: Client, session: dict): - path = os.path.join(CONTRACT_PATH, 'opcodes', 'chain_id.tz') - originate(client, session, path, 'Unit', 0) - client.call('bootstrap2', "chain_id", []) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_origination(self, client: Client, session): - path = os.path.join( - CONTRACT_PATH, 'mini_scenarios', 'authentication.tz' - ) - pubkey = IDENTITIES['bootstrap1']['public'] - originate(client, session, path, f'Pair 0 "{pubkey}"', 1000) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_first_run( - self, client: Client, session: dict - ): - destination = IDENTITIES['bootstrap2']['identity'] - operation = ( - '{DROP; NIL operation; ' - + f'PUSH address "{destination}"; ' - + 'CONTRACT unit; ASSERT_SOME; PUSH mutez 1000; UNIT; ' - + 'TRANSFER_TOKENS; CONS}' - ) - chain_id = client.rpc('get', 'chains/main/chain_id') - contract_address = session['contract'] - packed = client.pack( - f'Pair (Pair "{chain_id}" "{contract_address}") ' - + f'(Pair {operation} 0)', - 'pair (pair chain_id address)' - + '(pair (lambda unit (list operation)) nat)', - ) - signature = client.sign_bytes_of_string(packed, "bootstrap1") - client.call( - 'bootstrap2', - 'authentication', - ['--arg', f'Pair {operation} \"{signature}\"'], - ) - utils.bake(client, bake_for='bootstrap5') - - @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 3de6b39c49b8..09bc5db1c214 100644 --- a/tests_python/tests_alpha/test_contract.py +++ b/tests_python/tests_alpha/test_contract.py @@ -874,50 +874,6 @@ class TestView: originate(client, session, path, '4', 0) -@pytest.mark.incremental -@pytest.mark.contract -class TestChainId: - def test_chain_id_opcode(self, client: Client, session: dict): - path = os.path.join(CONTRACT_PATH, 'opcodes', 'chain_id.tz') - originate(client, session, path, 'Unit', 0) - client.call('bootstrap2', "chain_id", []) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_origination(self, client: Client, session): - path = os.path.join( - CONTRACT_PATH, 'mini_scenarios', 'authentication.tz' - ) - pubkey = IDENTITIES['bootstrap1']['public'] - originate(client, session, path, f'Pair 0 "{pubkey}"', 1000) - utils.bake(client, bake_for='bootstrap5') - - def test_chain_id_authentication_first_run( - self, client: Client, session: dict - ): - destination = IDENTITIES['bootstrap2']['identity'] - operation = ( - '{DROP; NIL operation; ' - + f'PUSH address "{destination}"; ' - + 'CONTRACT unit; ASSERT_SOME; PUSH mutez 1000; UNIT; ' - + 'TRANSFER_TOKENS; CONS}' - ) - chain_id = client.rpc('get', 'chains/main/chain_id') - contract_address = session['contract'] - packed = client.pack( - f'Pair (Pair "{chain_id}" "{contract_address}") ' - + f'(Pair {operation} 0)', - 'pair (pair chain_id address)' - + '(pair (lambda unit (list operation)) nat)', - ) - signature = client.sign_bytes_of_string(packed, "bootstrap1") - client.call( - 'bootstrap2', - 'authentication', - ['--arg', f'Pair {operation} \"{signature}\"'], - ) - utils.bake(client, bake_for='bootstrap5') - - @pytest.mark.contract class TestScriptHashMultiple: """Test octez-client hash script with diffent number and type of diff --git a/tezt/tests/main.ml b/tezt/tests/main.ml index b1937981c2ba..57a3db2c77ea 100644 --- a/tezt/tests/main.ml +++ b/tezt/tests/main.ml @@ -153,6 +153,7 @@ let register_protocol_tests_that_use_supports_correctly () = Run_script.register ~protocols ; Runtime_script_failure.register ~protocols ; Sapling.register ~protocols ; + Script_chain_id.register ~protocols ; Script_execution_ordering.register ~protocols ; Script_hash_regression.register ~protocols ; Self_address_transfer.register ~protocols ; diff --git a/tezt/tests/script_chain_id.ml b/tezt/tests/script_chain_id.ml new file mode 100644 index 000000000000..7857eef061fc --- /dev/null +++ b/tezt/tests/script_chain_id.ml @@ -0,0 +1,126 @@ +(*****************************************************************************) +(* *) +(* 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: Michelson / Opcodes + Invocation: dune exec tezt/tests/main.exe -- --file script_chain_id.ml + Subject: Tests of the [CHAIN_ID] Michelson instruction. +*) + +let contract_path ?kind protocol contract = + let protocol = + match protocol with + | Protocol.Alpha -> "alpha" + | _ -> sf "%03d" @@ Protocol.number protocol + in + let preamble = "tests_python" // sf "contracts_%s" protocol in + let contract = contract ^ ".tz" in + match kind with + | None -> preamble // contract + | Some kind -> preamble // kind // contract + +let test_chain_id_opcode = + Protocol.register_test + ~__FILE__ + ~title:"Chain ID Opcode" + ~tags:["client"; "contract"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + let alias = "chain_id" in + let* _contract = + Client.originate_contract + ~amount:Tez.zero + ~src:Constant.bootstrap2.alias + ~burn_cap:Tez.one + ~init:"Unit" + client + ~alias + ~prg:(contract_path protocol ~kind:"opcodes" alias) + in + Client.transfer + client + ~amount:Tez.zero + ~giver:Constant.bootstrap2.alias + ~receiver:alias + +let test_chain_id_authentication = + Protocol.register_test + ~__FILE__ + ~title:"Chain ID Authentication" + ~tags:["client"; "contract"] + @@ fun protocol -> + let* client = Client.init_mockup ~protocol () in + Log.info "Originate contract" ; + let pubkey = Account.Bootstrap.keys.(0).public_key in + let alias = "authentication" in + let* contract = + Client.originate_contract + ~amount:(Tez.of_int 1000) + ~src:Constant.bootstrap2.alias + ~burn_cap:Tez.one + ~init:(sf {|Pair 0 "%s"|} pubkey) + client + ~alias + ~prg:(contract_path protocol ~kind:"mini_scenarios" alias) + in + Log.info "First run" ; + let destination = Account.Bootstrap.keys.(1).public_key_hash in + let operation = + sf + {|{DROP; NIL operation; PUSH address "%s"; CONTRACT unit; ASSERT_SOME; PUSH mutez 1000; UNIT; TRANSFER_TOKENS; CONS}|} + destination + in + let* chain_id = RPC.Client.call client @@ RPC.get_chain_chain_id () in + let contract_address = contract in + let* packed = + let data = + sf + {|Pair (Pair "%s" "%s") (Pair %s 0)|} + chain_id + contract_address + operation + in + let typ = + {|pair (pair chain_id address) (pair (lambda unit (list operation)) nat)|} + in + let* res = Client.hash_data client ~data ~typ in + match res with + | [] -> failwith "packed not found" + | (_, packed) :: _ -> return packed + in + let* signature = + Client.sign_bytes client ~signer:Constant.bootstrap1.alias ~data:packed + in + Client.transfer + client + ~amount:Tez.zero + ~giver:Constant.bootstrap2.alias + ~receiver:alias + ~arg:(sf {|Pair %s "%s"|} operation signature) + +let register ~protocols = + test_chain_id_opcode protocols ; + test_chain_id_authentication protocols -- GitLab