move deploy tracker into a module

This commit is contained in:
Iuri Matias 2018-05-18 22:40:47 -04:00 committed by Jonathan Rainville
parent 78f64945ab
commit ba29f0e928
7 changed files with 72 additions and 51 deletions

View File

@ -4,7 +4,6 @@ let utils = require('../utils/utils.js');
let RunCode = require('../core/runCode.js');
let DeployTracker = require('./deploy_tracker.js');
let CodeGenerator = require('./code_generator.js');
class Deploy {
@ -24,12 +23,6 @@ class Deploy {
});
}
initTracker(cb) {
this.deployTracker = new DeployTracker({
logger: this.logger, chainConfig: this.chainConfig, env: this.env, events: this.events
}, cb);
}
determineArguments(suppliedArgs, contract) {
let realArgs = [], l, arg, contractName, referedContract;
@ -76,7 +69,6 @@ class Deploy {
checkAndDeployContract(contract, params, callback) {
let self = this;
let realArgs;
contract.error = false;
if (contract.deploy === false) {
@ -84,7 +76,7 @@ class Deploy {
return callback();
}
realArgs = self.determineArguments(params || contract.args, contract);
contract.realArgs = self.determineArguments(params || contract.args, contract);
if (contract.address !== undefined) {
try {
@ -98,29 +90,28 @@ class Deploy {
}
contract.deployedAddress = contract.address;
self.logger.info(contract.className.bold.cyan + __(" already deployed at ").green + contract.address.bold.cyan);
if (this.deployTracker) {
self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, contract.address);
self.deployTracker.save();
}
self.events.emit("deploy:contract:deployed", contract);
self.events.emit('contractsState', self.contractsManager.contractsState());
return callback();
}
if (!this.deployTracker) {
return self.contractToDeploy(contract, params, callback);
}
//if (!this.deployTracker) {
// return self.contractToDeploy(contract, params, callback);
//}
let trackedContract = self.deployTracker.getContract(contract.className, contract.realRuntimeBytecode, realArgs);
if (!trackedContract) {
// TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract
self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) {
if (!trackedContract) {
return self.contractToDeploy(contract, params, callback);
}
this.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) {
if (codeInChain !== "0x") {
self.contractAlreadyDeployed(contract, trackedContract, callback);
} else {
self.contractToDeploy(contract, params, callback);
}
self.blockchain.getCode(trackedContract.address, function(_getCodeErr, codeInChain) {
if (codeInChain !== "0x") {
self.contractAlreadyDeployed(contract, trackedContract, callback);
} else {
self.contractToDeploy(contract, params, callback);
}
});
});
}
@ -140,14 +131,13 @@ class Deploy {
contractToDeploy(contract, params, callback) {
const self = this;
let realArgs = self.determineArguments(params || contract.args, contract);
contract.realArgs = self.determineArguments(params || contract.args, contract);
this.deployContract(contract, realArgs, function (err, address) {
this.deployContract(contract, contract.realArgs, function (err, address) {
if (err) {
return callback(new Error(err));
}
self.deployTracker.trackContract(contract.className, contract.realRuntimeBytecode, realArgs, address);
self.deployTracker.save();
self.events.emit("deploy:contract:deployed", contract);
self.events.emit('contractsState', self.contractsManager.contractsState());
// always run contractCode so other functionality like 'afterDeploy' can also work
@ -357,6 +347,7 @@ class Deploy {
let self = this;
this.logger.info(__("deploying contracts"));
let contracts = this.contractsManager.listContracts();
this.events.emit("deploy:beforeAll");
async.eachOfSeries(contracts,
function (contract, key, callback) {

View File

@ -86,16 +86,14 @@ class DeployManager {
gasLimit: self.gasLimit
});
deploy.initTracker(function() {
deploy.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', contractsManager);
}
if (err && self.fatalErrors) {
return callback(err);
}
callback(null, contractsManager, web3);
});
deploy.deployAll(function (err) {
if (!err) {
self.events.emit('contractsDeployed', contractsManager);
}
if (err && self.fatalErrors) {
return callback(err);
}
callback(null, contractsManager, web3);
});
},
function runAfterDeployCommands(contractsManager, web3, callback) {

View File

@ -38,7 +38,7 @@ Config.prototype.loadConfigFiles = function(options) {
this.embarkConfig = fs.readJSONSync(options.embarkConfig);
this.embarkConfig.plugins = this.embarkConfig.plugins || {};
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context});
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context, env: this.env});
this.plugins.loadPlugins();
this.loadEmbarkConfigFile();

View File

@ -203,6 +203,9 @@ class Engine {
logger: this.logger
});
this.registerModule('deploytracker', {
});
this.contractsManager = new ContractsManager({
contractFiles: this.config.contractsFiles,
contractsConfig: this.config.contractsConfig,

View File

@ -28,6 +28,7 @@ var Plugin = function(options) {
this.logger = options.logger;
this.events = options.events;
this.config = options.config;
this.env = options.env;
this.loaded = false;
this.currentContext = options.context;
this.acceptedContext = options.pluginConfig.context || [constants.contexts.any];

View File

@ -10,6 +10,7 @@ var Plugins = function(options) {
this.events = options.events;
this.config = options.config;
this.context = options.context;
this.env = options.env;
};
Plugins.prototype.loadPlugins = function() {
@ -64,7 +65,8 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
events: this.events,
config: this.config,
isInternal: true,
context: this.context
context: this.context,
env: this.env
});
pluginWrapper.loadInternalPlugin();
this.plugins.push(pluginWrapper);

View File

@ -1,17 +1,42 @@
let fs = require('../core/fs.js');
let utils = require('../utils/utils.js');
let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js');
class DeployTracker {
constructor(options, cb) {
const self = this;
this.logger = options.logger;
this.events = options.events;
this.env = options.env;
this.chainConfig = options.chainConfig;
constructor(embark, options) {
this.logger = embark.logger;
this.events = embark.events;
// TODO: unclear where it comes from
this.env = options.env;
//this.chainConfig = options.chainConfig;
this.chainConfig = embark.config.chainTracker;
this.registerEvents();
}
registerEvents() {
const self = this;
this.events.on("deploy:beforeAll", this.setCurrentChain.bind(this));
this.events.on("deploy:contract:deployed", (contract) => {
self.trackContract(contract.className, contract.realRuntimeBytecode, contract.realArgs, contract.address);
self.save();
});
this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => {
let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs);
cb(trackedContract);
});
}
// TODO: just an event might not be enought to the async nature
// it needs to be a plugin api before deploy, that makes the deployment wait
setCurrentChain() {
const self = this;
if (this.chainConfig === false) {
this.currentChain = {contracts: []};
return cb();
//return cb();
}
this.events.request("blockchain:block:byNumber", 0, function(_err, block) {
@ -24,7 +49,7 @@ class DeployTracker {
self.currentChain = self.chainConfig[chainId];
self.currentChain.name = self.env;
cb();
//cb();
});
}
@ -56,6 +81,7 @@ class DeployTracker {
}
fs.writeJSONSync("./chains.json", this.chainConfig, {spaces: 2});
}
}
module.exports = DeployTracker;