Skip to content

Commit 4e5b40b

Browse files
committed
Merge branch 'develop'
2 parents 72dec76 + 2128bac commit 4e5b40b

File tree

14 files changed

+294
-47
lines changed

14 files changed

+294
-47
lines changed

bin/embark

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ var run = function(cmd) {
1515
}
1616

1717
var deploy = function(env, embarkConfig) {
18-
contractFiles = grunt.file.expand(embarkConfig.contracts);
19-
destFile = embarkConfig.output
20-
Embark.init()
21-
Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig)
22-
Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig)
23-
abi = Embark.deployContracts(env, contractFiles, destFile)
18+
var contractFiles = grunt.file.expand(embarkConfig.contracts);
19+
var destFile = embarkConfig.output;
20+
var chainFile = embarkConfig.chains;
21+
22+
Embark.init();
23+
Embark.blockchainConfig.loadConfigFile(embarkConfig.blockchainConfig);
24+
Embark.contractsConfig.loadConfigFile(embarkConfig.contractsConfig);
25+
26+
if (chainFile === undefined) {
27+
chainFile = './chains.json';
28+
}
29+
30+
abi = Embark.deployContracts(env, contractFiles, destFile, chainFile);
2431
grunt.file.write(destFile, abi);
2532
}
2633

2734
program
28-
.version('0.7.3')
35+
.version('0.8.0')
2936

