mirror of https://github.com/embarklabs/embark.git
Merge pull request #606 from embark-framework/bug_fix/events-dont-wrok
Fix events usage with websocket providers
This commit is contained in:
commit
d40ae2f374
|
@ -34,7 +34,6 @@ EmbarkJS.Contract = function(options) {
|
|||
|
||||
if (EmbarkJS.isNewWeb3(this.web3)) {
|
||||
ContractClass = new this.web3.eth.Contract(this.abi, this.address);
|
||||
ContractClass.setProvider(this.web3.currentProvider);
|
||||
ContractClass.options.data = this.code;
|
||||
ContractClass.options.from = this.from || this.web3.eth.defaultAccount;
|
||||
ContractClass.abi = ContractClass.options.abi;
|
||||
|
|
|
@ -124,6 +124,10 @@ class Cmd {
|
|||
.description(__('run dapp (default: %s)', 'development'))
|
||||
.action(function (env, options) {
|
||||
i18n.setOrDetectLocale(options.locale);
|
||||
embark.initConfig(env || 'development', {
|
||||
embarkConfig: 'embark.json',
|
||||
interceptLogs: false
|
||||
});
|
||||
embark.run({
|
||||
env: env || 'development',
|
||||
serverPort: options.port,
|
||||
|
|
|
@ -46,6 +46,7 @@ var Blockchain = function(options) {
|
|||
vmdebug: this.blockchainConfig.vmdebug || false,
|
||||
targetGasLimit: this.blockchainConfig.targetGasLimit || false,
|
||||
syncMode: this.blockchainConfig.syncMode,
|
||||
syncmode: this.blockchainConfig.syncmode,
|
||||
verbosity: this.blockchainConfig.verbosity
|
||||
};
|
||||
|
||||
|
|
|
@ -20,8 +20,8 @@ class GethCommands {
|
|||
cmd.push(`--datadir=${config.datadir}`);
|
||||
}
|
||||
|
||||
if (config.syncMode) {
|
||||
cmd.push("--syncmode=" + config.syncMode);
|
||||
if (config.syncmode || config.syncMode) {
|
||||
cmd.push("--syncmode=" + config.syncmode || config.syncMode);
|
||||
}
|
||||
|
||||
if (config.account && config.account.password) {
|
||||
|
|
|
@ -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 AccountParser = require('./accountParser');
|
||||
const fundAccount = require('./fundAccount');
|
||||
|
||||
const NO_ACCOUNTS = 'noAccounts';
|
||||
|
||||
class Provider {
|
||||
constructor(options) {
|
||||
this.web3 = options.web3;
|
||||
|
@ -16,63 +11,59 @@ class Provider {
|
|||
this.web3Endpoint = options.web3Endpoint;
|
||||
this.logger = options.logger;
|
||||
this.isDev = options.isDev;
|
||||
this.engine = new ProviderEngine();
|
||||
this.asyncMethods = {};
|
||||
}
|
||||
|
||||
startWeb3Provider(callback) {
|
||||
const self = this;
|
||||
|
||||
if (this.type === 'rpc') {
|
||||
self.engine.addProvider(new RpcSubprovider({
|
||||
rpcUrl: self.web3Endpoint
|
||||
}));
|
||||
self.provider = new this.web3.providers.HttpProvider(self.web3Endpoint);
|
||||
} else if (this.type === 'ws') {
|
||||
self.engine.addProvider(new WsSubprovider({
|
||||
rpcUrl: self.web3Endpoint,
|
||||
origin: this.blockchainConfig.wsOrigins.split(',')[0]
|
||||
}));
|
||||
self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "embark"}});
|
||||
} else {
|
||||
return callback(__("contracts config error: unknown deployment type %s", this.type));
|
||||
}
|
||||
|
||||
|
||||
// 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.web3.setProvider(self.provider);
|
||||
|
||||
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
|
||||
self.addresses = [];
|
||||
async.waterfall([
|
||||
function populateWeb3Wallet(next) {
|
||||
|
||||
if (!self.accounts.length) {
|
||||
return next(NO_ACCOUNTS);
|
||||
return callback();
|
||||
}
|
||||
self.accounts.forEach(account => {
|
||||
self.addresses.push(account.address);
|
||||
self.web3.eth.accounts.wallet.add(account);
|
||||
});
|
||||
self.asyncMethods = {
|
||||
eth_accounts: self.eth_accounts.bind(self)
|
||||
};
|
||||
next();
|
||||
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);
|
||||
}
|
||||
], function (err) {
|
||||
if (err && err !== NO_ACCOUNTS) {
|
||||
self.logger.error((err));
|
||||
}
|
||||
callback();
|
||||
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) {
|
||||
const self = this;
|
||||
|
@ -86,32 +77,6 @@ class Provider {
|
|||
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
|
||||
}, 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;
|
||||
|
|
|
@ -63,18 +63,23 @@ class Test {
|
|||
web3Endpoint: endpoint
|
||||
};
|
||||
console.info(`Connecting to node at ${endpoint}`.cyan);
|
||||
|
||||
return utils.pingEndpoint(host, port, type, protocol, this.engine.config.blockchainConfig.wsOrigins.split(',')[0], (err) => {
|
||||
if (err) {
|
||||
console.error(`Error connecting to the node, there might be an error in ${endpoint}`.red);
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
this.provider = new Provider(providerOptions);
|
||||
return this.provider.startWeb3Provider(callback);
|
||||
this.provider.startWeb3Provider(callback);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.simOptions.accounts) {
|
||||
this.simOptions.accounts = this.simOptions.accounts.map((account) => {
|
||||
if (!account.hexBalance) {
|
||||
account.hexBalance = '0x8AC7230489E80000'; // 10 ether
|
||||
}
|
||||
return {balance: account.hexBalance, secretKey: account.privateKey};
|
||||
});
|
||||
}
|
||||
|
@ -182,7 +187,11 @@ class Test {
|
|||
if (options.deployment.type !== 'rpc' && options.deployment.type !== 'ws') {
|
||||
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;
|
||||
}
|
||||
if (!resetServices) {
|
||||
|
@ -268,7 +277,9 @@ class Test {
|
|||
console.warn("Warning: default account has no funds");
|
||||
}
|
||||
next(null, accounts);
|
||||
}).catch((err) => { next(err); });
|
||||
}).catch((err) => {
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function deploy(accounts, next) {
|
||||
self.engine.deployManager.gasLimit = 6000000;
|
||||
|
@ -289,7 +300,13 @@ class Test {
|
|||
} else {
|
||||
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;
|
||||
if (self.contracts[contractName].options) {
|
||||
|
@ -337,7 +354,13 @@ class Test {
|
|||
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].options.data = contract.code;
|
||||
this.contracts[contractName].options.gas = 6000000;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -39,7 +39,6 @@
|
|||
"decompress": "^4.2.0",
|
||||
"deep-equal": "^1.0.1",
|
||||
"ejs": "^2.5.8",
|
||||
"embark-web3-provider-engine": "14.0.7",
|
||||
"eth-ens-namehash": "^2.0.8",
|
||||
"eth-lib": "^0.2.8",
|
||||
"ethereumjs-wallet": "^0.6.0",
|
||||
|
|
|
@ -38,6 +38,7 @@ describe('embark.Blockchain', function () {
|
|||
wsPort: 8546,
|
||||
wsRPC: true,
|
||||
targetGasLimit: false,
|
||||
syncmode: undefined,
|
||||
syncMode: undefined,
|
||||
verbosity: undefined,
|
||||
proxy: true
|
||||
|
@ -81,6 +82,7 @@ describe('embark.Blockchain', function () {
|
|||
wsRPC: true,
|
||||
targetGasLimit: false,
|
||||
syncMode: undefined,
|
||||
syncmode: undefined,
|
||||
verbosity: undefined,
|
||||
proxy: true
|
||||
};
|
||||
|
|
|
@ -2,8 +2,8 @@ module.exports = {
|
|||
default: {
|
||||
deployment: {
|
||||
host: "localhost",
|
||||
port: 8545,
|
||||
type: "rpc"
|
||||
port: 8546,
|
||||
type: "ws"
|
||||
},
|
||||
dappConnection: [
|
||||
"$WEB3",
|
||||
|
|
|
@ -38,11 +38,11 @@ contract("SimpleStorage", function () {
|
|||
});
|
||||
|
||||
it('listens to events', function (done) {
|
||||
SimpleStorage.once('EventOnSet2', async function(error, _result){
|
||||
SimpleStorage.once('EventOnSet2', async function (error, _result) {
|
||||
assert.strictEqual(error, null);
|
||||
let result = await SimpleStorage.methods.get().call();
|
||||
assert.strictEqual(parseInt(result, 10), 150);
|
||||
done();
|
||||
done(error);
|
||||
});
|
||||
|
||||
SimpleStorage.methods.set2(150, 100).send();
|
||||
|
|
Loading…
Reference in New Issue