Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/core/web3-module/test_keccak.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,11 @@ def test_keccak_raise_if_hexstr_and_text():
def test_keccak_raise_if_no_args():
with pytest.raises(TypeError):
Web3.keccak()


def test_solidity_keccak_bytes_padding():
# bytes1 value 0x01 should be padded to 32 bytes in Solidity
result = Web3.solidity_keccak(["bytes1"], [b"\x01"])
# The padded value is 0x01 followed by 31 zero bytes
expected = Web3.keccak(hexstr="0x01" + "00" * 31)
assert result == expected
6 changes: 6 additions & 0 deletions web3/_utils/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def hex_encode_abi_type(
return pad_hex(value, data_size)
elif is_bytes_type(abi_type):
if is_bytes(value):
# Pad fixed-size bytesN to 32 bytes (Solidity right-pads to 32 bytes in abi.encode)
if abi_type != "bytes":
hex_val = remove_0x_prefix(encode_hex(value))
# Right-pad to 32 bytes (64 hex chars)
padded = hex_val.ljust(64, "0")
return add_0x_prefix(HexStr(padded))
return encode_hex(value)
else:
return value
Expand Down
23 changes: 21 additions & 2 deletions web3/_utils/http_session_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys
from concurrent.futures import (
ThreadPoolExecutor,
)
Expand All @@ -19,6 +20,14 @@
from eth_typing import (
URI,
)

# aiohttp's `enable_cleanup_closed` parameter is deprecated on Python 3.12.8+
# and 3.13.1+ (CPython bug fix https://github.com/python/cpython/pull/118960).
# Passing it triggers a DeprecationWarning on those versions.
_ENABLE_CLEANUP_CLOSED = (
sys.version_info < (3, 12, 8)
or (3, 13) <= sys.version_info < (3, 13, 1)
)
import requests

from web3._utils.async_caching import (
Expand Down Expand Up @@ -190,7 +199,12 @@ async def async_cache_and_return_session(
session = ClientSession(
raise_for_status=True,
connector=TCPConnector(
force_close=True, enable_cleanup_closed=True
force_close=True,
**(
{"enable_cleanup_closed": True}
if _ENABLE_CLEANUP_CLOSED
else {}
),
),
)

Expand Down Expand Up @@ -237,7 +251,12 @@ async def async_cache_and_return_session(
_session = ClientSession(
raise_for_status=True,
connector=TCPConnector(
force_close=True, enable_cleanup_closed=True
force_close=True,
**(
{"enable_cleanup_closed": True}
if _ENABLE_CLEANUP_CLOSED
else {}
),
),
)
cached_session, evicted_items = self.session_cache.cache(
Expand Down
Loading