This commit is contained in:
Leonid Logvinov 2019-01-28 16:24:54 +01:00
parent 19064f8cbb
commit 0c12128f64
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
14 changed files with 93 additions and 126 deletions

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -1,7 +1,6 @@
{ {
"artifactsDir": "./generated-artifacts", "artifactsDir": "./generated-artifacts",
"contractsDir": "./contracts", "contractsDir": "./contracts",
"workspaceDir": "../..",
"useDockerisedSolc": true, "useDockerisedSolc": true,
"compilerSettings": { "compilerSettings": {
"optimizer": { "optimizer": {

View File

@ -503,8 +503,7 @@ export interface Source {
/** /**
* Options you can specify (as flags or in a compiler.json file) when invoking sol-compiler * Options you can specify (as flags or in a compiler.json file) when invoking sol-compiler
* contractsDir: Directory containing your package's Solidity contracts. Can contain nested directories. * contractsDir: Directory containing your project's Solidity contracts. Can contain nested directories.
* workspaceDir: Directory containing your project's Solidity contracts. All the contracts used in compilation must be withing it. Similar to --allow-paths in Solidity.
* artifactsDir: Directory where you want the generated artifacts.json written to * artifactsDir: Directory where you want the generated artifacts.json written to
* compilerSettings: Desired settings to pass to the Solidity compiler during compilation. * compilerSettings: Desired settings to pass to the Solidity compiler during compilation.
* (http://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html#compiler-input-and-output-json-description) * (http://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html#compiler-input-and-output-json-description)
@ -516,7 +515,6 @@ export interface Source {
*/ */
export interface CompilerOptions { export interface CompilerOptions {
contractsDir?: string; contractsDir?: string;
workspaceDir?: string;
artifactsDir?: string; artifactsDir?: string;
compilerSettings?: CompilerSettings; compilerSettings?: CompilerSettings;
contracts?: string[] | '*'; contracts?: string[] | '*';

View File

@ -28,10 +28,13 @@ import {
compileSolcJS, compileSolcJS,
createDirIfDoesNotExistAsync, createDirIfDoesNotExistAsync,
getContractArtifactIfExistsAsync, getContractArtifactIfExistsAsync,
getDependencyNameToPackagePath,
getSolcJSAsync, getSolcJSAsync,
getSourcesWithDependencies, getSourcesWithDependencies,
getSourceTreeHash, getSourceTreeHash,
makeContractPathsRelative,
parseSolidityVersionRange, parseSolidityVersionRange,
printCompilationErrorsAndWarnings,
} from './utils/compiler'; } from './utils/compiler';
import { constants } from './utils/constants'; import { constants } from './utils/constants';
import { fsWrapper } from './utils/fs_wrapper'; import { fsWrapper } from './utils/fs_wrapper';
@ -83,7 +86,6 @@ export class Compiler {
private readonly _resolver: Resolver; private readonly _resolver: Resolver;
private readonly _nameResolver: NameResolver; private readonly _nameResolver: NameResolver;
private readonly _contractsDir: string; private readonly _contractsDir: string;
private readonly _workspaceDir: string;
private readonly _compilerSettings: solc.CompilerSettings; private readonly _compilerSettings: solc.CompilerSettings;
private readonly _artifactsDir: string; private readonly _artifactsDir: string;
private readonly _solcVersionIfExists: string | undefined; private readonly _solcVersionIfExists: string | undefined;
@ -103,12 +105,6 @@ export class Compiler {
const passedOpts = opts || {}; const passedOpts = opts || {};
assert.doesConformToSchema('compiler.json', config, compilerOptionsSchema); assert.doesConformToSchema('compiler.json', config, compilerOptionsSchema);
this._contractsDir = path.resolve(passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR); this._contractsDir = path.resolve(passedOpts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR);
this._workspaceDir = path.resolve(passedOpts.workspaceDir || config.workspaceDir || this._contractsDir);
if (!this._contractsDir.includes(this._workspaceDir)) {
throw new Error(
`Contracts dir ${this._contractsDir} is outside of the workspace dir ${this._workspaceDir}`,
);
}
this._solcVersionIfExists = passedOpts.solcVersion || config.solcVersion; this._solcVersionIfExists = passedOpts.solcVersion || config.solcVersion;
this._compilerSettings = passedOpts.compilerSettings || config.compilerSettings || DEFAULT_COMPILER_SETTINGS; this._compilerSettings = passedOpts.compilerSettings || config.compilerSettings || DEFAULT_COMPILER_SETTINGS;
this._artifactsDir = passedOpts.artifactsDir || config.artifactsDir || DEFAULT_ARTIFACTS_DIR; this._artifactsDir = passedOpts.artifactsDir || config.artifactsDir || DEFAULT_ARTIFACTS_DIR;
@ -118,7 +114,7 @@ export class Compiler {
this._nameResolver = new NameResolver(this._contractsDir); this._nameResolver = new NameResolver(this._contractsDir);
const resolver = new FallthroughResolver(); const resolver = new FallthroughResolver();
resolver.appendResolver(new URLResolver()); resolver.appendResolver(new URLResolver());
resolver.appendResolver(new NPMResolver(this._contractsDir, this._workspaceDir)); resolver.appendResolver(new NPMResolver(this._contractsDir));
resolver.appendResolver(new RelativeFSResolver(this._contractsDir)); resolver.appendResolver(new RelativeFSResolver(this._contractsDir));
resolver.appendResolver(new FSResolver()); resolver.appendResolver(new FSResolver());
resolver.appendResolver(this._nameResolver); resolver.appendResolver(this._nameResolver);
@ -217,9 +213,10 @@ export class Compiler {
// map contract paths to data about them for later verification and persistence // map contract paths to data about them for later verification and persistence
const contractPathToData: ContractPathToData = {}; const contractPathToData: ContractPathToData = {};
const spyResolver = new SpyResolver(this._resolver);
const resolvedContractSources = [];
for (const contractName of contractNames) { for (const contractName of contractNames) {
const spyResolver = new SpyResolver(this._resolver);
const contractSource = spyResolver.resolve(contractName); const contractSource = spyResolver.resolve(contractName);
const sourceTreeHashHex = getSourceTreeHash( const sourceTreeHashHex = getSourceTreeHash(
spyResolver, spyResolver,
@ -249,40 +246,19 @@ export class Compiler {
}; };
} }
// add input to the right version batch // add input to the right version batch
versionToInputs[solcVersion].standardInput.sources[contractSource.path] = { for (const resolvedContractSource of spyResolver.resolvedContractSources) {
content: contractSource.source, versionToInputs[solcVersion].standardInput.sources[resolvedContractSource.absolutePath] = {
content: resolvedContractSource.source,
}; };
}
resolvedContractSources.push(...spyResolver.resolvedContractSources);
versionToInputs[solcVersion].contractsToCompile.push(contractSource.path); versionToInputs[solcVersion].contractsToCompile.push(contractSource.path);
} }
const allTouchedFiles = spyResolver.resolvedContractSources.map( const dependencyNameToPackagePath = getDependencyNameToPackagePath(resolvedContractSources);
contractSource => `${contractSource.absolutePath}`,
);
const NODE_MODULES = 'node_modules';
const allTouchedDependencies = _.filter(allTouchedFiles, filePath => filePath.includes(NODE_MODULES));
const dependencyNameToPackagePath: { [dependencyName: string]: string } = {};
_.map(allTouchedDependencies, dependencyFilePath => {
const lastNodeModulesStart = dependencyFilePath.lastIndexOf(NODE_MODULES);
const lastNodeModulesEnd = lastNodeModulesStart + NODE_MODULES.length;
const importPath = dependencyFilePath.substr(lastNodeModulesEnd + 1);
let packageName;
let packageScopeIfExists;
let dependencyName;
if (_.startsWith(importPath, '@')) {
[packageScopeIfExists, packageName] = importPath.split('/');
dependencyName = `${packageScopeIfExists}/${packageName}`;
} else {
[packageName] = importPath.split('/');
dependencyName = `${packageName}`;
}
const dependencyPackagePath = path.join(dependencyFilePath.substr(0, lastNodeModulesEnd), dependencyName);
dependencyNameToPackagePath[dependencyName] = dependencyPackagePath;
});
const compilerOutputs: StandardOutput[] = []; const compilerOutputs: StandardOutput[] = [];
for (const solcVersion of _.keys(versionToInputs)) {
const solcVersions = _.keys(versionToInputs);
for (const solcVersion of solcVersions) {
const input = versionToInputs[solcVersion]; const input = versionToInputs[solcVersion];
logUtils.warn( logUtils.warn(
`Compiling ${input.contractsToCompile.length} contracts (${ `Compiling ${input.contractsToCompile.length} contracts (${
@ -291,29 +267,37 @@ export class Compiler {
); );
let compilerOutput; let compilerOutput;
let fullSolcVersion; let fullSolcVersion;
input.standardInput.settings.remappings = _.map(
dependencyNameToPackagePath,
(dependencyPackagePath: string, dependencyName: string) => `${dependencyName}=${dependencyPackagePath}`,
);
if (this._useDockerisedSolc) { if (this._useDockerisedSolc) {
const dockerCommand = `docker run ethereum/solc:${solcVersion} --version`; const dockerCommand = `docker run ethereum/solc:${solcVersion} --version`;
const versionCommandOutput = execSync(dockerCommand).toString(); const versionCommandOutput = execSync(dockerCommand).toString();
const versionCommandOutputParts = versionCommandOutput.split(' '); const versionCommandOutputParts = versionCommandOutput.split(' ');
fullSolcVersion = versionCommandOutputParts[versionCommandOutputParts.length - 1].trim(); fullSolcVersion = versionCommandOutputParts[versionCommandOutputParts.length - 1].trim();
compilerOutput = compileDocker( compilerOutput = compileDocker(solcVersion, input.standardInput);
this._resolver,
this._contractsDir,
this._workspaceDir,
solcVersion,
dependencyNameToPackagePath,
input.standardInput,
);
} else { } else {
fullSolcVersion = binPaths[solcVersion]; fullSolcVersion = binPaths[solcVersion];
const solcInstance = await getSolcJSAsync(solcVersion); compilerOutput = compileSolcJS(solcVersion, input.standardInput);
compilerOutput = compileSolcJS(this._resolver, solcInstance, input.standardInput); }
compilerOutput.sources = makeContractPathsRelative(
compilerOutput.sources,
this._contractsDir,
dependencyNameToPackagePath,
);
compilerOutput.contracts = makeContractPathsRelative(
compilerOutput.contracts,
this._contractsDir,
dependencyNameToPackagePath,
);
if (!_.isUndefined(compilerOutput.errors)) {
printCompilationErrorsAndWarnings(compilerOutput.errors);
} }
compilerOutputs.push(compilerOutput); compilerOutputs.push(compilerOutput);
for (const contractPath of input.contractsToCompile) { for (const contractPath of input.contractsToCompile) {
// console.log('contractsPath', contractPath);
const contractName = contractPathToData[contractPath].contractName; const contractName = contractPathToData[contractPath].contractName;
const compiledContract = compilerOutput.contracts[contractPath][contractName]; const compiledContract = compilerOutput.contracts[contractPath][contractName];

View File

@ -2,7 +2,6 @@ export const compilerOptionsSchema = {
id: '/CompilerOptions', id: '/CompilerOptions',
properties: { properties: {
contractsDir: { type: 'string' }, contractsDir: { type: 'string' },
workspaceDir: { type: 'string' },
artifactsDir: { type: 'string' }, artifactsDir: { type: 'string' },
solcVersion: { type: 'string', pattern: '^\\d+.\\d+.\\d+$' }, solcVersion: { type: 'string', pattern: '^\\d+.\\d+.\\d+$' },
compilerSettings: { type: 'object' }, compilerSettings: { type: 'object' },

View File

@ -118,76 +118,35 @@ export function parseDependencies(contractSource: ContractSource): string[] {
/** /**
* Compiles the contracts and prints errors/warnings * Compiles the contracts and prints errors/warnings
* @param resolver Resolver * @param solcVersion Version of a solc compiler
* @param solcInstance Instance of a solc compiler
* @param standardInput Solidity standard JSON input * @param standardInput Solidity standard JSON input
*/ */
export function compileSolcJS( export async function compileSolcJSAsync(
resolver: Resolver, solcVersion: string,
solcInstance: solc.SolcInstance,
standardInput: solc.StandardInput, standardInput: solc.StandardInput,
): solc.StandardOutput { ): Promise<solc.StandardOutput> {
const solcInstance = await getSolcJSAsync(solcVersion);
const standardInputStr = JSON.stringify(standardInput); const standardInputStr = JSON.stringify(standardInput);
const standardOutputStr = solcInstance.compileStandardWrapper(standardInputStr, importPath => { const standardOutputStr = solcInstance.compileStandardWrapper(standardInputStr);
const sourceCodeIfExists = resolver.resolve(importPath);
return { contents: sourceCodeIfExists.source };
});
const compiled: solc.StandardOutput = JSON.parse(standardOutputStr); const compiled: solc.StandardOutput = JSON.parse(standardOutputStr);
if (!_.isUndefined(compiled.errors)) {
printCompilationErrorsAndWarnings(compiled.errors);
}
return compiled; return compiled;
} }
/** /**
* Compiles the contracts and prints errors/warnings * Compiles the contracts and prints errors/warnings
* @param resolver Resolver
* @param contractsDir Contracts directory
* @param workspaceDir Workspace directory
* @param solcVersion Version of a solc compiler * @param solcVersion Version of a solc compiler
* @param dependencyNameToPackagePath Mapping of dependency name to it's package path
* @param standardInput Solidity standard JSON input * @param standardInput Solidity standard JSON input
*/ */
export function compileDocker( export async function compileDockerAsync(
resolver: Resolver,
contractsDir: string,
workspaceDir: string,
solcVersion: string, solcVersion: string,
dependencyNameToPackagePath: { [dependencyName: string]: string },
standardInput: solc.StandardInput, standardInput: solc.StandardInput,
): solc.StandardOutput { ): Promise<solc.StandardOutput> {
const standardInputDocker = _.cloneDeep(standardInput); const standardInputStr = JSON.stringify(standardInput, null, 2);
standardInputDocker.settings.remappings = _.map(
dependencyNameToPackagePath,
(dependencyPackagePath: string, dependencyName: string) => `${dependencyName}=${dependencyPackagePath}`,
);
standardInputDocker.sources = _.mapKeys(
standardInputDocker.sources,
(_source: solc.Source, sourcePath: string) => resolver.resolve(sourcePath).absolutePath,
);
const standardInputStrDocker = JSON.stringify(standardInputDocker, null, 2);
const dockerCommand = const dockerCommand =
`docker run -i -a stdin -a stdout -a stderr -v ${workspaceDir}:${workspaceDir} ethereum/solc:${solcVersion} ` + `docker run -i -a stdin -a stdout -a stderr ethereum/solc:${solcVersion} ` + `solc --standard-json`;
`solc --standard-json --allow-paths ${workspaceDir}`; const standardOutputStr = execSync(dockerCommand, { input: standardInputStr }).toString();
const standardOutputStrDocker = execSync(dockerCommand, { input: standardInputStrDocker }).toString(); const compiled: solc.StandardOutput = JSON.parse(standardOutputStr);
const compiledDocker: solc.StandardOutput = JSON.parse(standardOutputStrDocker); return compiled;
if (!_.isUndefined(compiledDocker.errors)) {
printCompilationErrorsAndWarnings(compiledDocker.errors);
}
compiledDocker.sources = makeContractPathsRelative(
compiledDocker.sources,
contractsDir,
dependencyNameToPackagePath,
);
compiledDocker.contracts = makeContractPathsRelative(
compiledDocker.contracts,
contractsDir,
dependencyNameToPackagePath,
);
return compiledDocker;
} }
function makeContractPathRelative( function makeContractPathRelative(
@ -202,7 +161,13 @@ function makeContractPathRelative(
return contractPath; return contractPath;
} }
function makeContractPathsRelative( /**
* Makes the path relative removing all system-dependent data. Converts absolute paths to a format suitable for artifacts.
* @param absolutePathToSmth Absolute path to contract or source
* @param contractsDir Current package contracts directory location
* @param dependencyNameToPackagePath Mapping of dependency name to package path
*/
export function makeContractPathsRelative(
absolutePathToSmth: { [absoluteContractPath: string]: any }, absolutePathToSmth: { [absoluteContractPath: string]: any },
contractsDir: string, contractsDir: string,
dependencyNameToPackagePath: { [dependencyName: string]: string }, dependencyNameToPackagePath: { [dependencyName: string]: string },
@ -216,7 +181,7 @@ function makeContractPathsRelative(
* Separates errors from warnings, formats the messages and prints them. Throws if there is any compilation error (not warning). * Separates errors from warnings, formats the messages and prints them. Throws if there is any compilation error (not warning).
* @param solcErrors The errors field of standard JSON output that contains errors and warnings. * @param solcErrors The errors field of standard JSON output that contains errors and warnings.
*/ */
function printCompilationErrorsAndWarnings(solcErrors: solc.SolcError[]): void { export function printCompilationErrorsAndWarnings(solcErrors: solc.SolcError[]): void {
const SOLIDITY_WARNING = 'warning'; const SOLIDITY_WARNING = 'warning';
const errors = _.filter(solcErrors, entry => entry.severity !== SOLIDITY_WARNING); const errors = _.filter(solcErrors, entry => entry.severity !== SOLIDITY_WARNING);
const warnings = _.filter(solcErrors, entry => entry.severity === SOLIDITY_WARNING); const warnings = _.filter(solcErrors, entry => entry.severity === SOLIDITY_WARNING);
@ -392,3 +357,35 @@ export function addHexPrefixToContractBytecode(compiledContract: solc.StandardCo
} }
} }
} }
/**
* Takes the list of resolved contract sources from `SpyResolver` and produces a mapping from dependency name
* to package path used in `remappings` later.
* @param contractSources The list of resolved contract sources
*/
export function getDependencyNameToPackagePath(
contractSources: ContractSource[],
): { [dependencyName: string]: string } {
const allTouchedFiles = contractSources.map(contractSource => `${contractSource.absolutePath}`);
const NODE_MODULES = 'node_modules';
const allTouchedDependencies = _.filter(allTouchedFiles, filePath => filePath.includes(NODE_MODULES));
const dependencyNameToPackagePath: { [dependencyName: string]: string } = {};
_.map(allTouchedDependencies, dependencyFilePath => {
const lastNodeModulesStart = dependencyFilePath.lastIndexOf(NODE_MODULES);
const lastNodeModulesEnd = lastNodeModulesStart + NODE_MODULES.length;
const importPath = dependencyFilePath.substr(lastNodeModulesEnd + 1);
let packageName;
let packageScopeIfExists;
let dependencyName;
if (_.startsWith(importPath, '@')) {
[packageScopeIfExists, packageName] = importPath.split('/');
dependencyName = `${packageScopeIfExists}/${packageName}`;
} else {
[packageName] = importPath.split('/');
dependencyName = `${packageName}`;
}
const dependencyPackagePath = path.join(dependencyFilePath.substr(0, lastNodeModulesEnd), dependencyName);
dependencyNameToPackagePath[dependencyName] = dependencyPackagePath;
});
return dependencyNameToPackagePath;
}

View File

@ -8,11 +8,9 @@ import { Resolver } from './resolver';
export class NPMResolver extends Resolver { export class NPMResolver extends Resolver {
private readonly _packagePath: string; private readonly _packagePath: string;
private readonly _workspacePath: string; constructor(packagePath: string) {
constructor(packagePath: string, workspacePath: string = '/') {
super(); super();
this._packagePath = packagePath; this._packagePath = packagePath;
this._workspacePath = workspacePath;
} }
public resolveIfExists(importPath: string): ContractSource | undefined { public resolveIfExists(importPath: string): ContractSource | undefined {
if (!importPath.startsWith('/')) { if (!importPath.startsWith('/')) {
@ -25,11 +23,9 @@ export class NPMResolver extends Resolver {
[packageName, ...other] = importPath.split('/'); [packageName, ...other] = importPath.split('/');
} }
const pathWithinPackage = path.join(...other); const pathWithinPackage = path.join(...other);
for (
let currentPath = this._packagePath; let currentPath = this._packagePath;
currentPath.includes(this._workspacePath); const ROOT_PATH = '/';
currentPath = path.dirname(currentPath) while (currentPath !== ROOT_PATH) {
) {
const packagePath = _.isUndefined(packageScopeIfExists) const packagePath = _.isUndefined(packageScopeIfExists)
? packageName ? packageName
: path.join(packageScopeIfExists, packageName); : path.join(packageScopeIfExists, packageName);
@ -38,6 +34,7 @@ export class NPMResolver extends Resolver {
const fileContent = fs.readFileSync(lookupPath).toString(); const fileContent = fs.readFileSync(lookupPath).toString();
return { source: fileContent, path: importPath, absolutePath: lookupPath }; return { source: fileContent, path: importPath, absolutePath: lookupPath };
} }
currentPath = path.dirname(currentPath);
} }
} }
return undefined; return undefined;

View File

@ -95,7 +95,7 @@ declare module 'solc' {
optimizerEnabled: number, optimizerEnabled: number,
findImports: (importPath: string) => ImportContents, findImports: (importPath: string) => ImportContents,
): CompilationResult; ): CompilationResult;
compileStandardWrapper(input: string, findImports: (importPath: string) => ImportContents): string; compileStandardWrapper(input: string, findImports?: (importPath: string) => ImportContents): string;
} }
export function loadRemoteVersion( export function loadRemoteVersion(
versionName: string, versionName: string,