You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DefaultFunctionEncoder calculates offset incorrectly for nested structs
Steps To Reproduce
Minimal example:
Create function which has nested structs as parameters, like:
var f = new org.web3j.abi.datatypes.Function(
"myCustomFunction",
Arrays.asList(
new StaticStruct(
new StaticStruct(
new org.web3j.abi.datatypes.Address("0x1"),
new org.web3j.abi.datatypes.Address("0x2"))),
new org.web3j.abi.datatypes.DynamicBytes(new byte[]{1,2,3})),
Collections.emptyList());
Note that first argument is StaticStruct having one property which is also StaticStruct, nested one have two Address properties, which is important in this example. Also having dynamic type as second argument is important.
Execute FunctionEncoder.encode(f). Result will be:
In result, since second argument is dynamic (DynamicBytes type) offset is included.
Expected behavior
Offset value in that case should equal to 0x60.
Actual behavior
Offset value is (as you can see above) 0x40, this is because nested StaticStruct is calculated to have size of 1 (but 2 addresses are inside, so size should be 2).
Environment
Web3j version 4.10.3
Java 17
Operating System Fedora 38 x64, kernel Linux 6.8.7-100.fc38.x86_64
Additional context
My production case, Im attaching two executions:
first one with wrong offset (0xe0 in my case), failure:
DefaultFunctionEncoder calculates offset incorrectly for nested structs
Steps To Reproduce
Minimal example:
Note that first argument is StaticStruct having one property which is also StaticStruct, nested one have two Address properties, which is important in this example. Also having dynamic type as second argument is important.
FunctionEncoder.encode(f)
. Result will be:Expected behavior
Offset value in that case should equal to
0x60
.Actual behavior
Offset value is (as you can see above)
0x40
, this is because nested StaticStruct is calculated to have size of 1 (but 2 addresses are inside, so size should be 2).Environment
Additional context
My production case, Im attaching two executions:
0xe0
in my case), failure:0x100
), succeeded:I think the solution is to make
getLength
function recursive, and change line:web3j/abi/src/main/java/org/web3j/abi/DefaultFunctionEncoder.java
Line 102 in 5173684
to
count += getLength(((StaticArray) type).getValue())
The text was updated successfully, but these errors were encountered: