From 83563ff44a084cb8d9baeaae90d80e4b72fa8ed7 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 28 Nov 2019 20:47:43 +0100 Subject: [PATCH 1/6] Add tests for abstract contracts Which results in no gas estimation. --- test/compiler.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/compiler.js b/test/compiler.js index 5b3dd33c..b02e420b 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -468,6 +468,40 @@ function runTests (solc, versionText) { st.end(); }); + t.test('compiling standard JSON (abstract contract)', function (st) { + // <0.1.6 doesn't have this + if (!solc.features.multipleInputs) { + st.skip('Not supported by solc'); + st.end(); + return; + } + + var isVersion6 = semver.gt(solc.semver(), '0.5.99'); + + var input = { + 'language': 'Solidity', + 'settings': { + 'outputSelection': { + '*': { + '*': [ 'evm.bytecode', 'evm.gasEstimates' ] + } + } + }, + 'sources': { + 'c.sol': { + 'content': (isVersion6 ? 'abstract ' : '') + 'contract C { function f() public; }' + } + } + }; + + var output = JSON.parse(solc.compile(JSON.stringify(input))); + st.ok(expectNoError(output)); + var C = getBytecodeStandard(output, 'c.sol', 'C'); + st.ok(typeof C === 'string'); + st.ok(C.length === 0); + st.end(); + }); + t.test('compiling standard JSON (with imports)', function (st) { // <0.2.1 doesn't have this if (!solc.features.importCallback) { From 4ab296fcb71e35444b3a3dfa76b8ee0453d7eab7 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 28 Nov 2019 21:10:10 +0100 Subject: [PATCH 2/6] Add tests for compiling single file with compileStandard --- test/compiler.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test/compiler.js b/test/compiler.js index b02e420b..ea82a2ca 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -421,7 +421,40 @@ function runTests (solc, versionText) { st.end(); }); - t.test('compiling standard JSON', function (st) { + t.test('compiling standard JSON (single file)', function (st) { + var input = { + 'language': 'Solidity', + 'settings': { + 'outputSelection': { + '*': { + '*': [ 'evm.bytecode', 'evm.gasEstimates' ] + } + } + }, + 'sources': { + 'c.sol': { + 'content': 'contract C { function g() public { } function h() internal {} }' + } + } + }; + + var output = JSON.parse(solc.compile(JSON.stringify(input))); + st.ok(expectNoError(output)); + var C = getBytecodeStandard(output, 'c.sol', 'C'); + st.ok(typeof C === 'string'); + st.ok(C.length > 0); + var CGas = getGasEstimate(output, 'c.sol', 'C'); + st.ok(typeof CGas === 'object'); + st.ok(typeof CGas['creation'] === 'object'); + st.ok(typeof CGas['creation']['codeDepositCost'] === 'string'); + st.ok(typeof CGas['external'] === 'object'); + st.ok(typeof CGas['external']['g()'] === 'string'); + st.ok(typeof CGas['internal'] === 'object'); + st.ok(typeof CGas['internal']['h()'] === 'string'); + st.end(); + }); + + t.test('compiling standard JSON (multiple files)', function (st) { // <0.1.6 doesn't have this if (!solc.features.multipleInputs) { st.skip('Not supported by solc'); From 19f7af2fad7376dc7dea81eab33be3437bdfb129 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 28 Nov 2019 21:12:51 +0100 Subject: [PATCH 3/6] Add assertions to the linker --- linker.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/linker.js b/linker.js index 6116aab3..b4f169ab 100644 --- a/linker.js +++ b/linker.js @@ -1,3 +1,4 @@ +var assert = require('assert'); var keccak256 = require('js-sha3').keccak256; function libraryHashPlaceholder (input) { @@ -5,6 +6,8 @@ function libraryHashPlaceholder (input) { } var linkBytecode = function (bytecode, libraries) { + assert(typeof bytecode === 'string'); + assert(typeof libraries === 'object'); // NOTE: for backwards compatibility support old compiler which didn't use file names var librariesComplete = {}; for (var libraryName in libraries) { @@ -52,6 +55,7 @@ var linkBytecode = function (bytecode, libraries) { }; var findLinkReferences = function (bytecode) { + assert(typeof bytecode === 'string'); // find 40 bytes in the pattern of __...<36 digits>...__ // e.g. __Lib.sol:L_____________________________ var linkReferences = {}; From cb852830ef59a3cbed4160a62fdedd906561d443 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 28 Nov 2019 21:16:32 +0100 Subject: [PATCH 4/6] Fix translator in case no bytecode was produced --- translate.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/translate.js b/translate.js index 43e051ad..17dbc704 100644 --- a/translate.js +++ b/translate.js @@ -110,15 +110,15 @@ function translateJsonCompilerOutput (output, libraries) { 'evm': { 'legacyAssembly': contractInput['assembly'], 'bytecode': { - 'object': linker.linkBytecode(contractInput['bytecode'], libraries), + 'object': contractInput['bytecode'] && linker.linkBytecode(contractInput['bytecode'], libraries || {}), 'opcodes': contractInput['opcodes'], 'sourceMap': contractInput['srcmap'], - 'linkReferences': linker.findLinkReferences(contractInput['bytecode']) + 'linkReferences': contractInput['bytecode'] && linker.findLinkReferences(contractInput['bytecode']) }, 'deployedBytecode': { - 'object': linker.linkBytecode(contractInput['runtimeBytecode'], libraries), + 'object': contractInput['runtimeBytecode'] && linker.linkBytecode(contractInput['runtimeBytecode'], libraries || {}), 'sourceMap': contractInput['srcmapRuntime'], - 'linkReferences': linker.findLinkReferences(contractInput['runtimeBytecode']) + 'linkReferences': contractInput['runtimeBytecode'] && linker.findLinkReferences(contractInput['runtimeBytecode']) }, 'methodIdentifiers': contractInput['functionHashes'], 'gasEstimates': translatedGasEstimates From 3ad6ed1d4fcc7fdb609cf109b325464d5d10054f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Nov 2019 18:49:09 +0100 Subject: [PATCH 5/6] Fix expectError helper in tests --- test/compiler.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/compiler.js b/test/compiler.js index ea82a2ca..8875dbf5 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -55,9 +55,12 @@ function runTests (solc, versionText) { error = output.errors[error]; if (error.type === errorType) { if (message) { - return error.message.match(message) !== null; + if (error.message.match(message) !== null) { + return true; + } + } else { + return true; } - return true; } } } From 70775648a1e6306f44882dabd724dee78c96a515 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 29 Nov 2019 18:50:49 +0100 Subject: [PATCH 6/6] Add test case for forced warnings (only in >=0.4.0) --- test/compiler.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/compiler.js b/test/compiler.js index 8875dbf5..880d1352 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -676,6 +676,35 @@ function runTests (solc, versionText) { st.end(); }); + t.test('compiling standard JSON (with warning >=0.4.0)', function (st) { + // In 0.4.0 "pragma solidity" was added. Not including it is a warning. + if (semver.lt(solc.semver(), '0.4.0')) { + st.skip('Not supported by solc'); + st.end(); + return; + } + + var input = { + 'language': 'Solidity', + 'settings': { + 'outputSelection': { + '*': { + '*': [ 'evm.bytecode' ] + } + } + }, + 'sources': { + 'c.sol': { + 'content': 'contract C { function f() public { } }' + } + } + }; + + var output = JSON.parse(solc.compile(JSON.stringify(input))); + st.ok(expectError(output, 'Warning', 'Source file does not specify required compiler version!')); + st.end(); + }); + t.test('compiling standard JSON (using libraries) (using lowlevel API)', function (st) { // 0.4.0 has a bug with libraries if (semver.eq(solc.semver(), '0.4.0')) {