diff --git a/tests_python/tests_014/test_mockup.py b/tests_python/tests_014/test_mockup.py index 66b29e95082c37be319cc323c35312b6cafbdad1..0de1f26906d6ed6c251cd6e2139ab6a9c56913ca 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 @@ -104,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 c0dd5e5a94ba98ed0970b0c3fe8bcb417e7a443d..85aec03afa31c8e9e7c8ceb71d605fb805b43531 100644 --- a/tests_python/tests_015/test_mockup.py +++ b/tests_python/tests_015/test_mockup.py @@ -27,133 +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( - '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 @@ -186,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 258e9061a78181d7c890aa2f5e844582a174e968..c8de37b60abdcdb061d0c27b340a1e91ccb2a15a 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 @@ -106,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/lib_tezos/client.ml b/tezt/lib_tezos/client.ml index 814c3f0ff170e47a439cc8094add3096f06d10be..0deca8832705ce2d7c214b1244ad66da96867d56 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 729493872c7b023e9acc15079535ceb3b09c43a1..f314774d2cb5c4487a448ee91f7147ec2168a168 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 diff --git a/tezt/tests/mockup.ml b/tezt/tests/mockup.ml index 2e6a64b0ef828b55ec7090b2932fbaee65ce76a0..c469fcb86ff8761399143eae6092ed4e62a041c8 100644 --- a/tezt/tests/mockup.ml +++ b/tezt/tests/mockup.ml @@ -677,6 +677,100 @@ 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 + +(* 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 ; @@ -691,7 +785,9 @@ 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 ; + test_create_mockup_custom_bootstrap_accounts protocols let register_global_constants ~protocols = test_register_global_constant_success protocols ;