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

View File

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

View File

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

View File

@ -158,7 +158,8 @@ __embarkENS.setProvider = function(config) {
const ERROR_MESSAGE = 'ENS is not available in this chain';
self.registration = config.registration;
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()
.then((id) => {
const registryAddress = self.registryAddresses[id] || config.registryAddress;
@ -178,8 +179,10 @@ __embarkENS.setProvider = function(config) {
address: config.resolverAddress,
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
});
self.ready = true;
})
.catch(err => {
self.ready = true;
if (err.message.indexOf('Provider not set or invalid') > -1) {
console.warn(ERROR_MESSAGE);
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) {
const resolve = async (name) => {
if (!this.ens) {
throw new Error(providerNotSetError);
}
await this.waitForProviderReady();
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
throw new Error(defaultAccountNotSetError);
}
@ -230,9 +250,7 @@ __embarkENS.resolve = function (name, callback) {
__embarkENS.lookup = function (address, callback) {
const lookup = async (address) => {
if (!this.ens) {
throw new Error(providerNotSetError);
}
await this.waitForProviderReady();
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
throw new Error(defaultAccountNotSetError);
}