Merge pull request #709 from ethereum/fixDeployLibrary

Fix deploy library
pull/3094/head
yann300 7 years ago committed by GitHub
commit 48ff5e882d
  1. 4
      remix-debugger/nightwatch.js
  2. 2
      remix-debugger/package.json
  3. 1
      remix-lib/package.json
  4. 48
      remix-lib/src/execution/txFormat.js
  5. 70
      remix-lib/test/txFormat.js

@ -62,8 +62,8 @@ module.exports = {
'desiredCapabilities': { 'desiredCapabilities': {
'browserName': 'safari', 'browserName': 'safari',
'javascriptEnabled': true, 'javascriptEnabled': true,
'platform': 'OS X 10.10', 'platform': 'OS X 10.11',
'version': '8.0', 'version': '10.0',
'acceptSslCerts': true, 'acceptSslCerts': true,
'build': 'build-' + TRAVIS_JOB_NUMBER, 'build': 'build-' + TRAVIS_JOB_NUMBER,
'tunnel-identifier': 'remix_tests_' + TRAVIS_JOB_NUMBER 'tunnel-identifier': 'remix_tests_' + TRAVIS_JOB_NUMBER

@ -56,7 +56,7 @@
"nightwatch_remote_chrome": "nightwatch --config nightwatch.js --env chrome", "nightwatch_remote_chrome": "nightwatch --config nightwatch.js --env chrome",
"nightwatch_remote_firefox": "nightwatch --config nightwatch.js --env default", "nightwatch_remote_firefox": "nightwatch --config nightwatch.js --env default",
"nightwatch_remote_ie": "nightwatch --config nightwatch.js --env ie", "nightwatch_remote_ie": "nightwatch --config nightwatch.js --env ie",
"nightwatch_remote_parallel": "nightwatch --config nightwatch.js --env ie,safari,chrome,default", "nightwatch_remote_parallel": "nightwatch --config nightwatch.js --env safari,chrome,default",
"nightwatch_remote_safari": "nightwatch --config nightwatch.js --env safari", "nightwatch_remote_safari": "nightwatch --config nightwatch.js --env safari",
"onchange": "onchange build/app.js -- npm run lint", "onchange": "onchange build/app.js -- npm run lint",
"selenium": "selenium-standalone start", "selenium": "selenium-standalone start",

@ -14,6 +14,7 @@
], ],
"main": "./index.js", "main": "./index.js",
"dependencies": { "dependencies": {
"async": "^2.1.2",
"babel-preset-es2015": "^6.24.0", "babel-preset-es2015": "^6.24.0",
"babel-plugin-transform-object-assign": "^6.22.0", "babel-plugin-transform-object-assign": "^6.22.0",
"babel-eslint": "^7.1.1", "babel-eslint": "^7.1.1",

@ -2,6 +2,7 @@
var ethJSABI = require('ethereumjs-abi') var ethJSABI = require('ethereumjs-abi')
var helper = require('./txHelper') var helper = require('./txHelper')
var executionContext = require('./execution-context') var executionContext = require('./execution-context')
var asyncJS = require('async')
module.exports = { module.exports = {
@ -99,10 +100,37 @@ module.exports = {
atAddress: function () {}, atAddress: function () {},
linkBytecode: function (contract, contracts, udapp, callback, callbackStep) { linkBytecodeStandard: function (contract, contracts, udapp, callback, callbackStep) {
if (contract.evm.bytecode.object.indexOf('_') < 0) { asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => {
return callback(null, contract.evm.bytecode.object) asyncJS.eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => {
} var library = contracts[file][libName]
if (library) {
this.deployLibrary(file + ':' + libName, libName, library, contracts, udapp, (error, address) => {
if (error) {
return cbLibDeployed(error)
}
var hexAddress = address.toString('hex')
if (hexAddress.slice(0, 2) === '0x') {
hexAddress = hexAddress.slice(2)
}
contract.evm.bytecode.object = this.linkLibraryStandard(libName, hexAddress, contract)
cbLibDeployed()
}, callbackStep)
} else {
cbLibDeployed('Cannot find compilation data of library ' + libName)
}
}, (error) => {
cbFile(error)
})
}, (error) => {
if (error) {
callbackStep(error)
}
callback(error, contract.evm.bytecode.object)
})
},
linkBytecodeLegacy: function (contract, contracts, udapp, callback, callbackStep) {
var libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/) var libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/)
if (!libraryRefMatch) { if (!libraryRefMatch) {
return callback('Invalid bytecode format.') return callback('Invalid bytecode format.')
@ -129,12 +157,22 @@ module.exports = {
if (hexAddress.slice(0, 2) === '0x') { if (hexAddress.slice(0, 2) === '0x') {
hexAddress = hexAddress.slice(2) hexAddress = hexAddress.slice(2)
} }
contract.evm.bytecode.object = this.linkLibraryStandard(libraryShortName, hexAddress, contract)
contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object) contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object)
this.linkBytecode(contract, contracts, udapp, callback, callbackStep) this.linkBytecode(contract, contracts, udapp, callback, callbackStep)
}, callbackStep) }, callbackStep)
}, },
linkBytecode: function (contract, contracts, udapp, callback, callbackStep) {
if (contract.evm.bytecode.object.indexOf('_') < 0) {
return callback(null, contract.evm.bytecode.object)
}
if (contract.evm.bytecode.linkReferences && Object.keys(contract.evm.bytecode.linkReferences).length) {
this.linkBytecodeStandard(contract, contracts, udapp, callback, callbackStep)
} else {
this.linkBytecodeLegacy(contract, contracts, udapp, callback, callbackStep)
}
},
deployLibrary: function (libraryName, libraryShortName, library, contracts, udapp, callback, callbackStep) { deployLibrary: function (libraryName, libraryShortName, library, contracts, udapp, callback, callbackStep) {
var address = library.address var address = library.address
if (address) { if (address) {

@ -3,9 +3,10 @@ var tape = require('tape')
var txFormat = require('../src/execution/txFormat') var txFormat = require('../src/execution/txFormat')
var compiler = require('solc') var compiler = require('solc')
var compilerInput = require('../src/helpers/compilerHelper').compilerInput var compilerInput = require('../src/helpers/compilerHelper').compilerInput
var executionContext = require('../src/execution/execution-context')
var context var context
tape('ContractParameters - (TxFormat.buildData)', function (t) { tape('ContractParameters - (TxFormat.buildData) - format input parameters', function (t) {
var output = compiler.compileStandardWrapper(compilerInput(uintContract)) var output = compiler.compileStandardWrapper(compilerInput(uintContract))
output = JSON.parse(output) output = JSON.parse(output)
var contract = output.contracts['test.sol']['uintContractTest'] var contract = output.contracts['test.sol']['uintContractTest']
@ -30,6 +31,51 @@ function testWithInput (st, params, expected) {
}, () => {}) }, () => {})
} }
tape('ContractParameters - (TxFormat.buildData) - link Libraries', function (t) {
executionContext.setContext('vm')
var output = compiler.compileStandardWrapper(compilerInput(deploySimpleLib))
output = JSON.parse(output)
var contract = output.contracts['test.sol']['testContractLinkLibrary']
var fakeDeployedContracts = {
lib1: '0xf7a10e525d4b168f45f74db1b61f63d3e7619e11',
lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2: '0xf7a10e525d4b168f45f74db1b61f63d3e7619e33',
testContractLinkLibrary: '0xf7a10e525d4b168f45f74db1b61f63d3e7619e22'
}
var udapp = { runTx: (param, callback) => {
callback(null, {
result: {
createdAddress: fakeDeployedContracts[param.data.contractName]
}
})
} } // fake
context = { output, contract, udapp }
t.test('(TxFormat.buildData and link library (standard way))', function (st) {
st.plan(6)
testLinkLibrary(st, fakeDeployedContracts)
})
})
function testLinkLibrary (st, fakeDeployedContracts) {
var deployMsg = ['creation of library test.sol:lib1 pending...',
'creation of library test.sol:lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2 pending...']
txFormat.buildData('testContractLinkLibrary', context.contract, context.output.contracts, true, context.contract.abi[0], '', context.udapp
, (error, data) => {
if (error) { return st.fails(error) }
console.log(data)
var linkedbyteCode = data.dataHex
var libReference = context.contract.evm.bytecode.linkReferences['test.sol']['lib1']
st.equal(linkedbyteCode.substr(2 * libReference[0].start, 40), fakeDeployedContracts['lib1'].replace('0x', ''))
st.equal(linkedbyteCode.substr(2 * libReference[1].start, 40), fakeDeployedContracts['lib1'].replace('0x', ''))
libReference = context.contract.evm.bytecode.linkReferences['test.sol']['lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2']
st.equal(linkedbyteCode.substr(2 * libReference[0].start, 40), fakeDeployedContracts['lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2'].replace('0x', ''))
st.equal(linkedbyteCode.substr(2 * libReference[1].start, 40), fakeDeployedContracts['lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2'].replace('0x', ''))
}, (msg) => {
st.equal(msg, deployMsg[0])
deployMsg.shift()
})
}
var uintContract = `contract uintContractTest { var uintContract = `contract uintContractTest {
uint _tp; uint _tp;
address _ap; address _ap;
@ -38,3 +84,25 @@ var uintContract = `contract uintContractTest {
_ap = _a; _ap = _a;
} }
}` }`
var deploySimpleLib = `pragma solidity ^0.4.4;
library lib1 {
function getEmpty () {
}
}
library lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2 {
function getEmpty () {
}
}
contract testContractLinkLibrary {
function get () {
lib1.getEmpty();
lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2.getEmpty();
lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2_lib2.getEmpty();
lib1.getEmpty();
}
}`

Loading…
Cancel
Save