Merge pull request #606 from embark-framework/bug_fix/events-dont-wrok

Fix events usage with websocket providers
This commit is contained in:
Iuri Matias 2018-07-07 00:17:54 +03:00 committed by GitHub
commit d40ae2f374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 81 additions and 1042 deletions

View File

@ -34,7 +34,6 @@ EmbarkJS.Contract = function(options) {
if (EmbarkJS.isNewWeb3(this.web3)) { if (EmbarkJS.isNewWeb3(this.web3)) {
ContractClass = new this.web3.eth.Contract(this.abi, this.address); ContractClass = new this.web3.eth.Contract(this.abi, this.address);
ContractClass.setProvider(this.web3.currentProvider);
ContractClass.options.data = this.code; ContractClass.options.data = this.code;
ContractClass.options.from = this.from || this.web3.eth.defaultAccount; ContractClass.options.from = this.from || this.web3.eth.defaultAccount;
ContractClass.abi = ContractClass.options.abi; ContractClass.abi = ContractClass.options.abi;

View File

@ -124,6 +124,10 @@ class Cmd {
.description(__('run dapp (default: %s)', 'development')) .description(__('run dapp (default: %s)', 'development'))
.action(function (env, options) { .action(function (env, options) {
i18n.setOrDetectLocale(options.locale); i18n.setOrDetectLocale(options.locale);
embark.initConfig(env || 'development', {
embarkConfig: 'embark.json',
interceptLogs: false
});
embark.run({ embark.run({
env: env || 'development', env: env || 'development',
serverPort: options.port, serverPort: options.port,

View File

@ -46,6 +46,7 @@ var Blockchain = function(options) {
vmdebug: this.blockchainConfig.vmdebug || false, vmdebug: this.blockchainConfig.vmdebug || false,
targetGasLimit: this.blockchainConfig.targetGasLimit || false, targetGasLimit: this.blockchainConfig.targetGasLimit || false,
syncMode: this.blockchainConfig.syncMode, syncMode: this.blockchainConfig.syncMode,
syncmode: this.blockchainConfig.syncmode,
verbosity: this.blockchainConfig.verbosity verbosity: this.blockchainConfig.verbosity
}; };

View File

@ -20,8 +20,8 @@ class GethCommands {
cmd.push(`--datadir=${config.datadir}`); cmd.push(`--datadir=${config.datadir}`);
} }
if (config.syncMode) { if (config.syncmode || config.syncMode) {
cmd.push("--syncmode=" + config.syncMode); cmd.push("--syncmode=" + config.syncmode || config.syncMode);
} }
if (config.account && config.account.password) { if (config.account && config.account.password) {

View File

@ -1,12 +1,7 @@
const ProviderEngine = require('embark-web3-provider-engine');
const RpcSubprovider = require('embark-web3-provider-engine/subproviders/rpc');
const WsSubprovider = require('embark-web3-provider-engine/subproviders/websocket');
const async = require('async'); const async = require('async');
const AccountParser = require('./accountParser'); const AccountParser = require('./accountParser');
const fundAccount = require('./fundAccount'); const fundAccount = require('./fundAccount');
const NO_ACCOUNTS = 'noAccounts';
class Provider { class Provider {
constructor(options) { constructor(options) {
this.web3 = options.web3; this.web3 = options.web3;
@ -16,62 +11,58 @@ class Provider {
this.web3Endpoint = options.web3Endpoint; this.web3Endpoint = options.web3Endpoint;
this.logger = options.logger; this.logger = options.logger;
this.isDev = options.isDev; this.isDev = options.isDev;
this.engine = new ProviderEngine();
this.asyncMethods = {};
} }
startWeb3Provider(callback) { startWeb3Provider(callback) {
const self = this; const self = this;
if (this.type === 'rpc') { if (this.type === 'rpc') {
self.engine.addProvider(new RpcSubprovider({ self.provider = new this.web3.providers.HttpProvider(self.web3Endpoint);
rpcUrl: self.web3Endpoint
}));
} else if (this.type === 'ws') { } else if (this.type === 'ws') {
self.engine.addProvider(new WsSubprovider({ self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "embark"}});
rpcUrl: self.web3Endpoint,
origin: this.blockchainConfig.wsOrigins.split(',')[0]
}));
} else { } else {
return callback(__("contracts config error: unknown deployment type %s", this.type)); return callback(__("contracts config error: unknown deployment type %s", this.type));
} }
self.web3.setProvider(self.provider);
// network connectivity error
self.engine.on('error', (err) => {
// report connectivity errors as trace due to polling
self.logger.trace('web3 provider error: ', err);
self.logger.trace('stopping web3 provider due to error');
// prevent continuous polling errors
self.engine.stop();
});
self.engine.start();
self.web3.setProvider(self);
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger); self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
self.addresses = []; self.addresses = [];
async.waterfall([
function populateWeb3Wallet(next) { if (!self.accounts.length) {
if (!self.accounts.length) { return callback();
return next(NO_ACCOUNTS); }
} self.accounts.forEach(account => {
self.accounts.forEach(account => { self.addresses.push(account.address);
self.addresses.push(account.address); self.web3.eth.accounts.wallet.add(account);
self.web3.eth.accounts.wallet.add(account);
});
self.asyncMethods = {
eth_accounts: self.eth_accounts.bind(self)
};
next();
}
], function (err) {
if (err && err !== NO_ACCOUNTS) {
self.logger.error((err));
}
callback();
}); });
self.web3.eth.defaultAccount = self.addresses[0];
const realSend = self.provider.send.bind(self.provider);
self.provider.send = function (payload, cb) {
if (payload.method === 'eth_accounts') {
return realSend(payload, function (err, result) {
if (err) {
return cb(err);
}
result.result = result.result.concat(self.addresses);
cb(null, result);
});
}
realSend(payload, cb);
};
callback();
}
stop() {
if (this.provider && this.provider.removeAllListeners) {
this.provider.removeAllListeners('connect');
this.provider.removeAllListeners('error');
this.provider.removeAllListeners('end');
this.provider.removeAllListeners('data');
this.provider.responseCallbacks = {};
this.provider = null;
}
} }
fundAccounts(callback) { fundAccounts(callback) {
@ -86,32 +77,6 @@ class Provider {
fundAccount(self.web3, account.address, account.hexBalance, eachCb); fundAccount(self.web3, account.address, account.hexBalance, eachCb);
}, callback); }, callback);
} }
stop() {
this.engine.stop();
}
eth_accounts(payload, cb) {
return cb(null, this.addresses);
}
sendAsync(payload, callback) {
let method = this.asyncMethods[payload.method];
if (method) {
return method.call(method, payload, (err, result) => {
if (err) {
return callback(err);
}
let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result};
callback(null, response);
});
}
this.engine.sendAsync.apply(this.engine, arguments);
}
send() {
return this.engine.send.apply(this.engine, arguments);
}
} }
module.exports = Provider; module.exports = Provider;

View File

@ -48,7 +48,7 @@ class Test {
this.provider.stop(); this.provider.stop();
} }
if (this.simOptions.host) { if (this.simOptions.host) {
let {host, port, type, protocol, accounts} = this.simOptions; let {host, port, type, protocol, accounts} = this.simOptions;
if (!protocol) { if (!protocol) {
protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws'; protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws';
} }
@ -63,18 +63,23 @@ class Test {
web3Endpoint: endpoint web3Endpoint: endpoint
}; };
console.info(`Connecting to node at ${endpoint}`.cyan); console.info(`Connecting to node at ${endpoint}`.cyan);
return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => { return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => {
if (err) { if (err) {
console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red); console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red);
return callback(err); return callback(err);
} }
this.provider = new Provider(providerOptions); this.provider = new Provider(providerOptions);
return this.provider.startWeb3Provider(callback); this.provider.startWeb3Provider(callback);
}); });
} }
if (this.simOptions.accounts) { if (this.simOptions.accounts) {
this.simOptions.accounts = this.simOptions.accounts.map((account) => { this.simOptions.accounts = this.simOptions.accounts.map((account) => {
if (!account.hexBalance) {
account.hexBalance = '0x8AC7230489E80000'; // 10 ether
}
return {balance: account.hexBalance, secretKey: account.privateKey}; return {balance: account.hexBalance, secretKey: account.privateKey};
}); });
} }
@ -182,7 +187,11 @@ class Test {
if (options.deployment.type !== 'rpc' && options.deployment.type !== 'ws') { if (options.deployment.type !== 'rpc' && options.deployment.type !== 'ws') {
callback(__("contracts config error: unknown deployment type %s", options.deployment.type)); callback(__("contracts config error: unknown deployment type %s", options.deployment.type));
} }
Object.assign(self.simOptions, {host: options.deployment.host, port: options.deployment.port, type: options.deployment.type}); Object.assign(self.simOptions, {
host: options.deployment.host,
port: options.deployment.port,
type: options.deployment.type
});
resetServices = true; resetServices = true;
} }
if (!resetServices) { if (!resetServices) {
@ -268,7 +277,9 @@ class Test {
console.warn("Warning: default account has no funds"); console.warn("Warning: default account has no funds");
} }
next(null, accounts); next(null, accounts);
}).catch((err) => { next(err); }); }).catch((err) => {
next(err);
});
}, },
function deploy(accounts, next) { function deploy(accounts, next) {
self.engine.deployManager.gasLimit = 6000000; self.engine.deployManager.gasLimit = 6000000;
@ -289,7 +300,13 @@ class Test {
} else { } else {
data = self.contracts[contractName].options.data; data = self.contracts[contractName].options.data;
} }
Object.assign(self.contracts[contractName], new EmbarkJS.Contract({abi: contract.abiDefinition, address: contract.deployedAddress, from: self.web3.eth.defaultAccount, gas: 6000000, web3: self.web3})); Object.assign(self.contracts[contractName], new EmbarkJS.Contract({
abi: contract.abiDefinition,
address: contract.deployedAddress,
from: self.web3.eth.defaultAccount,
gas: 6000000,
web3: self.web3
}));
self.contracts[contractName].address = contract.deployedAddress; self.contracts[contractName].address = contract.deployedAddress;
if (self.contracts[contractName].options) { if (self.contracts[contractName].options) {
@ -337,7 +354,13 @@ class Test {
contract = this.engine.contractsManager.contracts[contractNames[0]]; contract = this.engine.contractsManager.contracts[contractNames[0]];
} }
} }
this.contracts[contractName] = new EmbarkJS.Contract({abi: contract.abiDefinition, address: contract.address, from: this.web3.eth.defaultAccount, gas: 6000000, web3: this.web3}); this.contracts[contractName] = new EmbarkJS.Contract({
abi: contract.abiDefinition,
address: contract.address,
from: this.web3.eth.defaultAccount,
gas: 6000000,
web3: this.web3
});
this.contracts[contractName].address = contract.address; this.contracts[contractName].address = contract.address;
this.contracts[contractName].options.data = contract.code; this.contracts[contractName].options.data = contract.code;
this.contracts[contractName].options.gas = 6000000; this.contracts[contractName].options.gas = 6000000;

