aboutsummaryrefslogtreecommitdiffstats
path: root/packages/deployer/src/compiler.ts
diff options
context:
space:
mode:
authorAmir Bandeali <abandeali1@gmail.com>2018-02-18 06:07:05 +0800
committerAmir Bandeali <abandeali1@gmail.com>2018-02-21 05:19:12 +0800
commit7fb66bf71a0a86c693a0411c6e03d81982b9054e (patch)
tree524e67ee16acf9bd993f439e9850128a40311596 /packages/deployer/src/compiler.ts
parent097fc477a2e06b8004d98e77dc17d98ab26ab3f1 (diff)
downloaddexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar.gz
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar.bz2
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar.lz
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar.xz
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.tar.zst
dexon-sol-tools-7fb66bf71a0a86c693a0411c6e03d81982b9054e.zip
Check dependencies when determining if should be recompiled
Diffstat (limited to 'packages/deployer/src/compiler.ts')
-rw-r--r--packages/deployer/src/compiler.ts206
1 files changed, 138 insertions, 68 deletions
diff --git a/packages/deployer/src/compiler.ts b/packages/deployer/src/compiler.ts
index 2b0b81c44..6eca4b57f 100644
--- a/packages/deployer/src/compiler.ts
+++ b/packages/deployer/src/compiler.ts
@@ -5,19 +5,25 @@ import solc = require('solc');
import * as Web3 from 'web3';
import { binPaths } from './solc/bin_paths';
+import { constants } from './utils/constants';
import { fsWrapper } from './utils/fs_wrapper';
import {
CompilerOptions,
ContractArtifact,
- ContractData,
+ ContractNetworkData,
ContractNetworks,
+ ContractSourceData,
ContractSources,
+ ContractSpecificSourceData,
ImportContents,
} from './utils/types';
import { utils } from './utils/utils';
-const SOLIDITY_FILE_EXTENSION = '.sol';
const ALL_CONTRACTS_IDENTIFIER = '*';
+const SOLIDITY_VERSION_REGEX = /(?:solidity\s\^?)([0-9]{1,2}[.][0-9]{1,2}[.][0-9]{1,2})/;
+const SOLIDITY_FILE_EXTENSION_REGEX = /(.*\.sol)/;
+const IMPORT_REGEX = /(import\s)/;
+const DEPENDENCY_PATH_REGEX = /"([^"]+)"/;
export class Compiler {
private _contractsDir: string;
@@ -25,12 +31,13 @@ export class Compiler {
private _optimizerEnabled: number;
private _artifactsDir: string;
private _contractSourcesIfExists?: ContractSources;
- private _solcErrors: Set<string>;
- private _specifiedContracts: Set<string>;
+ private _solcErrors: Set<string> = new Set();
+ private _specifiedContracts: Set<string> = new Set();
+ private _contractSourceData: ContractSourceData = {};
/**
* Recursively retrieves Solidity source code from directory.
* @param dirPath Directory to search.
- * @return Mapping of contract name to contract source.
+ * @return Mapping of contract fileName to contract source.
*/
private static async _getContractSourcesAsync(dirPath: string): Promise<ContractSources> {
let dirContents: string[] = [];
@@ -40,15 +47,16 @@ export class Compiler {
throw new Error(`No directory found at ${dirPath}`);
}
let sources: ContractSources = {};
- for (const name of dirContents) {
- const contentPath = `${dirPath}/${name}`;
- if (path.extname(name) === SOLIDITY_FILE_EXTENSION) {
+ for (const fileName of dirContents) {
+ const contentPath = `${dirPath}/${fileName}`;
+ if (path.extname(fileName) === constants.SOLIDITY_FILE_EXTENSION) {
try {
const opts = {
encoding: 'utf8',
};
- sources[name] = await fsWrapper.readFileAsync(contentPath, opts);
- utils.consoleLog(`Reading ${name} source...`);
+ const source = await fsWrapper.readFileAsync(contentPath, opts);
+ sources[fileName] = source;
+ utils.consoleLog(`Reading ${fileName} source...`);
} catch (err) {
utils.consoleLog(`Could not find file at ${contentPath}`);
}
@@ -60,19 +68,58 @@ export class Compiler {
...nestedSources,
};
} catch (err) {
- utils.consoleLog(`${contentPath} is not a directory or ${SOLIDITY_FILE_EXTENSION} file`);
+ utils.consoleLog(`${contentPath} is not a directory or ${constants.SOLIDITY_FILE_EXTENSION} file`);
}
}
}
return sources;
}
/**
+ * Gets contract dependendencies and keccak256 hash from source.
+ * @param source Source code of contract.
+ * @return Object with contract dependencies and keccak256 hash of source.
+ */
+ private static _getContractSpecificSourceData(source: string): ContractSpecificSourceData {
+ const dependencies: string[] = [];
+ const sourceHash = `0x${ethUtil.sha3(source).toString('hex')}`;
+ const solc_version = Compiler._parseSolidityVersion(source);
+ const contractSpecificSourceData: ContractSpecificSourceData = {
+ dependencies,
+ solc_version,
+ keccak256: sourceHash,
+ };
+ const lines = source.split('\n');
+ _.forEach(lines, line => {
+ if (!_.isNull(line.match(IMPORT_REGEX))) {
+ const dependencyMatch = line.match(DEPENDENCY_PATH_REGEX);
+ if (!_.isNull(dependencyMatch)) {
+ const dependencyPath = dependencyMatch[1];
+ const fileName = path.basename(dependencyPath);
+ contractSpecificSourceData.dependencies.push(fileName);
+ }
+ }
+ });
+ return contractSpecificSourceData;
+ }
+ /**
+ * Finds dependencies, keccak256 hashes, and compile flag for each contract.
+ * @param sources Mapping of contract file name to source code.
+ * @return Dependencies, keccak256 hash, and compile flag for each contract.
+ */
+ private static _getContractSourceData(sources: ContractSources): ContractSourceData {
+ const contractSourceData: ContractSourceData = {};
+ _.forIn(sources, (source, fileName) => {
+ contractSourceData[fileName] = Compiler._getContractSpecificSourceData(source);
+ });
+ return contractSourceData;
+ }
+ /**
* Searches Solidity source code for compiler version.
* @param source Source code of contract.
* @return Solc compiler version.
*/
private static _parseSolidityVersion(source: string): string {
- const solcVersionMatch = source.match(/(?:solidity\s\^?)([0-9]{1,2}[.][0-9]{1,2}[.][0-9]{1,2})/);
+ const solcVersionMatch = source.match(SOLIDITY_VERSION_REGEX);
if (_.isNull(solcVersionMatch)) {
throw new Error('Could not find Solidity version in source');
}
@@ -88,7 +135,7 @@ export class Compiler {
* @return The error message with directories truncated from the contract path.
*/
private static _getNormalizedErrMsg(errMsg: string): string {
- const errPathMatch = errMsg.match(/(.*\.sol)/);
+ const errPathMatch = errMsg.match(SOLIDITY_FILE_EXTENSION_REGEX);
if (_.isNull(errPathMatch)) {
throw new Error('Could not find a path in error message');
}
@@ -107,7 +154,6 @@ export class Compiler {
this._networkId = opts.networkId;
this._optimizerEnabled = opts.optimizerEnabled;
this._artifactsDir = opts.artifactsDir;
- this._solcErrors = new Set();
this._specifiedContracts = opts.specifiedContracts;
}
/**
@@ -116,11 +162,12 @@ export class Compiler {
public async compileAllAsync(): Promise<void> {
await this._createArtifactsDirIfDoesNotExistAsync();
this._contractSourcesIfExists = await Compiler._getContractSourcesAsync(this._contractsDir);
- const contractBaseNames = _.keys(this._contractSourcesIfExists);
- const compiledContractPromises = _.map(contractBaseNames, async (contractBaseName: string): Promise<void> => {
- return this._compileContractAsync(contractBaseName);
- });
- await Promise.all(compiledContractPromises);
+ this._contractSourceData = Compiler._getContractSourceData(this._contractSourcesIfExists);
+ const fileNames = this._specifiedContracts.has(ALL_CONTRACTS_IDENTIFIER)
+ ? _.keys(this._contractSourcesIfExists)
+ : Array.from(this._specifiedContracts.values());
+ await Promise.all(_.map(fileNames, async fileName => this._setCompileActionAsync(fileName)));
+ await Promise.all(_.map(fileNames, async fileName => this._compileContractAsync(fileName)));
this._solcErrors.forEach(errMsg => {
utils.consoleLog(errMsg);
@@ -128,55 +175,27 @@ export class Compiler {
}
/**
* Compiles contract and saves artifact to artifactsDir.
- * @param contractBaseName Name of contract with '.sol' extension.
+ * @param fileName Name of contract with '.sol' extension.
*/
- private async _compileContractAsync(contractBaseName: string): Promise<void> {
+ private async _compileContractAsync(fileName: string): Promise<void> {
if (_.isUndefined(this._contractSourcesIfExists)) {
throw new Error('Contract sources not yet initialized');
}
-
- const source = this._contractSourcesIfExists[contractBaseName];
- const contractName = path.basename(contractBaseName, SOLIDITY_FILE_EXTENSION);
- const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`;
- const sourceHash = `0x${ethUtil.sha3(source).toString('hex')}`;
- const isContractSpecified =
- this._specifiedContracts.has(ALL_CONTRACTS_IDENTIFIER) || this._specifiedContracts.has(contractName);
-
- let currentArtifactString: string;
- let currentArtifact: ContractArtifact;
- let oldNetworks: ContractNetworks;
- let shouldCompile: boolean;
- try {
- const opts = {
- encoding: 'utf8',
- };
- currentArtifactString = await fsWrapper.readFileAsync(currentArtifactPath, opts);
- currentArtifact = JSON.parse(currentArtifactString);
- oldNetworks = currentArtifact.networks;
- const oldNetwork: ContractData = oldNetworks[this._networkId];
- shouldCompile =
- (_.isUndefined(oldNetwork) ||
- oldNetwork.keccak256 !== sourceHash ||
- oldNetwork.optimizer_enabled !== this._optimizerEnabled) &&
- isContractSpecified;
- } catch (err) {
- shouldCompile = isContractSpecified;
- }
-
- if (!shouldCompile) {
+ const contractSpecificSourceData = this._contractSourceData[fileName];
+ if (!contractSpecificSourceData.shouldCompile) {
return;
}
-
+ const source = this._contractSourcesIfExists[fileName];
const input = {
- [contractBaseName]: source,
+ [fileName]: source,
};
- const solcVersion = Compiler._parseSolidityVersion(source);
- const fullSolcVersion = binPaths[solcVersion];
+
+ const fullSolcVersion = binPaths[contractSpecificSourceData.solc_version];
const solcBinPath = `./solc/solc_bin/${fullSolcVersion}`;
const solcBin = require(solcBinPath);
const solcInstance = solc.setupMethods(solcBin);
- utils.consoleLog(`Compiling ${contractBaseName}...`);
+ utils.consoleLog(`Compiling ${fileName}...`);
const sourcesToCompile = {
sources: input,
};
@@ -187,19 +206,20 @@ export class Compiler {
);
if (!_.isUndefined(compiled.errors)) {
- _.each(compiled.errors, errMsg => {
+ _.forEach(compiled.errors, errMsg => {
const normalizedErrMsg = Compiler._getNormalizedErrMsg(errMsg);
this._solcErrors.add(normalizedErrMsg);
});
}
- const contractIdentifier = `${contractBaseName}:${contractName}`;
+ const contractName = path.basename(fileName, constants.SOLIDITY_FILE_EXTENSION);
+ const contractIdentifier = `${fileName}:${contractName}`;
const abi: Web3.ContractAbi = JSON.parse(compiled.contracts[contractIdentifier].interface);
const unlinked_binary = `0x${compiled.contracts[contractIdentifier].bytecode}`;
const updated_at = Date.now();
- const contractData: ContractData = {
- solc_version: solcVersion,
- keccak256: sourceHash,
+ const contractNetworkData: ContractNetworkData = {
+ solc_version: contractSpecificSourceData.solc_version,
+ keccak256: contractSpecificSourceData.keccak256,
optimizer_enabled: this._optimizerEnabled,
abi,
unlinked_binary,
@@ -207,26 +227,55 @@ export class Compiler {
};
let newArtifact: ContractArtifact;
- if (!_.isUndefined(currentArtifactString)) {
+ const currentArtifact = (await this._getContractArtifactOrReturnAsync(fileName)) as ContractArtifact;
+ if (!_.isUndefined(currentArtifact)) {
newArtifact = {
...currentArtifact,
networks: {
- ...oldNetworks,
- [this._networkId]: contractData,
+ ...currentArtifact.networks,
+ [this._networkId]: contractNetworkData,
},
};
} else {
newArtifact = {
contract_name: contractName,
networks: {
- [this._networkId]: contractData,
+ [this._networkId]: contractNetworkData,
},
};
}
const artifactString = utils.stringifyWithFormatting(newArtifact);
+ const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`;
await fsWrapper.writeFileAsync(currentArtifactPath, artifactString);
- utils.consoleLog(`${contractBaseName} artifact saved!`);
+ utils.consoleLog(`${fileName} artifact saved!`);
+ }
+ /**
+ * Recursively sets the compile action for a specific contract and dependencies.
+ * @param fileName Name of contracts file.
+ */
+ private async _setCompileActionAsync(fileName: string): Promise<void> {
+ const contractSpecificSourceData = this._contractSourceData[fileName];
+ if (_.isUndefined(contractSpecificSourceData)) {
+ throw new Error(`Contract data for ${fileName} not yet set`);
+ }
+ if (_.isUndefined(contractSpecificSourceData.shouldCompile)) {
+ const contractArtifact = (await this._getContractArtifactOrReturnAsync(fileName)) as ContractArtifact;
+ if (_.isUndefined(contractArtifact)) {
+ contractSpecificSourceData.shouldCompile = true;
+ } else {
+ const contractNetworkData = contractArtifact.networks[this._networkId];
+ contractSpecificSourceData.shouldCompile =
+ contractNetworkData.keccak256 !== contractSpecificSourceData.keccak256 ||
+ this._optimizerEnabled !== contractNetworkData.optimizer_enabled ||
+ contractNetworkData.solc_version !== contractSpecificSourceData.solc_version;
+ }
+ }
+ _.forEach(contractSpecificSourceData.dependencies, async dependency => {
+ await this._setCompileActionAsync(dependency);
+ contractSpecificSourceData.shouldCompile =
+ contractSpecificSourceData.shouldCompile || this._contractSourceData[dependency].shouldCompile;
+ });
}
/**
* Callback to resolve dependencies with `solc.compile`.
@@ -238,8 +287,8 @@ export class Compiler {
if (_.isUndefined(this._contractSourcesIfExists)) {
throw new Error('Contract sources not yet initialized');
}
- const contractBaseName = path.basename(importPath);
- const source = this._contractSourcesIfExists[contractBaseName];
+ const fileName = path.basename(importPath);
+ const source = this._contractSourcesIfExists[fileName];
const importContents: ImportContents = {
contents: source,
};
@@ -254,4 +303,25 @@ export class Compiler {
await fsWrapper.mkdirAsync(this._artifactsDir);
}
}
+ /**
+ * Gets contract data on network or returns if an artifact does not exist.
+ * @param fileName Name of contracts file.
+ * @return Contract data on network or undefined.
+ */
+ private async _getContractArtifactOrReturnAsync(fileName: string): Promise<ContractArtifact | void> {
+ let contractArtifact;
+ const contractName = path.basename(fileName, constants.SOLIDITY_FILE_EXTENSION);
+ const currentArtifactPath = `${this._artifactsDir}/${contractName}.json`;
+ try {
+ const opts = {
+ encoding: 'utf8',
+ };
+ const contractArtifactString = await fsWrapper.readFileAsync(currentArtifactPath, opts);
+ contractArtifact = JSON.parse(contractArtifactString);
+ return contractArtifact;
+ } catch (err) {
+ utils.consoleLog(`Artifact for ${fileName} does not exist`);
+ return contractArtifact;
+ }
+ }
}