@0x/sol-compiler: Address review feedback.

This commit is contained in:
Lawrence Forman 2020-04-01 13:42:45 -04:00
parent 74647f0e88
commit b9a68c0b8e
8 changed files with 31 additions and 39 deletions

View File

@ -91,7 +91,6 @@ export class Compiler {
overrides: Partial<CompilerOptions> = {}, overrides: Partial<CompilerOptions> = {},
file: string = 'compiler.json', file: string = 'compiler.json',
): Promise<CompilerOptions> { ): Promise<CompilerOptions> {
// TODO: Look for config file in parent directories if not found in current directory
const fileConfig: CompilerOptions = (await promisify(fs.stat)(file)).isFile const fileConfig: CompilerOptions = (await promisify(fs.stat)(file)).isFile
? JSON.parse((await promisify(fs.readFile)(file, 'utf8')).toString()) ? JSON.parse((await promisify(fs.readFile)(file, 'utf8')).toString())
: {}; : {};

View File

@ -1,9 +1,3 @@
import { CompilerOptions } from 'ethereum-types';
import { SolcWrapperV05 } from './solc_wrapper_v05'; import { SolcWrapperV05 } from './solc_wrapper_v05';
export class SolcWrapperV04 extends SolcWrapperV05 { export const SolcWrapperV04 = SolcWrapperV05;
constructor(solcVersion: string, opts: CompilerOptions) {
super(solcVersion, opts);
}
}

View File

@ -32,14 +32,8 @@ export const DEFAULT_COMPILER_SETTINGS: solc.CompilerSettings = {
export class SolcWrapperV05 extends SolcWrapper { export class SolcWrapperV05 extends SolcWrapper {
protected readonly _compilerSettings: solc.CompilerSettings; protected readonly _compilerSettings: solc.CompilerSettings;
public static normalizeOutput( public static normalizeOutput(output: StandardOutput): StandardOutput {
output: StandardOutput,
importRemappings: ImportPrefixRemappings,
opts: CompilerOptions,
): StandardOutput {
const _output = _.cloneDeep(output); const _output = _.cloneDeep(output);
// _output.sources = makeContractPathsRelative(_output.sources, opts.contractsDir!, importRemappings);
// _output.contracts = makeContractPathsRelative(_output.contracts, opts.contractsDir!, importRemappings);
// tslint:disable-next-line forin // tslint:disable-next-line forin
for (const contractPath in _output.contracts) { for (const contractPath in _output.contracts) {
// tslint:disable-next-line forin // tslint:disable-next-line forin
@ -94,7 +88,7 @@ export class SolcWrapperV05 extends SolcWrapper {
} }
return { return {
input, input,
output: SolcWrapperV05.normalizeOutput(output, importRemappings, this._opts), output: SolcWrapperV05.normalizeOutput(output),
}; };
} }

View File

@ -161,18 +161,15 @@ export async function compileDockerAsync(
standardInput: solc.StandardInput, standardInput: solc.StandardInput,
): Promise<solc.StandardOutput> { ): Promise<solc.StandardOutput> {
const standardInputStr = JSON.stringify(standardInput, null, 2); const standardInputStr = JSON.stringify(standardInput, null, 2);
// prettier-ignore
const dockerArgs = [ const dockerArgs = [
'run', 'run',
'-i', '-i',
'-a', '-a', 'stdin',
'stdin', '-a', 'stdout',
'-a', '-a', 'stderr',
'stdout',
'-a',
'stderr',
`ethereum/solc:${solidityVersion}`, `ethereum/solc:${solidityVersion}`,
'solc', 'solc', '--standard-json',
'--standard-json',
]; ];
return new Promise<solc.StandardOutput>((accept, reject) => { return new Promise<solc.StandardOutput>((accept, reject) => {
const p = spawn('docker', dockerArgs, { shell: true, stdio: ['pipe', 'inherit', 'inherit'] }); const p = spawn('docker', dockerArgs, { shell: true, stdio: ['pipe', 'inherit', 'inherit'] });
@ -479,7 +476,7 @@ export function getDependencyNameToPackagePath(
* Extract the solidity version (e.g., '0.5.9') from a solc version (e.g., `0.5.9+commit.34d3134f`). * Extract the solidity version (e.g., '0.5.9') from a solc version (e.g., `0.5.9+commit.34d3134f`).
*/ */
export function getSolidityVersionFromSolcVersion(solcVersion: string): string { export function getSolidityVersionFromSolcVersion(solcVersion: string): string {
const m = /(\d+\.\d+\.\d+)\+commit\.[a-f0-9]{8}/.exec(solcVersion); const m = /(\d+\.\d+\.\d+)\+commit\.[a-fA-F0-9]{8}/.exec(solcVersion);
if (!m) { if (!m) {
throw new Error(`Unable to parse solc version string "${solcVersion}"`); throw new Error(`Unable to parse solc version string "${solcVersion}"`);
} }
@ -490,7 +487,7 @@ export function getSolidityVersionFromSolcVersion(solcVersion: string): string {
* Strips any extra characters before and after the version + commit hash of a solc version string. * Strips any extra characters before and after the version + commit hash of a solc version string.
*/ */
export function normalizeSolcVersion(fullSolcVersion: string): string { export function normalizeSolcVersion(fullSolcVersion: string): string {
const m = /\d+\.\d+\.\d+\+commit\.[a-f0-9]{8}/.exec(fullSolcVersion); const m = /\d+\.\d+\.\d+\+commit\.[a-fA-F0-9]{8}/.exec(fullSolcVersion);
if (!m) { if (!m) {
throw new Error(`Unable to parse solc version string "${fullSolcVersion}"`); throw new Error(`Unable to parse solc version string "${fullSolcVersion}"`);
} }

View File

@ -1,19 +1,22 @@
import { join } from 'path'; import { hexUtils } from '@0x/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import { CompilerOptions, ContractArtifact } from 'ethereum-types'; import { CompilerOptions, ContractArtifact } from 'ethereum-types';
import 'mocha'; import 'mocha';
import { join } from 'path';
import { Compiler } from '../src/compiler'; import { Compiler } from '../src/compiler';
import { fsWrapper } from '../src/utils/fs_wrapper'; import { fsWrapper } from '../src/utils/fs_wrapper';
import { exchange_binary } from './fixtures/exchange_bin'; import { exchange_binary } from './fixtures/exchange_bin';
import { v6_contract_binary } from './fixtures/v6_contract_bin';
import { chaiSetup } from './util/chai_setup'; import { chaiSetup } from './util/chai_setup';
import { constants } from './util/constants'; import { constants } from './util/constants';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
const METADATA_SIZE = 43;
describe('#Compiler', function(): void { describe('#Compiler', function(): void {
this.timeout(constants.timeoutMs); // tslint:disable-line:no-invalid-this this.timeout(constants.timeoutMs); // tslint:disable-line:no-invalid-this
const artifactsDir = `${__dirname}/fixtures/artifacts`; const artifactsDir = `${__dirname}/fixtures/artifacts`;
@ -41,14 +44,12 @@ describe('#Compiler', function(): void {
}; };
const exchangeArtifactString = await fsWrapper.readFileAsync(exchangeArtifactPath, opts); const exchangeArtifactString = await fsWrapper.readFileAsync(exchangeArtifactPath, opts);
const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString); const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
// The last 43 bytes of the binaries are metadata which may not be equivalent const unlinkedBinaryWithoutMetadata = hexUtils.slice(
const metadataByteLength = 43; exchangeArtifact.compilerOutput.evm.bytecode.object,
const metadataHexLength = metadataByteLength * 2; 0,
const unlinkedBinaryWithoutMetadata = exchangeArtifact.compilerOutput.evm.bytecode.object.slice( -METADATA_SIZE,
2,
-metadataHexLength,
); );
const exchangeBinaryWithoutMetadata = exchange_binary.slice(0, -metadataHexLength); const exchangeBinaryWithoutMetadata = hexUtils.slice(exchange_binary, 0, -METADATA_SIZE);
expect(unlinkedBinaryWithoutMetadata).to.equal(exchangeBinaryWithoutMetadata); expect(unlinkedBinaryWithoutMetadata).to.equal(exchangeBinaryWithoutMetadata);
}); });
it("should throw when Whatever.sol doesn't contain a Whatever contract", async () => { it("should throw when Whatever.sol doesn't contain a Whatever contract", async () => {
@ -129,7 +130,12 @@ describe('#Compiler', function(): void {
}; };
const exchangeArtifactString = await fsWrapper.readFileAsync(artifactPath, opts); const exchangeArtifactString = await fsWrapper.readFileAsync(artifactPath, opts);
const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString); const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
const bin = exchangeArtifact.compilerOutput.evm.bytecode.object; const actualBinaryWithoutMetadata = hexUtils.slice(
expect(bin.slice(2)).to.length.to.be.gt(0); exchangeArtifact.compilerOutput.evm.bytecode.object,
0,
-METADATA_SIZE,
);
const expectedBinaryWithoutMetadata = hexUtils.slice(v6_contract_binary, 0, -METADATA_SIZE);
expect(actualBinaryWithoutMetadata).to.eq(expectedBinaryWithoutMetadata);
}); });
}); });

View File

@ -16,7 +16,7 @@
*/ */
pragma solidity ^0.6.4; pragma solidity 0.6.4;
contract V6Contract { contract V6Contract {

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
export const v6_contract_binary =
'0x6080604052348015600f57600080fd5b5060405161011238038061011283398181016040526020811015603157600080fd5b8101908080519060200190929190505050806000819055505060ba806100586000396000f3fe608060405236600a57005b348015601557600080fd5b506040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260048152602001807f6e6f70650000000000000000000000000000000000000000000000000000000081525060200191505060405180910390fdfea26469706673582212208084a572151fb41e40aa4d1197e387cba7b4f0cbd982e34682974038667b564764736f6c63430006040033';