From 63187263509f8c9071ceeeb4e4cde2acfd736578 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Wed, 28 Sep 2022 13:53:01 +0200 Subject: [PATCH 1/4] Tests/python: remove mockup tests accidentally reintroduced in !6419 --- tests_python/tests_015/test_mockup.py | 80 --------------------------- 1 file changed, 80 deletions(-) diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index c0dd5e5a94ba..d4acfe4da5d4 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -27,86 +27,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -@pytest.mark.client -def test_list_mockup_protocols(sandbox: Sandbox): - """Executes `octez-client list mockup protocols` - The call must succeed and return a non empty list. - """ - try: - client = sandbox.create_client() - protocols = client.list_mockup_protocols().mockup_protocols - assert protocols - finally: - shutil.rmtree(client.base_dir) - - -@pytest.mark.client -def test_create_mockup_dir_exists_nonempty(sandbox: Sandbox): - """Executes `octez-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is a non empty directory which is NOT a mockup - directory. The call must fail. - """ - with tempfile.TemporaryDirectory(prefix='octez-client.') as base_dir: - # Make the directory not empty - with open(os.path.join(base_dir, "whatever"), "w") as handle: - handle.write("") - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - assert res == CreateMockupResult.DIR_NOT_EMPTY - - -@pytest.mark.client -def test_retrieve_addresses(mockup_client: Client): - """Retrieves known addresses of a fresh mockup. - The call must succeed. - """ - addresses = mockup_client.get_known_addresses().wallet - assert addresses == { - 'bootstrap1': 'tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx', - 'bootstrap2': 'tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN', - 'bootstrap3': 'tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU', - 'bootstrap4': 'tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv', - 'bootstrap5': 'tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv', - } - - -@pytest.mark.client -def test_create_mockup_already_initialized(mockup_client: Client): - """Executes `octez-client --base-dir /tmp/mdir create mockup` - when /tmp/mdir is not fresh. - The call must fail. - """ - # mockup was created already by fixture, try to create it second time: - res = mockup_client.create_mockup( - protocol=protocol.HASH, check=False - ).create_mockup_result - # it should fail: - assert res == CreateMockupResult.ALREADY_INITIALIZED - - -@pytest.mark.client -def test_transfer(mockup_client: Client): - """Executes `octez-client --base-dir /tmp/mdir -M mockup - transfer 1 from bootstrap1 to bootstrap2` - in a valid mockup environment. - The call must succeed and the balances must be updated correctly. - """ - giver = "bootstrap1" - receiver = "bootstrap2" - transferred = 1.0 - - giver_balance_before = mockup_client.get_balance(giver) - receiver_balance_before = mockup_client.get_balance(receiver) - mockup_client.transfer(transferred, giver, receiver) - giver_balance_after = mockup_client.get_balance(giver) - receiver_balance_after = mockup_client.get_balance(receiver) - - assert giver_balance_after < giver_balance_before - transferred - assert receiver_balance_after == receiver_balance_before + transferred - - # It's impossible to guess values of chain_id, these ones have been # obtained by looking at the output of `compute chain id from seed` @pytest.mark.parametrize( -- GitLab From fc53a2e0256225236a7ef558e9536b25e915a712 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Fri, 23 Sep 2022 17:52:59 +0200 Subject: [PATCH 2/4] Tezt: migrate [create_mockup_custom_constants] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 47 ------------------------- tests_python/tests_015/test_mockup.py | 47 ------------------------- tests_python/tests_alpha/test_mockup.py | 47 ------------------------- tezt/tests/mockup.ml | 43 +++++++++++++++++++++- 4 files changed, 42 insertions(+), 142 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index 66b29e95082c..6da010244957 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -25,53 +25,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -# It's impossible to guess values of chain_id, these ones have been -# obtained by looking at the output of `compute chain id from seed` -@pytest.mark.parametrize( - 'chain_id', - [ - "NetXcqTGZX74DxG", - "NetXaFDF7xZQCpR", - "NetXkKbtqncJcAz", - "NetXjjE5cZUeWPy", - "NetXi7C1pyLhQNe", - ], -) -@pytest.mark.parametrize( - 'initial_timestamp', ["2020-07-21T17:11:10+02:00", "1970-01-01T00:00:00Z"] -) -@pytest.mark.client -def test_create_mockup_custom_constants( - sandbox: Sandbox, chain_id: str, initial_timestamp: str -): - """Tests `octez-client create mockup` --protocols-constants argument - The call must succeed. - - Args: - mockup_client: the client to use - chain_id (str): the string to pass for field `chain_id` - initial_timestamp(str): an ISO-8601 formatted date string - """ - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-custom-constants', mode='w+t' - ) as json_file: - json_data = { - "hard_gas_limit_per_operation": "400000", - "chain_id": chain_id, - "initial_timestamp": initial_timestamp, - } - json.dump(json_data, json_file) - json_file.flush() - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, protocol_constants_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - - def _create_accounts_list(): """ Returns a list of dictionary with 3 entries, that are diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index d4acfe4da5d4..b96eef55efca 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -27,53 +27,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -# It's impossible to guess values of chain_id, these ones have been -# obtained by looking at the output of `compute chain id from seed` -@pytest.mark.parametrize( - 'chain_id', - [ - "NetXcqTGZX74DxG", - "NetXaFDF7xZQCpR", - "NetXkKbtqncJcAz", - "NetXjjE5cZUeWPy", - "NetXi7C1pyLhQNe", - ], -) -@pytest.mark.parametrize( - 'initial_timestamp', ["2020-07-21T17:11:10+02:00", "1970-01-01T00:00:00Z"] -) -@pytest.mark.client -def test_create_mockup_custom_constants( - sandbox: Sandbox, chain_id: str, initial_timestamp: str -): - """Tests `octez-client create mockup` --protocols-constants argument - The call must succeed. - - Args: - mockup_client: the client to use - chain_id (str): the string to pass for field `chain_id` - initial_timestamp(str): an ISO-8601 formatted date string - """ - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-custom-constants', mode='w+t' - ) as json_file: - json_data = { - "hard_gas_limit_per_operation": "400000", - "chain_id": chain_id, - "initial_timestamp": initial_timestamp, - } - json.dump(json_data, json_file) - json_file.flush() - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, protocol_constants_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - - def _create_accounts_list(): """ Returns a list of dictionary with 3 entries, that are diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index 258e9061a781..4a02b4aa5c66 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -27,53 +27,6 @@ _BA_FLAG = "bootstrap-accounts" _PC_FLAG = "protocol-constants" -# It's impossible to guess values of chain_id, these ones have been -# obtained by looking at the output of `compute chain id from seed` -@pytest.mark.parametrize( - 'chain_id', - [ - "NetXcqTGZX74DxG", - "NetXaFDF7xZQCpR", - "NetXkKbtqncJcAz", - "NetXjjE5cZUeWPy", - "NetXi7C1pyLhQNe", - ], -) -@pytest.mark.parametrize( - 'initial_timestamp', ["2020-07-21T17:11:10+02:00", "1970-01-01T00:00:00Z"] -) -@pytest.mark.client -def test_create_mockup_custom_constants( - sandbox: Sandbox, chain_id: str, initial_timestamp: str -): - """Tests `octez-client create mockup` --protocols-constants argument - The call must succeed. - - Args: - mockup_client: the client to use - chain_id (str): the string to pass for field `chain_id` - initial_timestamp(str): an ISO-8601 formatted date string - """ - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-custom-constants', mode='w+t' - ) as json_file: - json_data = { - "hard_gas_limit_per_operation": "400000", - "chain_id": chain_id, - "initial_timestamp": initial_timestamp, - } - json.dump(json_data, json_file) - json_file.flush() - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, protocol_constants_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - - def _create_accounts_list(): """ Returns a list of dictionary with 3 entries, that are diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index 2e6a64b0ef82..e6e925f87f11 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -677,6 +677,46 @@ let test_create_mockup_already_initialized = in unit +(* Tests [tezos-client create mockup]s [--protocols-constants] + argument. The call must succeed. *) +let test_create_mockup_custom_constants = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Create mockup with mockup-custom protocol constants." + ~tags:["mockup"; "client"; "mockup_protocol_constants"] + @@ fun protocol -> + let iter = Fun.flip Lwt_list.iter_s in + (* [chain_id] is the string to pass for field [chain_id]. It's + impossible to guess values of [chain_id], these ones have been * + obtained by looking at the output of [compute chain id from + seed]. *) + iter + [ + "NetXcqTGZX74DxG"; + "NetXaFDF7xZQCpR"; + "NetXkKbtqncJcAz"; + "NetXjjE5cZUeWPy"; + "NetXi7C1pyLhQNe"; + ] + @@ fun chain_id -> + (* initial_timestamp is an ISO-8601 formatted date string *) + iter ["2020-07-21T17:11:10+02:00"; "1970-01-01T00:00:00Z"] + @@ fun initial_timestamp -> + let parameter_file = Temp.file "tezos-custom-constants.json" in + let json_fields = + [ + ("hard_gas_limit_per_operation", `String "400000"); + ("chain_id", `String chain_id); + ("initial_timestamp", `String initial_timestamp); + ] + in + let json_data : JSON.u = `O json_fields in + JSON.encode_to_file_u parameter_file json_data ; + + let client = Client.create_with_mode Client.Mockup in + let* () = Client.create_mockup ~protocol ~parameter_file client in + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -691,7 +731,8 @@ let register ~protocols = test_storage_from_file protocols ; test_create_mockup_dir_exists_nonempty protocols ; test_retrieve_addresses protocols ; - test_create_mockup_already_initialized protocols + test_create_mockup_already_initialized protocols ; + test_create_mockup_custom_constants protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab From 493462f4c463725171eeef2f8579e5af485354e0 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Fri, 23 Sep 2022 18:16:29 +0200 Subject: [PATCH 3/4] Tezt/Client: add [--bootstrap-accounts] to [create mockup] --- tezt/lib_tezos/client.ml | 15 +++++++++++---- tezt/lib_tezos/client.mli | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/tezt/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 814c3f0ff170..0deca8832705 100644 --- a/tezt/lib_tezos/client.ml +++ b/tezt/lib_tezos/client.ml @@ -939,19 +939,26 @@ let get_balance_for ?endpoint ~account client = in return @@ Tez.parse_floating output -let spawn_create_mockup ?(sync_mode = Synchronous) ?parameter_file ~protocol - client = +let spawn_create_mockup ?(sync_mode = Synchronous) ?parameter_file + ?bootstrap_accounts_file ~protocol client = let cmd = let common = ["--protocol"; Protocol.hash protocol; "create"; "mockup"] in (match sync_mode with | Synchronous -> common | Asynchronous -> common @ ["--asynchronous"]) @ optional_arg "protocol-constants" Fun.id parameter_file + @ optional_arg "bootstrap-accounts" Fun.id bootstrap_accounts_file in spawn_command client cmd -let create_mockup ?sync_mode ?parameter_file ~protocol client = - spawn_create_mockup ?sync_mode ?parameter_file ~protocol client +let create_mockup ?sync_mode ?parameter_file ?bootstrap_accounts_file ~protocol + client = + spawn_create_mockup + ?sync_mode + ?parameter_file + ?bootstrap_accounts_file + ~protocol + client |> Process.check let spawn_submit_proposals ?(key = Constant.bootstrap1.alias) ?(wait = "none") diff --git a/tezt/lib_tezos/client.mli b/tezt/lib_tezos/client.mli index 729493872c7b..f314774d2cb5 100644 --- a/tezt/lib_tezos/client.mli +++ b/tezt/lib_tezos/client.mli @@ -731,6 +731,7 @@ val spawn_get_balance_for : val create_mockup : ?sync_mode:mockup_sync_mode -> ?parameter_file:string -> + ?bootstrap_accounts_file:string -> protocol:Protocol.t -> t -> unit Lwt.t @@ -739,6 +740,7 @@ val create_mockup : val spawn_create_mockup : ?sync_mode:mockup_sync_mode -> ?parameter_file:string -> + ?bootstrap_accounts_file:string -> protocol:Protocol.t -> t -> Process.t -- GitLab From a562ab6bdf9ba043a6b56998d2d62accab39bf9c Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Fri, 23 Sep 2022 18:18:58 +0200 Subject: [PATCH 4/4] Tezt: migrate [create_mockup..._bootstrap_accounts] of [test_mockup.py] --- tests_python/tests_014/test_mockup.py | 28 ------------ tests_python/tests_015/test_mockup.py | 28 ------------ tests_python/tests_alpha/test_mockup.py | 28 ------------ tezt/tests/mockup.ml | 57 ++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 85 deletions(-) diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index 6da010244957..0de1f26906d6 100644 --- a/tests_python/tests_014/test_mockup.py +++ b/tests_python/tests_014/test_mockup.py @@ -57,34 +57,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_create_mockup_custom_bootstrap_accounts(sandbox: Sandbox): - """Tests `octez-client create mockup` --bootstrap-accounts argument - The call must succeed. - """ - accounts_list = _create_accounts_list() - - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-bootstrap-accounts', mode='w+t' - ) as json_file: - json.dump(accounts_list, json_file) - json_file.flush() - # Follow pattern of mockup_client fixture: - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, bootstrap_accounts_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - addresses_result = mock_client.get_known_addresses() - names_sent = sorted([account["name"] for account in accounts_list]) - names_witnessed = sorted(list(addresses_result.wallet.keys())) - assert names_sent == names_witnessed - - @pytest.mark.client def test_transfer_bad_base_dir(sandbox: Sandbox): """Executes `octez-client --base-dir /tmp/mdir create mockup` diff --git a/tests_python/tests_015/test_mockup.py b/tests_python/tests_015/test_mockup.py index b96eef55efca..85aec03afa31 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -59,34 +59,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_create_mockup_custom_bootstrap_accounts(sandbox: Sandbox): - """Tests `octez-client create mockup` --bootstrap-accounts argument - The call must succeed. - """ - accounts_list = _create_accounts_list() - - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-bootstrap-accounts', mode='w+t' - ) as json_file: - json.dump(accounts_list, json_file) - json_file.flush() - # Follow pattern of mockup_client fixture: - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, bootstrap_accounts_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - addresses_result = mock_client.get_known_addresses() - names_sent = sorted([account["name"] for account in accounts_list]) - names_witnessed = sorted(list(addresses_result.wallet.keys())) - assert names_sent == names_witnessed - - @pytest.mark.client def test_transfer_bad_base_dir(sandbox: Sandbox): """Executes `octez-client --base-dir /tmp/mdir create mockup` diff --git a/tests_python/tests_alpha/test_mockup.py b/tests_python/tests_alpha/test_mockup.py index 4a02b4aa5c66..c8de37b60abd 100644 --- a/tests_python/tests_alpha/test_mockup.py +++ b/tests_python/tests_alpha/test_mockup.py @@ -59,34 +59,6 @@ def _create_accounts_list(): return accounts_list -@pytest.mark.client -def test_create_mockup_custom_bootstrap_accounts(sandbox: Sandbox): - """Tests `octez-client create mockup` --bootstrap-accounts argument - The call must succeed. - """ - accounts_list = _create_accounts_list() - - # Use another directory so that the constants change takes effect - with tempfile.TemporaryDirectory( - prefix='octez-client.' - ) as base_dir, tempfile.NamedTemporaryFile( - prefix='tezos-bootstrap-accounts', mode='w+t' - ) as json_file: - json.dump(accounts_list, json_file) - json_file.flush() - # Follow pattern of mockup_client fixture: - unmanaged_client = sandbox.create_client(base_dir=base_dir) - res = unmanaged_client.create_mockup( - protocol=protocol.HASH, bootstrap_accounts_file=json_file.name - ).create_mockup_result - assert res == CreateMockupResult.OK - mock_client = sandbox.create_client(base_dir=base_dir, mode="mockup") - addresses_result = mock_client.get_known_addresses() - names_sent = sorted([account["name"] for account in accounts_list]) - names_witnessed = sorted(list(addresses_result.wallet.keys())) - assert names_sent == names_witnessed - - @pytest.mark.client def test_transfer_bad_base_dir(sandbox: Sandbox): """Executes `octez-client --base-dir /tmp/mdir create mockup` diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index e6e925f87f11..c469fcb86ff8 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -717,6 +717,60 @@ let test_create_mockup_custom_constants = let* () = Client.create_mockup ~protocol ~parameter_file client in unit +(* A [mockup_bootstrap_account] represents a bootstrap accounts as + taken by the [--bootstrap-accounts] option of mockup mode *) +type mockup_bootstrap_account = {name : string; sk_uri : string; amount : Tez.t} + +let test_accounts : mockup_bootstrap_account list = + [ + { + name = "bootstrap0"; + sk_uri = "edsk2uqQB9AY4FvioK2YMdfmyMrer5R8mGFyuaLLFfSRo8EoyNdht3"; + amount = Tez.of_int 2000000000000; + }; + { + name = "bootstrap1"; + sk_uri = "edsk3gUfUPyBSfrS9CCgmCiQsTCHGkviBDusMxDJstFtojtc1zcpsh"; + amount = Tez.of_int 1000000000000; + }; + ] + +let mockup_bootstrap_account_to_json {name; sk_uri; amount} : JSON.u = + `O + [ + ("name", `String name); + ("sk_uri", `String ("unencrypted:" ^ sk_uri)); + ("amount", `String (Tez.to_string amount)); + ] + +(* Tests [tezos-client create mockup --bootstrap-accounts] + argument. The call must succeed. *) +let test_create_mockup_custom_bootstrap_accounts = + Protocol.register_test + ~__FILE__ + ~title:"(Mockup) Create mockup with mockup-custom bootstrap accounts." + ~tags:["mockup"; "client"; "mockup_bootstrap_accounts"] + @@ fun protocol -> + let bootstrap_accounts_file = Temp.file "tezos-bootstrap-accounts.json" in + JSON.encode_to_file_u + bootstrap_accounts_file + (`A (List.map mockup_bootstrap_account_to_json test_accounts)) ; + + let client = Client.create_with_mode Client.Mockup in + let* () = Client.create_mockup ~protocol ~bootstrap_accounts_file client in + + let names_sent = + test_accounts |> List.map (fun {name; _} -> name) |> List.rev + in + let* accounts_witnessed = Client.list_known_addresses client in + let names_witnessed = List.map fst accounts_witnessed in + Check.( + (names_witnessed = names_sent) + ~__LOC__ + (list string) + ~error_msg:"Expected names %R, got %L") ; + unit + let register ~protocols = test_rpc_list protocols ; test_same_transfer_twice protocols ; @@ -732,7 +786,8 @@ let register ~protocols = test_create_mockup_dir_exists_nonempty protocols ; test_retrieve_addresses protocols ; test_create_mockup_already_initialized protocols ; - test_create_mockup_custom_constants protocols + test_create_mockup_custom_constants protocols ; + test_create_mockup_custom_bootstrap_accounts protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ; -- GitLab