Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore[test]: update hexbytes version and tests #3904

Merged
merged 11 commits into from
Apr 4, 2024
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
Expand Down
10 changes: 5 additions & 5 deletions tests/functional/builtins/codegen/test_keccak256.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
UndeclaredDefinition,
UnknownType,
)
from vyper.utils import method_id


def test_external_contract_calls(get_contract, get_contract_with_gas_estimation):
Expand Down Expand Up @@ -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()
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
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]
Expand All @@ -2520,17 +2521,17 @@ 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]):
pass
"""

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]
Expand Down
36 changes: 18 additions & 18 deletions tests/functional/codegen/features/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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] == {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/functional/syntax/test_msg_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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])
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/utils/test_keccak256.py
Original file line number Diff line number Diff line change
@@ -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"
Loading