3037
program.command('new [name]').description('New application').action(function(name) {
3138
if (name === undefined) {

boilerplate/chains.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

boilerplate/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"license": "ISC",
1111
"homepage": "",
1212
"devDependencies": {
13-
"embark-framework": "^0.7.3",
14-
"grunt-embark": "^0.2.0",
13+
"embark-framework": "^0.8.0",
14+
"grunt-embark": "^0.3.0",
1515
"grunt-contrib-clean": "^0.6.0",
1616
"grunt-contrib-coffee": "^0.13.0",
1717
"grunt-contrib-concat": "^0.5.1",

lib/chain_manager.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var fs = require('fs');
2+
var web3 = require('web3');
3+
var sha3_256 = require('js-sha3').sha3_256;
4+
5+
ChainManager = function() {
6+
this.currentChain = {};
7+
this.file = "";
8+
}
9+
10+
ChainManager.prototype.loadConfigFile = function(filename) {
11+
try {
12+
var obj = JSON.parse(fs.readFileSync(filename));
13+
this.file = filename;
14+
this.chainManagerConfig = obj;
15+
} catch (e) {
16+
throw new Error("error reading " + filename);
17+
}
18+
return this;
19+
};
20+
21+
ChainManager.prototype.loadConfig = function(config) {
22+
this.chainManagerConfig = config;
23+
return this;
24+
};
25+
26+
ChainManager.prototype.init = function(env, config) {
27+
web3.setProvider(new web3.providers.HttpProvider("http://" + config.rpcHost + ":" + config.rpcPort));
28+
29+
var chainId = web3.eth.getBlock(0).hash;
30+
31+
if (this.chainManagerConfig[chainId] === undefined) {
32+
this.chainManagerConfig[chainId] = {contracts: {}};
33+
}
34+
35+
this.currentChain = this.chainManagerConfig[chainId];
36+
}
37+
38+
ChainManager.prototype.addContract = function(contractName, code, address) {
39+
this.currentChain.contracts[sha3_256(code)] = {
40+
name: contractName,
41+
address: address
42+
}
43+
}
44+
45+
ChainManager.prototype.getContract = function(code) {
46+
return this.currentChain.contracts[sha3_256(code)];
47+
}
48+
49+
ChainManager.prototype.save = function() {
50+
fs.writeFileSync(this.file, JSON.stringify(this.chainManagerConfig));
51+
}
52+
53+
module.exports = ChainManager;
54+

lib/config/contracts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ContractsConfig.prototype.compileContracts = function(env) {
122122
contract.gasLimit = contractConfig.gas_limit || contract.gasLimit;
123123
contract.args = contractConfig.args || [];
124124
contract.address = contractConfig.address;
125+
contract.onDeploy = contractConfig.onDeploy || [];
125126

126127
if (contractConfig.instanceOf !== undefined) {
127128
contract.types.push('instance');

lib/deploy.js

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ sleep = function sleep(ms) {
1010
while (new Date().getTime() < start + ms);
1111
}
1212

13-
Deploy = function(env, contractFiles, blockchainConfig, contractsConfig) {
13+
Deploy = function(env, contractFiles, blockchainConfig, contractsConfig, chainManager) {
1414
//this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
1515
this.blockchainConfig = blockchainConfig;
16+
this.chainManager = chainManager;
17+
this.chainManager.init(env, this.blockchainConfig);
1618

1719
//this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
1820
this.contractsManager = contractsConfig;
@@ -56,59 +58,90 @@ Deploy.prototype.deploy_contracts = function(env) {
5658
className = all_contracts[k];
5759
contract = this.contractDB[className];
5860

61+
5962
if (contract.address !== undefined) {
6063
this.deployedContracts[className] = contract.address;
6164

6265
//console.log("contract " + className + " at " + contractAddress);
6366
console.log("contract " + className + " at " + contract.address);
6467
}
6568
else {
66-
contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
67-
68-
realArgs = [];
69-
for (var l = 0; l < contract.args.length; l++) {
70-
arg = contract.args[l];
71-
if (arg[0] === "$") {
72-
realArgs.push(this.deployedContracts[arg.substr(1)]);
73-
} else {
74-
realArgs.push(arg);
75-
}
69+
var chainContract = this.chainManager.getContract(contract.compiled.code);
70+
71+
if (chainContract != undefined) {
72+
console.log("contract " + className + " is unchanged and already deployed at " + chainContract.address);
7673
}
74+
else {
7775

78-
contractParams = realArgs;
79-
contractParams.push({
80-
from: primaryAddress,
81-
data: contract.compiled.code,
82-
gas: contract.gasLimit,
83-
gasPrice: contract.gasPrice
84-
});
76+
contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
8577

86-
console.log('trying to obtain ' + className + ' address...');
78+
realArgs = [];
79+
for (var l = 0; l < contract.args.length; l++) {
80+
arg = contract.args[l];
81+
if (arg[0] === "$") {
82+
realArgs.push(this.deployedContracts[arg.substr(1)]);
83+
} else {
84+
realArgs.push(arg);
85+
}
86+
}
8787

88-
while((receipt = this.deploy_contract(contractObject, contractParams)) === false) {
89-
console.log("timeout... failed to deploy contract.. retrying...");
90-
}
88+
contractParams = realArgs;
89+
contractParams.push({
90+
from: primaryAddress,
91+
data: contract.compiled.code,
92+
gas: contract.gasLimit,
93+
gasPrice: contract.gasPrice
94+
});
9195

92-
var contractAddress = receipt.contractAddress;
96+
console.log('trying to obtain ' + className + ' address...');
9397

94-
if (web3.eth.getCode(contractAddress) === "0x") {
95-
console.log("=========");
96-
console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working");
97-
console.log("try adjusting your gas values");
98-
console.log("=========");
99-
}
100-
else {
101-
console.log("deployed " + className + " at " + contractAddress);
102-
}
98+
while((receipt = this.deploy_contract(contractObject, contractParams)) === false) {
99+
console.log("timeout... failed to deploy contract.. retrying...");
100+
}
103101

104-
this.deployedContracts[className] = contractAddress;
102+
var contractAddress = receipt.contractAddress;
105103

106-
console.log("deployed " + className + " at " + contractAddress);
104+
if (web3.eth.getCode(contractAddress) === "0x") {
105+
console.log("=========");
106+
console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working");
107+
console.log("try adjusting your gas values");
108+
console.log("=========");
109+
}
110+
else {
111+
console.log("deployed " + className + " at " + contractAddress);
112+
}
113+
114+
this.deployedContracts[className] = contractAddress;
115+
this.chainManager.addContract(className, contract.compiled.code, contractAddress);
116+
this.chainManager.save();
117+
118+
console.log("deployed " + className + " at " + contractAddress);
119+
this.execute_cmds(contract.onDeploy);
120+
}
107121
}
108122
}
109123

110124
};
111125

126+
Deploy.prototype.execute_cmds = function(cmds) {
127+
if (cmds.length === 0) return;
128+
129+
eval(this.generate_abi_file());
130+
for (var i = 0; i < cmds.length; i++) {
131+
var cmd = cmds[i];
132+
133+
for(className in this.deployedContracts) {
134+
var contractAddress = this.deployedContracts[className];
135+
136+
var re = new RegExp("\\$" + className, 'g');
137+
cmd = cmd.replace(re, '"' + contractAddress + '"');
138+
}
139+
140+
console.log("executing: " + cmd);
141+
eval(cmd);
142+
}
143+
}
144+
112145
Deploy.prototype.generate_abi_file = function() {
113146
var result;
114147

lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ var Deploy = require('./deploy.js');
1616
var Release = require('./ipfs.js');
1717
var Config = require('./config/config.js');
1818
var Compiler = require('./compiler.js');
19+
var ChainManager = require('./chain_manager.js');
1920

2021
Embark = {
2122
init: function() {
2223
this.blockchainConfig = (new Config.Blockchain());
2324
this.compiler = (new Compiler(this.blockchainConfig));
2425
this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler));
26+
this.chainManager = (new ChainManager());
2527
},
2628

2729
tests: function(contractFiles) {
@@ -33,9 +35,11 @@ Embark = {
3335
chain.startChain(use_tmp);
3436
},
3537

36-
deployContracts: function(env, contractFiles, destFile) {
38+
deployContracts: function(env, contractFiles, destFile, chainFile) {
3739
this.contractsConfig.init(contractFiles, env);
38-
var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig);
40+
41+
this.chainManager.loadConfigFile(chainFile)
42+
var deploy = new Deploy(env, contractFiles, this.blockchainConfig.config(env), this.contractsConfig, this.chainManager);
3943
deploy.deploy_contracts(env);
4044
return deploy.generate_abi_file(destFile);
4145
},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "embark-framework",
3-
"version": "0.7.3",
3+
"version": "0.8.0",
44
"description": "",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"
@@ -17,6 +17,7 @@
1717
"grunt": "^0.4.5",
1818
"hashmerge": "^1.0.2",
1919
"jasmine": "^2.3.1",
20+
"js-sha3": "^0.3.1",
2021
"meteor-build-client": "^0.1.6",
2122
"methodmissing": "^0.0.3",
2223
"mkdirp": "^0.5.1",

test/chain_manager.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var ChainManager = require('../lib/chain_manager.js');
2+
var Config = require('../lib/config/config.js');
3+
var Blockchain = require('../lib/blockchain.js');
4+
var assert = require('assert');
5+
var fs = require('fs');
6+
7+
describe('embark.chain_manager', function() {
8+
var chainFile = './test/support/chain_manager.json';
9+
fs.writeFileSync(chainFile, '{}');
10+
11+
var chainManager = (new ChainManager()).loadConfigFile(chainFile);
12+
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config('development');
13+
14+
describe('#init', function() {
15+
chainManager.init('development', blockchainConfig);
16+
17+
it('should initialize chain', function() {
18+
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f']
19+
assert.equal(chain != undefined, true);
20+
});
21+
});
22+
23+
describe('#addContract', function() {
24+
25+
it('should register a contract in the chain', function() {
26+
chainManager.addContract("Foo", "123456", "0x123");
27+
28+
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f']
29+
var contract = chain.contracts["d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e"]
30+
31+
assert.equal(contract.name, "Foo");
32+
assert.equal(contract.address, "0x123");
33+
});
34+
35+
});
36+
37+
describe('#getContract', function() {
38+
39+
it('should a contract in the chain', function() {
40+
var contract = chainManager.getContract("123456");
41+
42+
assert.equal(contract.name, "Foo");
43+
assert.equal(contract.address, "0x123");
44+
});
45+
46+
});
47+
48+
describe('#save', function() {
49+
50+
it('should save changes in the chain', function() {
51+
chainManager.save();
52+
53+
var chainFile = './test/support/chain_manager.json';
54+
var content = fs.readFileSync(chainFile).toString();
55+
assert.equal(content, '{"0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f":{"contracts":{"d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e":{"name":"Foo","address":"0x123"}}}}');
56+
});
57+
58+
});
59+
60+
});

0 commit comments

Comments
 (0)