web3swift icon indicating copy to clipboard operation
web3swift copied to clipboard

Incorrect decoding of dynamic array

Open 6od9i opened this issue 2 years ago • 2 comments

When decode dynamic array with elements type - dynamicBytes, correctly decoded only two elements, all next elements replaces with second element.

Dynamic array decoded in method - "decodeSingleType" - case .array - case .dynamicSize - subType.isStatic = false

  1. subType.isStatic checks twice - in line 108 - global 'if' of this case and in line 138 - if in decode elements cycle, but subtype is constant there

  2. In decode elements cycle - line 142 - commented line " subpointer = consumedUnwrapped // need to go by nextElementPointer " where consumedUnwrapped returns from "decodeSingleType" function, for type "dynamicBytes" which returns tuple with decoded element and next element address - "type.memoryUsage", but type.memoryUsage will set to "subpointer" value without increments and it works only for first element with 0-address and second element with "type.memoryUsage" - address

All next elements will be found by "type.memoryUsage"-address!

This problem can be fixed with next in the following ways: 1 - if increment subpointer in line 142 by size of finded element, for this need remove dublicated "if" in 138 line 2 - when dynamicBytes type decoded in "decodeSignleType" method we can return in tuple - "nextElementPointer" value, to save it in "subpointer" and incement at next step of decoding element cycle in "followTheData" - method

6od9i avatar May 11 '22 03:05 6od9i

can you please include an associated sample case that illustrates this problem?

mloit avatar May 11 '22 16:05 mloit

"00000000000000000000000000000000000000000000000000000000010f02e40000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000153907c266e000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000007c9921748c1e497d000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000008eca082346c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000ec50e561e8734" This hex value of data with type "outputs": [ { "internalType": "uint256", "name": "blockNumber", "type": "uint256" }, { "internalType": "bytes[]", "name": "returnData", "type": "bytes[]" }

6od9i avatar May 13 '22 07:05 6od9i

Please reopen it if it represents in 3.0.*

yaroslavyaroslav avatar Oct 23 '22 12:10 yaroslavyaroslav

Not fixed

6od9i avatar Nov 03 '22 11:11 6od9i

nothing has been changed in the In the ABIDecoding file (only comments removed) i use version with fix like

ABIDecoding.swift .... 88 str -> return (dataSlice as AnyObject, nextElementPointer) ....

and it works good

6od9i avatar Nov 03 '22 12:11 6od9i

i use version with fix like ABIDecoding.swift .... and it works good

Do you implement the fix for that, is that true? If so you're more than welcome to open a PR with that fix.

yaroslavyaroslav avatar Nov 03 '22 15:11 yaroslavyaroslav

Cool! Now it works! Thank you!

6od9i avatar Nov 07 '22 12:11 6od9i