From 60ba06349d79666aac61005f3d22b3b8b1735430 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 2 Apr 2024 21:26:28 +0200 Subject: [PATCH 1/9] Update hexbytes version and tests --- setup.py | 1 + .../builtins/codegen/test_keccak256.py | 10 +++--- .../test_external_contract_calls.py | 4 +-- .../codegen/features/test_logging.py | 36 +++++++++---------- tests/functional/syntax/test_msg_data.py | 2 +- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/setup.py b/setup.py index 933e8bfa4b..4e81b80b07 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ "eth-tester[py-evm]>=0.10.0b4,<0.11", "eth_abi>=4.0.0,<5.0.0", "py-evm>=0.10.0b4,<0.11", + "hexbytes>1.0,<2.0", "web3==6.0.0", "lark==1.1.9", "hypothesis[lark]>=6.0,<7.0", diff --git a/tests/functional/builtins/codegen/test_keccak256.py b/tests/functional/builtins/codegen/test_keccak256.py index 3b0b9f2018..8b5d14495e 100644 --- a/tests/functional/builtins/codegen/test_keccak256.py +++ b/tests/functional/builtins/codegen/test_keccak256.py @@ -18,10 +18,10 @@ def bar() -> bytes32: c = get_contract_with_gas_estimation(hash_code) for inp in (b"", b"cow", b"s" * 31, b"\xff" * 32, b"\n" * 33, b"g" * 64, b"h" * 65): - assert "0x" + c.foo(inp).hex() == keccak(inp).hex() + assert c.foo(inp).hex() == keccak(inp).hex() - assert "0x" + c.bar().hex() == keccak(b"inp").hex() - assert "0x" + c.foob().hex() == keccak(b"inp").hex() + assert c.bar().hex() == keccak(b"inp").hex() + assert c.foob().hex() == keccak(b"inp").hex() def test_hash_code2(get_contract_with_gas_estimation): @@ -96,7 +96,7 @@ def foo() -> bytes32: return x """ c = get_contract_with_gas_estimation(code) - assert "0x" + c.foo().hex() == keccak(hex_to_int(hex_val).to_bytes(32, "big")).hex() + assert c.foo().hex() == keccak(hex_to_int(hex_val).to_bytes(32, "big")).hex() def test_hash_constant_string(get_contract_with_gas_estimation, keccak): @@ -110,4 +110,4 @@ def foo() -> bytes32: return x """ c = get_contract_with_gas_estimation(code) - assert "0x" + c.foo().hex() == keccak(str_val.encode()).hex() + assert c.foo().hex() == keccak(str_val.encode()).hex() diff --git a/tests/functional/codegen/calling_convention/test_external_contract_calls.py b/tests/functional/codegen/calling_convention/test_external_contract_calls.py index 0f5f915a48..0108c7cf42 100644 --- a/tests/functional/codegen/calling_convention/test_external_contract_calls.py +++ b/tests/functional/codegen/calling_convention/test_external_contract_calls.py @@ -2503,7 +2503,7 @@ def foo(a: {typ}): pass """ c1 = get_contract(code) - sig = keccak(f"foo({typ})".encode()).hex()[:10] + sig = keccak(f"foo({typ})".encode()).to_0x_hex()[:10] encoded = abi.encode(f"({typ})", (val,)).hex() data = f"{sig}{encoded}" @@ -2528,7 +2528,7 @@ def foo(a: DynArray[{typ}, 3], b: String[5]): """ c1 = get_contract(code) - sig = keccak(f"foo({typ}[],string)".encode()).hex()[:10] + sig = keccak(f"foo({typ}[],string)".encode()).to_0x_hex()[:10] encoded = abi.encode(f"({typ}[],string)", val).hex() data = f"{sig}{encoded}" diff --git a/tests/functional/codegen/features/test_logging.py b/tests/functional/codegen/features/test_logging.py index cf64d271a9..55ab408081 100644 --- a/tests/functional/codegen/features/test_logging.py +++ b/tests/functional/codegen/features/test_logging.py @@ -34,7 +34,7 @@ def foo(): event_id = keccak(bytes("MyLog()", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -66,7 +66,7 @@ def foo(): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -96,7 +96,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bool,address)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -172,8 +172,8 @@ def bar(): event_id = keccak(bytes("MyLog(int128,address)", "utf-8")) # Event id is always the first topic - assert receipt1["logs"][0]["topics"][0] == event_id.hex() - assert receipt2["logs"][0]["topics"][0] == event_id.hex() + assert receipt1["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt2["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -224,7 +224,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -260,7 +260,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128[2],uint256[3],int128[2][2])", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { @@ -303,7 +303,7 @@ def foo(arg1: Bytes[29], arg2: Bytes[31]): event_id = keccak(bytes("MyLog(bytes,bytes,bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -341,7 +341,7 @@ def foo(_arg1: Bytes[20]): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -370,7 +370,7 @@ def foo(_arg1: Bytes[5]): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -406,7 +406,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bytes,bytes,address,address,uint256)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -453,7 +453,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -506,8 +506,8 @@ def foo(): event_id2 = keccak(bytes("YourLog(address,(uint256,bytes,(string,fixed168x10)))", "utf-8")) # Event id is always the first topic - assert logs1["topics"][0] == event_id1.hex() - assert logs2["topics"][0] == event_id2.hex() + assert logs1["topics"][0] == event_id1.to_0x_hex() + assert logs2["topics"][0] == event_id2.to_0x_hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -1081,7 +1081,7 @@ def foo(a: Bytes[36], b: int128, c: String[7]): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() topic1 = f"0x{keccak256(b'bar').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1126,7 +1126,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() topic1 = f"0x{keccak256(b'potato').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1180,7 +1180,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() topic1 = f"0x{keccak256(b'zonk').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1222,7 +1222,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.hex() + assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() topic1 = f"0x{keccak256(b'wow').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 diff --git a/tests/functional/syntax/test_msg_data.py b/tests/functional/syntax/test_msg_data.py index dfeb9cea00..40e636eefc 100644 --- a/tests/functional/syntax/test_msg_data.py +++ b/tests/functional/syntax/test_msg_data.py @@ -41,7 +41,7 @@ def foo(bar: uint256) -> Bytes[36]: contract = get_contract(code) # 2fbebd38000000000000000000000000000000000000000000000000000000000000002a - method_id = keccak(text="foo(uint256)").hex()[2:10] # 2fbebd38 + method_id = keccak(text="foo(uint256)").hex()[:8] # 2fbebd38 encoded_42 = w3.to_bytes(42).hex() # 2a expected_result = method_id + "00" * 31 + encoded_42 From 9e67463d0915ff46f063163eb4b3e43e4b00ee7a Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 2 Apr 2024 21:33:10 +0200 Subject: [PATCH 2/9] Double dependency merge conflict --- setup.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 4e81b80b07..1bcba53107 100644 --- a/setup.py +++ b/setup.py @@ -16,13 +16,12 @@ "eth-tester[py-evm]>=0.10.0b4,<0.11", "eth_abi>=4.0.0,<5.0.0", "py-evm>=0.10.0b4,<0.11", - "hexbytes>1.0,<2.0", "web3==6.0.0", "lark==1.1.9", "hypothesis[lark]>=6.0,<7.0", "eth-stdlib==0.2.7", "setuptools", - "hexbytes<1.0", + "hexbytes>1.0,<2.0", "typing_extensions", # we can remove this once dependencies upgrade to eth-rlp>=2.0 ], "lint": [ From 76c91aa259c0f19b6997044622643559db61c688 Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 2 Apr 2024 21:43:52 +0200 Subject: [PATCH 3/9] Use vyper.utils.keccak256 --- tests/conftest.py | 4 +-- .../test_external_contract_calls.py | 8 ++--- .../codegen/features/test_logging.py | 36 +++++++++---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index d0681cdf42..dc336ee789 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ from vyper.compiler.input_bundle import FilesystemInputBundle, InputBundle from vyper.compiler.settings import OptimizationLevel, Settings, _set_debug_mode from vyper.ir import compile_ir, optimizer -from vyper.utils import ERC5202_PREFIX +from vyper.utils import ERC5202_PREFIX, keccak256 # Import the base fixtures pytest_plugins = ["tests.fixtures.memorymock"] @@ -144,7 +144,7 @@ def chdir_tmp_path(tmp_path): # CMC 2024-03-01 this doesn't need to be a fixture @pytest.fixture def keccak(): - return Web3.keccak + return keccak256 @pytest.fixture diff --git a/tests/functional/codegen/calling_convention/test_external_contract_calls.py b/tests/functional/codegen/calling_convention/test_external_contract_calls.py index 0108c7cf42..f92e07e681 100644 --- a/tests/functional/codegen/calling_convention/test_external_contract_calls.py +++ b/tests/functional/codegen/calling_convention/test_external_contract_calls.py @@ -2503,9 +2503,9 @@ def foo(a: {typ}): pass """ c1 = get_contract(code) - sig = keccak(f"foo({typ})".encode()).to_0x_hex()[:10] + sig = keccak(f"foo({typ})".encode()).hex()[:8] encoded = abi.encode(f"({typ})", (val,)).hex() - data = f"{sig}{encoded}" + data = f"0x{sig}{encoded}" # Static size is short by 1 byte malformed = data[:-2] @@ -2528,9 +2528,9 @@ def foo(a: DynArray[{typ}, 3], b: String[5]): """ c1 = get_contract(code) - sig = keccak(f"foo({typ}[],string)".encode()).to_0x_hex()[:10] + sig = keccak(f"foo({typ}[],string)".encode()).hex()[:8] encoded = abi.encode(f"({typ}[],string)", val).hex() - data = f"{sig}{encoded}" + data = f"0x{sig}{encoded}" # Dynamic size is short by 1 byte malformed = data[:264] diff --git a/tests/functional/codegen/features/test_logging.py b/tests/functional/codegen/features/test_logging.py index 55ab408081..40f163d837 100644 --- a/tests/functional/codegen/features/test_logging.py +++ b/tests/functional/codegen/features/test_logging.py @@ -34,7 +34,7 @@ def foo(): event_id = keccak(bytes("MyLog()", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -66,7 +66,7 @@ def foo(): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -96,7 +96,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bool,address)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -172,8 +172,8 @@ def bar(): event_id = keccak(bytes("MyLog(int128,address)", "utf-8")) # Event id is always the first topic - assert receipt1["logs"][0]["topics"][0] == event_id.to_0x_hex() - assert receipt2["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt1["logs"][0]["topics"][0] == "0x" + event_id.hex() + assert receipt2["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -224,7 +224,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -260,7 +260,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128[2],uint256[3],int128[2][2])", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { @@ -303,7 +303,7 @@ def foo(arg1: Bytes[29], arg2: Bytes[31]): event_id = keccak(bytes("MyLog(bytes,bytes,bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -341,7 +341,7 @@ def foo(_arg1: Bytes[20]): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -370,7 +370,7 @@ def foo(_arg1: Bytes[5]): event_id = keccak(bytes("MyLog(bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -406,7 +406,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bytes,bytes,address,address,uint256)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -453,7 +453,7 @@ def foo(): event_id = keccak(bytes("MyLog(int128,bytes)", "utf-8")) # Event id is always the first topic - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "anonymous": False, @@ -506,8 +506,8 @@ def foo(): event_id2 = keccak(bytes("YourLog(address,(uint256,bytes,(string,fixed168x10)))", "utf-8")) # Event id is always the first topic - assert logs1["topics"][0] == event_id1.to_0x_hex() - assert logs2["topics"][0] == event_id2.to_0x_hex() + assert logs1["topics"][0] == "0x" + event_id1.hex() + assert logs2["topics"][0] == "0x" + event_id2.hex() # Event abi is created correctly assert c._classic_contract.abi[0] == { "name": "MyLog", @@ -1081,7 +1081,7 @@ def foo(a: Bytes[36], b: int128, c: String[7]): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() topic1 = f"0x{keccak256(b'bar').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1126,7 +1126,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() topic1 = f"0x{keccak256(b'potato').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1180,7 +1180,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() topic1 = f"0x{keccak256(b'zonk').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 @@ -1222,7 +1222,7 @@ def foo(): # Event id is always the first topic event_id = keccak(b"MyLog(bytes,int128,string)") - assert receipt["logs"][0]["topics"][0] == event_id.to_0x_hex() + assert receipt["logs"][0]["topics"][0] == "0x" + event_id.hex() topic1 = f"0x{keccak256(b'wow').hex()}" assert receipt["logs"][0]["topics"][1] == topic1 From cf1ef521e04b446e5401f33e535f06eb09ececdb Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 2 Apr 2024 21:55:26 +0200 Subject: [PATCH 4/9] hexbytes>=1.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1bcba53107..2379f59379 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ "hypothesis[lark]>=6.0,<7.0", "eth-stdlib==0.2.7", "setuptools", - "hexbytes>1.0,<2.0", + "hexbytes>=1.2", "typing_extensions", # we can remove this once dependencies upgrade to eth-rlp>=2.0 ], "lint": [ From 96eaadbe4efa63ce3ab4b671972ba87fa36bd2ef Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Tue, 2 Apr 2024 22:21:27 +0200 Subject: [PATCH 5/9] Update arg --- tests/functional/syntax/test_msg_data.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/functional/syntax/test_msg_data.py b/tests/functional/syntax/test_msg_data.py index 40e636eefc..48443965ac 100644 --- a/tests/functional/syntax/test_msg_data.py +++ b/tests/functional/syntax/test_msg_data.py @@ -15,7 +15,7 @@ def foo() -> Bytes[4]: contract = get_contract(code) - assert contract.foo() == bytes(keccak(text="foo()")[:4]) + assert contract.foo() == bytes(keccak(b"foo()")[:4]) def test_slicing_start_index_other_than_zero(get_contract): @@ -41,7 +41,7 @@ def foo(bar: uint256) -> Bytes[36]: contract = get_contract(code) # 2fbebd38000000000000000000000000000000000000000000000000000000000000002a - method_id = keccak(text="foo(uint256)").hex()[:8] # 2fbebd38 + method_id = keccak(b"foo(uint256)").hex()[:8] # 2fbebd38 encoded_42 = w3.to_bytes(42).hex() # 2a expected_result = method_id + "00" * 31 + encoded_42 @@ -73,7 +73,7 @@ def foo() -> (uint256, Bytes[4], uint256): """ contract = get_contract(code) - assert contract.foo() == [2**256 - 1, bytes(keccak(text="foo()")[:4]), 2**256 - 1] + assert contract.foo() == [2**256 - 1, bytes(keccak(b"foo()")[:4]), 2**256 - 1] def test_assignment_to_storage(w3, get_contract, keccak): @@ -88,7 +88,7 @@ def foo(): contract = get_contract(code) contract.foo(transact={"from": acct}) - assert contract.cache() == bytes(keccak(text="foo()")[:4]) + assert contract.cache() == bytes(keccak(b"foo()")[:4]) def test_get_len(get_contract): From 3950238cfb480831d129551227a2cbd190f9905e Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Thu, 4 Apr 2024 10:44:58 +0200 Subject: [PATCH 6/9] Review comments --- setup.py | 1 - .../functional/builtins/codegen/test_keccak256.py | 14 +++++++++----- .../test_external_contract_calls.py | 9 +++++---- tests/functional/syntax/test_msg_data.py | 8 ++++---- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/setup.py b/setup.py index 2379f59379..e87a9d409c 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,6 @@ "eth-stdlib==0.2.7", "setuptools", "hexbytes>=1.2", - "typing_extensions", # we can remove this once dependencies upgrade to eth-rlp>=2.0 ], "lint": [ "black==23.12.0", diff --git a/tests/functional/builtins/codegen/test_keccak256.py b/tests/functional/builtins/codegen/test_keccak256.py index 8b5d14495e..090f6b07af 100644 --- a/tests/functional/builtins/codegen/test_keccak256.py +++ b/tests/functional/builtins/codegen/test_keccak256.py @@ -1,6 +1,10 @@ from vyper.utils import hex_to_int +def test_sanity(keccak): + assert keccak(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" + + def test_hash_code(get_contract_with_gas_estimation, keccak): hash_code = """ @external @@ -18,10 +22,10 @@ def bar() -> bytes32: c = get_contract_with_gas_estimation(hash_code) for inp in (b"", b"cow", b"s" * 31, b"\xff" * 32, b"\n" * 33, b"g" * 64, b"h" * 65): - assert c.foo(inp).hex() == keccak(inp).hex() + assert c.foo(inp) == keccak(inp) - assert c.bar().hex() == keccak(b"inp").hex() - assert c.foob().hex() == keccak(b"inp").hex() + assert c.bar() == keccak(b"inp") + assert c.foob() == keccak(b"inp") def test_hash_code2(get_contract_with_gas_estimation): @@ -96,7 +100,7 @@ def foo() -> bytes32: return x """ c = get_contract_with_gas_estimation(code) - assert c.foo().hex() == keccak(hex_to_int(hex_val).to_bytes(32, "big")).hex() + assert c.foo() == keccak(hex_to_int(hex_val).to_bytes(32, "big")) def test_hash_constant_string(get_contract_with_gas_estimation, keccak): @@ -110,4 +114,4 @@ def foo() -> bytes32: return x """ c = get_contract_with_gas_estimation(code) - assert c.foo().hex() == keccak(str_val.encode()).hex() + assert c.foo() == keccak(str_val.encode()) diff --git a/tests/functional/codegen/calling_convention/test_external_contract_calls.py b/tests/functional/codegen/calling_convention/test_external_contract_calls.py index f92e07e681..6cec121f3b 100644 --- a/tests/functional/codegen/calling_convention/test_external_contract_calls.py +++ b/tests/functional/codegen/calling_convention/test_external_contract_calls.py @@ -12,6 +12,7 @@ UndeclaredDefinition, UnknownType, ) +from vyper.utils import method_id def test_external_contract_calls(get_contract, get_contract_with_gas_estimation): @@ -2496,14 +2497,14 @@ def do_stuff(f: Foo) -> uint256: @pytest.mark.parametrize("typ,val", [("address", TEST_ADDR)]) -def test_calldata_clamp(w3, get_contract, tx_failed, keccak, typ, val): +def test_calldata_clamp(w3, get_contract, tx_failed, typ, val): code = f""" @external def foo(a: {typ}): pass """ c1 = get_contract(code) - sig = keccak(f"foo({typ})".encode()).hex()[:8] + sig = method_id(f"foo({typ})").hex() encoded = abi.encode(f"({typ})", (val,)).hex() data = f"0x{sig}{encoded}" @@ -2520,7 +2521,7 @@ def foo(a: {typ}): @pytest.mark.parametrize("typ,val", [("address", ([TEST_ADDR] * 3, "vyper"))]) -def test_dynamic_calldata_clamp(w3, get_contract, tx_failed, keccak, typ, val): +def test_dynamic_calldata_clamp(w3, get_contract, tx_failed, typ, val): code = f""" @external def foo(a: DynArray[{typ}, 3], b: String[5]): @@ -2528,7 +2529,7 @@ def foo(a: DynArray[{typ}, 3], b: String[5]): """ c1 = get_contract(code) - sig = keccak(f"foo({typ}[],string)".encode()).hex()[:8] + sig = method_id(f"foo({typ}[],string)").hex() encoded = abi.encode(f"({typ}[],string)", val).hex() data = f"0x{sig}{encoded}" diff --git a/tests/functional/syntax/test_msg_data.py b/tests/functional/syntax/test_msg_data.py index 48443965ac..2c7923c78e 100644 --- a/tests/functional/syntax/test_msg_data.py +++ b/tests/functional/syntax/test_msg_data.py @@ -3,6 +3,7 @@ from vyper import compiler from vyper.exceptions import StructureException, TypeMismatch +from vyper.utils import method_id def test_variable_assignment(get_contract, keccak): @@ -14,8 +15,7 @@ def foo() -> Bytes[4]: """ contract = get_contract(code) - - assert contract.foo() == bytes(keccak(b"foo()")[:4]) + assert contract.foo() == method_id("foo()") def test_slicing_start_index_other_than_zero(get_contract): @@ -73,7 +73,7 @@ def foo() -> (uint256, Bytes[4], uint256): """ contract = get_contract(code) - assert contract.foo() == [2**256 - 1, bytes(keccak(b"foo()")[:4]), 2**256 - 1] + assert contract.foo() == [2**256 - 1, method_id("foo()"), 2**256 - 1] def test_assignment_to_storage(w3, get_contract, keccak): @@ -88,7 +88,7 @@ def foo(): contract = get_contract(code) contract.foo(transact={"from": acct}) - assert contract.cache() == bytes(keccak(b"foo()")[:4]) + assert contract.cache() == method_id("foo()") def test_get_len(get_contract): From 0e3d8727568418f359191ea718b8b3765b473afd Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Thu, 4 Apr 2024 10:10:03 -0400 Subject: [PATCH 7/9] simplify a test --- tests/functional/syntax/test_msg_data.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/functional/syntax/test_msg_data.py b/tests/functional/syntax/test_msg_data.py index 2c7923c78e..20ec9382ec 100644 --- a/tests/functional/syntax/test_msg_data.py +++ b/tests/functional/syntax/test_msg_data.py @@ -41,11 +41,11 @@ def foo(bar: uint256) -> Bytes[36]: contract = get_contract(code) # 2fbebd38000000000000000000000000000000000000000000000000000000000000002a - method_id = keccak(b"foo(uint256)").hex()[:8] # 2fbebd38 - encoded_42 = w3.to_bytes(42).hex() # 2a - expected_result = method_id + "00" * 31 + encoded_42 + selector_hex = method_id("foo(uint256)") # 2fbebd38 + encoded_42 = (42).to_bytes(32, "big") + expected_result = selector_hex + encoded_42 - assert contract.foo(42).hex() == expected_result + assert contract.foo(42) == expected_result @pytest.mark.parametrize("bar", [0, 1, 42, 2**256 - 1]) From 4a6e98f491d12a41add38a89297658960df3d61b Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Thu, 4 Apr 2024 10:11:33 -0400 Subject: [PATCH 8/9] move keccak256 sanity check to utils test --- tests/functional/builtins/codegen/test_keccak256.py | 4 ---- tests/unit/utils/test_keccak256.py | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 tests/unit/utils/test_keccak256.py diff --git a/tests/functional/builtins/codegen/test_keccak256.py b/tests/functional/builtins/codegen/test_keccak256.py index 090f6b07af..3f73be551a 100644 --- a/tests/functional/builtins/codegen/test_keccak256.py +++ b/tests/functional/builtins/codegen/test_keccak256.py @@ -1,10 +1,6 @@ from vyper.utils import hex_to_int -def test_sanity(keccak): - assert keccak(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" - - def test_hash_code(get_contract_with_gas_estimation, keccak): hash_code = """ @external diff --git a/tests/unit/utils/test_keccak256.py b/tests/unit/utils/test_keccak256.py new file mode 100644 index 0000000000..c84af83b8d --- /dev/null +++ b/tests/unit/utils/test_keccak256.py @@ -0,0 +1,4 @@ +def test_keccak_sanity(keccak): + # ensure keccak is keccak256, not sha3 + # https://ethereum.stackexchange.com/a/107985 + assert keccak(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" From de753bf5f6e0d5d4132a8b956eb4f0fd28d51fc4 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Thu, 4 Apr 2024 10:14:04 -0400 Subject: [PATCH 9/9] update a comment --- tests/unit/utils/test_keccak256.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_keccak256.py b/tests/unit/utils/test_keccak256.py index c84af83b8d..3fbe883150 100644 --- a/tests/unit/utils/test_keccak256.py +++ b/tests/unit/utils/test_keccak256.py @@ -1,4 +1,4 @@ def test_keccak_sanity(keccak): - # ensure keccak is keccak256, not sha3 + # sanity check -- ensure keccak is keccak256, not sha3 # https://ethereum.stackexchange.com/a/107985 assert keccak(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"