refactor blockchain lib + tests
This commit is contained in:
parent
1a5ddea9a8
commit
e43c0a38d6
|
@ -1,12 +1,13 @@
|
|||
var Deploy;
|
||||
var Config = require('./config/config.js');
|
||||
|
||||
startChain = function(env) {
|
||||
config = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
|
||||
Blockchain = function(blockchainConfig) {
|
||||
this.config = blockchainConfig;
|
||||
}
|
||||
|
||||
address = config.account.address;
|
||||
Blockchain.prototype.generate_basic_command = function() {
|
||||
var config = this.config;
|
||||
var address = config.account.address;
|
||||
|
||||
cmd = "geth ";
|
||||
var cmd = "geth ";
|
||||
|
||||
if (config.datadir !== "default") {
|
||||
cmd += "--datadir=\"" + config.datadir + "\" ";
|
||||
|
@ -32,21 +33,20 @@ startChain = function(env) {
|
|||
cmd += "--password " + config.account.password + " ";
|
||||
}
|
||||
|
||||
if (config.account.init) {
|
||||
console.log("=== initializating account");
|
||||
console.log("running: " + cmd + " account list");
|
||||
result = exec(cmd + "account list");
|
||||
console.log("finished");
|
||||
console.log("=== output is " + result.output);
|
||||
if (result.output.indexOf("Fatal") < 0) {
|
||||
console.log("=== already initialized");
|
||||
address = result.output.match(/{(\w+)}/)[1];
|
||||
} else {
|
||||
console.log("running: " + cmd + " account new");
|
||||
output = exec(cmd + " account new");
|
||||
address = output.output.match(/{(\w+)}/)[1];
|
||||
}
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Blockchain.prototype.list_command = function() {
|
||||
return this.generate_basic_command() + "account list ";
|
||||
}
|
||||
|
||||
Blockchain.prototype.init_command = function() {
|
||||
return this.generate_basic_command() + "account new ";
|
||||
}
|
||||
|
||||
Blockchain.prototype.run_command = function(address) {
|
||||
var cmd = this.generate_basic_command();
|
||||
var config = this.config;
|
||||
|
||||
if (address !== void 0) {
|
||||
cmd += "--unlock " + address + " ";
|
||||
|
@ -60,15 +60,34 @@ startChain = function(env) {
|
|||
cmd += "js node_modules/embark-framework/js/mine.js";
|
||||
}
|
||||
|
||||
console.log("running: " + cmd);
|
||||
|
||||
console.log("=== running geth");
|
||||
|
||||
exec(cmd);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Blockchain = {
|
||||
startChain: startChain
|
||||
Blockchain.prototype.get_address = function() {
|
||||
var config = this.config;
|
||||
var address = null;
|
||||
|
||||
if (config.account.init) {
|
||||
console.log("running: " + this.list_command());
|
||||
result = exec(this.list_command());
|
||||
|
||||
if (result.output.indexOf("Fatal") < 0) {
|
||||
console.log("=== already initialized");
|
||||
address = result.output.match(/{(\w+)}/)[1];
|
||||
} else {
|
||||
console.log("running: " + this.init_command());
|
||||
result = exec(this.init_command());
|
||||
address = output.output.match(/{(\w+)}/)[1];
|
||||
}
|
||||
}
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
Blockchain.prototype.startChain = function() {
|
||||
var address = this.get_address();
|
||||
console.log("running: " + this.run_command(address));
|
||||
exec(this.run_command(address));
|
||||
}
|
||||
|
||||
module.exports = Blockchain
|
||||
|
|
|
@ -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\" --logfile=\"/tmp/embark.log\" --port 30303 --rpc --rpcport 8101 --networkid "+blockchainConfig.networkId+" --rpccorsdomain \"*\" --minerthreads \"1\" --mine --maxpeers 0 --password config/password ");
|
||||
});
|
||||
});
|
||||
|
||||
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 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 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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue