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

fix abi alignment for multidimensional arrays #2764

Merged
merged 4 commits into from
Dec 18, 2022
Merged
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
1 change: 1 addition & 0 deletions newsfragments/2764.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixing abi encoding for multidimensional arrays.
27 changes: 27 additions & 0 deletions tests/core/utilities/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ def test_get_tuple_type_str_parts(input, expected):
{
"name": "a",
"type": "uint256"
},
{
"components": [
{
"name": "x",
"type": "uint256"
},
{
"name": "y",
"type": "uint256"
}
],
"name": "b",
"type": "tuple[][]"
}
],
"name": "f",
Expand All @@ -129,11 +143,13 @@ def test_get_tuple_type_str_parts(input, expected):
"(uint256,uint256[],(uint256,uint256)[])", # Type of s
"(uint256,uint256)", # Type of t
"uint256", # Type of a
"(uint256,uint256)[][]", # Type of b
),
(
(1, [2, 3, 4], [(5, 6), (7, 8), (9, 10)]), # Value for s
(11, 12), # Value for t
13, # Value for a
[[(14, 15), (16, 17)], [(18, 19)]], # Value for b
),
)

Expand All @@ -148,6 +164,7 @@ def test_get_tuple_type_str_parts(input, expected):
},
"t": {"x": 11, "y": 12},
"a": 13,
"b": [[{"x": 14, "y": 15}, {"x": 16, "y": 17}], [{"x": 18, "y": 19}]],
},
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -157,6 +174,7 @@ def test_get_tuple_type_str_parts(input, expected):
"s": {"a": 1, "b": [2, 3, 4], "c": [(5, 6), (7, 8), {"x": 9, "y": 10}]},
"t": {"x": 11, "y": 12},
"a": 13,
"b": [[(14, 15), (16, 17)], [{"x": 18, "y": 19}]],
},
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -166,6 +184,7 @@ def test_get_tuple_type_str_parts(input, expected):
"s": {"a": 1, "b": [2, 3, 4], "c": [(5, 6), (7, 8), (9, 10)]},
"t": (11, 12),
"a": 13,
"b": [[(14, 15), (16, 17)], [(18, 19)]],
},
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -175,6 +194,7 @@ def test_get_tuple_type_str_parts(input, expected):
"s": (1, [2, 3, 4], [(5, 6), (7, 8), (9, 10)]),
"t": (11, 12),
"a": 13,
"b": [[(14, 15), (16, 17)], [(18, 19)]],
},
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -184,6 +204,7 @@ def test_get_tuple_type_str_parts(input, expected):
(1, [2, 3, 4], [(5, 6), (7, 8), (9, 10)]),
(11, 12),
13,
[[(14, 15), (16, 17)], [(18, 19)]],
),
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -193,6 +214,10 @@ def test_get_tuple_type_str_parts(input, expected):
"s": {"a": 1, "b": [2, 3, 4], "c": [(5, 6), (7, 8), MyXYTuple(x=9, y=10)]},
"t": MyXYTuple(x=11, y=12),
"a": 13,
"b": [
[MyXYTuple(x=14, y=15), MyXYTuple(x=16, y=17)],
[MyXYTuple(x=18, y=19)],
],
},
GET_ABI_INPUTS_OUTPUT,
),
Expand All @@ -219,6 +244,7 @@ def test_get_aligned_abi_inputs(abi, args, expected):
"s": {"a": 1, "b": [2, 3, 4], "c": ["56", (7, 8), (9, 10)]},
"t": (11, 12),
"a": 13,
"b": [[(14, 15), (16, 17)], [(18, 19)]],
},
),
(
Expand All @@ -227,6 +253,7 @@ def test_get_aligned_abi_inputs(abi, args, expected):
"s": {"a": 1, "b": [2, 3, 4], "c": {(5, 6), (7, 8), (9, 10)}},
"t": (11, 12),
"a": 13,
"b": [[(14, 15), (16, 17)], [(18, 19)]],
},
),
)
Expand Down
4 changes: 3 additions & 1 deletion web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,12 @@ def _align_abi_input(arg_abi: ABIFunctionParams, arg: Any) -> Tuple[Any, ...]:
# according to its corresponding abi.
sub_abis = arg_abi["components"]
else:
num_dims = tuple_dims.count("[")

# Arg is list tuple. A non-list version of its abi will be used to
# align each element in `arg`.
new_abi = copy.copy(arg_abi)
new_abi["type"] = tuple_prefix
new_abi["type"] = tuple_prefix + "[]" * (num_dims - 1)

sub_abis = itertools.repeat(new_abi) # type: ignore

Expand Down