fix(@embark/blockchain): make disabling blockchain feature work

Users can turn of blockchain support if they want to using the blockchain.js
configuration. In practice however, this has never properly worked as several
places in Embark's codebase weren't actually honoring that configuration value.

This commit introduces the necessary changes so that disabling blockchain support will:

No longer generate blockchain related EmbarkJS artifacts
No longer try to deploy Smart Contracts but still compile them
This commit is contained in:
Pascal Precht 2019-12-05 16:44:44 +01:00 committed by Iuri Matias
parent b82a240933
commit 446197baff
9 changed files with 51 additions and 13 deletions

View File

@ -27,13 +27,16 @@ class App extends React.Component {
componentDidMount() {
EmbarkJS.onReady((err) => {
this.setState({blockchainEnabled: true});
if (err) {
// If err is not null then it means something went wrong connecting to ethereum
// you can use this to ask the user to enable metamask for e.g
return this.setState({error: err.message || err});
}
EmbarkJS.Blockchain.isAvailable().then(result => {
this.setState({blockchainEnabled: result});
});
EmbarkJS.Messages.isAvailable().then(result => {
this.setState({whisperEnabled: result});
});

View File

@ -89,6 +89,13 @@ Blockchain.setProvider = function(providerName, options) {
provider.init(options);
};
Blockchain.isAvailable = function () {
if (!this.blockchainConnector) {
return Promise.resolve(false);
}
return this.blockchainConnector.getNetworkId().then(_ => true);
};
Blockchain.doConnect = function(connectionList, opts, doneCb) {
const self = this;

View File

@ -6,7 +6,10 @@ import Utils from './utils';
var EmbarkJS = {
onReady: function (cb) {
Blockchain.execWhenReady(cb);
if (Blockchain.blockchainConnector) {
return Blockchain.execWhenReady(cb);
}
cb();
},
enableEthereum: function () {
return Blockchain.enableEthereum();

View File

@ -132,6 +132,9 @@ class ENS {
}
async addArtifactFile(_params, cb) {
if (!this.embark.config.blockchainConfig.enabled) {
return cb();
}
this.getEnsConfig((err, config) => {
this.events.request("pipeline:register", {
path: [this.config.embarkConfig.generationDir, 'config'],
@ -143,20 +146,29 @@ class ENS {
}
async setProviderAndRegisterDomains(cb = (() => {})) {
if (!this.embark.config.blockchainConfig.enabled) {
return cb();
}
this.getEnsConfig(async (err, config) => {
if (err) {
return cb(err);
}
if (this.doSetENSProvider) {
this.setupEmbarkJS(config);
}
const web3 = await this.web3;
const networkId = await web3.eth.net.getId();
const isKnownNetwork = Boolean(ensContractAddresses[networkId]);
const shouldRegisterSubdomain = this.config.namesystemConfig.register && this.config.namesystemConfig.register.subdomains && Object.keys(this.config.namesystemConfig.register.subdomains).length;
if (isKnownNetwork || !shouldRegisterSubdomain) {
return cb();
try {
const web3 = await this.web3;
const networkId = await web3.eth.net.getId();
const isKnownNetwork = Boolean(ensContractAddresses[networkId]);
const shouldRegisterSubdomain = this.config.namesystemConfig.register && this.config.namesystemConfig.register.subdomains && Object.keys(this.config.namesystemConfig.register.subdomains).length;
if (isKnownNetwork || !shouldRegisterSubdomain) {
return cb();
}
this.registerConfigDomains(config, cb);
} catch (e) {
cb(e);
}
this.registerConfigDomains(config, cb);
});
}

View File

@ -16,7 +16,9 @@ class EmbarkWeb3 {
this.events = embark.events;
this.config = embark.config;
this.setupEmbarkJS();
if (this.config.blockchainConfig.enabled) {
this.setupEmbarkJS();
}
embark.registerActionForEvent("deployment:contract:deployed", {priority: 40}, this.registerInVm.bind(this));
embark.registerActionForEvent("deployment:contract:undeployed", this.registerInVm.bind(this));

View File

@ -43,6 +43,10 @@ class Blockchain {
});
this.events.setCommandHandler("blockchain:node:start", (blockchainConfig, cb) => {
if (!blockchainConfig.enabled) {
return cb();
}
const clientName = blockchainConfig.client;
const started = () => {
this.startedClient = clientName;
@ -108,6 +112,9 @@ class Blockchain {
}
addArtifactFile(_params, cb) {
if (!this.blockchainConfig.enabled) {
cb();
}
this.events.request("config:contractsConfig", (_err, contractsConfig) => {
async.map(contractsConfig.dappConnection, (conn, mapCb) => {
if (conn === '$EMBARK') {
@ -120,7 +127,7 @@ class Blockchain {
this.logger.error(__('Error getting dapp connection'));
return cb(err);
}
let config = {
const config = {
provider: contractsConfig.library || 'web3',
dappConnection: results,
dappAutoEnable: contractsConfig.dappAutoEnable,

View File

@ -19,6 +19,9 @@ class Deployment {
});
this.events.setCommandHandler('deployment:contracts:deploy', (contractsList, contractDependencies, cb) => {
if (!this.blockchainConfig.enabled) {
return cb();
}
this.deployContracts(contractsList, contractDependencies, cb);
});
}

View File

@ -12,6 +12,7 @@ class EmbarkJS {
constructor(embark, _options) {
this.embark = embark;
this.embarkConfig = embark.config.embarkConfig;
this.blockchainConfig = embark.config.blockchainConfig;
this.events = embark.events;
this.logger = embark.logger;
this.contractArtifacts = {};

View File

@ -24,7 +24,7 @@ class Pipeline {
generateAll(cb) {
async.waterfall([
(next) => {
this.plugins.runActionsForEvent("pipeline:generateAll:before", {}, (err) => {
this.plugins.runActionsForEvent("pipeline:generateAll:before", (err) => {
next(err);
});
},