diff --git a/bin/embark b/bin/embark index d70fefaaa..0eb7c3a3d 100644 --- a/bin/embark +++ b/bin/embark @@ -13,7 +13,7 @@ var run = function(cmd) { } program - .version('0.4.3') + .version('0.5.0') program.command('new [name]').description('New application').action(function(name) { if (name === undefined) { diff --git a/lib/config/config.js b/lib/config/config.js index 04d192fbf..3dfc3d64e 100644 --- a/lib/config/config.js +++ b/lib/config/config.js @@ -1,9 +1,11 @@ var readYaml = require('read-yaml'); BlockchainConfig = require('./blockchain.js'); +ContractsConfig = require('./contracts.js'); Config = { - Blockchain: BlockchainConfig + Blockchain: BlockchainConfig, + Contracts: ContractsConfig } module.exports = Config diff --git a/lib/config/contracts.js b/lib/config/contracts.js new file mode 100644 index 000000000..d18619625 --- /dev/null +++ b/lib/config/contracts.js @@ -0,0 +1,59 @@ +var readYaml = require('read-yaml'); +var fs = require('fs'); +var Blockchain = require('./blockchain.js'); + +ContractsConfig = function(files, blockchainConfig, web3) { + this.all_contracts = []; + this.contractDB = {}; + this.contractFiles = files; + this.web3 = web3; + + try { + this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort)); + primaryAddress = this.web3.eth.coinbase; + this.web3.eth.defaultAccount = primaryAddress; + } catch (_error) { + e = _error; + throw new Error("can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running"); + } + + console.log("address is : " + primaryAddress); +}; + +ContractsConfig.prototype.loadConfigFile = function(filename) { + try { + this.contractConfig = readYaml.sync(filename); + } catch (e) { + throw new Error("error reading " + filename); + } + return this; +} + +ContractsConfig.prototype.loadConfig = function(config) { + this.contractConfig = config; + return this; +} + +ContractsConfig.prototype.config = function(env) { + return this.contractConfig[env]; +} + +ContractsConfig.prototype.compileContracts = function() { + var contractFile, source, j; + + for (j = 0; j < this.contractFiles.length; j++) { + contractFile = this.contractFiles[j]; + source = fs.readFileSync(contractFile).toString() + + console.log("compiling " + contractFile); + compiled_contracts = this.web3.eth.compile.solidity(source); + for (className in compiled_contracts) { + var contract = compiled_contracts[className]; + this.all_contracts.push(className); + this.contractDB[className] = contract; + } + } +} + +module.exports = ContractsConfig + diff --git a/lib/deploy.js b/lib/deploy.js index a4d4f5fad..c4e7784b3 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -9,7 +9,8 @@ deployContracts = function(env, contractFiles, destFile) { blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); - contractsConfig = readYaml.sync("config/contracts.yml")[env || "development"]; + contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml'); + contractsConfig = contractsManager.config(env); try { web3.setProvider(new web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort)); @@ -48,22 +49,9 @@ deployContracts = function(env, contractFiles, destFile) { } } - all_contracts = []; - - contractDB = {}; - - for (j = 0, len1 = contractFiles.length; j < len1; j++) { - contractFile = contractFiles[j]; - //source = grunt.file.read(contractFile); - source = fs.readFileSync(contractFile).toString() - console.log("deploying " + contractFile); - compiled_contracts = web3.eth.compile.solidity(source); - for (className in compiled_contracts) { - contract = compiled_contracts[className]; - all_contracts.push(className); - contractDB[className] = contract; - } - } + contractsManager.compileContracts(); + all_contracts = contractsManager.all_contracts; + contractDB = contractsManager.contractDB; all_contracts.sort((function(_this) { return function(a, b) { diff --git a/lib/test.js b/lib/test.js index 2f7a44099..ee735936a 100644 --- a/lib/test.js +++ b/lib/test.js @@ -18,8 +18,8 @@ TestContractWrapper = (function() { } TestContractWrapper.prototype.initializeContract = function() { - example_abi = JSON.stringify(contract.info.abiDefinition) - example_binary = contract.code.slice(2) + example_abi = JSON.stringify(this.contract.info.abiDefinition) + example_binary = this.contract.code.slice(2) py_exec("example_abi = '" + example_abi + "'") py_exec("example_abi") @@ -67,19 +67,11 @@ request = function(className, args) { //TODO: get the files from the config contractFiles = grunt.file.expand("./app/contracts/**/*.sol") - contractDB = {} + blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); + contractsConfig = new Config.Contracts(contractFiles, blockchainConfig); - var i; - for (i = 0, len = contractFiles.length; i < len; i++) { - var contractFile = contractFiles[i]; - var source = fs.readFileSync(contractFile).toString() - - compiled_contracts = web3.eth.compile.solidity(source) - for (className in compiled_contracts) { - var contract = compiled_contracts[className]; - contractDB[className] = contract; - } - } + contractsConfig.compileContracts(); + contractDB = contractsConfig.contractDB; var contract = contractDB[className]; return TestContract(contract, className, args) diff --git a/package.json b/package.json index f5c758b09..256051ffe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "embark-framework", - "version": "0.4.3", + "version": "0.5.0", "description": "", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -41,6 +41,8 @@ "license": "ISC", "devDependencies": { "grunt-mocha-test": "^0.12.7", - "mocha": "^2.2.5" + "mocha": "^2.2.5", + "mocha-sinon": "^1.1.4", + "sinon": "^1.15.4" } } diff --git a/test/config.contracts.js b/test/config.contracts.js new file mode 100644 index 000000000..a8c6fa522 --- /dev/null +++ b/test/config.contracts.js @@ -0,0 +1,61 @@ +var Config = require('../lib/config/config.js'); +var assert = require('assert'); +var sinon = require('sinon'); +var web3 = require('web3'); +require('mocha-sinon'); + +describe('embark.config.contracts', function() { + var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); + //sinon.stub(web3, "setProvider"); + //sinon.stub(web3.eth.compile, "solidity", function() { + // return "compiled_code"; + //}); + + describe('#loadConfigFile', function() { + it('should read and load yml file', function() { + var contractsConfig = new Config.Contracts([], blockchainConfig, web3); + 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, web3); + 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(files, blockchainConfig, web3); + contractsConfig.loadConfigFile('test/support/contracts.yml'); + contractsConfig.compileContracts(); + }); + + it('add contracts to a list', function() { + assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "AnotherStorage" ]); + }); + }); + }); +}); + diff --git a/test/support/contracts.yml b/test/support/contracts.yml new file mode 100644 index 000000000..56addd58b --- /dev/null +++ b/test/support/contracts.yml @@ -0,0 +1,2 @@ +development: +staging: diff --git a/test/support/contracts/another_storage.sol b/test/support/contracts/another_storage.sol new file mode 100644 index 000000000..ce3c0ac6b --- /dev/null +++ b/test/support/contracts/another_storage.sol @@ -0,0 +1,14 @@ +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/test/support/contracts/simple_storage.sol b/test/support/contracts/simple_storage.sol new file mode 100644 index 000000000..0b12e43db --- /dev/null +++ b/test/support/contracts/simple_storage.sol @@ -0,0 +1,14 @@ +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; + } +}