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

Bugfix/issue 70 #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .idea/.gitignore
Empty file.
Empty file added .idea/encodings.xml
Empty file.
Empty file added .idea/jarRepositories.xml
Empty file.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>web3j-sokt</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>com.github.zafarkhaja</groupId>
<artifactId>java-semver</artifactId>
<version>0.9.0</version>
</dependency>

</dependencies>

Expand Down
82 changes: 74 additions & 8 deletions src/main/java/org/web3j/mavenplugin/solidity/SolidityCompiler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.web3j.mavenplugin.solidity;

import com.github.zafarkhaja.semver.Version;
import org.apache.maven.plugin.logging.Log;
import org.web3j.sokt.SolcInstance;
import org.web3j.sokt.SolidityFile;
import org.web3j.tuples.generated.Tuple2;

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -23,6 +21,9 @@ public class SolidityCompiler {

private Log LOG;

private static final String LOWEST_PRAGMA_VERSION = "0.0.0";
private static final String HIGHEST_PRAGMA_VERSION = "1000.1000.1000";

private static SolidityCompiler INSTANCE;
private String usedSolCVersion;

Expand Down Expand Up @@ -73,7 +74,7 @@ public CompilerResult compileSrc(
}

private Process getSolcProcessFromSokt(String rootDirectory, Collection<String> sources, String[] pathPrefixes, Options[] options) throws IOException {
SolidityFile solidityFile = new SolidityFile(Paths.get(rootDirectory, sources.iterator().next()).toFile().getAbsolutePath());
SolidityFile solidityFile = checkPragmaVersions(rootDirectory, sources);
SolcInstance instance = solidityFile.getCompilerInstance(".web3j", true);
if (!instance.installed()) instance.install();
usedSolCVersion = instance.getSolcRelease().getVersion();
Expand All @@ -83,6 +84,72 @@ private Process getSolcProcessFromSokt(String rootDirectory, Collection<String>
return process;
}

private SolidityFile checkPragmaVersions(String rootDirectory, Collection<String> sources) {
SolidityFile solidityFile = null;
if (sources != null) {
solidityFile = findMinAndMaxPragma(rootDirectory, sources);
}
return solidityFile;
}

//narrow down solidity versions to the most common
private SolidityFile findMinAndMaxPragma(String rootDirectory, Collection<String> sources) {
Version minVersion = Version.valueOf(LOWEST_PRAGMA_VERSION);
Version maxVersion = Version.valueOf(HIGHEST_PRAGMA_VERSION);
SolidityFile selectedSolidityFile = null;
for (String source : sources) {
SolidityFile solidityFile = new SolidityFile(Paths.get(rootDirectory, source).toFile().getAbsolutePath());
Tuple2<Version,Version> minMaxTuple = getCurrentVersion(rootDirectory, solidityFile);
Version minCurrentVersion = minMaxTuple.component1();
Version maxCurrentVersion = minMaxTuple.component2();
boolean changedMin = false;
boolean changedMax = false;
if (minCurrentVersion.greaterThanOrEqualTo(minVersion)) {
minVersion = minCurrentVersion;
changedMin = true;
}
if (maxCurrentVersion.lessThanOrEqualTo(maxVersion)) {
maxVersion = maxCurrentVersion;
changedMax = true;
}
if (changedMin && changedMax) {
selectedSolidityFile = solidityFile;
}
}
if ((minVersion.greaterThan(maxVersion))) {
throw new RuntimeException("There are some contracts that have higher lowest pragma version then the highest pragma versions of other contracts.");
}
return selectedSolidityFile;
}

private Tuple2<Version, Version> getCurrentVersion(String rootDirectory, SolidityFile solidityFile) {
Version minCurrentVersion;
Version maxCurrentVersion;
String[] versions = cutVersionOperator(solidityFile.getVersionPragma()).replace(";","").split(" ");
if (versions.length == 1) {
minCurrentVersion = Version.valueOf(versions[0]);
maxCurrentVersion = Version.forIntegers(minCurrentVersion.getMajorVersion(), minCurrentVersion.getMinorVersion() + 1, minCurrentVersion.getPatchVersion());
} else {
Version firstVersion = Version.valueOf(versions[0]);
Version secondVersion = Version.valueOf(versions[1]);
if (firstVersion.greaterThan(secondVersion)) {
minCurrentVersion = secondVersion;
maxCurrentVersion = firstVersion;
} else {
minCurrentVersion = firstVersion;
maxCurrentVersion = secondVersion;
}
}
return new Tuple2<>(minCurrentVersion, maxCurrentVersion);
}

private String cutVersionOperator(String version) {
return version.replace(">", "")
.replace("<", "")
.replace("=", "")
.replace("^", "");
}

private Map<String, String> getAbsolutePathPrefixes(String rootDirectory, String[] pathPrefixes) {
return Stream.of(pathPrefixes)
.map(pathPrefix -> replaceMakePathPrefixAbsolute(rootDirectory, pathPrefix))
Expand Down Expand Up @@ -132,8 +199,7 @@ public enum Options {
BIN("bin"),
INTERFACE("interface"),
ABI("abi"),
METADATA("metadata")
;
METADATA("metadata");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.web3j.mavenplugin.solidity;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static org.hamcrest.CoreMatchers.containsString;
Expand Down Expand Up @@ -65,6 +70,23 @@ public void pragmaVersionTooHigh() {
}
}

@Test
public void differentPragmas() {
//delete /home/$user/.web3j folder to download evrerytime (fresh install) new solc version
String currentUsersHomeDir = System.getProperty("user.home");
try {
FileUtils.deleteDirectory(Paths.get(currentUsersHomeDir + "/.web3j").toFile());
} catch (IOException e) {
e.printStackTrace();
}
List<String> source = new LinkedList<>();
//it will download 0.6.12 version
source.add("File2.sol");
source.add("File1.sol");
CompilerResult result = solidityCompiler.compileSrc("src/test/resources/issue-70", source, new String[0], SolidityCompiler.Options.ABI, SolidityCompiler.Options.BIN);
assertTrue(result.errors.isEmpty());
}

@Before
public void loadCompiler() throws MojoExecutionException {
solidityCompiler = SolidityCompiler.getInstance(new SystemStreamLog());
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/issue-70/File1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity >=0.5.0 <0.6.0;

contract File1 {

}
5 changes: 5 additions & 0 deletions src/test/resources/issue-70/File2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity >=0.5.0 <0.7.0;

contract File2 {

}