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

Commit

Permalink
@0x/sol-compiler: Address review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dorothy-zbornak committed Apr 1, 2020
1 parent 74647f0 commit b9a68c0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 39 deletions.
1 change: 0 additions & 1 deletion packages/sol-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export class Compiler {
overrides: Partial<CompilerOptions> = {},
file: string = 'compiler.json',
): 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
? JSON.parse((await promisify(fs.readFile)(file, 'utf8')).toString())
: {};
Expand Down
8 changes: 1 addition & 7 deletions packages/sol-compiler/src/solc_wrapper_v04.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { CompilerOptions } from 'ethereum-types';

import { SolcWrapperV05 } from './solc_wrapper_v05';

export class SolcWrapperV04 extends SolcWrapperV05 {
constructor(solcVersion: string, opts: CompilerOptions) {
super(solcVersion, opts);
}
}
export const SolcWrapperV04 = SolcWrapperV05;
10 changes: 2 additions & 8 deletions packages/sol-compiler/src/solc_wrapper_v05.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ export const DEFAULT_COMPILER_SETTINGS: solc.CompilerSettings = {
export class SolcWrapperV05 extends SolcWrapper {
protected readonly _compilerSettings: solc.CompilerSettings;

public static normalizeOutput(
output: StandardOutput,
importRemappings: ImportPrefixRemappings,
opts: CompilerOptions,
): StandardOutput {
public static normalizeOutput(output: StandardOutput): StandardOutput {
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
for (const contractPath in _output.contracts) {
// tslint:disable-next-line forin
Expand Down Expand Up @@ -94,7 +88,7 @@ export class SolcWrapperV05 extends SolcWrapper {
}
return {
input,
output: SolcWrapperV05.normalizeOutput(output, importRemappings, this._opts),
output: SolcWrapperV05.normalizeOutput(output),
};
}

Expand Down
17 changes: 7 additions & 10 deletions packages/sol-compiler/src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,15 @@ export async function compileDockerAsync(
standardInput: solc.StandardInput,
): Promise<solc.StandardOutput> {
const standardInputStr = JSON.stringify(standardInput, null, 2);
// prettier-ignore
const dockerArgs = [
'run',
'-i',
'-a',
'stdin',
'-a',
'stdout',
'-a',
'stderr',
'-a', 'stdin',
'-a', 'stdout',
'-a', 'stderr',
`ethereum/solc:${solidityVersion}`,
'solc',
'--standard-json',
'solc', '--standard-json',
];
return new Promise<solc.StandardOutput>((accept, reject) => {
const p = spawn('docker', dockerArgs, { shell: true, stdio: ['pipe', 'inherit', 'inherit'] });
Expand Down Expand Up @@ -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`).
*/
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) {
throw new Error(`Unable to parse solc version string "${solcVersion}"`);
}
Expand All @@ -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.
*/
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) {
throw new Error(`Unable to parse solc version string "${fullSolcVersion}"`);
}
Expand Down
28 changes: 17 additions & 11 deletions packages/sol-compiler/test/compiler_test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { join } from 'path';

import { hexUtils } from '@0x/utils';
import * as chai from 'chai';
import { CompilerOptions, ContractArtifact } from 'ethereum-types';
import 'mocha';
import { join } from 'path';

import { Compiler } from '../src/compiler';
import { fsWrapper } from '../src/utils/fs_wrapper';

import { exchange_binary } from './fixtures/exchange_bin';
import { v6_contract_binary } from './fixtures/v6_contract_bin';
import { chaiSetup } from './util/chai_setup';
import { constants } from './util/constants';

chaiSetup.configure();
const expect = chai.expect;

const METADATA_SIZE = 43;

describe('#Compiler', function(): void {
this.timeout(constants.timeoutMs); // tslint:disable-line:no-invalid-this
const artifactsDir = `${__dirname}/fixtures/artifacts`;
Expand Down Expand Up @@ -41,14 +44,12 @@ describe('#Compiler', function(): void {
};
const exchangeArtifactString = await fsWrapper.readFileAsync(exchangeArtifactPath, opts);
const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
// The last 43 bytes of the binaries are metadata which may not be equivalent
const metadataByteLength = 43;
const metadataHexLength = metadataByteLength * 2;
const unlinkedBinaryWithoutMetadata = exchangeArtifact.compilerOutput.evm.bytecode.object.slice(
2,
-metadataHexLength,
const unlinkedBinaryWithoutMetadata = hexUtils.slice(
exchangeArtifact.compilerOutput.evm.bytecode.object,
0,
-METADATA_SIZE,
);
const exchangeBinaryWithoutMetadata = exchange_binary.slice(0, -metadataHexLength);
const exchangeBinaryWithoutMetadata = hexUtils.slice(exchange_binary, 0, -METADATA_SIZE);
expect(unlinkedBinaryWithoutMetadata).to.equal(exchangeBinaryWithoutMetadata);
});
it("should throw when Whatever.sol doesn't contain a Whatever contract", async () => {
Expand Down Expand Up @@ -129,7 +130,12 @@ describe('#Compiler', function(): void {
};
const exchangeArtifactString = await fsWrapper.readFileAsync(artifactPath, opts);
const exchangeArtifact: ContractArtifact = JSON.parse(exchangeArtifactString);
const bin = exchangeArtifact.compilerOutput.evm.bytecode.object;
expect(bin.slice(2)).to.length.to.be.gt(0);
const actualBinaryWithoutMetadata = hexUtils.slice(
exchangeArtifact.compilerOutput.evm.bytecode.object,
0,
-METADATA_SIZE,
);
const expectedBinaryWithoutMetadata = hexUtils.slice(v6_contract_binary, 0, -METADATA_SIZE);
expect(actualBinaryWithoutMetadata).to.eq(expectedBinaryWithoutMetadata);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

pragma solidity ^0.6.4;
pragma solidity 0.6.4;


contract V6Contract {
Expand Down
2 changes: 1 addition & 1 deletion packages/sol-compiler/test/fixtures/exchange_bin.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/sol-compiler/test/fixtures/v6_contract_bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const v6_contract_binary =
'0x6080604052348015600f57600080fd5b5060405161011238038061011283398181016040526020811015603157600080fd5b8101908080519060200190929190505050806000819055505060ba806100586000396000f3fe608060405236600a57005b348015601557600080fd5b506040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260048152602001807f6e6f70650000000000000000000000000000000000000000000000000000000081525060200191505060405180910390fdfea26469706673582212208084a572151fb41e40aa4d1197e387cba7b4f0cbd982e34682974038667b564764736f6c63430006040033';

0 comments on commit b9a68c0

Please sign in to comment.