958
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,6 @@
"decompress": "^4.2.0", "decompress": "^4.2.0",
"deep-equal": "^1.0.1", "deep-equal": "^1.0.1",
"ejs": "^2.5.8", "ejs": "^2.5.8",
"embark-web3-provider-engine": "14.0.7",
"eth-ens-namehash": "^2.0.8", "eth-ens-namehash": "^2.0.8",
"eth-lib": "^0.2.8", "eth-lib": "^0.2.8",
"ethereumjs-wallet": "^0.6.0", "ethereumjs-wallet": "^0.6.0",

View File

@ -38,6 +38,7 @@ describe('embark.Blockchain', function () {
wsPort: 8546, wsPort: 8546,
wsRPC: true, wsRPC: true,
targetGasLimit: false, targetGasLimit: false,
syncmode: undefined,
syncMode: undefined, syncMode: undefined,
verbosity: undefined, verbosity: undefined,
proxy: true proxy: true
@ -81,6 +82,7 @@ describe('embark.Blockchain', function () {
wsRPC: true, wsRPC: true,
targetGasLimit: false, targetGasLimit: false,
syncMode: undefined, syncMode: undefined,
syncmode: undefined,
verbosity: undefined, verbosity: undefined,
proxy: true proxy: true
}; };

View File

@ -2,8 +2,8 @@ module.exports = {
default: { default: {
deployment: { deployment: {
host: "localhost", host: "localhost",
port: 8545, port: 8546,
type: "rpc" type: "ws"
}, },
dappConnection: [ dappConnection: [
"$WEB3", "$WEB3",

View File

@ -38,11 +38,11 @@ contract("SimpleStorage", function () {
}); });
it('listens to events', function (done) { it('listens to events', function (done) {
SimpleStorage.once('EventOnSet2', async function(error, _result){ SimpleStorage.once('EventOnSet2', async function (error, _result) {
assert.strictEqual(error, null); assert.strictEqual(error, null);
let result = await SimpleStorage.methods.get().call(); let result = await SimpleStorage.methods.get().call();
assert.strictEqual(parseInt(result, 10), 150); assert.strictEqual(parseInt(result, 10), 150);
done(); done(error);
}); });
SimpleStorage.methods.set2(150, 100).send(); SimpleStorage.methods.set2(150, 100).send();