hyperledger-web3j / web3j

Lightweight Java and Android library for integration with Ethereum clients
https://www.web3labs.com/web3j-sdk
Other
5.12k stars 1.69k forks source link

Error: Expected identifier, got 'LParen' constructor() when running my web3j project #1394

Closed rampatra closed 3 years ago

rampatra commented 3 years ago

My sample web3j java project was running fine but suddenly I started getting this error when I ran my vanilla Java project (with web3j as a dependency) (not sure what I changed except the installed Java version in my system). The error is :

Error: Expected identifier, got 'LParen' constructor() {

I am actually not sure what the problem is here as the solidity files compile fine but when I run my java project, I get the above error. So, what may be the problem here? Any hints?

rampatra commented 3 years ago

I am compiling my solidity files via web3j-maven-plugin by running mvn web3j:generate-sources.

And, if I remove the pragma version and the constructor, it compiles and runs fine. So, I am now sure that this is related to an incompatible solidity version. I need at least 0.7.4 but my project is somehow using 0.4.19.

This is what I get in my system's console:

$ solc --version
solc, the solidity compiler commandline interface
Version: 0.8.3+commit.8d00100c.Darwin.appleclang
rach-id commented 3 years ago

Can you try :

solidity {
      version = "<solidity_version>"
}

in your build.gradle ?

rach-id commented 3 years ago

Oh, you're using maven. Can you set that same property ?

rach-id commented 3 years ago

The maven plugin is a little lagging behind... can you switch to gradle ? I doubt you will have this problem using that

rampatra commented 3 years ago

@SweeXordious thanks for helping me out here. Do you mean like this?

<plugin>
    <groupId>org.web3j</groupId>
    <artifactId>web3j-maven-plugin</artifactId>
    <version>4.6.5</version>
    <configuration>
        <soliditySourceFiles/>
        <solidity>
            <version>
                0.7.4
            </version>
        </solidity>
    </configuration>
</plugin>

We are stuck with maven for now. We can't move to gradle as our pipelines, etc. is all maven, unfotunately.

rach-id commented 3 years ago

maybe try that way and see if it will be compiled as needed

rampatra commented 3 years ago

Tried, didn't work. Is this PR related to this issue: https://github.com/web3j/web3j-maven-plugin/pull/57 ?

rach-id commented 3 years ago

can you share the logs you are seeing ?

rampatra commented 3 years ago

Sure.

Error:(3, 1) src/main/resources/MyContract.sol:3:1: Error: Source file requires different compiler version (current compiler is 0.4.19-develop.2018.1.12+commit.c4cbbb05.Darwin.appleclang - note that nightly builds are considered to be strictly less than the released version
pragma solidity 0.7.4;
^--------------------^

When I remove the pragma line and the constructor and other 0.7.4 features, it works. Basically, whatever 0.7.4 solidity features I use, it complains as it using an older version of solidity compiler here, i.e., 0.4.19

rach-id commented 3 years ago

do you have multiple contracts you're compiling with different versions ?

rampatra commented 3 years ago

No, all contracts have pragma solidity 0.7.4;

rach-id commented 3 years ago

That is weird. Can you create a dummy project with the config you are using and share it with me ?

rampatra commented 3 years ago

Sure. But I am not using any custom configs. Just this: https://github.com/web3j/web3j/issues/1394#issuecomment-808192413. The pom.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>poc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.8.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.web3j</groupId>
                <artifactId>web3j-maven-plugin</artifactId>
                <version>4.6.5</version>
                <configuration>
                    <soliditySourceFiles/>
                    <solidity>
                        <version>
                            0.7.4
                        </version>
                    </solidity>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

As you can see it is a plain java maven project with web3j and web3j-maven-plugin in it. And, I am running a local Besu n/w following these instructions: https://besu.hyperledger.org/en/stable/Tutorials/Private-Network/Create-IBFT-Network/.

rampatra commented 3 years ago

What surprises me is that this java project was working fine when I was running besu in docker (before installing it on my machine). But after I followed the instructions here (https://besu.hyperledger.org/en/stable/Tutorials/Private-Network/Create-IBFT-Network/) to set besu locally, my java project gave me problems. Something strange going on here, I feel.

rach-id commented 3 years ago

Can you please try with:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <solidity.version>0.7.4</solidity.version>
    </properties>
rampatra commented 3 years ago

Tried, no luck :(

rach-id commented 3 years ago

what is the issue now ? are you sure you're not messing something else ? because I tested it on a dummy project with a complicated contract 0.7.4 and it worked fine. Can you provide more logs ?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>poc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <solidity.version>0.7.4</solidity.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.8.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.web3j</groupId>
                <artifactId>web3j-maven-plugin</artifactId>
                <version>4.6.5</version>
                <configuration>
                    <soliditySourceFiles/>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
rampatra commented 3 years ago

Only this error I am getting now: https://github.com/web3j/web3j/issues/1394#issuecomment-808204122

112645391-54fe4a80-8e3e-11eb-977b-5e3eeea572c6

Did adding the solidity version in the pom file have any impact on your side? It didn't on mine. Also, what solc is installed locally in your case? In other words, what does solc --version return?

I wish there was a way to see what solidity version web3j is using. 🤔

rach-id commented 3 years ago

solc --version won't give you which compiler it is using to compile the contracts. What the solidity plugin will do is check the contracts for which solidity version they are using, install that version on your machine automatically and use it to compile the contract.

rampatra commented 3 years ago

I think I read somewhere it uses the one present in your system path. Maybe I am wrong. But if what you're saying is the case then I totally don't get my issue here :(

Because I am sure the web3 plugin is getting the wrong sol version as when I remove the constructor and the pragma line from the contract, it compiles and runs fine.

rach-id commented 3 years ago

Did you make sure to add the solidity.version tag ?

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <solidity.version>0.7.4</solidity.version>
    </properties>
rampatra commented 3 years ago

Yes, I have added the version in the pom and then I ran mvn web3j:generate-sources (this successfully generates the java files from my solidity files), and finally, I ran the java main method from the IDE. And, when running the main method, I get the above error.

So, based on the above, I think the maven plugin is doing its job but the web3j library isn't able to build my code because of the incorrect solidity version. Am I right in my understanding?

rach-id commented 3 years ago

I don't think it is related to web3j-core... Because once the wrappers are generated successfully, we no longer touch the smart contracts, and we use the wrappers for all future interactions. Your exception says that there is an error with the constructor of the smart contract.

rampatra commented 3 years ago

If this is the case then why am I seeing the error while running and not while doing mvn web3j:generate-sources? I mean is there some role of web3j maven plugin while building and running the project? I thought the maven plugin only comes into play when running mvn web3j:generate-sources (that is, for converting the sol files to java files). That's it. Am I mistaken?

rach-id commented 3 years ago

When running the application, it also executes the generate-sources automatically in case the wrappers are not yet generated or they changed, but your issue is different. I can't judge from the inputs you're giving me. I need more details

rampatra commented 3 years ago

besu-poc copy.zip

This is the project I am trying to run. You can simply run the main() method from the Main.java class. If you need more info, I can surely help you with that as well.

rach-id commented 3 years ago

I can't reproduce. When I run the project I get:

java.lang.RuntimeException: java.lang.RuntimeException: Error processing transaction request: transaction underpriced

when trying to deploy the auth contract. Try to clean the project directory maybe

rampatra commented 3 years ago

You can run your ethereum n/w with min gas price of 0. Or, may change the price in ContractGasPrice.java as per your eth network.

Btw, this is the longest chat I've ever had on Github. Thanks for your help once again 🙏

perusworld commented 3 years ago

I am suspecting something to do with your path settings i am able to run the besu-poc project just fine, i changed the version to 0.7.5 (looks like this is the max it supports) and ran mvn web3j:generate-sources it automatically downloaded the solc for that and compile and i was able to start the app and issue coins. btw i don't have solc installed in my path. Not sure if that helps

[INFO] --- web3j-maven-plugin:4.6.5:generate-sources (default-cli) @ besu-poc --- [INFO] No Solidity directory specified, using default directory [src/main/resources] [INFO] No Solidity contracts specified, using the default [**/*.sol] [INFO] Adding to process 'Coin.sol' [INFO] Adding to process 'SafeMath.sol' [INFO] Adding to process 'Auth.sol' Solidity version 0.7.5 is not installed. Downloading and installing it to ~/.web3j/solc/0.7.5 [INFO] Built Class for contract 'Coin' [WARNING] Unable to parse input ABI file [INFO] Built Class for contract 'SafeMath' [INFO] Built Class for contract 'Auth' [INFO] ------------------------------------------------------------------------

rach-id commented 3 years ago

@perusworld Same here. The plugin is supposed to do download the correct solc version for you and compile the contracts using it. @rampatra hope it works for you now.

rampatra commented 3 years ago

@SweeXordious unfortunately, it still doesn't work. I even tried deleting the ~/.web3j folder, restarting my machine, etc.

I think somewhere it's cached in my system 🤔 Do you know if there are any folders I can remove and try re-running the application?

rach-id commented 3 years ago

@rampatra any progress on this ? can I close ?

rampatra commented 3 years ago

@SweeXordious no, I didn't work much on this over the weekend but would again start debugging now. Btw, do you have any idea regarding this? Now, we are at least sure that my code is okay but there's something wrong with my environment. The web3j library is somehow picking up the wrong Solidity version. So, any folders to delete, any cache to clear, anything to reinstall/uninstall?

rach-id commented 3 years ago

Maybe try creating another project and see if the issue still persists ? (without copy pasting anything)

rampatra commented 3 years ago

Finally, I was able to get past this issue.

The issue was apparently because of Intellij-Solidity plugin. 🤦‍♂️

Removing this plugin from the IDE, made the project run fine. Closing this issue now. Can't thank enough to both of you for helping me out here.