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

Big fix for codegen when Array of struct and struct used together #2068

Merged
merged 4 commits into from
Jul 4, 2024

Conversation

tonykwok1992
Copy link
Contributor

@tonykwok1992 tonykwok1992 commented Jun 14, 2024

What does this PR do?

After fixing #2061, there is still an edge case where array of struct and struct are used together in the same contract e.g Foo and Foo[2]. Fix this by having normalizeNamedType to take care of static array

Also, structIdentifer is using hashCode which does not have uniqueness Property, it could subject to quite significant collision.

This should also fix #2056. Added test case for the example in the issue

Where should the reviewer start?

All files

Why is it needed?

Bug fix

Checklist

  • I've read the contribution guidelines.
  • I've added tests (if applicable).
  • I've added a changelog entry if necessary.

return ((internalType == null ? type : internalType.isEmpty() ? type : internalType)
+ components.stream()
.map(namedType -> String.valueOf(namedType.structIdentifier()))
.collect(Collectors.joining()))
.hashCode();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashCode does not have uniqueness Property, it could subject to quite significant collision as a identifier

Signed-off-by: tonykwok1992 <[email protected]>
Signed-off-by: tonykwok1992 <[email protected]>
@@ -587,10 +587,6 @@ private List<TypeSpec> buildStructTypes(final List<AbiDefinition> functionDefini
private static String getStructName(String internalType) {
final String fullStructName = internalType.substring(internalType.lastIndexOf(" ") + 1);
String tempStructName = fullStructName.substring(fullStructName.lastIndexOf(".") + 1);
int arrayPos = tempStructName.indexOf("[");
Copy link
Contributor Author

@tonykwok1992 tonykwok1992 Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in my previous PR merged https://github.com/hyperledger/web3j/pull/2061/files#diff-11944d4d901c6c86e0bb4f4bb1af18f9b3644a325a39e7cc7437878613330294R590

However, after fixing normalizeNamedType to handle static array, this is no longer needed, although there is no harm to leave it there

@@ -637,7 +646,7 @@ private List<AbiDefinition.NamedType> extractStructs(
parameters.addAll(definition.getOutputs());
return parameters.stream()
.map(this::normalizeNamedType)
.filter(namedType -> namedType.getType().startsWith("tuple"));
Copy link
Contributor Author

@tonykwok1992 tonykwok1992 Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in my previous PR merged https://github.com/hyperledger/web3j/pull/2061/files#diff-11944d4d901c6c86e0bb4f4bb1af18f9b3644a325a39e7cc7437878613330294R590

However, after fixing normalizeNamedType to handle static array, this is no longer needed, although there is no harm to leave it there

@@ -620,6 +617,18 @@ private NamedType normalizeNamedType(NamedType namedType) {
.getInternalType()
.substring(0, namedType.getInternalType().length() - 2),
namedType.isIndexed());
} else if (namedType.getType().startsWith("tuple[")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main change is to add handling for static array in normalizedType

@gtebrean
Copy link
Contributor

gtebrean commented Jul 3, 2024

Looks good from my side and also we really apreciate the effort! I'm not approving it yet because I want also to do some checks to make sure, but it should be fine as it is.

@gtebrean gtebrean merged commit 8646abe into hyperledger-web3j:main Jul 4, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not generate java class of static struct in some case.
2 participants