fix ens test and race condition in setting ENS provider

This commit is contained in:
Jonathan Rainville 2019-07-12 15:40:46 -04:00
parent 5d5c962ed2
commit 783b555dd2
4 changed files with 51 additions and 32 deletions

View File

@ -28,10 +28,7 @@ class CodeGenerator {
this.rpcHost = this.blockchainConfig.rpcHost || ''; this.rpcHost = this.blockchainConfig.rpcHost || '';
this.rpcPort = this.blockchainConfig.rpcPort || ''; this.rpcPort = this.blockchainConfig.rpcPort || '';
this.contractsConfig = embark.config.contractsConfig || {}; this.contractsConfig = embark.config.contractsConfig || {};
this.storageConfig = embark.config.storageConfig || {}; this.config = embark.config;
this.communicationConfig = embark.config.communicationConfig || {};
this.namesystemConfig = embark.config.namesystemConfig || {};
this.webServerConfig = embark.config.webServerConfig || {};
this.env = options.env || 'development'; this.env = options.env || 'development';
this.plugins = options.plugins; this.plugins = options.plugins;
this.events = embark.events; this.events = embark.events;
@ -240,31 +237,31 @@ class CodeGenerator {
} }
generateNamesInitialization(useEmbarkJS) { generateNamesInitialization(useEmbarkJS) {
if (!useEmbarkJS || this.namesystemConfig === {}) return ""; if (!useEmbarkJS || this.config.namesystemConfig === {}) return "";
let result = "\n"; let result = "\n";
result += Templates.define_when_env_loaded(); result += Templates.define_when_env_loaded();
result += this._getInitCode('names', this.namesystemConfig); result += this._getInitCode('names', this.config.namesystemConfig);
return result; return result;
} }
generateStorageInitialization(useEmbarkJS) { generateStorageInitialization(useEmbarkJS) {
if (!useEmbarkJS || this.storageConfig === {}) return ""; if (!useEmbarkJS || this.config.storageConfig === {}) return "";
let result = "\n"; let result = "\n";
result += Templates.define_when_env_loaded(); result += Templates.define_when_env_loaded();
result += this._getInitCode('storage', this.storageConfig); result += this._getInitCode('storage', this.config.storageConfig);
return result; return result;
} }
generateCommunicationInitialization(useEmbarkJS) { generateCommunicationInitialization(useEmbarkJS) {
if (!useEmbarkJS || this.communicationConfig === {}) return ""; if (!useEmbarkJS || this.config.communicationConfig === {}) return "";
let result = "\n"; let result = "\n";
result += Templates.define_when_env_loaded(); result += Templates.define_when_env_loaded();
result += this._getInitCode('communication', this.communicationConfig); result += this._getInitCode('communication', this.config.communicationConfig);
return result; return result;
} }
@ -438,10 +435,10 @@ class CodeGenerator {
getInitProviderCode() { getInitProviderCode() {
const codeTypes = { const codeTypes = {
blockchain: this.blockchainConfig || {}, blockchain: this.config.blockchainConfig || {},
communication: this.communicationConfig || {}, communication: this.config.communicationConfig || {},
names: this.namesystemConfig || {}, names: this.config.namesystemConfig || {},
storage: this.storageConfig || {} storage: this.config.storageConfig || {}
}; };
return this.plugins.getPluginsFor("initConsoleCode").reduce((acc, plugin) => { return this.plugins.getPluginsFor("initConsoleCode").reduce((acc, plugin) => {

View File

@ -73,8 +73,7 @@ class ENS {
this.ensConfig = ensConfig; this.ensConfig = ensConfig;
this.configured = false; this.configured = false;
this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, dappArtifacts.symlinkDir); this.modulesPath = dappPath(embark.config.embarkConfig.generationDir, dappArtifacts.symlinkDir);
this.consoleCmdsRegistered = false; this.initated = false;
this.eventsRegistered = false;
this.events.setCommandHandler("ens:resolve", this.ensResolve.bind(this)); this.events.setCommandHandler("ens:resolve", this.ensResolve.bind(this));
this.events.setCommandHandler("ens:isENSName", this.isENSName.bind(this)); this.events.setCommandHandler("ens:isENSName", this.isENSName.bind(this));
@ -88,7 +87,7 @@ class ENS {
} }
init(cb = () => {}) { init(cb = () => {}) {
if (this.config.namesystemConfig === {} || if (this.initated || this.config.namesystemConfig === {} ||
this.config.namesystemConfig.enabled !== true || this.config.namesystemConfig.enabled !== true ||
!this.config.namesystemConfig.available_providers || !this.config.namesystemConfig.available_providers ||
this.config.namesystemConfig.available_providers.indexOf('ens') < 0) { this.config.namesystemConfig.available_providers.indexOf('ens') < 0) {
@ -100,6 +99,7 @@ class ENS {
this.registerEvents(); this.registerEvents();
this.registerConsoleCommands(); this.registerConsoleCommands();
this.addENSToEmbarkJS(cb); this.addENSToEmbarkJS(cb);
this.initated = true;
} }
reset() { reset() {
@ -107,10 +107,6 @@ class ENS {
} }
registerConsoleCommands() { registerConsoleCommands() {
if (this.consoleCmdsRegistered) {
return;
}
this.consoleCmdsRegistered = true;
this.embark.registerConsoleCommand({ this.embark.registerConsoleCommand({
usage: 'resolve [name]', usage: 'resolve [name]',
description: __('Resolves an ENS name'), description: __('Resolves an ENS name'),
@ -153,10 +149,6 @@ class ENS {
} }
registerEvents() { registerEvents() {
if (this.eventsRegistered) {
return;
}
this.eventsRegistered = true;
this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this)); this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this));
this.events.on('blockchain:reseted', this.reset.bind(this)); this.events.on('blockchain:reseted', this.reset.bind(this));
this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this)); this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this));
@ -444,6 +436,7 @@ class ENS {
return (namesConfig.provider === 'ens' && namesConfig.enabled === true); return (namesConfig.provider === 'ens' && namesConfig.enabled === true);
}; };
console.log('SETTING ENS');
// TODO This stacks the setProviders making it so that we call it multiple times // TODO This stacks the setProviders making it so that we call it multiple times
this.embark.addProviderInit('names', code, shouldInit); this.embark.addProviderInit('names', code, shouldInit);
this.embark.addConsoleProviderInit('names', code, shouldInit); this.embark.addConsoleProviderInit('names', code, shouldInit);

View File

@ -32,6 +32,7 @@ class Test {
storage: {}, storage: {},
communication: {} communication: {}
}; };
this.needToRestetEmbarkJS = false;
this.events.setCommandHandler("blockchain:provider:contract:accounts:get", cb => { this.events.setCommandHandler("blockchain:provider:contract:accounts:get", cb => {
this.events.request("blockchain:getAccounts", cb); this.events.request("blockchain:getAccounts", cb);
@ -183,6 +184,7 @@ class Test {
options[moduleName] = options[moduleName] || {}; options[moduleName] = options[moduleName] || {};
if (!deepEqual(options[moduleName], this.moduleConfigs[moduleName])) { if (!deepEqual(options[moduleName], this.moduleConfigs[moduleName])) {
this.moduleConfigs[moduleName] = options[moduleName]; this.moduleConfigs[moduleName] = options[moduleName];
this.needToRestetEmbarkJS = true;
restartModules.push((paraCb) => { restartModules.push((paraCb) => {
self.events.request(`config:${moduleName}Config:set`, recursiveMerge({}, self.originalConfigObj[`${moduleName}Config`], options[moduleName]), () => { self.events.request(`config:${moduleName}Config:set`, recursiveMerge({}, self.originalConfigObj[`${moduleName}Config`], options[moduleName]), () => {
self.events.request(`module:${moduleName}:reset`, paraCb); self.events.request(`module:${moduleName}:reset`, paraCb);
@ -199,6 +201,7 @@ class Test {
config(options, callback) { config(options, callback) {
const self = this; const self = this;
self.needConfig = false; self.needConfig = false;
self.needToRestetEmbarkJS = false;
if (typeof (options) === 'function') { if (typeof (options) === 'function') {
callback = options; callback = options;
options = {}; options = {};
@ -279,6 +282,14 @@ class Test {
self.error = false; self.error = false;
next(null, accounts); next(null, accounts);
}); });
},
function checkIfNeedToResetEmbarkJS(accounts, next) {
if (!self.needToRestetEmbarkJS) {
return next(null, accounts);
}
self.events.request("runcode:embarkjs:reset", (err) => {
next(err, accounts);
});
} }
], (err, accounts) => { ], (err, accounts) => {
if (err) { if (err) {

View File

@ -158,7 +158,8 @@ __embarkENS.setProvider = function(config) {
const ERROR_MESSAGE = 'ENS is not available in this chain'; const ERROR_MESSAGE = 'ENS is not available in this chain';
self.registration = config.registration; self.registration = config.registration;
self.env = config.env; self.env = config.env;
// FIXME EmbarkJS.onReady odesn't work. Possibility of a race condition self.ready = false;
// FIXME EmbarkJS.onReady doesn't work. Possibility of a race condition
EmbarkJS.Blockchain.blockchainConnector.getNetworkId() EmbarkJS.Blockchain.blockchainConnector.getNetworkId()
.then((id) => { .then((id) => {
const registryAddress = self.registryAddresses[id] || config.registryAddress; const registryAddress = self.registryAddresses[id] || config.registryAddress;
@ -178,8 +179,10 @@ __embarkENS.setProvider = function(config) {
address: config.resolverAddress, address: config.resolverAddress,
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance() web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
}); });
self.ready = true;
}) })
.catch(err => { .catch(err => {
self.ready = true;
if (err.message.indexOf('Provider not set or invalid') > -1) { if (err.message.indexOf('Provider not set or invalid') > -1) {
console.warn(ERROR_MESSAGE); console.warn(ERROR_MESSAGE);
return; return;
@ -188,11 +191,28 @@ __embarkENS.setProvider = function(config) {
}); });
}; };
__embarkENS.waitForProviderReady = function() {
return new Promise((resolve, reject) => {
const self = this;
function checkReady() {
if (self.ready === undefined) {
return reject(providerNotSetError);
}
if (self.ready) {
if (!self.ens) {
return reject(providerNotSetError);
}
return resolve();
}
setTimeout(checkReady, 100);
}
checkReady();
});
};
__embarkENS.resolve = function (name, callback) { __embarkENS.resolve = function (name, callback) {
const resolve = async (name) => { const resolve = async (name) => {
if (!this.ens) { await this.waitForProviderReady();
throw new Error(providerNotSetError);
}
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) { if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
throw new Error(defaultAccountNotSetError); throw new Error(defaultAccountNotSetError);
} }
@ -230,9 +250,7 @@ __embarkENS.resolve = function (name, callback) {
__embarkENS.lookup = function (address, callback) { __embarkENS.lookup = function (address, callback) {
const lookup = async (address) => { const lookup = async (address) => {
if (!this.ens) { await this.waitForProviderReady();
throw new Error(providerNotSetError);
}
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) { if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
throw new Error(defaultAccountNotSetError); throw new Error(defaultAccountNotSetError);
} }