From 177e2c2361f0e8277120f47cc157c8a06eef5353 Mon Sep 17 00:00:00 2001 From: gtebrean <99179176+gtebrean@users.noreply.github.com> Date: Thu, 8 Aug 2024 20:20:56 +0300 Subject: [PATCH 1/2] add fix Signed-off-by: gtebrean <99179176+gtebrean@users.noreply.github.com> --- CHANGELOG.md | 3 ++- .../java/org/web3j/abi/datatypes/BytesType.java | 9 ++++++--- .../web3j/codegen/SolidityFunctionWrapper.java | 5 ++++- .../SolidityFunctionWrapperGeneratorTest.java | 16 ++++++++++++---- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8049f2f8..707877ad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline ### Bug Fixes -* Bug fix for Int256 decode range [2^248, type(int256).max] and [ type(int256.min), -(2^248) ) +* Bug fix for Int256 decode range [#2070](https://github.com/hyperledger/web3j/pull/2070) +* Bug fix for BytesType.bytes32PaddedLength ### Features diff --git a/abi/src/main/java/org/web3j/abi/datatypes/BytesType.java b/abi/src/main/java/org/web3j/abi/datatypes/BytesType.java index 4d83a2b52..d56771bb8 100644 --- a/abi/src/main/java/org/web3j/abi/datatypes/BytesType.java +++ b/abi/src/main/java/org/web3j/abi/datatypes/BytesType.java @@ -27,9 +27,12 @@ public BytesType(byte[] src, String type) { @Override public int bytes32PaddedLength() { - return value.length <= 32 - ? MAX_BYTE_LENGTH - : (value.length / MAX_BYTE_LENGTH + 1) * MAX_BYTE_LENGTH; + if (value.length < MAX_BYTE_LENGTH) { + return MAX_BYTE_LENGTH; + } else if (value.length % MAX_BYTE_LENGTH == 0) { + return value.length; + } + return (value.length / MAX_BYTE_LENGTH + 1) * MAX_BYTE_LENGTH; } @Override diff --git a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java index 4093f668b..674d24ac9 100644 --- a/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java +++ b/codegen/src/main/java/org/web3j/codegen/SolidityFunctionWrapper.java @@ -1506,7 +1506,10 @@ List buildFunctions( // Create function that returns the ABI encoding of the Solidity function call. if (abiFuncs) { functionName = "getABI_" + functionName; - methodBuilder = MethodSpec.methodBuilder(functionName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC); + methodBuilder = + MethodSpec.methodBuilder(functionName) + .addModifiers(Modifier.PUBLIC) + .addModifiers(Modifier.STATIC); addParameters(methodBuilder, functionDefinition.getInputs()); buildAbiFunction(functionDefinition, methodBuilder, inputParams, useUpperCase); results.add(methodBuilder.build()); diff --git a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java index abf7c2ae3..efab656f8 100644 --- a/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java +++ b/codegen/src/test/java/org/web3j/codegen/SolidityFunctionWrapperGeneratorTest.java @@ -62,8 +62,9 @@ public void testGetFileNoExtension() { @Test public void testAbiFuncsGeneration() throws Exception { - testCodeGeneration(emptyList(),"abifuncs", "AbiFuncs", JAVA_TYPES_ARG, true, false, true); - testCodeGeneration(emptyList(),"abifuncs", "AbiFuncs", SOLIDITY_TYPES_ARG, true, false, true); + testCodeGeneration(emptyList(), "abifuncs", "AbiFuncs", JAVA_TYPES_ARG, true, false, true); + testCodeGeneration( + emptyList(), "abifuncs", "AbiFuncs", SOLIDITY_TYPES_ARG, true, false, true); } @Test @@ -268,11 +269,18 @@ public void testStaticArrayOfStructsInStructGenerationCompareJavaFile() throws E compareJavaFile("StaticArrayOfStructsInStruct", true, false); } - private void compareJavaFile(String inputFileName, boolean useBin, boolean abiFuncs) throws Exception { + private void compareJavaFile(String inputFileName, boolean useBin, boolean abiFuncs) + throws Exception { String contract = inputFileName.toLowerCase(); String packagePath = generateCode( - emptyList(), contract, inputFileName, JAVA_TYPES_ARG, useBin, false, abiFuncs); + emptyList(), + contract, + inputFileName, + JAVA_TYPES_ARG, + useBin, + false, + abiFuncs); File fileActual = new File(tempDirPath, packagePath + "/" + inputFileName + ".java"); File fileExpected = new File( From d58099d5ebbf19a2e85ada982664ffc0a6b9b97b Mon Sep 17 00:00:00 2001 From: gtebrean <99179176+gtebrean@users.noreply.github.com> Date: Thu, 8 Aug 2024 20:24:39 +0300 Subject: [PATCH 2/2] update changelog Signed-off-by: gtebrean <99179176+gtebrean@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 707877ad9..a526e3316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline ### Bug Fixes * Bug fix for Int256 decode range [#2070](https://github.com/hyperledger/web3j/pull/2070) -* Bug fix for BytesType.bytes32PaddedLength +* Bug fix for BytesType.bytes32PaddedLength [#2089](https://github.com/hyperledger/web3j/pull/2089) ### Features