diff --git a/lib/blockchain.js b/lib/blockchain.js index de6579d8f..32fe06e59 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -18,10 +18,15 @@ var Blockchain = function(blockchainConfig, Client) { port: this.blockchainConfig.port || 30303, nodiscover: this.blockchainConfig.nodiscover || false, mine: this.blockchainConfig.mine || false, - whisper: this.blockchainConfig.whisper || true, account: this.blockchainConfig.account || {} }; + if (this.blockchainConfig.whisper === false) { + this.config.whisper = false; + } else { + this.config.whisper = (this.blockchainConfig.whisper || true); + } + this.client = new Client({config: this.config}); }; diff --git a/old_test/blockchain.js b/old_test/blockchain.js new file mode 100644 index 000000000..aa7580b14 --- /dev/null +++ b/old_test/blockchain.js @@ -0,0 +1,46 @@ +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/test/chain_manager.js b/old_test/chain_manager.js similarity index 100% rename from test/chain_manager.js rename to old_test/chain_manager.js diff --git a/test/compiler.js b/old_test/compiler.js similarity index 100% rename from test/compiler.js rename to old_test/compiler.js diff --git a/test/config.blockchain.js b/old_test/config.blockchain.js similarity index 100% rename from test/config.blockchain.js rename to old_test/config.blockchain.js diff --git a/test/config.contracts.js b/old_test/config.contracts.js similarity index 100% rename from test/config.contracts.js rename to old_test/config.contracts.js diff --git a/test/deploy.js b/old_test/deploy.js similarity index 100% rename from test/deploy.js rename to old_test/deploy.js diff --git a/test/support/address.yml b/old_test/support/address.yml similarity index 100% rename from test/support/address.yml rename to old_test/support/address.yml diff --git a/test/support/arguments.yml b/old_test/support/arguments.yml similarity index 100% rename from test/support/arguments.yml rename to old_test/support/arguments.yml diff --git a/test/support/arguments2.yml b/old_test/support/arguments2.yml similarity index 100% rename from test/support/arguments2.yml rename to old_test/support/arguments2.yml diff --git a/test/support/arguments3.yml b/old_test/support/arguments3.yml similarity index 100% rename from test/support/arguments3.yml rename to old_test/support/arguments3.yml diff --git a/test/support/blockchain.yml b/old_test/support/blockchain.yml similarity index 100% rename from test/support/blockchain.yml rename to old_test/support/blockchain.yml diff --git a/test/support/chain_manager.json b/old_test/support/chain_manager.json similarity index 100% rename from test/support/chain_manager.json rename to old_test/support/chain_manager.json diff --git a/test/support/contracts.yml b/old_test/support/contracts.yml similarity index 100% rename from test/support/contracts.yml rename to old_test/support/contracts.yml diff --git a/test/support/contracts/another_storage.sol b/old_test/support/contracts/another_storage.sol similarity index 100% rename from test/support/contracts/another_storage.sol rename to old_test/support/contracts/another_storage.sol diff --git a/test/support/contracts/cash.se b/old_test/support/contracts/cash.se similarity index 100% rename from test/support/contracts/cash.se rename to old_test/support/contracts/cash.se diff --git a/test/support/contracts/crowdsale.sol b/old_test/support/contracts/crowdsale.sol similarity index 100% rename from test/support/contracts/crowdsale.sol rename to old_test/support/contracts/crowdsale.sol diff --git a/test/support/contracts/data_source.sol b/old_test/support/contracts/data_source.sol similarity index 100% rename from test/support/contracts/data_source.sol rename to old_test/support/contracts/data_source.sol diff --git a/test/support/contracts/error.sol b/old_test/support/contracts/error.sol similarity index 100% rename from test/support/contracts/error.sol rename to old_test/support/contracts/error.sol diff --git a/test/support/contracts/manager.sol b/old_test/support/contracts/manager.sol similarity index 100% rename from test/support/contracts/manager.sol rename to old_test/support/contracts/manager.sol diff --git a/test/support/contracts/other.se b/old_test/support/contracts/other.se similarity index 100% rename from test/support/contracts/other.se rename to old_test/support/contracts/other.se diff --git a/test/support/contracts/simple_storage.sol b/old_test/support/contracts/simple_storage.sol similarity index 100% rename from test/support/contracts/simple_storage.sol rename to old_test/support/contracts/simple_storage.sol diff --git a/test/support/contracts/token.sol b/old_test/support/contracts/token.sol similarity index 100% rename from test/support/contracts/token.sol rename to old_test/support/contracts/token.sol diff --git a/test/support/contracts/wallet.sol b/old_test/support/contracts/wallet.sol similarity index 100% rename from test/support/contracts/wallet.sol rename to old_test/support/contracts/wallet.sol diff --git a/test/support/contracts/wallets.sol b/old_test/support/contracts/wallets.sol similarity index 100% rename from test/support/contracts/wallets.sol rename to old_test/support/contracts/wallets.sol diff --git a/test/support/instances.yml b/old_test/support/instances.yml similarity index 100% rename from test/support/instances.yml rename to old_test/support/instances.yml diff --git a/test/test.js b/old_test/test.js similarity index 100% rename from test/test.js rename to old_test/test.js diff --git a/test/abi.js b/test/abi.js new file mode 100644 index 000000000..96351e1b5 --- /dev/null +++ b/test/abi.js @@ -0,0 +1,59 @@ +/*globals describe, it*/ +var ABIGenerator = require('../lib/abi.js'); +var assert = require('assert'); + +// TODO: instead 'eval' the code with a fake web3 object +// and check the generate code interacts as expected +describe('embark.ABIGenerator', function() { + + describe('#generateProvider', function() { + var generator = new ABIGenerator({rpcHost: 'somehost', rpcPort: '1234'}, {}); + + it('should generate code to connect to a provider', function() { + var providerCode = "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; + + assert.equal(generator.generateProvider(), providerCode); + }); + }); + + describe('#generateContracts', function() { + var generator = new ABIGenerator({}, { + contracts: { + SimpleStorage: { + abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], + gasEstimates: 12000, + deployedAddress: "0x123", + code: '12345' + }, + Foo: { + abiDefinition: [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initialValue","type":"uint256"}],"type":"constructor"}], + gasEstimates: 12000, + deployedAddress: "0x124", + code: '123456' + } + } + }); + + describe('with EmbarkJS', function() { + var withEmbarkJS = true; + + it('should generate contract code', function() { + var contractCode = "\n\nSimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\nFoo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});"; + assert.equal(generator.generateContracts(withEmbarkJS), contractCode); + }); + }); + + describe('with default interface', function() { + var withEmbarkJS = false; + + it('should generate contract code', function() { + var contractCode = "\n\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');"; + assert.equal(generator.generateContracts(withEmbarkJS), contractCode); + }); + }); + + }); + + //describe('#generateABI', function() { + //}); +}); diff --git a/test/blockchain.js b/test/blockchain.js index aa7580b14..45f1e3d9d 100644 --- a/test/blockchain.js +++ b/test/blockchain.js @@ -1,46 +1,59 @@ -var Config = require('../lib/config/config.js'); +/*globals describe, it*/ 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('embark.Blockchain', function() { + //var Client = function() {}; + //Client.prototype.name = "ClientName"; - describe('#generate_basic_command', function() { - var blockchain = new Blockchain(blockchainConfig); + describe('#initializer', function() { + //var client = new Client(); - 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('with empty config', function() { + it('should have a default config', function() { + var config = { + networkType: 'custom', + genesisBlock: false, + datadir: false, + mineWhenNeeded: false, + rpcHost: 'localhost', + rpcPort: 8545, + rpcCorsDomain: false, + networkId: 12301, + port: 30303, + nodiscover: false, + mine: false, + whisper: true, + account: {} + }; + var blockchain = Blockchain(config, 'geth'); - 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"); + assert.deepEqual(blockchain.config, config); }); }); - }); + describe('with config', function() { + it('should take config params', function() { + var config = { + networkType: 'livenet', + genesisBlock: 'foo/bar/genesis.json', + datadir: '/foo/datadir/', + mineWhenNeeded: true, + rpcHost: 'someserver', + rpcPort: 12345, + rpcCorsDomain: true, + networkId: 1, + port: 123456, + nodiscover: true, + mine: true, + whisper: false, + account: {} + }; + var blockchain = Blockchain(config, 'geth'); + + assert.deepEqual(blockchain.config, config); + }); + }); + + }); });