Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1596 from 0xProject/fix/sol-compiler-args
Browse files Browse the repository at this point in the history
Sol-compiler bug fixes
  • Loading branch information
LogvinovLeon authored Feb 7, 2019
2 parents 2ef3af0 + 2ff0f54 commit c55974a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 62 deletions.
12 changes: 12 additions & 0 deletions packages/sol-compiler/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
{
"note": "Fix a bug when smart recompilation wasn't working because of remappings",
"pr": 1575
},
{
"note": "Fix a bug that made `opts` required instead of optional",
"pr": 1596
},
{
"note": "Remove `bin_paths` and fetch the list of Solidity compilers from Github",
"pr": 1596
},
{
"note": "Fix a bug causing `ast` and `legacyAST` to not be added to the artifacts even when requested",
"pr": 1596
}
]
},
Expand Down
11 changes: 6 additions & 5 deletions packages/sol-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import * as semver from 'semver';
import solc = require('solc');

import { compilerOptionsSchema } from './schemas/compiler_options_schema';
import { binPaths } from './solc/bin_paths';
import {
addHexPrefixToContractBytecode,
compileDockerAsync,
compileSolcJSAsync,
createDirIfDoesNotExistAsync,
getContractArtifactIfExistsAsync,
getDependencyNameToPackagePath,
getSolcJSReleasesAsync,
getSourcesWithDependencies,
getSourceTreeHash,
makeContractPathsRelative,
Expand Down Expand Up @@ -96,12 +96,12 @@ export class Compiler {
* @return An instance of the Compiler class.
*/
constructor(opts?: CompilerOptions) {
assert.doesConformToSchema('opts', opts, compilerOptionsSchema);
const passedOpts = opts || {};
assert.doesConformToSchema('opts', passedOpts, compilerOptionsSchema);
// TODO: Look for config file in parent directories if not found in current directory
const config: CompilerOptions = fs.existsSync(CONFIG_FILE)
? JSON.parse(fs.readFileSync(CONFIG_FILE).toString())
: {};
const passedOpts = opts || {};
assert.doesConformToSchema('compiler.json', config, compilerOptionsSchema);
this._contractsDir = path.resolve(passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR);
this._solcVersionIfExists = passedOpts.solcVersion || config.solcVersion;
Expand Down Expand Up @@ -211,6 +211,7 @@ export class Compiler {
// map contract paths to data about them for later verification and persistence
const contractPathToData: ContractPathToData = {};

const solcJSReleases = await getSolcJSReleasesAsync();
const resolvedContractSources = [];
for (const contractName of contractNames) {
const spyResolver = new SpyResolver(this._resolver);
Expand All @@ -226,7 +227,7 @@ export class Compiler {
}
contractPathToData[contractSource.path] = contractData;
const solcVersion = _.isUndefined(this._solcVersionIfExists)
? semver.maxSatisfying(_.keys(binPaths), parseSolidityVersionRange(contractSource.source))
? semver.maxSatisfying(_.keys(solcJSReleases), parseSolidityVersionRange(contractSource.source))
: this._solcVersionIfExists;
const isFirstContractWithThisVersion = _.isUndefined(versionToInputs[solcVersion]);
if (isFirstContractWithThisVersion) {
Expand Down Expand Up @@ -272,7 +273,7 @@ export class Compiler {
fullSolcVersion = versionCommandOutputParts[versionCommandOutputParts.length - 1].trim();
compilerOutput = await compileDockerAsync(solcVersion, input.standardInput);
} else {
fullSolcVersion = binPaths[solcVersion];
fullSolcVersion = solcJSReleases[solcVersion];
compilerOutput = await compileSolcJSAsync(solcVersion, input.standardInput);
}
if (!_.isUndefined(compilerOutput.errors)) {
Expand Down
52 changes: 0 additions & 52 deletions packages/sol-compiler/src/solc/bin_paths.ts

This file was deleted.

23 changes: 18 additions & 5 deletions packages/sol-compiler/src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import * as path from 'path';
import * as requireFromString from 'require-from-string';
import * as solc from 'solc';

import { binPaths } from '../solc/bin_paths';

import { constants } from './constants';
import { fsWrapper } from './fs_wrapper';
import { CompilationError } from './types';
import { BinaryPaths, CompilationError } from './types';

/**
* Gets contract data on network or returns if an artifact does not exist.
Expand Down Expand Up @@ -119,6 +117,20 @@ export function parseDependencies(contractSource: ContractSource): string[] {
return dependencies;
}

let solcJSReleasesCache: BinaryPaths | undefined;

/**
* Fetches the list of available solidity compilers
*/
export async function getSolcJSReleasesAsync(): Promise<BinaryPaths> {
if (_.isUndefined(solcJSReleasesCache)) {
const versionList = await fetch('https://ethereum.github.io/solc-bin/bin/list.json');
const versionListJSON = await versionList.json();
solcJSReleasesCache = versionListJSON.releases;
}
return solcJSReleasesCache as BinaryPaths;
}

/**
* Compiles the contracts and prints errors/warnings
* @param solcVersion Version of a solc compiler
Expand Down Expand Up @@ -239,7 +251,7 @@ export function getSourcesWithDependencies(
contractPath: string,
fullSources: { [sourceName: string]: { id: number } },
): { sourceCodes: { [sourceName: string]: string }; sources: { [sourceName: string]: { id: number } } } {
const sources = { [contractPath]: { id: fullSources[contractPath].id } };
const sources = { [contractPath]: fullSources[contractPath] };
const sourceCodes = { [contractPath]: resolver.resolve(contractPath).source };
recursivelyGatherDependencySources(
resolver,
Expand Down Expand Up @@ -319,7 +331,8 @@ function recursivelyGatherDependencySources(
* @param solcVersion The compiler version. e.g. 0.5.0
*/
export async function getSolcJSAsync(solcVersion: string): Promise<solc.SolcInstance> {
const fullSolcVersion = binPaths[solcVersion];
const solcJSReleases = await getSolcJSReleasesAsync();
const fullSolcVersion = solcJSReleases[solcVersion];
if (_.isUndefined(fullSolcVersion)) {
throw new Error(`${solcVersion} is not a known compiler version`);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sol-compiler/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface ContractSourceData {
[contractName: string]: ContractSpecificSourceData;
}

export interface BinaryPaths {
[key: string]: string;
}

export interface ContractSpecificSourceData {
solcVersionRange: string;
sourceHash: Buffer;
Expand Down
3 changes: 3 additions & 0 deletions packages/sol-compiler/test/compiler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('#Compiler', function(): void {
contractsDir,
contracts: constants.contracts,
};
it('should create a Compiler with empty opts', async () => {
const _compiler = new Compiler(); // tslint:disable-line no-unused-variable
});
it('should create an Exchange artifact with the correct unlinked binary', async () => {
compilerOpts.contracts = ['Exchange'];

Expand Down

0 comments on commit c55974a

Please sign in to comment.