diff --git a/setup.py b/setup.py index 933e8bfa4b..e87a9d409c 100644 --- a/setup.py +++ b/setup.py @@ -21,8 +21,7 @@ "hypothesis[lark]>=6.0,<7.0", "eth-stdlib==0.2.7", "setuptools", - "hexbytes<1.0", - "typing_extensions", # we can remove this once dependencies upgrade to eth-rlp>=2.0 + "hexbytes>=1.2", ], "lint": [ "black==23.12.0", diff --git a/tests/conftest.py b/tests/conftest.py index 2eacba777e..45d1b99b6c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,7 +25,7 @@ from vyper.evm.opcodes import version_check from vyper.exceptions import EvmVersionException 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"] @@ -146,7 +146,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/builtins/codegen/test_keccak256.py b/tests/functional/builtins/codegen/test_keccak256.py index 3b0b9f2018..3f73be551a 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) == keccak(inp) - assert "0x" + c.bar().hex() == keccak(b"inp").hex() - assert "0x" + 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 +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() == keccak(hex_to_int(hex_val).to_bytes(32, "big")) 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() == 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 0f5f915a48..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,16 +2497,16 @@ 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()[:10] + sig = method_id(f"foo({typ})").hex() 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] @@ -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,9 +2529,9 @@ def foo(a: DynArray[{typ}, 3], b: String[5]): """ c1 = get_contract(code) - sig = keccak(f"foo({typ}[],string)".encode()).hex()[:10] + sig = method_id(f"foo({typ}[],string)").hex() 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 cf64d271a9..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.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.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.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.hex() - assert receipt2["logs"][0]["topics"][0] == event_id.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.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.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.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.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.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.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.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.hex() - assert logs2["topics"][0] == event_id2.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.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.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.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.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 diff --git a/tests/functional/syntax/test_msg_data.py b/tests/functional/syntax/test_msg_data.py index dfeb9cea00..20ec9382ec 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(text="foo()")[:4]) + assert contract.foo() == method_id("foo()") def test_slicing_start_index_other_than_zero(get_contract): @@ -41,11 +41,11 @@ def foo(bar: uint256) -> Bytes[36]: contract = get_contract(code) # 2fbebd38000000000000000000000000000000000000000000000000000000000000002a - method_id = keccak(text="foo(uint256)").hex()[2:10] # 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]) @@ -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, 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(text="foo()")[:4]) + assert contract.cache() == method_id("foo()") def test_get_len(get_contract): diff --git a/tests/unit/utils/test_keccak256.py b/tests/unit/utils/test_keccak256.py new file mode 100644 index 0000000000..3fbe883150 --- /dev/null +++ b/tests/unit/utils/test_keccak256.py @@ -0,0 +1,4 @@ +def test_keccak_sanity(keccak): + # sanity check -- ensure keccak is keccak256, not sha3 + # https://ethereum.stackexchange.com/a/107985 + assert keccak(b"").hex() == "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"