refactor: start moving contracts code to its own file + specs

This commit is contained in:
Iuri Matias 2015-06-28 16:11:42 -04:00
parent e9c34168a2
commit eac991971a
10 changed files with 169 additions and 35 deletions

View File

@ -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) {

View File

@ -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

59
lib/config/contracts.js Normal file
View File

@ -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

View File

@ -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) {

View File

@ -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)

View File

@ -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"
}
}

61
test/config.contracts.js Normal file
View File

@ -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" ]);
});
});
});
});

View File

@ -0,0 +1,2 @@
development:
staging:

View File

@ -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;
}
}

View File

@ -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;
}
}