Skip to content

Commit f0a3b3d

Browse files
committed
Move eth-abi functions to the local web3 codec
1 parent 79ef6a1 commit f0a3b3d

17 files changed

+196
-195
lines changed

docs/overview.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -359,23 +359,23 @@ For example, if an abi specifies a type of ``bytes4``, web3 will handle all of t
359359

360360
.. code-block:: python
361361
362-
- '' # valid - empty string that is less than 4 bytes and can be decoded as a hex string
363-
- '0x' # valid - empty hex string
364-
- b'' # valid - empty bytestring
365-
- b'ab' # valid - less than 4 bytes
366-
- '0xab' # valid - hex string with less than 4 bytes
367-
- '1234' # valid - string that can be decoded as a hex string
368-
- '0xabcd' # valid - hex string with exactly 4 bytes
369-
- b'1234' # valid - bytestring with exactly 4 bytes
362+
- '' # valid - empty string less than 4 bytes and can be decoded as a hex string
363+
- '0x' # valid - empty hex string
364+
- b'' # valid - empty bytestring
365+
- b'ab' # valid - less than 4 bytes
366+
- '0xab' # valid - hex string with less than 4 bytes
367+
- '1234' # valid - string that can be decoded as a hex string
368+
- '0x61626364' # valid - hex string with exactly 4 bytes
369+
- b'1234' # valid - bytestring with exactly 4 bytes
370370
371371
The following values will raise an error by default:
372372

373373
.. code-block:: python
374374
375-
- b'abcde' # invalid - more than 4 bytes
376-
- '0xabcde' # invalid - hex string with more than 4 bytes
377-
- 2 # invalid - wrong type
378-
- 'ah' # invalid - string not valid hex
375+
- b'abcde' # invalid - more than 4 bytes
376+
- '0x6162636423' # invalid - hex string with more than 4 bytes
377+
- 2 # invalid - wrong type
378+
- 'ah' # invalid - string not valid hex
379379
380380
However, you may want to be stricter with acceptable values for bytes types.
381381
For this you can use the ``enable_strict_bytes_type_checking`` method,
@@ -386,17 +386,17 @@ specified byte size will raise an error. Using the same ``bytes4`` example:
386386

387387
.. code-block:: python
388388
389-
- '0xabcd' # valid - hex string with exactly 4 bytes
390-
- b'1234' # valid - bytestring with exactly 4 bytes
391-
- b'ab' # invalid - less than 4 bytes
392-
- '0xab' # invalid - hex string with less than 4 bytes
393-
- '' # invalid - needs a 0x to be interpreted as hex
394-
- '1234' # invalid - needs a 0x to be interpreted as hex
395-
- b'' # invalid - less than 4 bytes
396-
- b'abcde' # invalid - more than 4 bytes
397-
- '0xabcde' # invalid - hex string with more than 4 bytes
398-
- 2 # invalid - wrong type
399-
- 'ah' # invalid - string not valid hex
389+
- '0x61626364' # valid - hex string with exactly 4 bytes
390+
- b'1234' # valid - bytestring with exactly 4 bytes
391+
- b'ab' # invalid - less than 4 bytes
392+
- '0xab' # invalid - hex string with less than 4 bytes
393+
- '' # invalid - needs a 0x to be interpreted as hex
394+
- '1234' # invalid - needs a 0x to be interpreted as hex
395+
- b'' # invalid - less than 4 bytes
396+
- b'abcde' # invalid - more than 4 bytes
397+
- '0x6162636423' # invalid - hex string with more than 4 bytes
398+
- 2 # invalid - wrong type
399+
- 'ah' # invalid - string not valid hex
400400
401401
402402
For example, the following contract code will generate the abi below and some bytecode:

tests/core/contracts/test_contract_constructor_encoding.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import pytest
22

3-
from eth_abi import (
4-
encode_abi,
5-
)
63
from eth_utils import (
74
encode_hex,
85
remove_0x_prefix,
@@ -53,13 +50,27 @@ def test_error_if_invalid_arguments_supplied(WithConstructorArgumentsContract, a
5350
'bytes_arg',
5451
(
5552
b'abcd',
56-
'61626364',
5753
'0x61626364',
5854
),
5955
)
60-
def test_contract_constructor_encoding_encoding(WithConstructorArgumentsContract, bytes_arg):
56+
def test_contract_constructor_encoding_encoding(web3, WithConstructorArgumentsContract, bytes_arg):
6157
deploy_data = WithConstructorArgumentsContract._encode_constructor_data([1234, bytes_arg])
6258
encoded_args = '0x00000000000000000000000000000000000000000000000000000000000004d26162636400000000000000000000000000000000000000000000000000000000' # noqa: E501
63-
expected_ending = encode_hex(encode_abi(['uint256', 'bytes32'], [1234, b'abcd']))
59+
expected_ending = encode_hex(web3.codec.encode_abi(['uint256', 'bytes32'], [1234, b'abcd']))
6460
assert expected_ending == encoded_args
6561
assert deploy_data.endswith(remove_0x_prefix(expected_ending))
62+
63+
64+
def test_contract_constructor_encoding_encoding_warning(web3, WithConstructorArgumentsContract):
65+
with pytest.warns(
66+
DeprecationWarning,
67+
match="in v6 it will be invalid to pass a hexstring without the \"0x\" prefix"
68+
):
69+
70+
deploy_data = WithConstructorArgumentsContract._encode_constructor_data([1234, '61626364'])
71+
encoded_args = '0x00000000000000000000000000000000000000000000000000000000000004d26162636400000000000000000000000000000000000000000000000000000000' # noqa: E501
72+
expected_ending = encode_hex(
73+
web3.codec.encode_abi(['uint256', 'bytes32'], [1234, b'abcd'])
74+
)
75+
assert expected_ending == encoded_args
76+
assert deploy_data.endswith(remove_0x_prefix(expected_ending))

tests/core/contracts/test_contract_method_to_argument_matching.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def test_error_when_no_function_name_match(web3):
128128
([], []),
129129
([b'arst'], ['bytes32']),
130130
(['0xf00b47'], ['bytes32']),
131-
([''], ['bytes32']),
132131
(['0x'], ['bytes32']),
133132
([1234567890], ['uint256']),
134133
# ([255], ['uint8']), # TODO: enable
@@ -145,6 +144,16 @@ def test_finds_function_with_matching_args(web3, arguments, expected_types):
145144
assert set(get_abi_input_types(abi)) == set(expected_types)
146145

147146

147+
def test_finds_function_with_matching_args_deprecation_warning(web3):
148+
Contract = web3.eth.contract(abi=MULTIPLE_FUNCTIONS)
149+
150+
with pytest.warns(DeprecationWarning):
151+
abi = Contract._find_matching_fn_abi('a', [''])
152+
assert abi['name'] == 'a'
153+
assert len(abi['inputs']) == len(['bytes32'])
154+
assert set(get_abi_input_types(abi)) == set(['bytes32'])
155+
156+
148157
def test_error_when_duplicate_match(web3):
149158
Contract = web3.eth.contract(abi=MULTIPLE_FUNCTIONS)
150159

tests/core/contracts/test_extracting_event_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_event_data_extraction(web3,
176176
else:
177177
assert event_topic in log_entry['topics']
178178

179-
event_data = get_event_data(event_abi, log_entry)
179+
event_data = get_event_data(web3.codec, event_abi, log_entry)
180180

181181
assert event_data['args'] == expected_args
182182
assert event_data['blockHash'] == txn_receipt['blockHash']
@@ -207,7 +207,7 @@ def test_dynamic_length_argument_extraction(web3,
207207
string_0_topic = web3.keccak(text=string_0)
208208
assert string_0_topic in log_entry['topics']
209209

210-
event_data = get_event_data(event_abi, log_entry)
210+
event_data = get_event_data(web3.codec, event_abi, log_entry)
211211

212212
expected_args = {
213213
"arg0": string_0_topic,

tests/core/contracts/test_extracting_event_data_old.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_event_data_extraction(web3,
9595
else:
9696
assert event_topic in log_entry['topics']
9797

98-
event_data = get_event_data(event_abi, log_entry)
98+
event_data = get_event_data(web3.codec, event_abi, log_entry)
9999

100100
assert event_data['args'] == expected_args
101101
assert event_data['blockHash'] == txn_receipt['blockHash']
@@ -126,7 +126,7 @@ def test_dynamic_length_argument_extraction(web3,
126126
string_0_topic = web3.keccak(text=string_0)
127127
assert string_0_topic in log_entry['topics']
128128

129-
event_data = get_event_data(event_abi, log_entry)
129+
event_data = get_event_data(web3.codec, event_abi, log_entry)
130130

131131
expected_args = {
132132
"arg0": string_0_topic,

tests/core/filtering/test_utils_functions.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
)
44
import pytest
55

6-
from eth_abi import (
7-
encode_abi,
8-
)
9-
106
from web3._utils.filters import (
117
match_fn,
128
)
@@ -83,19 +79,21 @@
8379
),
8480
)
8581
)
86-
def test_match_fn_with_various_data_types(data, expected, match_data_and_abi):
82+
def test_match_fn_with_various_data_types(web3, data, expected, match_data_and_abi):
8783
abi_types, match_data = zip(*match_data_and_abi)
88-
encoded_data = encode_abi(abi_types, data)
89-
assert match_fn(match_data_and_abi, encoded_data) == expected
84+
# import pdb
85+
# pdb.set_trace()
86+
encoded_data = web3.codec.encode_abi(abi_types, data)
87+
assert match_fn(web3, match_data_and_abi, encoded_data) == expected
9088

9189

92-
def test_wrong_type_match_data():
90+
def test_wrong_type_match_data(web3):
9391
data = ("hello", "goodbye")
9492
match_data_and_abi = (
9593
("string", (50505050,)),
9694
("string", (50505050,)),
9795
)
9896
abi_types, match_data = zip(*match_data_and_abi)
99-
encoded_data = encode_abi(abi_types, data)
97+
encoded_data = web3.codec.encode_abi(abi_types, data)
10098
with pytest.raises(ValueError):
101-
match_fn(match_data_and_abi, encoded_data)
99+
match_fn(web3, match_data_and_abi, encoded_data)

tests/core/utilities/test_abi_is_encodable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
)
7676
def test_is_encodable(value, _type, expected):
7777
w3 = Web3(EthereumTesterProvider())
78-
actual = w3.is_encodable(_type, value)
78+
actual = w3.codec.is_encodable(_type, value)
7979
assert actual is expected
8080

8181

@@ -107,5 +107,5 @@ def test_is_encodable_strict(value, _type, expected):
107107
w3 = Web3(EthereumTesterProvider())
108108
w3.enable_strict_bytes_type_checking()
109109

110-
actual = w3.is_encodable(_type, value)
110+
actual = w3.codec.is_encodable(_type, value)
111111
assert actual is expected

tests/core/utilities/test_construct_event_data_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ def hex_and_pad(i):
6868
),
6969
)
7070
)
71-
def test_construct_event_data_set(event_abi, arguments, expected):
72-
actual = construct_event_data_set(event_abi, arguments)
71+
def test_construct_event_data_set(web3, event_abi, arguments, expected):
72+
actual = construct_event_data_set(event_abi, web3.codec, arguments)
7373
assert actual == expected

tests/core/utilities/test_construct_event_filter_params.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
}),
4747
),
4848
)
49-
def test_construct_event_filter_params(event_abi, fn_kwargs, expected):
50-
_, actual = construct_event_filter_params(event_abi, **fn_kwargs)
49+
def test_construct_event_filter_params(web3, event_abi, fn_kwargs, expected):
50+
_, actual = construct_event_filter_params(event_abi, web3.codec, **fn_kwargs)
5151
assert actual == expected
5252

5353

@@ -68,7 +68,7 @@ def hex_and_pad(i):
6868
]),
6969
),
7070
)
71-
def test_construct_event_filter_params_for_data_filters(event_abi, fn_kwargs,
71+
def test_construct_event_filter_params_for_data_filters(event_abi, web3, fn_kwargs,
7272
expected):
73-
actual, _ = construct_event_filter_params(event_abi, **fn_kwargs)
73+
actual, _ = construct_event_filter_params(event_abi, web3.codec, **fn_kwargs)
7474
assert actual == expected

tests/core/utilities/test_construct_event_topics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ def hex_and_pad(i):
7575
),
7676
)
7777
)
78-
def test_construct_event_topics(event_abi, arguments, expected):
79-
actual = construct_event_topic_set(event_abi, arguments)
78+
def test_construct_event_topics(web3, event_abi, arguments, expected):
79+
actual = construct_event_topic_set(event_abi, web3.codec, arguments)
8080
assert actual == expected

0 commit comments

Comments
 (0)