embark/test/deploy.js

262 lines
8.9 KiB
JavaScript
Raw Normal View History

2015-07-03 08:59:33 +00:00
var Config = require('../lib/config/config.js');
var Deploy = require('../lib/deploy.js');
2015-07-04 03:23:21 +00:00
var Compiler = require('../lib/compiler.js');
var ChainManager = require('../lib/chain_manager.js');
2015-07-03 08:59:33 +00:00
var assert = require('assert');
var web3 = require('web3');
2015-07-03 08:59:33 +00:00
2015-12-06 21:02:13 +00:00
// TODO: replace with ethersim
2016-02-09 02:12:09 +00:00
var Web3 = require('web3');
var web3 = new Web3();
2015-12-06 21:02:13 +00:00
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8101"));
setDeployConfig = function(config) {
var _blockchainConfig = (new Config.Blockchain()).loadConfigFile(config.blockchain);
2015-07-04 03:23:21 +00:00
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);
2015-08-01 15:19:21 +00:00
contractsConfig.init(config.files, 'development');
compiler.init('development');
2016-02-09 02:12:09 +00:00
return new Deploy('development', config.files, blockchainConfig, contractsConfig, chainManager, true, false, web3);
}
2015-07-03 08:59:33 +00:00
2016-06-01 01:11:42 +00:00
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() {
2015-07-03 08:59:33 +00:00
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() {
2015-07-06 12:19:25 +00:00
var deploy = setDeployConfig({
files: files,
blockchain: 'test/support/blockchain.yml',
contracts: 'test/support/arguments.yml'
});
2016-06-01 01:11:42 +00:00
it("should deploy contracts", function(fn) {
var doneWrap = new Done(fn);
2016-02-09 02:12:09 +00:00
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);
}
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
2016-02-09 02:12:09 +00:00
});
});
2015-07-03 08:59:33 +00:00
});
describe('#generate_abi_file', function() {
2015-07-06 12:19:25 +00:00
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() {
2016-02-09 02:12:09 +00:00
var result = "";
result += deploy.generate_provider_file();
result += deploy.generate_abi_file();
2016-05-31 11:46:45 +00:00
assert.strictEqual(result, "var web3 = new Web3();web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];blockchain = {\"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\":[],\"testnet\":false,\"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}}}};");
});
});
2015-07-03 08:59:33 +00:00
});
2015-08-01 02:50:51 +00:00
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'
});
2016-06-01 01:11:42 +00:00
it("should deploy contracts", function(fn) {
var doneWrap = new Done(fn);
2016-02-09 02:12:09 +00:00
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);
}
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
2016-02-09 02:12:09 +00:00
});
2015-08-01 02:50:51 +00:00
});
});
});
describe('contracts instances', function() {
var files = [
'test/support/contracts/simple_storage.sol'
];
describe('#deploy_contracts', function() {
2015-07-06 12:19:25 +00:00
var deploy = setDeployConfig({
files: files,
blockchain: 'test/support/blockchain.yml',
contracts: 'test/support/instances.yml'
});
2016-06-01 01:11:42 +00:00
it("should deploy contracts", function(fn) {
var doneWrap = new Done(fn);
2016-02-09 02:12:09 +00:00
deploy.deploy_contracts("development", function() {
var all_contracts = ['BarStorage', 'FooStorage'];
for(var i=0; i < all_contracts.length; i++) {
var className = all_contracts[i];
2016-02-09 02:12:09 +00:00
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
}
assert.notEqual(deploy.deployedContracts.hasOwnProperty('SimpleStorage'), true);
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
2016-02-09 02:12:09 +00:00
});
});
2015-07-03 08:59:33 +00:00
});
2015-07-03 08:59:33 +00:00
});
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'
});
2016-06-01 01:11:42 +00:00
it("should deploy contracts", function(fn) {
var doneWrap = new Done(fn);
2016-02-09 02:12:09 +00:00
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);
}
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
2016-02-09 02:12:09 +00:00
});
});
2016-06-01 01:11:42 +00:00
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);
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
});
});
});
2015-07-06 12:19:25 +00:00
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'
});
2016-06-01 01:11:42 +00:00
it("should not deploy contracts with addresses defined", function(fn) {
var doneWrap = new Done(fn);
2016-02-09 02:12:09 +00:00
deploy.deploy_contracts("development", function() {
var expected_deploys = ['SimpleStorage', 'BarStorage', 'FooStorage'];
2015-07-06 12:19:25 +00:00
2016-02-09 02:12:09 +00:00
for(var i=0; i < expected_deploys.length; i++) {
var className = expected_deploys[i];
2015-07-06 12:19:25 +00:00
2016-02-09 02:12:09 +00:00
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
}
2015-07-06 12:19:25 +00:00
2016-02-09 02:12:09 +00:00
assert.equal(deploy.deployedContracts['SimpleStorage'], '0x123');
assert.equal(deploy.deployedContracts['BarStorage'], '0x234');
2016-06-01 01:11:42 +00:00
doneWrap.trigger();
2016-02-09 02:12:09 +00:00
});
2015-07-06 12:19:25 +00:00
});
});
});
2015-07-03 08:59:33 +00:00
});