From acd97140520b9da50c16c13defc0d161c7b53999 Mon Sep 17 00:00:00 2001 From: bout3fiddy <11488427+bout3fiddy@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:34:12 +0200 Subject: [PATCH 1/6] use dict index to get value instead of guessing filename --- boa_zksync/compile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boa_zksync/compile.py b/boa_zksync/compile.py index 7baa688..6c85519 100644 --- a/boa_zksync/compile.py +++ b/boa_zksync/compile.py @@ -40,7 +40,8 @@ def compile_zksync( with open(filename) as file: source_code = file.read() - compile_output = output[filename.removeprefix("./")] + # remove prefix if exists: + compile_output, vyper_version, zkvyper_version = output.values() bytecode = to_bytes(compile_output.pop("bytecode")) return ZksyncCompilerData( contract_name, source_code, compiler_args, bytecode, **compile_output From fd5ded67a91200effc67b4fd1c0658d4f49f876f Mon Sep 17 00:00:00 2001 From: bout3fiddy <11488427+bout3fiddy@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:43:23 +0200 Subject: [PATCH 2/6] add checks to ensure key names and num keys, if changed, breaks the loading of the compiled output into a zkvyper compiler data object --- boa_zksync/compile.py | 6 +++--- boa_zksync/compiler_utils.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/boa_zksync/compile.py b/boa_zksync/compile.py index 6c85519..309258d 100644 --- a/boa_zksync/compile.py +++ b/boa_zksync/compile.py @@ -7,6 +7,7 @@ from boa.rpc import to_bytes +from boa_zksync.compiler_utils import get_contract_key from boa_zksync.types import ZksyncCompilerData @@ -39,9 +40,8 @@ def compile_zksync( if source_code is None: with open(filename) as file: source_code = file.read() - - # remove prefix if exists: - compile_output, vyper_version, zkvyper_version = output.values() + + compile_output = get_contract_key(output) bytecode = to_bytes(compile_output.pop("bytecode")) return ZksyncCompilerData( contract_name, source_code, compiler_args, bytecode, **compile_output diff --git a/boa_zksync/compiler_utils.py b/boa_zksync/compiler_utils.py index 10d0d94..b6746f5 100644 --- a/boa_zksync/compiler_utils.py +++ b/boa_zksync/compiler_utils.py @@ -75,3 +75,17 @@ def detect_expr_type(source_code, contract): except InvalidType: pass return None + + +def get_contract_key(output): + # we need this helper method to get the correct key containing compiler output + # from the compiler. Assuming key names could change and also assuming that the + # number of keys could change, this method breaks if any of that happens: + + excluded_keys = {"version", "zk_version"} + contract_keys = set(output.keys()) - excluded_keys + + if len(contract_keys) != 1: + raise ValueError(f"Expected exactly one contract key, found {len(contract_keys)}") + + return next(iter(contract_keys)) \ No newline at end of file From e574dce257e29a9ba31f4a98198b53f84e22bde6 Mon Sep 17 00:00:00 2001 From: bout3fiddy <11488427+bout3fiddy@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:48:25 +0200 Subject: [PATCH 3/6] directly return output instead of key --- boa_zksync/compile.py | 4 ++-- boa_zksync/compiler_utils.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/boa_zksync/compile.py b/boa_zksync/compile.py index 309258d..12b0059 100644 --- a/boa_zksync/compile.py +++ b/boa_zksync/compile.py @@ -7,7 +7,7 @@ from boa.rpc import to_bytes -from boa_zksync.compiler_utils import get_contract_key +from boa_zksync.compiler_utils import get_compiler_output from boa_zksync.types import ZksyncCompilerData @@ -41,7 +41,7 @@ def compile_zksync( with open(filename) as file: source_code = file.read() - compile_output = get_contract_key(output) + compile_output = get_compiler_output(output) bytecode = to_bytes(compile_output.pop("bytecode")) return ZksyncCompilerData( contract_name, source_code, compiler_args, bytecode, **compile_output diff --git a/boa_zksync/compiler_utils.py b/boa_zksync/compiler_utils.py index b6746f5..8116e3f 100644 --- a/boa_zksync/compiler_utils.py +++ b/boa_zksync/compiler_utils.py @@ -77,7 +77,7 @@ def detect_expr_type(source_code, contract): return None -def get_contract_key(output): +def get_compiler_output(output): # we need this helper method to get the correct key containing compiler output # from the compiler. Assuming key names could change and also assuming that the # number of keys could change, this method breaks if any of that happens: @@ -88,4 +88,4 @@ def get_contract_key(output): if len(contract_keys) != 1: raise ValueError(f"Expected exactly one contract key, found {len(contract_keys)}") - return next(iter(contract_keys)) \ No newline at end of file + return output[next(iter(contract_keys))] \ No newline at end of file From e42232a8cbcac55996fddb95fdf5afd7d2a2f233 Mon Sep 17 00:00:00 2001 From: bout3fiddy <11488427+bout3fiddy@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:51:20 +0200 Subject: [PATCH 4/6] add tests --- tests/test_get_compiler_output.py | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/test_get_compiler_output.py diff --git a/tests/test_get_compiler_output.py b/tests/test_get_compiler_output.py new file mode 100644 index 0000000..d7fcaa7 --- /dev/null +++ b/tests/test_get_compiler_output.py @@ -0,0 +1,39 @@ +import pytest + +from boa_zksync.compiler_utils import get_compiler_output + + +def test_get_compiler_output(): + + output_dict = { + "blabla": 123, + "zk_version": 456, + "version": 789, + } + + get_compiler_output(output_dict) == 123 + + +def test_get_compiler_output_revert_too_many_keys(): + + output_dict = { + "blabla": 123, + "zk_version": 456, + "version": 789, + "new_compiler_output_key": 101112, + } + + with pytest.raises(ValueError, match="Expected exactly one contract key, found 2"): + get_compiler_output(output_dict) + + +def test_get_compiler_output_revert_unexpected_key(): + + output_dict = { + "blabla": 123, + "zk_versions": 456, + "version": 789, + } + + with pytest.raises(ValueError, match="Expected exactly one contract key, found 2"): + get_compiler_output(output_dict) From 81df1705b7bdd0e3d6d3a194b888581925f2ee1d Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 15 Aug 2024 17:09:50 +0200 Subject: [PATCH 5/6] Omit proxy key, update error message --- boa_zksync/compile.py | 2 +- boa_zksync/compiler_utils.py | 12 ++++----- boa_zksync/deployer.py | 1 + tests/test_get_compiler_output.py | 44 +++++++++++++++---------------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/boa_zksync/compile.py b/boa_zksync/compile.py index 12b0059..d005fa6 100644 --- a/boa_zksync/compile.py +++ b/boa_zksync/compile.py @@ -40,7 +40,7 @@ def compile_zksync( if source_code is None: with open(filename) as file: source_code = file.read() - + compile_output = get_compiler_output(output) bytecode = to_bytes(compile_output.pop("bytecode")) return ZksyncCompilerData( diff --git a/boa_zksync/compiler_utils.py b/boa_zksync/compiler_utils.py index 8116e3f..66fd7df 100644 --- a/boa_zksync/compiler_utils.py +++ b/boa_zksync/compiler_utils.py @@ -81,11 +81,11 @@ def get_compiler_output(output): # we need this helper method to get the correct key containing compiler output # from the compiler. Assuming key names could change and also assuming that the # number of keys could change, this method breaks if any of that happens: - - excluded_keys = {"version", "zk_version"} + + excluded_keys = {"version", "zk_version", "__VYPER_MINIMAL_PROXY_CONTRACT"} contract_keys = set(output.keys()) - excluded_keys - + if len(contract_keys) != 1: - raise ValueError(f"Expected exactly one contract key, found {len(contract_keys)}") - - return output[next(iter(contract_keys))] \ No newline at end of file + raise ValueError(f"Expected exactly one contract key, found {contract_keys}") + + return output[next(iter(contract_keys))] diff --git a/boa_zksync/deployer.py b/boa_zksync/deployer.py index 2299f85..b8e1457 100644 --- a/boa_zksync/deployer.py +++ b/boa_zksync/deployer.py @@ -92,6 +92,7 @@ def env(self) -> "ZksyncEnv": """ env = Env.get_singleton() from boa_zksync.environment import ZksyncEnv + assert isinstance( env, ZksyncEnv ), "ZksyncDeployer can only be used in zkSync environments" diff --git a/tests/test_get_compiler_output.py b/tests/test_get_compiler_output.py index d7fcaa7..2eed8eb 100644 --- a/tests/test_get_compiler_output.py +++ b/tests/test_get_compiler_output.py @@ -4,36 +4,34 @@ def test_get_compiler_output(): - - output_dict = { - "blabla": 123, - "zk_version": 456, - "version": 789, - } - - get_compiler_output(output_dict) == 123 - -def test_get_compiler_output_revert_too_many_keys(): - + output_dict = {"blabla": 123, "zk_version": 456, "version": 789} + + assert get_compiler_output(output_dict) == 123 + + +def test_get_compiler_output_too_many_keys(): + output_dict = { "blabla": 123, "zk_version": 456, "version": 789, "new_compiler_output_key": 101112, } - - with pytest.raises(ValueError, match="Expected exactly one contract key, found 2"): + + with pytest.raises( + ValueError, + match="Expected exactly one contract key, found {'blabla', 'new_compiler_output_key'}", + ): get_compiler_output(output_dict) - -def test_get_compiler_output_revert_unexpected_key(): - - output_dict = { - "blabla": 123, - "zk_versions": 456, - "version": 789, - } - - with pytest.raises(ValueError, match="Expected exactly one contract key, found 2"): + +def test_get_compiler_output_unexpected_key(): + + output_dict = {"blabla": 123, "zk_versions": 456, "version": 789} + + with pytest.raises( + ValueError, + match="Expected exactly one contract key, found {'blabla', 'zk_versions'}", + ): get_compiler_output(output_dict) From 9f5464bbf2af4dbf4c10d3350d0a93236896fe15 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 20 Aug 2024 08:42:44 +0200 Subject: [PATCH 6/6] Make tests deterministic --- boa_zksync/compiler_utils.py | 3 ++- tests/test_get_compiler_output.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/boa_zksync/compiler_utils.py b/boa_zksync/compiler_utils.py index 66fd7df..b85e14a 100644 --- a/boa_zksync/compiler_utils.py +++ b/boa_zksync/compiler_utils.py @@ -86,6 +86,7 @@ def get_compiler_output(output): contract_keys = set(output.keys()) - excluded_keys if len(contract_keys) != 1: - raise ValueError(f"Expected exactly one contract key, found {contract_keys}") + unexpected = ", ".join(sorted(contract_keys)) + raise ValueError(f"Expected exactly one contract key, found {unexpected}") return output[next(iter(contract_keys))] diff --git a/tests/test_get_compiler_output.py b/tests/test_get_compiler_output.py index 2eed8eb..ae262ac 100644 --- a/tests/test_get_compiler_output.py +++ b/tests/test_get_compiler_output.py @@ -21,7 +21,7 @@ def test_get_compiler_output_too_many_keys(): with pytest.raises( ValueError, - match="Expected exactly one contract key, found {'blabla', 'new_compiler_output_key'}", + match="Expected exactly one contract key, found blabla, new_compiler_output_key", ): get_compiler_output(output_dict) @@ -32,6 +32,6 @@ def test_get_compiler_output_unexpected_key(): with pytest.raises( ValueError, - match="Expected exactly one contract key, found {'blabla', 'zk_versions'}", + match="Expected exactly one contract key, found blabla, zk_versions", ): get_compiler_output(output_dict)