From 558286467bc7c05ea1a97a1fdfde0ad1ba7e279f Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Wed, 15 Aug 2018 11:46:23 -0700 Subject: extract interfaces for re-used complex data types --- packages/sol-compiler/src/compiler.ts | 51 +++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'packages/sol-compiler/src/compiler.ts') diff --git a/packages/sol-compiler/src/compiler.ts b/packages/sol-compiler/src/compiler.ts index c162d65f4..286f15073 100644 --- a/packages/sol-compiler/src/compiler.ts +++ b/packages/sol-compiler/src/compiler.ts @@ -53,6 +53,23 @@ const DEFAULT_COMPILER_SETTINGS: solc.CompilerSettings = { }; const CONFIG_FILE = 'compiler.json'; +interface VersionToInputs { + [solcVersion: string]: { + standardInput: solc.StandardInput; + contractsToCompile: string[]; + }; +} + +interface ContractPathToData { + [contractPath: string]: ContractData; +} + +interface ContractData { + currentArtifactIfExists: ContractArtifact | void; + sourceTreeHashHex: string; + contractName: string; +} + /** * The Compiler facilitates compiling Solidity smart contracts and saves the results * to artifact files. @@ -137,24 +154,15 @@ export class Compiler { * @param fileName Name of contract with '.sol' extension. */ private async _compileContractsAsync(contractNames: string[]): Promise { - const versionToInputs: { - [solcVersion: string]: { - standardInput: solc.StandardInput; - contractsToCompile: string[]; - }; - } = {}; + // batch input contracts together based on the version of the compiler that they require. + const versionToInputs: VersionToInputs = {}; - const contractData: { - [contractPath: string]: { - currentArtifactIfExists: ContractArtifact | void; - sourceTreeHashHex: string; - contractName: string; - }; - } = {}; + // map contract paths to data about them for later verification and persistence + const contractPathToData: ContractPathToData = {}; for (const contractName of contractNames) { const contractSource = this._resolver.resolve(contractName); - contractData[contractSource.path] = { + contractPathToData[contractSource.path] = { contractName, currentArtifactIfExists: await getContractArtifactIfExistsAsync(this._artifactsDir, contractName), sourceTreeHashHex: `0x${this._getSourceTreeHash( @@ -162,14 +170,15 @@ export class Compiler { ).toString('hex')}`, }; let shouldCompile = false; - if (_.isUndefined(contractData[contractSource.path].currentArtifactIfExists)) { + if (_.isUndefined(contractPathToData[contractSource.path].currentArtifactIfExists)) { shouldCompile = true; } else { - const currentArtifact = contractData[contractSource.path].currentArtifactIfExists as ContractArtifact; + const currentArtifact = contractPathToData[contractSource.path] + .currentArtifactIfExists as ContractArtifact; const isUserOnLatestVersion = currentArtifact.schemaVersion === constants.LATEST_ARTIFACT_VERSION; const didCompilerSettingsChange = !_.isEqual(currentArtifact.compiler.settings, this._compilerSettings); const didSourceChange = - currentArtifact.sourceTreeHashHex !== contractData[contractSource.path].sourceTreeHashHex; + currentArtifact.sourceTreeHashHex !== contractPathToData[contractSource.path].sourceTreeHashHex; shouldCompile = !isUserOnLatestVersion || didCompilerSettingsChange || didSourceChange; } if (!shouldCompile) { @@ -216,7 +225,7 @@ export class Compiler { for (const contractPath of input.contractsToCompile) { await this._verifyAndPersistCompiledContractAsync( contractPath, - contractData[contractPath], + contractPathToData[contractPath], fullSolcVersion, compiled, ); @@ -225,11 +234,7 @@ export class Compiler { } private async _verifyAndPersistCompiledContractAsync( contractPath: string, - contractMetadata: { - currentArtifactIfExists: ContractArtifact | void; - sourceTreeHashHex: string; - contractName: string; - }, + contractMetadata: ContractData, fullSolcVersion: string, compiled: solc.StandardOutput, ): Promise { -- cgit v1.2.3