Repository navigation
fix(abi): encode and decode static arrays with dynamic elements - #2334
Open
Saurav1375 wants to merge 2 commits into
Open
Saurav1375 wants to merge 2 commits into
Saurav1375 wants to merge 2 commits into
Conversation
Saurav1375
requested review from
NickSneo,
conor10 and
gtebrean
as code owners
November 12, 2026 14:32
Static arrays whose elements are dynamic (bytes[2], string[2], uint256[][2], bytes[2][2], ...) are themselves dynamic: they take one offset word in the head of a tuple. Nested static arrays of static elements (uint256[2][2]) are encoded in place. - Decoder: advance the head by one word for dynamic static arrays and by the full static size otherwise, decided recursively (previously length * 32 bytes, which silently returned wrong values for every following field, e.g. (bytes[2], uint256)). - Decoder: fix the inner-array offset for nested static arrays, and build nested StaticArray instances with an explicit component class instead of resolving "uint256[]"/"uint256[2]" through AbiTypes (which threw). - Encoder: treat static arrays as dynamic when any nested element is, so the head offset is correct and bytes[2][2] / bytes[2][] get offsets. Supersedes LFDT-web3j#2282. Signed-off-by: Saurav1375 <sauravgupta1375@gmail.com>
Struct decoding picked each field's type from the raw constructor parameter class. For a field declared as StaticArray2<DynamicBytes> (bytes[2]) or StaticArray2<StaticArray2<Uint256>> (uint256[2][2]) the element type was lost, so decoding threw "Array types must be wrapped in a TypeReference", and the struct type string used in event signatures was "(staticarray2,uint256)" instead of "(bytes[2],uint256)", so events with such structs were never matched. - Read static-array fields from the constructor's generic parameter types, decode them in place when static and through their offset when dynamic (bytes[2], string[2], uint256[][2]). - Size static structs by their real word count: a uint256[2][2] field is 4 words, not 1. Structs without static-array fields keep the existing flattened field count. - Build struct type strings from the generic parameter types. Signed-off-by: Saurav1375 <sauravgupta1375@gmail.com>
Saurav1375
force-pushed
the
fix/abi-static-array-dynamic-elements
branch
from
November 12, 2026 14:32
d083897 to
84b38e6
Compare
3 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
required
It fixes ABI encoding and decoding of static arrays whose elements are dynamic (
bytes[2],string[2],uint256[][2],bytes[2][2],bytes[2][]) and of nested static arrays (uint256[2][2],uint256[][2][2]). Under the ABI spec,T[k]is dynamic wheneverTis dynamic. It then takes a single offset word in the head of a tuple. web3j only applied this rule partly, so it produced the following bugs.1. Decoding silently returns wrong values for every field after a
bytes[2](and similar)(bytes[2], uint256)decodes theuint256as64instead of1965.(bytes[2], uint256, string[2], uint256)gives[[lol1,omg2], 320, [,], 64]instead of[[lol1,omg2], 1965, [wat3,yes4], 42], with no exception.(bytes[2], bytes, uint256)throwsStringIndexOutOfBoundsException.DefaultFunctionReturnDecoder.buildadvances the head bylength * 32bytes for a static array, even when the array is dynamic and only occupies one offset word:DefaultFunctionReturnDecoder.java#L109and#L143. Onlystring[k]had a special case, and only for class-basedTypeReferences. Type references built withTypeReference.makeTypeReference("string[2]")were still wrong.Utils.isDynamic(TypeReference)andUtils.staticSizeInWords(TypeReference). Both recurse through any nesting depth. Both static-array branches now advance by one word if the array is dynamic, and by its full in-place size otherwise.getDataOffsetuses the same recursive check.2. Nested static arrays cannot be decoded at all
uint256[][2]→UnsupportedOperationException: Unsupported type encountered: uint256[]uint256[2][2]→Unsupported type encountered: uint256[2]bytes[2][2]→StringIndexOutOfBoundsExceptionuint256[][2][2]→IndexOutOfBoundsExceptionTypeDecoder.instantiateStaticArrayuses the deprecatedStaticArray(List)constructor. That constructor resolves the component class from the element's type string, andAbiTypescannot resolve array type strings:TypeDecoder.java#L867.decodeArrayElements, the position after an inner static array is computed by reading the array's first value as if it were a length:TypeDecoder.java#L1014. This only works by accident when that value is small.(Class, List)constructor, using the element's class.bytes[2][2]). Otherwise they are read in place and the position advances by their static size (uint256[2][2]).3. Static arrays with dynamic elements cannot be passed as function inputs (the call reverts)
take(bytes[2], uint256)andsum(uint256[][2], uint256)revert on-chain.cast calldatain the first head word:0x60instead of0x40.DefaultFunctionEncoder.getLengthcounts one head slot per array element instead of one slot for the whole dynamic array:DefaultFunctionEncoder.java#L102.TypeEncoder.isDynamiconly looks at the direct component class, sobytes[2][2]is treated as static:TypeEncoder.java#L62,#L90.bytes[2][]gets no element offsets:TypeEncoder.java#L362.TypeEncoder.isDynamic(Type)checks nested elements.getLengthcounts any dynamic parameter as one slot.4. Struct fields that are static arrays cannot be decoded, and their events never match
This is the second commit.
bytes[2],string[2],uint256[][2]oruint256[2][2]field throwsUnsupportedOperationException: Array types must be wrapped in a TypeReference.Utils.getStructTypereturns(staticarray2,uint256)instead of(bytes[2],uint256). Event topics computed from it are wrong, soget...Events(receipt)silently returns an empty list.getStructTypetake each field's type from the raw constructor parameter class:TypeDecoder.java#L465,#L682,Utils.java#L67. ForStaticArray2<DynamicBytes>that loses the element type.uint256[2][2]field counted as 1 word instead of 4.Utils.staticArrayParameterType). Static ones are decoded in place, dynamic ones through their offset.Utils.staticStructSizeInWordscounts static-array fields at full size. It is used everywhere a static struct's size was computed from its field count. Structs without static-array fields keep the existing count.Where should the reviewer start?
required
abi/src/main/java/org/web3j/abi/Utils.java:isDynamic(TypeReference)andstaticSizeInWords(TypeReference). These are the single source of truth for "does this take one offset word, or N words in place". For structs, seestaticStructSizeInWordsandstaticArrayParameterType(second commit).DefaultFunctionReturnDecoder.build/staticArrayHeadLength: the top-level offset fix.TypeDecoder.decodeArrayElements(nestedStaticArraybranch) andinstantiateStaticArray.TypeEncoder.isDynamic/encode, andDefaultFunctionEncoder.getLength: the encoder side.abi/src/test/java/org/web3j/abi/StaticArrayDynamicElementsTest.java. Every expected encoding is generated by Foundry'scast abi-encode(the command is in a comment above each vector). This makes the reference an independent ABI implementation rather than web3j itself. Each tuple ends in auint256, so a wrong head size shows up as a wrong trailing value.abi/src/test/java/org/web3j/abi/StructStaticArrayFieldsTest.java: structs shaped like the ones web3j codegen generates. It covers struct type strings, decoding, and encoding, againstcast abi-encodevectors.Why is it needed?
required
These types are common in real contracts, for example
bytes[2]proofs,string[2]pairs and fixed-size batches of dynamic arrays. Onmain:uint256[2][2],uint256[][2]) cannot be decoded at all.Relation to existing PRs
This supersedes #2282 by @snuderl ("fix(abi): StaticArray offset incorrect for DynamicBytes/DynamicArray elements"). That PR has had no activity since April 2026. It correctly identified the top-level offset bug, and this PR keeps its approach and test case. It also addresses the open feedback on #2282:
bytes[2][2]works. The nested test that fix(abi): StaticArray offset incorrect for DynamicBytes/DynamicArray elements #2282 left@Disabledis covered and passing.instantiateStaticArrayand the nested-array advance).spotlessCheckand DCO failed on fix(abi): StaticArray offset incorrect for DynamicBytes/DynamicArray elements #2282. Both pass here.Before / after
A small sample project (not part of this PR) depends on the local web3j checkout through a Gradle composite build (
includeBuild '../web3j'). ItsabiCheckstask does two things:cast abi-encode.The task is run once with
mainchecked out and once with this branch:upstream/mainBefore (
main): wrong values with no exception, crashes on nested arrays, and reverted calls.After (this branch):
StaticArrayDynamicElementsTest: 22 of 24 tests fail when the new test class is run againstmain's code. All 24 pass on this branch. The init script only prints one line per test result.Before (
maincode, new test class copied in):After (this branch, with
spotlessCheck):StructStaticArrayFieldsTest(struct fields, second commit): 4 of 5 tests fail againstmain's code. Encoding already works once the first commit is in. All 5 pass on this branch.End to end, with generated wrappers for contracts whose structs have
bytes[2],string[2],uint256[][2]anduint256[2][2]fields: see #2335. Code generation for these structs is fixed there, and the last screenshot in #2335 shows both PRs merged locally passing 10 / 10 on anvil.Test results
./gradlew :abi:spotlessCheck :abi:test: 216 tests, 0 failures (29 new); spotless clean./gradlew check(all modules, without integration tests): 1211 tests, 0 failures, 3 skipped./gradlew -Pintegration-tests=true :integration-tests:test: 87 tests, 0 failures, 18 skipped (run locally withJAVA_TOOL_OPTIONS=-Dapi.version=1.44before the Docker 29 fix in build: run integration tests on Docker 29 and fix -Pintegration-tests=false #2327 was merged; CI now runs them without it)mainand this branch (values and exception messages), so non-array decoding is unchanged.Related PRs
Generated wrappers had separate problems with these types. They are fixed in #2335 (
core/codegen), which is independent of this PR:bytes[2]fields were generated asStaticStruct;uint256[][2]fields did not compile.Checklist