From 2c2b348e7680ae3384cc886a298fca9f2ed14d90 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 16 Dec 2017 12:59:43 -0500 Subject: [PATCH 1/5] remove old code --- old_test/blockchain.js | 46 --- old_test/chain_manager.js | 66 ----- old_test/compiler.js | 69 ----- old_test/config.blockchain.js | 150 ---------- old_test/config.contracts.js | 115 -------- old_test/deploy.js | 261 ------------------ old_test/support/address.yml | 13 - old_test/support/arguments.yml | 14 - old_test/support/arguments2.yml | 13 - old_test/support/arguments3.yml | 15 - old_test/support/blockchain.yml | 24 -- old_test/support/chain_manager.json | 1 - old_test/support/contracts.yml | 2 - .../support/contracts/another_storage.sol | 14 - old_test/support/contracts/cash.se | 75 ----- old_test/support/contracts/crowdsale.sol | 51 ---- old_test/support/contracts/data_source.sol | 11 - old_test/support/contracts/error.sol | 14 - old_test/support/contracts/manager.sol | 11 - old_test/support/contracts/other.se | 75 ----- old_test/support/contracts/simple_storage.sol | 14 - old_test/support/contracts/token.sol | 19 -- old_test/support/contracts/wallet.sol | 8 - old_test/support/contracts/wallets.sol | 9 - old_test/support/instances.yml | 12 - old_test/test.js | 29 -- 26 files changed, 1131 deletions(-) delete mode 100644 old_test/blockchain.js delete mode 100644 old_test/chain_manager.js delete mode 100644 old_test/compiler.js delete mode 100644 old_test/config.blockchain.js delete mode 100644 old_test/config.contracts.js delete mode 100644 old_test/deploy.js delete mode 100644 old_test/support/address.yml delete mode 100644 old_test/support/arguments.yml delete mode 100644 old_test/support/arguments2.yml delete mode 100644 old_test/support/arguments3.yml delete mode 100644 old_test/support/blockchain.yml delete mode 100644 old_test/support/chain_manager.json delete mode 100644 old_test/support/contracts.yml delete mode 100644 old_test/support/contracts/another_storage.sol delete mode 100644 old_test/support/contracts/cash.se delete mode 100644 old_test/support/contracts/crowdsale.sol delete mode 100644 old_test/support/contracts/data_source.sol delete mode 100644 old_test/support/contracts/error.sol delete mode 100644 old_test/support/contracts/manager.sol delete mode 100644 old_test/support/contracts/other.se delete mode 100644 old_test/support/contracts/simple_storage.sol delete mode 100644 old_test/support/contracts/token.sol delete mode 100644 old_test/support/contracts/wallet.sol delete mode 100644 old_test/support/contracts/wallets.sol delete mode 100644 old_test/support/instances.yml delete mode 100644 old_test/test.js diff --git a/old_test/blockchain.js b/old_test/blockchain.js deleted file mode 100644 index aa7580b1..00000000 --- a/old_test/blockchain.js +++ /dev/null @@ -1,46 +0,0 @@ -var Config = require('../lib/config/config.js'); -var Blockchain = require('../lib/blockchain.js'); -var assert = require('assert'); -var sinon = require('sinon'); - -describe('embark.blockchain', function() { - var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); - - describe('#generate_basic_command', function() { - var blockchain = new Blockchain(blockchainConfig); - - it('should return correct cmd', function() { - assert.strictEqual(blockchain.generate_basic_command(), "geth --datadir=\"/tmp/embark\" --password config/password --port 30303 --rpc --rpcport 8101 --rpcaddr localhost --networkid "+blockchainConfig.networkId+" --rpccorsdomain=\"*\" --minerthreads \"1\" --mine --rpcapi \"eth,web3\" --maxpeers 4 "); - }); - }); - - describe('#list_command', function() { - var blockchain = new Blockchain(blockchainConfig); - blockchain.generate_basic_command = sinon.stub().returns("geth "); - - it('should generate command to list accounts', function() { - assert.equal(blockchain.list_command(), "geth --datadir=\"/tmp/embark\" --password config/password account list "); - }); - }); - - describe('#init_command', function() { - var blockchain = new Blockchain(blockchainConfig); - blockchain.generate_basic_command = sinon.stub().returns("geth "); - - it('should generate command to create an account', function() { - assert.equal(blockchain.init_command(), "geth --datadir=\"/tmp/embark\" --password config/password account new "); - }); - }); - - describe('#run_command', function() { - describe('with mine when needed config set', function() { - var blockchain = new Blockchain(blockchainConfig); - blockchain.generate_basic_command = sinon.stub().returns("geth "); - - it('should generate run command with script ', function() { - assert.equal(blockchain.run_command(), "geth js node_modules/embark-framework/js/mine.js"); - }); - }); - }); - -}); diff --git a/old_test/chain_manager.js b/old_test/chain_manager.js deleted file mode 100644 index 456ddc9a..00000000 --- a/old_test/chain_manager.js +++ /dev/null @@ -1,66 +0,0 @@ -var ChainManager = require('../lib/chain_manager.js'); -var Config = require('../lib/config/config.js'); -var Blockchain = require('../lib/blockchain.js'); -var assert = require('assert'); -var fs = require('fs'); - -// TODO: replace with ethersim -var Web3 = require('web3'); -var web3 = new Web3(); -web3.setProvider(new web3.providers.HttpProvider("http://localhost:8101")); - -describe('embark.chain_manager', function() { - var chainFile = './test/support/chain_manager.json'; - fs.writeFileSync(chainFile, '{}'); - - var chainManager = (new ChainManager()).loadConfigFile(chainFile); - var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config('development'); - - describe('#init', function() { - chainManager.init('development', blockchainConfig, web3); - - it('should initialize chain', function() { - var chain = chainManager.chainManagerConfig['0x245a1b878a79ee9d0fd46e19c89d0cefbaa475e74e45fa133e022da45943b111'] - assert.equal(chain != undefined, true); - }); - }); - - describe('#addContract', function() { - - it('should register a contract in the chain', function() { - chainManager.addContract("Foo", "123456", [], "0x123"); - - console.log(chainManager.chainManagerConfig); - var chain = chainManager.chainManagerConfig['0x245a1b878a79ee9d0fd46e19c89d0cefbaa475e74e45fa133e022da45943b111'] - var contract = chain.contracts["d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b"] - - assert.equal(contract.name, "Foo"); - assert.equal(contract.address, "0x123"); - }); - - }); - - describe('#getContract', function() { - - it('should a contract in the chain', function() { - var contract = chainManager.getContract("Foo", "123456", []); - - assert.equal(contract.name, "Foo"); - assert.equal(contract.address, "0x123"); - }); - - }); - - describe('#save', function() { - - it('should save changes in the chain', function() { - chainManager.save(); - - var chainFile = './test/support/chain_manager.json'; - var content = fs.readFileSync(chainFile).toString(); - assert.equal(content, '{"0x245a1b878a79ee9d0fd46e19c89d0cefbaa475e74e45fa133e022da45943b111\":{"contracts":{"d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b\":{"name":"Foo","address":"0x123"}}}}'); - }); - - }); - -}); diff --git a/old_test/compiler.js b/old_test/compiler.js deleted file mode 100644 index 0d1b8d19..00000000 --- a/old_test/compiler.js +++ /dev/null @@ -1,69 +0,0 @@ -var Compiler = require('../lib/compiler.js'); -var assert = require('assert'); - -describe('embark.compiler', function() { - - describe('compile a file', function() { - var files = [ - 'test/support/contracts/simple_storage.sol' - ]; - - it("should build a correct compiled object", function() { - var compiler = new Compiler(); - - var compiledFile = compiler.compile(files); - - assert.equal(compiledFile.SimpleStorage.code, '60606040526040516020806075833950608060405251600081905550604e8060276000396000f3606060405260e060020a60003504632a1afcd98114602e57806360fe47b11460365780636d4ce63c146040575b005b604460005481565b600435600055602c565b6000545b6060908152602090f3'); - - assert.equal(JSON.stringify(compiledFile.SimpleStorage.info.abiDefinition), '[{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}]'); - }); - - }); - - describe('compile a file with an error', function() { - var files = [ - 'test/support/contracts/error.sol' - ]; - - it("throw an error", function() { - var compiler = new Compiler(); - - assert.throws(function() { compiler.compile(files) }, Error); - }); - - }); - - describe('compile a serpent file', function() { - var files = [ - 'test/support/contracts/cash.se' - ]; - - it("should build a correct compiled object", function() { - var compiler = new Compiler(); - - var compiledFile = compiler.compile(files); - - assert.equal(compiledFile.cash.code, '6000603f536a0186a000000000000000006040604059905901600090526000815232816020015280905020556103658061003a60003961039f56600061047f537c010000000000000000000000000000000000000000000000000000000060003504638357984f81141561005f57600435604052604060405990590160009052600081526040518160200152809050205460605260206060f35b63693200ce8114156101465760043560a05260243560c0523260e0526040604059905901600090526000815260e051816020015280905020546101005260c051610100511215156101385760c0516040604059905901600090526000815260e05181602001528090502054036040604059905901600090526000815260e0518160200152809050205560c0516040604059905901600090526000815260a05181602001528090502054016040604059905901600090526000815260a0518160200152809050205560c0516101c05260206101c0f3610145565b60006101e05260206101e0f35b5b6380b97fc081141561024c5760043560a05260243560c05260443561020052326102005114151561017e576000610220526020610220f35b6040604059905901600090526000815261020051816020015280905020546101005260c0516101005112151561023e5760c0516040604059905901600090526000815261020051816020015280905020540360406040599059016000905260008152610200518160200152809050205560c0516040604059905901600090526000815260a05181602001528090502054016040604059905901600090526000815260a0518160200152809050205560c0516102e05260206102e0f361024b565b6000610300526020610300f35b5b634c764abc8114156102b4576004356103205260243561034052610340516040604059905901600090526000815261032051816020015280905020540360406040599059016000905260008152610320518160200152809050205560016103a05260206103a0f35b63a92c9b8381141561031c57600435610320526024356103405261034051604060405990590160009052600081526103205181602001528090502054016040604059905901600090526000815261032051816020015280905020556001610400526020610400f35b631d62e92281141561036357600435604052602435610420526104205160406040599059016000905260008152604051816020015280905020556001610460526020610460f35b505b6000f3'); - - assert.equal(JSON.stringify(compiledFile.cash.info.abiDefinition), '[{\"name\":\"addCash(int256,int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"ID\",\"type\":\"int256\"},{\"name\":\"amount\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true},{\"name\":\"balance(int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"address\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true},{\"name\":\"send(int256,int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"recver\",\"type\":\"int256\"},{\"name\":\"value\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true},{\"name\":\"sendFrom(int256,int256,int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"recver\",\"type\":\"int256\"},{\"name\":\"value\",\"type\":\"int256\"},{\"name\":\"from\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true},{\"name\":\"setCash(int256,int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"address\",\"type\":\"int256\"},{\"name\":\"balance\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true},{\"name\":\"subtractCash(int256,int256)\",\"type\":\"function\",\"inputs\":[{\"name\":\"ID\",\"type\":\"int256\"},{\"name\":\"amount\",\"type\":\"int256\"}],\"outputs\":[{\"name\":\"out\",\"type\":\"int256\"}],\"constant\":true}]'); - - }); - - }); - - describe('compile a file with an error', function() { - var files = [ - 'test/support/contracts/error.sol' - ]; - - it("throw an error", function() { - var compiler = new Compiler(); - - assert.throws(function() { compiler.compile(files) }, Error); - }); - - }); - - -}); - diff --git a/old_test/config.blockchain.js b/old_test/config.blockchain.js deleted file mode 100644 index 021e413e..00000000 --- a/old_test/config.blockchain.js +++ /dev/null @@ -1,150 +0,0 @@ -var Config = require('../lib/config/config.js'); -var assert = require('assert'); - -describe('embark.config.blockchain', function() { - var blockchainConfig = new Config.Blockchain(); - - describe('#loadConfigFile', function() { - it('should read and load yml file', function() { - blockchainConfig.loadConfigFile('test/support/blockchain.yml'); - - assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('development'), true) - assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('staging'), true) - }); - - it('should throw exception reading invalid file', function() { - assert.throws(function() { blockchainConfig.loadConfigFile('test/support/invalid.yml') }, Error); - }); - }); - - describe('#loadConfig', function() { - it('should load config', function() { - var hsh = { - development: {}, - staging: {} - }; - - blockchainConfig.loadConfig(hsh); - - assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('development'), true) - assert.equal(blockchainConfig.blockchainConfig.hasOwnProperty('staging'), true) - }); - }); - - describe('#config', function() { - - it('should load environment', function() { - var hsh = { - development: { - rpc_host: 'localhost', - rpc_port: 8101, - rpc_whitelist: "*", - network_id: 0, - minerthreads: 1, - genesis_block: 'config/genesis.json', - datadir: '/tmp/embark', - chains: 'chains_development.json', - deploy_timeout: 45, - mine_when_needed: true, - gas_limit: 123, - gas_price: 100, - console: false, - account: { - init: true, - password: 'config/password' - } - }, - staging: {} - }; - - blockchainConfig.loadConfig(hsh); - - assert.deepEqual(blockchainConfig.config('development'), { - rpcHost: 'localhost', - rpcPort: 8101, - gasLimit: 123, - gasPrice: 100, - rpcWhitelist: "*", - testnet: false, - bootNodes: [], - whisper: false, - minerthreads: 1, - nat: [], - genesisBlock: 'config/genesis.json', - datadir: '/tmp/embark', - chains: 'chains_development.json', - deployTimeout: 45, - deploy_synchronously: false, - networkId: 0, - maxPeers: 4, - mine: false, - port: "30303", - console_toggle: false, - mine_when_needed: true, - geth_extra_opts: [], - account: { - init: true, - password: 'config/password' - } - }) - }); - - it('should return defaults', function() { - var hsh = { - development: { - rpc_host: 'localhost', - rpc_port: 8101, - rpc_whitelist: "*", - network_id: 0, - minerthreads: 1, - datadir: '/tmp/embark', - chains: undefined, - mine_when_needed: true, - console: false, - account: { - init: true, - password: 'config/password' - }, - }, - staging: {} - }; - - blockchainConfig.loadConfig(hsh); - - assert.deepEqual(blockchainConfig.config('development'), { - rpcHost: 'localhost', - rpcPort: 8101, - gasLimit: 500000, - gasPrice: 10000000000000, - rpcWhitelist: "*", - testnet: false, - bootNodes: [], - whisper: false, - minerthreads: 1, - nat: [], - genesisBlock: undefined, - datadir: '/tmp/embark', - chains: undefined, - deployTimeout: 20, - deploy_synchronously: false, - networkId: 0, - maxPeers: 4, - mine: false, - port: "30303", - console_toggle: false, - mine_when_needed: true, - geth_extra_opts: [], - account: { - init: true, - password: 'config/password' - } - }) - }); - - it('should load environment', function() { - var blockchainConfig = new Config.Blockchain(); - assert.throws(function() { blockchainConfig.config('development') }, Error); - }); - }); - -}); diff --git a/old_test/config.contracts.js b/old_test/config.contracts.js deleted file mode 100644 index d9d70ce1..00000000 --- a/old_test/config.contracts.js +++ /dev/null @@ -1,115 +0,0 @@ -var Config = require('../lib/config/config.js'); -var Compiler = require('../lib/compiler.js'); -var assert = require('assert'); -var sinon = require('sinon'); -require('mocha-sinon'); - -describe('embark.config.contracts', function() { - var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); - var blockchainConfig = _blockchainConfig.config("development"); - var compiler = new Compiler(_blockchainConfig); - - describe('#loadConfigFile', function() { - it('should read and load yml file', function() { - var contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/contracts.yml'); - - assert.equal(contractsConfig.contractConfig.hasOwnProperty('development'), true) - assert.equal(contractsConfig.contractConfig.hasOwnProperty('staging'), true) - }); - - it('should throw exception reading invalid file', function() { - assert.throws(function() { contractsConfig.loadConfigFile('test/support/invalid.yml') }, Error); - }); - }); - - describe('#loadConfig', function() { - it('should load config', function() { - var contractsConfig = new Config.Contracts([], blockchainConfig, compiler); - var hsh = { - development: {}, - staging: {} - }; - - contractsConfig.loadConfig(hsh); - - assert.equal(contractsConfig.contractConfig.hasOwnProperty('development'), true) - assert.equal(contractsConfig.contractConfig.hasOwnProperty('staging'), true) - }); - }); - - describe('#compileContracts', function() { - context("simple contracts", function() { - before(function() { - files = [ - 'test/support/contracts/simple_storage.sol', - 'test/support/contracts/another_storage.sol' - ] - contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/contracts.yml'); - contractsConfig.init(files, 'development'); - contractsConfig.compileContracts(); - }); - - it('add contracts to a list', function() { - //assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "AnotherStorage" ]); - assert.deepEqual(contractsConfig.all_contracts, [ "AnotherStorage", "SimpleStorage" ]); - }); - }); - - context("contracts as arguments to other contracts", function() { - before(function() { - files = [ - 'test/support/contracts/wallet.sol', - 'test/support/contracts/simple_storage.sol', - 'test/support/contracts/another_storage.sol', - 'test/support/contracts/wallets.sol' - ] - contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/arguments.yml'); - contractsConfig.init(files, 'development'); - contractsConfig.compileContracts('development'); - }); - - it('add contracts to a list', function() { - assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "AnotherStorage", "Wallet", "Wallets" ]); - }); - }); - - context("contracts instances", function() { - before(function() { - files = [ - 'test/support/contracts/simple_storage.sol' - ] - contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/instances.yml'); - contractsConfig.init(files, 'development'); - contractsConfig.compileContracts('development'); - }); - - it('add contracts to a list', function() { - assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "BarStorage", "FooStorage" ]); - }); - }); - - context("contracts as arguments to other contracts with stubs", function() { - before(function() { - files = [ - 'test/support/contracts/crowdsale.sol', - 'test/support/contracts/token.sol' - ] - contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/arguments2.yml'); - contractsConfig.init(files, 'development'); - contractsConfig.compileContracts('development'); - }); - - it('add contracts to a list', function() { - assert.deepEqual(contractsConfig.all_contracts, [ "token", "Crowdsale" ]); - }); - }); - - }); - -}); - diff --git a/old_test/deploy.js b/old_test/deploy.js deleted file mode 100644 index 9f33ea9f..00000000 --- a/old_test/deploy.js +++ /dev/null @@ -1,261 +0,0 @@ -var Config = require('../lib/config/config.js'); -var Deploy = require('../lib/deploy.js'); -var Compiler = require('../lib/compiler.js'); -var ChainManager = require('../lib/chain_manager.js'); -var assert = require('assert'); -var web3 = require('web3'); - -// TODO: replace with ethersim -var Web3 = require('web3'); -var web3 = new Web3(); -web3.setProvider(new web3.providers.HttpProvider("http://localhost:8101")); - -setDeployConfig = function(config) { - var _blockchainConfig = (new Config.Blockchain()).loadConfigFile(config.blockchain); - var blockchainConfig = _blockchainConfig.config("development"); - var compiler = new Compiler(_blockchainConfig); - var contractsConfig = new Config.Contracts(blockchainConfig, compiler); - var chainManager = (new ChainManager()).loadConfigFile('./test/support/chain_manager.json'); - contractsConfig.loadConfigFile(config.contracts); - contractsConfig.init(config.files, 'development'); - compiler.init('development'); - return new Deploy('development', config.files, blockchainConfig, contractsConfig, chainManager, true, false, web3); -} - -function Done(fn) { - var self = this; - var called = false; - - /** - * - * @param {*} params... - */ - this.trigger = function (params) { - if(called) { - return; - } - - fn.apply(self, arguments); - called = true; - }; -} - -describe('embark.deploy', function() { - - describe('contracts as arguments to other contracts', function() { - var files = [ - 'test/support/contracts/wallet.sol', - 'test/support/contracts/simple_storage.sol', - 'test/support/contracts/another_storage.sol', - 'test/support/contracts/wallets.sol' - ]; - - describe('#deploy_contracts', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/arguments.yml' - }); - - it("should deploy contracts", function(fn) { - var doneWrap = new Done(fn); - - deploy.deploy_contracts("development", function() { - var all_contracts = ['Wallet', 'SimpleStorage', 'AnotherStorage', 'Wallets']; - for(var i=0; i < all_contracts.length; i++) { - var className = all_contracts[i]; - - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } - - doneWrap.trigger(); - }); - - }); - - }); - - describe('#generate_abi_file', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/arguments.yml' - }); - deploy.deployedContracts = { - "SimpleStorage": "0x123", - "AnotherStorage": "0x234" - } - deploy.contractDB = { - "SimpleStorage": {compiled: {info: {abiDefinition: 123}}}, - "AnotherStorage": {compiled: {info: {abiDefinition: 234}}} - } - - it("should deploy contracts", function() { - var result = ""; - - result += deploy.generate_provider_file(); - result += deploy.generate_abi_file(); - - assert.strictEqual(result, "var web3 = new Web3();web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];blockchain = {\"testnet\":false,\"rpcHost\":\"localhost\",\"rpcPort\":8101,\"gasLimit\":1000000,\"gasPrice\":10000000000000,\"rpcWhitelist\":\"*\",\"nat\":[],\"minerthreads\":1,\"genesisBlock\":\"config/genesis.json\",\"datadir\":\"/tmp/embark\",\"bootNodes\":[],\"deployTimeout\":20,\"networkId\":"+deploy.blockchainConfig.networkId+",\"maxPeers\":4,\"mine\":false,\"port\":\"30303\",\"console_toggle\":false,\"mine_when_needed\":true,\"whisper\":false,\"account\":{\"init\":true,\"password\":\"config/password\"},\"geth_extra_opts\":[],\"deploy_synchronously\":false};SimpleStorageAbi = 123;SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);SimpleStorage = SimpleStorageContract.at('0x123');AnotherStorageAbi = 234;AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);AnotherStorage = AnotherStorageContract.at('0x234');contractDB = {\"SimpleStorage\":{\"compiled\":{\"info\":{\"abiDefinition\":123}}},\"AnotherStorage\":{\"compiled\":{\"info\":{\"abiDefinition\":234}}}};"); - }); - }); - }); - - describe('contracts as arguments to other contracts with stubs', function() { - var files = [ - 'test/support/contracts/crowdsale.sol', - 'test/support/contracts/token.sol' - ]; - - describe('#deploy_contracts', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/arguments2.yml' - }); - - it("should deploy contracts", function(fn) { - var doneWrap = new Done(fn); - - deploy.deploy_contracts("development", function() { - - var all_contracts = ['token', 'Crowdsale']; - for(var i=0; i < all_contracts.length; i++) { - var className = all_contracts[i]; - - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } - - doneWrap.trigger(); - }); - - }); - - }); - - }); - - describe('contracts instances', function() { - var files = [ - 'test/support/contracts/simple_storage.sol' - ]; - - describe('#deploy_contracts', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/instances.yml' - }); - - it("should deploy contracts", function(fn) { - var doneWrap = new Done(fn); - - deploy.deploy_contracts("development", function() { - - var all_contracts = ['BarStorage', 'FooStorage']; - for(var i=0; i < all_contracts.length; i++) { - var className = all_contracts[i]; - - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } - assert.notEqual(deploy.deployedContracts.hasOwnProperty('SimpleStorage'), true); - - doneWrap.trigger(); - }); - }); - - }); - - }); - - describe('contracts deploy script', function() { - var files = [ - 'test/support/contracts/data_source.sol', - 'test/support/contracts/manager.sol' - ]; - - describe('#deploy_contracts', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/arguments3.yml' - }); - - it("should deploy contracts", function(fn) { - var doneWrap = new Done(fn); - - deploy.deploy_contracts("development", function() { - var all_contracts = ['DataSource', 'MyDataSource', 'Manager']; - for(var i=0; i < all_contracts.length; i++) { - var className = all_contracts[i]; - - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } - - doneWrap.trigger(); - }); - }); - - it("should execute deploy changes", function(fn) { - var doneWrap = new Done(fn); - - web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101')); - web3.eth.defaultAccount = web3.eth.accounts[0]; - - data_source_abi = deploy.contractDB['DataSource'].compiled.info.abiDefinition; - data_source_address = deploy.deployedContracts['DataSource']; - my_data_source_abi = deploy.contractDB['MyDataSource'].compiled.info.abiDefinition; - my_data_source_address = deploy.deployedContracts['MyDataSource']; - manager_abi = deploy.contractDB['Manager'].compiled.info.abiDefinition; - manager_address = deploy.deployedContracts['Manager']; - - DataSource = web3.eth.contract(data_source_abi).at(data_source_address); - MyDataSource = web3.eth.contract(my_data_source_abi).at(my_data_source_address); - ManagerSource = web3.eth.contract(manager_abi).at(manager_address); - - assert.equal(DataSource.storeData().toNumber(), 5); - assert.equal(Manager.data().toString(), my_data_source_address); - - doneWrap.trigger(); - }); - - }); - - }); - - describe('contracts with addresses defined', function() { - var files = [ - 'test/support/contracts/simple_storage.sol' - ]; - - describe('#deploy_contracts', function() { - var deploy = setDeployConfig({ - files: files, - blockchain: 'test/support/blockchain.yml', - contracts: 'test/support/address.yml' - }); - - it("should not deploy contracts with addresses defined", function(fn) { - var doneWrap = new Done(fn); - - deploy.deploy_contracts("development", function() { - var expected_deploys = ['SimpleStorage', 'BarStorage', 'FooStorage']; - - for(var i=0; i < expected_deploys.length; i++) { - var className = expected_deploys[i]; - - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } - - assert.equal(deploy.deployedContracts['SimpleStorage'], '0x123'); - assert.equal(deploy.deployedContracts['BarStorage'], '0x234'); - - doneWrap.trigger(); - }); - }); - - }); - - }); - -}); diff --git a/old_test/support/address.yml b/old_test/support/address.yml deleted file mode 100644 index 9da32368..00000000 --- a/old_test/support/address.yml +++ /dev/null @@ -1,13 +0,0 @@ -development: - SimpleStorage: - address: 0x123 - args: - - 100 - BarStorage: - address: 0x234 - instanceOf: SimpleStorage - FooStorage: - instanceOf: SimpleStorage - args: - - 200 -staging: diff --git a/old_test/support/arguments.yml b/old_test/support/arguments.yml deleted file mode 100644 index fc0fe03b..00000000 --- a/old_test/support/arguments.yml +++ /dev/null @@ -1,14 +0,0 @@ -development: - Wallet: - args: - - $AnotherStorage - SimpleStorage: - args: - - 100 - AnotherStorage: - args: - - 100 - Wallets: - args: - - $Wallet -staging: diff --git a/old_test/support/arguments2.yml b/old_test/support/arguments2.yml deleted file mode 100644 index 86765c6b..00000000 --- a/old_test/support/arguments2.yml +++ /dev/null @@ -1,13 +0,0 @@ -development: - token: - args: - Crowdsale: - stubs: - - token - args: - - 0x123 - - 100000000000000000000 - - 30 - - 20000000000000000 - - $token -staging: diff --git a/old_test/support/arguments3.yml b/old_test/support/arguments3.yml deleted file mode 100644 index 7fe418f8..00000000 --- a/old_test/support/arguments3.yml +++ /dev/null @@ -1,15 +0,0 @@ -development: - DataSource: - args: - MyDataSource: - args: - instanceOf: DataSource - Manager: - stubs: - - DataSource - args: - - $DataSource - onDeploy: - - DataSource.set(5) - - Manager.update($MyDataSource) -staging: diff --git a/old_test/support/blockchain.yml b/old_test/support/blockchain.yml deleted file mode 100644 index 3612a11c..00000000 --- a/old_test/support/blockchain.yml +++ /dev/null @@ -1,24 +0,0 @@ -development: - rpc_host: localhost - rpc_port: 8101 - rpc_whitelist: "*" - minerthreads: 1 - genesis_block: config/genesis.json - datadir: /tmp/embark - mine_when_needed: true - gas_limit: 1000000 - gas_price: 10000000000000 - console: false - account: - init: true - password: config/password -staging: - rpc_host: localhost - rpc_port: 8101 - rpc_whitelist: "*" - datadir: default - network_id: 0 - console: true - account: - init: false - address: diff --git a/old_test/support/chain_manager.json b/old_test/support/chain_manager.json deleted file mode 100644 index 602dff64..00000000 --- a/old_test/support/chain_manager.json +++ /dev/null @@ -1 +0,0 @@ -{"0x245a1b878a79ee9d0fd46e19c89d0cefbaa475e74e45fa133e022da45943b111":{"contracts":{"d5d91a8825c5c253dff531ddda2354c4014f5699b7bcbea70207cfdcb37b6c8b":{"name":"Foo","address":"0x123"}}}} \ No newline at end of file diff --git a/old_test/support/contracts.yml b/old_test/support/contracts.yml deleted file mode 100644 index 56addd58..00000000 --- a/old_test/support/contracts.yml +++ /dev/null @@ -1,2 +0,0 @@ -development: -staging: diff --git a/old_test/support/contracts/another_storage.sol b/old_test/support/contracts/another_storage.sol deleted file mode 100644 index ce3c0ac6..00000000 --- a/old_test/support/contracts/another_storage.sol +++ /dev/null @@ -1,14 +0,0 @@ -contract AnotherStorage { - uint public storedData; - - function SimpleStorage(uint initialValue) { - storedData = initialValue; - } - - function set(uint x) { - storedData = x; - } - function get() constant returns (uint retVal) { - return storedData; - } -} diff --git a/old_test/support/contracts/cash.se b/old_test/support/contracts/cash.se deleted file mode 100644 index c70f0c57..00000000 --- a/old_test/support/contracts/cash.se +++ /dev/null @@ -1,75 +0,0 @@ -# This software (Augur) allows buying && selling event outcomes in ethereum -# Copyright (C) 2015 Forecast Foundation -# This program is free software; you can redistribute it &&/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is free software: you can redistribute it &&/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# Any questions please contact joey@augur.net - -data cashcoinBalances[] - -def init(): - # test initial funds - self.cashcoinBalances[tx.origin] = 100000*2^64 - -# @return: cash balance of address -def balance(address): - return(self.cashcoinBalances[address]) - -# should send values as fixed point in UI (1 is 2^64, 4 is 4*2^64, .5 is 2^63, etc.) -# so cashcoin fees could just go to root branch, or we could not have fees besides -# gas fee to do a send transaction -# @return: value sent, 0 if fails -def send(recver, value): - sender = tx.origin - senderBalance = self.cashcoinBalances[sender] - if(senderBalance >= value): - self.cashcoinBalances[sender] -= value - self.cashcoinBalances[recver] += value - return(value) - else: - return(0) - -# @return value of cash sent; fail is 0 -def sendFrom(recver, value, from): - if(from!=tx.origin): - return(0) - senderBalance = self.cashcoinBalances[from] - if(senderBalance >= value): - self.cashcoinBalances[from] -= value - self.cashcoinBalances[recver] += value - return(value) - else: - return(0) - -# make sure only coming from specific contracts -def subtractCash(ID, amount): - #if(!self.whitelist.check(msg.sender)): - # return(-1) - self.cashcoinBalances[ID] -= amount - return(1) - -def addCash(ID, amount): - #if(!self.whitelist.check(msg.sender)): - # return(-1) - self.cashcoinBalances[ID] += amount - return(1) - -def setCash(address, balance): - #if !self.whitelist.check(msg.sender): - # return(-1) - self.cashcoinBalances[address] = balance - return(1) diff --git a/old_test/support/contracts/crowdsale.sol b/old_test/support/contracts/crowdsale.sol deleted file mode 100644 index a882dc81..00000000 --- a/old_test/support/contracts/crowdsale.sol +++ /dev/null @@ -1,51 +0,0 @@ -contract token { mapping (address => uint) public coinBalanceOf; function token() {} function sendCoin(address receiver, uint amount) returns(bool sufficient) { } } - -contract Crowdsale { - - address public beneficiary; - uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; - token public tokenReward; - Funder[] public funders; - event FundTransfer(address backer, uint amount, bool isContribution); - - /* data structure to hold information about campaign contributors */ - struct Funder { - address addr; - uint amount; - } - - /* at initialization, setup the owner */ - function Crowdsale(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, token _reward) { - beneficiary = _beneficiary; - fundingGoal = _fundingGoal; - deadline = now + _duration * 1 minutes; - price = _price; - tokenReward = token(_reward); - } - - /* The function without name is the default function that is called whenever anyone sends funds to a contract */ - function () { - uint amount = msg.value; - funders[funders.length++] = Funder({addr: msg.sender, amount: amount}); - amountRaised += amount; - tokenReward.sendCoin(msg.sender, amount / price); - FundTransfer(msg.sender, amount, true); - } - - modifier afterDeadline() { if (now >= deadline) _ } - - /* checks if the goal or time limit has been reached and ends the campaign */ - function checkGoalReached() afterDeadline { - if (amountRaised >= fundingGoal){ - beneficiary.send(amountRaised); - FundTransfer(beneficiary, amountRaised, false); - } else { - FundTransfer(0, 11, false); - for (uint i = 0; i < funders.length; ++i) { - funders[i].addr.send(funders[i].amount); - FundTransfer(funders[i].addr, funders[i].amount, false); - } - } - suicide(beneficiary); - } -} diff --git a/old_test/support/contracts/data_source.sol b/old_test/support/contracts/data_source.sol deleted file mode 100644 index 90b0c784..00000000 --- a/old_test/support/contracts/data_source.sol +++ /dev/null @@ -1,11 +0,0 @@ -contract DataSource { - uint public storeData; - - function DataSource() { - } - - function set(uint num) { - storeData = num; - } - -} diff --git a/old_test/support/contracts/error.sol b/old_test/support/contracts/error.sol deleted file mode 100644 index c15c5158..00000000 --- a/old_test/support/contracts/error.sol +++ /dev/null @@ -1,14 +0,0 @@ -contract SimpleStorage { - uint public storedData; - - function SimpleStorage(uint initialValue) { - storedData2 = initialValue; - } - - function set(uint x) { - storedData = x; - } - function get() constant returns (uint retVal) { - return storedData; - } -} diff --git a/old_test/support/contracts/manager.sol b/old_test/support/contracts/manager.sol deleted file mode 100644 index 0445cee2..00000000 --- a/old_test/support/contracts/manager.sol +++ /dev/null @@ -1,11 +0,0 @@ -contract Manager { - address public data; - - function Manager(address dataAddress) { - data = dataAddress; - } - - function update(address _addr) { - data = _addr; - } -} diff --git a/old_test/support/contracts/other.se b/old_test/support/contracts/other.se deleted file mode 100644 index c70f0c57..00000000 --- a/old_test/support/contracts/other.se +++ /dev/null @@ -1,75 +0,0 @@ -# This software (Augur) allows buying && selling event outcomes in ethereum -# Copyright (C) 2015 Forecast Foundation -# This program is free software; you can redistribute it &&/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is free software: you can redistribute it &&/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# Any questions please contact joey@augur.net - -data cashcoinBalances[] - -def init(): - # test initial funds - self.cashcoinBalances[tx.origin] = 100000*2^64 - -# @return: cash balance of address -def balance(address): - return(self.cashcoinBalances[address]) - -# should send values as fixed point in UI (1 is 2^64, 4 is 4*2^64, .5 is 2^63, etc.) -# so cashcoin fees could just go to root branch, or we could not have fees besides -# gas fee to do a send transaction -# @return: value sent, 0 if fails -def send(recver, value): - sender = tx.origin - senderBalance = self.cashcoinBalances[sender] - if(senderBalance >= value): - self.cashcoinBalances[sender] -= value - self.cashcoinBalances[recver] += value - return(value) - else: - return(0) - -# @return value of cash sent; fail is 0 -def sendFrom(recver, value, from): - if(from!=tx.origin): - return(0) - senderBalance = self.cashcoinBalances[from] - if(senderBalance >= value): - self.cashcoinBalances[from] -= value - self.cashcoinBalances[recver] += value - return(value) - else: - return(0) - -# make sure only coming from specific contracts -def subtractCash(ID, amount): - #if(!self.whitelist.check(msg.sender)): - # return(-1) - self.cashcoinBalances[ID] -= amount - return(1) - -def addCash(ID, amount): - #if(!self.whitelist.check(msg.sender)): - # return(-1) - self.cashcoinBalances[ID] += amount - return(1) - -def setCash(address, balance): - #if !self.whitelist.check(msg.sender): - # return(-1) - self.cashcoinBalances[address] = balance - return(1) diff --git a/old_test/support/contracts/simple_storage.sol b/old_test/support/contracts/simple_storage.sol deleted file mode 100644 index 0b12e43d..00000000 --- a/old_test/support/contracts/simple_storage.sol +++ /dev/null @@ -1,14 +0,0 @@ -contract SimpleStorage { - uint public storedData; - - function SimpleStorage(uint initialValue) { - storedData = initialValue; - } - - function set(uint x) { - storedData = x; - } - function get() constant returns (uint retVal) { - return storedData; - } -} diff --git a/old_test/support/contracts/token.sol b/old_test/support/contracts/token.sol deleted file mode 100644 index 9f67a151..00000000 --- a/old_test/support/contracts/token.sol +++ /dev/null @@ -1,19 +0,0 @@ -contract token { - mapping (address => uint) public coinBalanceOf; - event CoinTransfer(address sender, address receiver, uint amount); - - /* Initializes contract with initial supply tokens to the creator of the contract */ - function token(uint supply) { - //coinBalanceOf[msg.sender] = (supply || 10000); - coinBalanceOf[msg.sender] = 10000; - } - - /* Very simple trade function */ - function sendCoin(address receiver, uint amount) returns(bool sufficient) { - if (coinBalanceOf[msg.sender] < amount) return false; - coinBalanceOf[msg.sender] -= amount; - coinBalanceOf[receiver] += amount; - CoinTransfer(msg.sender, receiver, amount); - return true; - } -} diff --git a/old_test/support/contracts/wallet.sol b/old_test/support/contracts/wallet.sol deleted file mode 100644 index 576e8648..00000000 --- a/old_test/support/contracts/wallet.sol +++ /dev/null @@ -1,8 +0,0 @@ -contract Wallet { - address currency; - - function Wallet(address c) { - currency = c; - } - -} diff --git a/old_test/support/contracts/wallets.sol b/old_test/support/contracts/wallets.sol deleted file mode 100644 index 38e3f0eb..00000000 --- a/old_test/support/contracts/wallets.sol +++ /dev/null @@ -1,9 +0,0 @@ -contract Wallets { - address wallet; - - function Wallet(address w) { - wallet = w; - } - -} - diff --git a/old_test/support/instances.yml b/old_test/support/instances.yml deleted file mode 100644 index 04172a48..00000000 --- a/old_test/support/instances.yml +++ /dev/null @@ -1,12 +0,0 @@ -development: - SimpleStorage: - deploy: false - args: - - 100 - BarStorage: - instanceOf: SimpleStorage - FooStorage: - instanceOf: SimpleStorage - args: - - 200 -staging: diff --git a/old_test/test.js b/old_test/test.js deleted file mode 100644 index 1c0d1660..00000000 --- a/old_test/test.js +++ /dev/null @@ -1,29 +0,0 @@ -var Config = require('../lib/config/config.js'); -var Test = require('../lib/test.js'); -var Compiler = require('../lib/compiler.js'); -var assert = require('assert'); - -//var contractFiles = grunt.file.expand("./app/contracts/**/*.sol") - -describe('embark.test', function() { - //var files = [ - // 'test/support/contracts/simple_storage.sol' - //] - //var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); - //var blockchainConfig = _blockchainConfig.config("development"); - //var compiler = new Compiler(_blockchainConfig); - //var contractsConfig = new Config.Contracts(blockchainConfig, compiler); - //contractsConfig.loadConfigFile('test/support/contracts.yml'); - //contractsConfig.init(files, 'development'); - - //describe('simple test', function() { - // var embarkSpec = new Test(contractsConfig, files); - - // it('execute simple test', function() { - // var SimpleStorage = embarkSpec.request('SimpleStorage', [100]) - - // assert.equal(SimpleStorage.storedData(), '100'); - // }); - //}); - -}); From ce4f75ddc4e8f27362da643ac60d7a03a055a897 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 16 Dec 2017 15:39:30 -0500 Subject: [PATCH 2/5] extract solc to its own module --- lib/contracts/compiler.js | 93 +---------------- lib/contracts/contracts.js | 9 +- lib/contracts/deploy_manager.js | 1 - lib/core/engine.js | 10 ++ lib/core/plugin.js | 5 + lib/core/plugins.js | 11 +- lib/modules/solidity/index.js | 100 +++++++++++++++++++ lib/{contracts => modules/solidity}/solcP.js | 0 lib/{contracts => modules/solidity}/solcW.js | 6 +- 9 files changed, 130 insertions(+), 105 deletions(-) create mode 100644 lib/modules/solidity/index.js rename lib/{contracts => modules/solidity}/solcP.js (100%) rename lib/{contracts => modules/solidity}/solcW.js (89%) diff --git a/lib/contracts/compiler.js b/lib/contracts/compiler.js index eb82a9b3..054e6bf2 100644 --- a/lib/contracts/compiler.js +++ b/lib/contracts/compiler.js @@ -1,21 +1,14 @@ /*jshint esversion: 6, loopfunc: true */ let async = require('../utils/async_extend.js'); -let SolcW = require('./solcW.js'); class Compiler { constructor(options) { this.plugins = options.plugins; this.logger = options.logger; - this.solcVersion = options.solcVersion; - this.contractDirectories = options.contractDirectories; } compile_contracts(contractFiles, cb) { - - let available_compilers = { - //".se": this.compile_serpent - ".sol": this.compile_solidity.bind(this) - }; + let available_compilers = {}; if (this.plugins) { let compilerPlugins = this.plugins.getPluginsFor('compilers'); @@ -48,90 +41,6 @@ class Compiler { } ); } - - compile_solidity(contractFiles, cb) { - let self = this; - let input = {}; - let solcW; - async.waterfall([ - function prepareInput(callback) { - async.each(contractFiles, - function(file, fileCb) { - let filename = file.filename; - - for (let directory of self.contractDirectories) { - filename = filename.replace(directory, ''); - } - - file.content(function(fileContent) { - input[filename] = fileContent; - fileCb(); - }); - }, - function (err) { - callback(err); - } - ); - }, - function loadCompiler(callback) { - // TODO: there ino need to load this twice - solcW = new SolcW({logger: self.logger, solcVersion: self.solcVersion}); - if (solcW.isCompilerLoaded()) { - return callback(); - } - - self.logger.info("loading solc compiler.."); - solcW.load_compiler(function (err) { - callback(err); - }); - }, - function compileContracts(callback) { - self.logger.info("compiling contracts..."); - solcW.compile({sources: input}, 1, function (output) { - if (output.errors) { - for (let i=0; i= 0) { - return callback(new Error("Solidity errors: " + output.errors).message); - } - } - self.logger.warn(output.errors.join('\n')); - } - callback(null, output); - }); - }, - function createCompiledObject(output, callback) { - let json = output.contracts; - - let compiled_object = {}; - - for (let contractName in json) { - let contract = json[contractName]; - - // Pull out filename:classname - // [0] filename:classname - // [1] filename - // [2] classname - const regex = /(.*):(.*)/; - const className = contractName.match(regex)[2]; - const filename = contractName.match(regex)[1]; - - compiled_object[className] = {}; - compiled_object[className].code = contract.bytecode; - compiled_object[className].runtimeBytecode = contract.runtimeBytecode; - compiled_object[className].realRuntimeBytecode = contract.runtimeBytecode.slice(0, -68); - compiled_object[className].swarmHash = contract.runtimeBytecode.slice(-68).slice(0, 64); - compiled_object[className].gasEstimates = contract.gasEstimates; - compiled_object[className].functionHashes = contract.functionHashes; - compiled_object[className].abiDefinition = JSON.parse(contract.interface); - compiled_object[className].filename = filename; - } - - callback(null, compiled_object); - } - ], function (err, result) { - cb(err, result); - }); - } } module.exports = Compiler; diff --git a/lib/contracts/contracts.js b/lib/contracts/contracts.js index 8c7616f4..00a26059 100644 --- a/lib/contracts/contracts.js +++ b/lib/contracts/contracts.js @@ -8,17 +8,10 @@ let Compiler = require('./compiler.js'); class ContractsManager { constructor(options) { this.contractFiles = options.contractFiles; - this.contractDirectories = options.contractDirectories; this.contractsConfig = options.contractsConfig; this.contracts = {}; this.logger = options.logger; this.plugins = options.plugins; - if (!options.contractsConfig.versions) { - this.solcVersion = "0.4.17"; - } else { - this.solcVersion = options.contractsConfig.versions.solc; - } - this.contractDependencies = {}; } @@ -26,7 +19,7 @@ class ContractsManager { let self = this; async.waterfall([ function compileContracts(callback) { - let compiler = new Compiler({plugins: self.plugins, logger: self.logger, solcVersion: self.solcVersion, contractDirectories: self.contractDirectories}); + let compiler = new Compiler({plugins: self.plugins, logger: self.logger}); compiler.compile_contracts(self.contractFiles, function (err, compiledObject) { self.compiledContracts = compiledObject; callback(err); diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 9614af1f..975abae0 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -26,7 +26,6 @@ class DeployManager { function buildContracts(callback) { let contractsManager = new ContractsManager({ contractFiles: self.config.contractsFiles, - contractDirectories: self.config.contractDirectories, contractsConfig: self.config.contractsConfig, logger: self.logger, plugins: self.plugins diff --git a/lib/core/engine.js b/lib/core/engine.js index 050c7528..d689f3fe 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -46,6 +46,10 @@ class Engine { this.servicesMonitor.startMonitor(); } + registerModule(moduleName, options) { + this.plugins.loadInternalPlugin(moduleName, options); + } + startService(serviceName, _options) { let options = _options || {}; @@ -124,6 +128,12 @@ class Engine { deploymentService(options) { let self = this; + + this.registerModule('solidity', { + solcVersion: self.config.contractsConfig.solcVersion, + contractDirectories: self.config.contractDirectories + }); + this.deployManager = new DeployManager({ web3: options.web3 || self.web3, trackContracts: options.trackContracts, diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 45a498f0..59272374 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -5,6 +5,7 @@ var utils = require('../utils/utils.js'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { this.name = options.name; + this.isInternal = options.isInternal; this.pluginModule = options.pluginModule; this.pluginPath = options.pluginPath; this.pluginConfig = options.pluginConfig; @@ -31,6 +32,10 @@ Plugin.prototype.loadPlugin = function() { (this.pluginModule.call(this, this)); }; +Plugin.prototype.loadInternalPlugin = function() { + new this.pluginModule(this, this.pluginConfig); +}; + Plugin.prototype.loadPluginFile = function(filename) { return fs.readFileSync(this.pathToFile(filename)).toString(); }; diff --git a/lib/core/plugins.js b/lib/core/plugins.js index c3319183..d2ba61b8 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -27,11 +27,20 @@ Plugins.prototype.listPlugins = function() { return list; }; +Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) { + var pluginPath = utils.joinPath('../modules/', pluginName, 'index.js'); + var plugin = require(pluginPath); + + var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config, isInternal: true}); + pluginWrapper.loadInternalPlugin(); + this.plugins.push(pluginWrapper); +}; + Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) { var pluginPath = utils.joinPath(process.env.PWD, 'node_modules', pluginName); var plugin = require(pluginPath); - var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config}); + var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config, isInternal: false}); pluginWrapper.loadPlugin(); this.plugins.push(pluginWrapper); }; diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js new file mode 100644 index 00000000..2087268b --- /dev/null +++ b/lib/modules/solidity/index.js @@ -0,0 +1,100 @@ +let async = require('../../utils/async_extend.js'); +let SolcW = require('./solcW.js'); + +class Solidity { + + constructor(embark, options) { + this.logger = embark.logger; + this.solcVersion = options.solcVersion; + this.contractDirectories = options.contractDirectories; + + embark.registerCompiler(".sol", this.compile_solidity.bind(this)); + } + + compile_solidity(contractFiles, cb) { + let self = this; + let input = {}; + let solcW; + async.waterfall([ + function prepareInput(callback) { + async.each(contractFiles, + function(file, fileCb) { + let filename = file.filename; + + for (let directory of self.contractDirectories) { + filename = filename.replace(directory, ''); + } + + file.content(function(fileContent) { + input[filename] = fileContent; + fileCb(); + }); + }, + function (err) { + callback(err); + } + ); + }, + function loadCompiler(callback) { + // TODO: there ino need to load this twice + solcW = new SolcW({logger: self.logger, solcVersion: self.solcVersion}); + if (solcW.isCompilerLoaded()) { + return callback(); + } + + self.logger.info("loading solc compiler.."); + solcW.load_compiler(function (err) { + callback(err); + }); + }, + function compileContracts(callback) { + self.logger.info("compiling contracts..."); + solcW.compile({sources: input}, 1, function (output) { + if (output.errors) { + for (let i=0; i= 0) { + return callback(new Error("Solidity errors: " + output.errors).message); + } + } + self.logger.warn(output.errors.join('\n')); + } + callback(null, output); + }); + }, + function createCompiledObject(output, callback) { + let json = output.contracts; + + let compiled_object = {}; + + for (let contractName in json) { + let contract = json[contractName]; + + // Pull out filename:classname + // [0] filename:classname + // [1] filename + // [2] classname + const regex = /(.*):(.*)/; + const className = contractName.match(regex)[2]; + const filename = contractName.match(regex)[1]; + + compiled_object[className] = {}; + compiled_object[className].code = contract.bytecode; + compiled_object[className].runtimeBytecode = contract.runtimeBytecode; + compiled_object[className].realRuntimeBytecode = contract.runtimeBytecode.slice(0, -68); + compiled_object[className].swarmHash = contract.runtimeBytecode.slice(-68).slice(0, 64); + compiled_object[className].gasEstimates = contract.gasEstimates; + compiled_object[className].functionHashes = contract.functionHashes; + compiled_object[className].abiDefinition = JSON.parse(contract.interface); + compiled_object[className].filename = filename; + } + + callback(null, compiled_object); + } + ], function (err, result) { + cb(err, result); + }); + } + +} + +module.exports = Solidity; diff --git a/lib/contracts/solcP.js b/lib/modules/solidity/solcP.js similarity index 100% rename from lib/contracts/solcP.js rename to lib/modules/solidity/solcP.js diff --git a/lib/contracts/solcW.js b/lib/modules/solidity/solcW.js similarity index 89% rename from lib/contracts/solcW.js rename to lib/modules/solidity/solcW.js index a801d1ec..d557187a 100644 --- a/lib/contracts/solcW.js +++ b/lib/modules/solidity/solcW.js @@ -1,9 +1,9 @@ -let utils = require('../utils/utils.js'); +let utils = require('../../utils/utils.js'); let solcProcess; let compilerLoaded = false; -var Npm = require('../pipeline/npm.js'); +var Npm = require('../../pipeline/npm.js'); let path = require('path'); -let currentSolcVersion = require('../../package.json').dependencies.solc; +let currentSolcVersion = require('../../../package.json').dependencies.solc; class SolcW { From ce6c5706345c98e32c9a4178a1f39f899a9b8e75 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 16 Dec 2017 15:48:54 -0500 Subject: [PATCH 3/5] use utils joinPath instead of path.join --- lib/modules/solidity/solcW.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/modules/solidity/solcW.js b/lib/modules/solidity/solcW.js index d557187a..aecf6674 100644 --- a/lib/modules/solidity/solcW.js +++ b/lib/modules/solidity/solcW.js @@ -2,7 +2,6 @@ let utils = require('../../utils/utils.js'); let solcProcess; let compilerLoaded = false; var Npm = require('../../pipeline/npm.js'); -let path = require('path'); let currentSolcVersion = require('../../../package.json').dependencies.solc; class SolcW { @@ -33,7 +32,7 @@ class SolcW { if (err) { return done(err); } - let requirePath = path.join(process.env.PWD, location); + let requirePath = utils.joinPath(process.env.PWD, location); solcProcess.send({action: 'loadCompiler', solcLocation: requirePath}); }); } From 67dd4a22cb8da24d20ddb917f4635fbf1b53933b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 16 Dec 2017 16:05:46 -0500 Subject: [PATCH 4/5] update tests --- lib/core/engine.js | 2 +- lib/core/plugin.js | 2 +- test/compiler.js | 9 ++++++++- test/contracts.js | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index d689f3fe..785fc4fd 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -130,7 +130,7 @@ class Engine { let self = this; this.registerModule('solidity', { - solcVersion: self.config.contractsConfig.solcVersion, + solcVersion: self.config.contractsConfig.versions.solc, contractDirectories: self.config.contractDirectories }); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 59272374..c7812e5c 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -33,7 +33,7 @@ Plugin.prototype.loadPlugin = function() { }; Plugin.prototype.loadInternalPlugin = function() { - new this.pluginModule(this, this.pluginConfig); + new this.pluginModule(this, this.pluginConfig); /*eslint no-new: "off"*/ }; Plugin.prototype.loadPluginFile = function(filename) { diff --git a/test/compiler.js b/test/compiler.js index 8ad45e23..e0f32da2 100644 --- a/test/compiler.js +++ b/test/compiler.js @@ -1,5 +1,6 @@ /*globals describe, it*/ let Compiler = require('../lib/contracts/compiler.js'); +let SolidityCompiler = require('../lib/modules/solidity'); let TestLogger = require('../lib/core/test_logger.js'); let File = require('../lib/core/file.js'); let assert = require('assert'); @@ -9,8 +10,14 @@ let readFile = function(file) { return new File({filename: file, type: 'dapp_file', path: file}); }; +var apiObject = { + registerCompiler: function() {}, + logger: new TestLogger({}) +}; + describe('embark.Compiler', function() { - let compiler = new Compiler({logger: new TestLogger({}), solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); + //let compiler = new Compiler({logger: new TestLogger({})}); + let compiler = new SolidityCompiler(apiObject, {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); describe('#compile_solidity', function() { this.timeout(0); diff --git a/test/contracts.js b/test/contracts.js index eeb33d9f..0d395776 100644 --- a/test/contracts.js +++ b/test/contracts.js @@ -9,7 +9,7 @@ let readFile = function(file) { return new File({filename: file, type: 'dapp_file', path: file}); }; -describe('embark.Contratcs', function() { +describe('embark.Contracts', function() { this.timeout(0); describe('simple', function() { let contractsManager = new ContractsManager({ From 8999d619c5da097ade8907bc6acf8089afb5d85b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 16 Dec 2017 17:55:27 -0500 Subject: [PATCH 5/5] update tests --- lib/core/test.js | 3 ++- test/contracts.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/core/test.js b/lib/core/test.js index 8bd1f3bd..25c670e6 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -51,7 +51,8 @@ Test.prototype.deployAll = function(contractsConfig, cb) { async.waterfall([ function getConfig(callback) { - self.engine.config.contractsConfig = {contracts: contractsConfig}; + let _versions_default = self.engine.config.contractsConfig.versions; + self.engine.config.contractsConfig = {contracts: contractsConfig, versions: _versions_default}; callback(); }, function startServices(callback) { diff --git a/test/contracts.js b/test/contracts.js index 0d395776..027ec312 100644 --- a/test/contracts.js +++ b/test/contracts.js @@ -2,9 +2,13 @@ let ContractsManager = require('../lib/contracts/contracts.js'); let Logger = require('../lib/core/logger.js'); let File = require('../lib/core/file.js'); +let TestLogger = require('../lib/core/test_logger.js'); let assert = require('assert'); let fs = require('fs'); +//let SolidityCompiler = require('../lib/modules/solidity'); +let Plugins = require('../lib/core/plugins.js'); + let readFile = function(file) { return new File({filename: file, type: 'dapp_file', path: file}); }; @@ -12,7 +16,13 @@ let readFile = function(file) { describe('embark.Contracts', function() { this.timeout(0); describe('simple', function() { + let plugins = new Plugins({ + logger: new TestLogger({}) + }); + plugins.loadInternalPlugin('Solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); + let contractsManager = new ContractsManager({ + plugins: plugins, contractFiles: [ readFile('test/contracts/simple_storage.sol'), readFile('test/contracts/token.sol') @@ -85,7 +95,13 @@ describe('embark.Contracts', function() { }); describe('config with contract instances', function() { + let plugins = new Plugins({ + logger: new TestLogger({}) + }); + plugins.loadInternalPlugin('Solidity', {solcVersion: '0.4.17', contractDirectories: ['app/contracts/']}); + let contractsManager = new ContractsManager({ + plugins: plugins, contractFiles: [ readFile('test/contracts/simple_storage.sol'), readFile('test/contracts/token_storage.sol')