8000 Contract wrapper generator should convert array items to native types by dukei · Pull Request #346 · LFDT-web3j/web3j · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Contract wrapper generator should convert array items to native types #346

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

Merged
merged 4 commits into from
Feb 27, 2018

Conversation

dukei
Copy link
Contributor
@dukei dukei commented Jan 31, 2018

This pull request fixes the following bug:

Suppose contract function returns bytes32[]. The generator generates method returning List<byte[]> (for --javaTypes flag). But in fact the method returns List<Bytes32> - only Array gets converted. So web3j codegen does not convert items in arrays to native types.

Suggested fix: generated functions perform deep conversion for arrays.

Warning: for converting arrays the generator creates method convertToNative in each contract class. This method currently uses Java8 streams. Consider rewriting this function (and moving it to base Contract class) to support Android.

I did not move it to Contract base class myself because in this case I will not be able to load web3j from maven until you release the version with this pull request merged. Now only code generator is affected.

All tests passed.

…rray was converted to List but contained items remained unchanged.

All tests pass
@codecov
Copy link
codecov bot commented Jan 31, 2018

Codecov Report

Merging #346 into master will decrease coverage by 0.31%.
The diff coverage is 72.09%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #346      +/-   ##
============================================
- Coverage     76.78%   76.47%   -0.32%     
- Complexity     1597     1621      +24     
============================================
  Files           214      215       +1     
  Lines          5880     6052     +172     
  Branches        925      956      +31     
============================================
+ Hits           4515     4628     +113     
- Misses         1149     1192      +43     
- Partials        216      232      +16
Impacted Files Coverage Δ Complexity Δ
core/src/main/java/org/web3j/tx/Contract.java 72.72% <0%> (-2.49%) 39 <0> (ø)
...ava/org/web3j/codegen/SolidityFunctionWrapper.java 95.97% <79.48%> (-1.45%) 104 <0> (+3)
.../web3j/protocol/parity/methods/response/Trace.java 72.5% <0%> (-4.69%) 65% <0%> (ø)
...ore/src/main/java/org/web3j/utils/Observables.java 80% <0%> (-4%) 9% <0%> (-1%)
core/src/main/java/org/web3j/ens/EnsResolver.java 67.24% <0%> (-2.21%) 14% <0%> (+3%)
.../org/web3j/protocol/core/methods/response/Log.java 92.7% <0%> (-0.98%) 63% <0%> (ø)
crypto/src/main/java/org/web3j/crypto/Sign.java 62.36% <0%> (-0.97%) 12% <0%> (+1%)
...ocol/core/methods/response/TransactionReceipt.java 92.3% <0%> (-0.8%) 79% <0%> (ø)
rlp/src/main/java/org/web3j/rlp/RlpEncoder.java 88.57% <0%> (ø) 15% <0%> (ø) ⬇️
rlp/src/main/java/org/web3j/rlp/RlpDecoder.java 85.71% <0%> (ø) 12% <0%> (?)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2c877d7...b09bf4c. Read the comment docs.

@conor10
Copy link
Contributor
conor10 commented Feb 9, 2018

Thanks for spotting this and submitting a fix.

There is one issue with generating the streams - all smart contract wrapper code that is generated should be Java 1.6 compatible, otherwise Android users would have to use a different version of the command line tools which is not appropriate.

Is it possible to submit the change against the contract class and manually insert into the smart contract wrappers you're working with?

@dukei
Copy link
Contributor Author
dukei commented Feb 9, 2018

Sorry, I didn't quite catch what you mean. But if you are talking about wrapper java 1.6 compatibility it can be easily achieved by moving convertToNative method from wrapper into the Contract base class and removing buildListConverter method (and the call to it) from SolidityFunctionWrapper.

If you need web3j source code to be compatible with java 1.6 too, you can rewrite convertToNative into the following:

protected static <OriginalType, NewType> List<NewType> convertToNative(List<OriginalType> arr) {
    List<NewType> out = new ArrayList<NewType>();
    for(Iterator<OriginalType> it = arr.iterator(); it.hasNext(); ){
        out.add(it.next());
    }
    return out;
}

Will it solve the problem? If yes I can update the pull request with these changes.

@iikirilov iikirilov assigned iikirilov and unassigned iikirilov Feb 23, 2018
@iikirilov iikirilov added the needs-review issue/PR needs review from maintainer label Feb 23, 2018
@iikirilov
Copy link
Contributor
iikirilov commented Feb 23, 2018

@dukei Please go ahead and update this PR as you have suggested so it can be merged.

Moved convertToNative to Contract class
@dukei
Copy link
Contributor Author
dukei commented Feb 26, 2018

@iikirilov I have made the changes to support Java 6. I am a bit confused why coverage tests fail this time. I have mostly removed some code since the last commit.

@@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
Copy link
Contributor

Choose a reason for hiding this comment

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

This still needs removing.

@@ -430,4 +431,13 @@ public final String getDeployedAddress(String networkId) {
return addr == null ? getStaticDeployedAddress(networkId) : addr;
}

@SuppressWarnings("unchecked")
protected static <OriginalTypeT extends Type, NewTypeT>
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we just use S & T here? I'm not so keen on lengthy parameterised type names.

@dukei
Copy link
Contributor Author
dukei commented Feb 27, 2018

@conor10 I made the changes you requested

@conor10 conor10 merged commit 8a84214 into LFDT-web3j:master Feb 27, 2018
@conor10
Copy link
Contributor
conor10 commented Feb 27, 2018

Thanks!

franz-see pushed a commit to franz-see/web3j that referenced this pull request Aug 3, 2018
Contract wrapper generator should convert array items to native types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review issue/PR needs review from maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0