From 92c8f00ba802f701d1859f7f1f4ccdfabd1aeb6e Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 29 Jun 2018 17:09:19 -0400 Subject: [PATCH] wip fixes --- lib/cmds/blockchain/blockchain.js | 5 +++-- lib/contracts/provider.js | 21 ++++++++++++++++--- lib/tests/test.js | 20 +++++++++++++++++- test_apps/test_app/config/blockchain.json | 16 ++++---------- .../test_app/test/simple_storage_spec.js | 21 ++++++++++++++----- 5 files changed, 60 insertions(+), 23 deletions(-) diff --git a/lib/cmds/blockchain/blockchain.js b/lib/cmds/blockchain/blockchain.js index d1904875..2ae37326 100644 --- a/lib/cmds/blockchain/blockchain.js +++ b/lib/cmds/blockchain/blockchain.js @@ -37,12 +37,13 @@ var Blockchain = function(options) { whisper: (this.blockchainConfig.whisper === undefined) || this.blockchainConfig.whisper, maxpeers: ((this.blockchainConfig.maxpeers === 0) ? 0 : (this.blockchainConfig.maxpeers || 25)), bootnodes: this.blockchainConfig.bootnodes || "", - rpcApi: (this.blockchainConfig.rpcApi || ['eth', 'web3', 'net', 'debug']), + rpcApi: (this.blockchainConfig.rpcApi || ['eth', 'web3', 'net', 'debug', 'pubsub']), wsRPC: (this.blockchainConfig.wsRPC === undefined) || this.blockchainConfig.wsRPC, wsHost: this.blockchainConfig.wsHost || 'localhost', wsPort: this.blockchainConfig.wsPort || 8546, wsOrigins: this.blockchainConfig.wsOrigins || false, - wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh', 'debug']), + // wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh', 'debug']), + wsApi: (this.blockchainConfig.wsApi || ['eth', 'web3', 'net', 'shh', 'debug', 'pubsub']), vmdebug: this.blockchainConfig.vmdebug || false, targetGasLimit: this.blockchainConfig.targetGasLimit || false, syncMode: this.blockchainConfig.syncMode, diff --git a/lib/contracts/provider.js b/lib/contracts/provider.js index 94cbba60..b41a137c 100644 --- a/lib/contracts/provider.js +++ b/lib/contracts/provider.js @@ -1,6 +1,12 @@ 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 CacheSubprovider = require('embark-web3-provider-engine/subproviders/cache.js'); +const FixtureSubprovider = require('embark-web3-provider-engine/subproviders/fixture.js'); +const FilterSubprovider = require('embark-web3-provider-engine/subproviders/filters.js'); +const VmSubprovider = require('embark-web3-provider-engine/subproviders/vm.js'); +const NonceSubprovider = require('embark-web3-provider-engine/subproviders/nonce-tracker.js'); +const SubscriptionSubprovider = require('embark-web3-provider-engine/subproviders/subscriptions'); const async = require('async'); const AccountParser = require('./accountParser'); const fundAccount = require('./fundAccount'); @@ -26,11 +32,18 @@ class Provider extends ProviderEngine { startWeb3Provider(callback) { const self = this; +// cache layer +// self.addProvider(new CacheSubprovider()) + + // self.addProvider(new NonceSubprovider()) + if (this.type === 'rpc') { self.addProvider(new RpcSubprovider({ rpcUrl: self.web3Endpoint })); } else if (this.type === 'ws') { + console.log('USing ws'); + self.addProvider(new SubscriptionSubprovider()); self.addProvider(new WsSubprovider({ rpcUrl: self.web3Endpoint, origin: this.blockchainConfig.wsOrigins.split(',')[0] @@ -40,17 +53,19 @@ class Provider extends ProviderEngine { } + // network connectivity error self.on('error', (err) => { + console.log('ERR', JSON.stringify(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.stop(); }); - self.web3.setProvider(self); + self.web3.setProvider(self); self.start(); self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger); diff --git a/lib/tests/test.js b/lib/tests/test.js index 2ff57dc4..7941ca03 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -44,6 +44,7 @@ class Test { } initWeb3Provider(callback) { + const self = this; if (this.provider) { this.provider.stop(); } @@ -63,13 +64,30 @@ 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); } + + /*const ProviderEngine = require('embark-web3-provider-engine'); + const WsSubprovider = require('embark-web3-provider-engine/subproviders/websocket'); + const SubscriptionSubprovider = require('embark-web3-provider-engine/subproviders/subscriptions'); + const engine = new ProviderEngine(); + self.web3 = new Web3(engine); + + engine.addProvider(new SubscriptionSubprovider()); + engine.addProvider(new WsSubprovider({ + rpcUrl: endpoint, + origin: self.engine.config.blockchainConfig.wsOrigins.split(',')[0] + })); + + engine.start(); + return callback();*/ + this.provider = new Provider(providerOptions); - return this.provider.startWeb3Provider(callback); + this.provider.startWeb3Provider(callback); }); } diff --git a/test_apps/test_app/config/blockchain.json b/test_apps/test_app/config/blockchain.json index 9b88dd4a..31e1fe83 100644 --- a/test_apps/test_app/config/blockchain.json +++ b/test_apps/test_app/config/blockchain.json @@ -18,13 +18,13 @@ "wsPort": 8546, "proxy": true }, - "privateNet": { + "privatenet": { "enabled": true, "networkType": "custom", "networkId": "1337", "isDev": false, - "genesisBlock": "config/privateNet/genesis.json", - "datadir": ".embark/privateNet/datadir", + "genesisBlock": "config/privatenet/genesis.json", + "datadir": ".embark/privatenet/datadir", "mineWhenNeeded": true, "nodiscover": true, "maxpeers": 0, @@ -32,7 +32,7 @@ "rpcPort": 8545, "rpcCorsDomain": "auto", "account": { - "password": "config/privateNet/password" + "password": "config/privatenet/password" }, "targetGasLimit": 8000000, "wsOrigins": "auto", @@ -67,13 +67,5 @@ "account": { "password": "config/livenet/password" } - }, - "privatenet": { - "networkType": "custom", - "rpcHost": "localhost", - "rpcPort": 8545, - "datadir": "yourdatadir", - "networkId": "123", - "bootnodes": "" } } diff --git a/test_apps/test_app/test/simple_storage_spec.js b/test_apps/test_app/test/simple_storage_spec.js index 230c39d9..6addd659 100644 --- a/test_apps/test_app/test/simple_storage_spec.js +++ b/test_apps/test_app/test/simple_storage_spec.js @@ -3,6 +3,11 @@ const SimpleStorage = require('Embark/contracts/SimpleStorage'); let accounts; config({ + deployment: { + type: 'ws', + host: 'localhost', + port: '8546' + }, contracts: { "SimpleStorage": { args: [100], @@ -38,14 +43,20 @@ contract("SimpleStorage", function () { }); it('listens to events', function (done) { - SimpleStorage.once('EventOnSet2', async function(error, _result){ - assert.strictEqual(error, null); + SimpleStorage.once('EventOnSet2', function(error, _result){ + console.log('error', error); + console.log('result', _result); + + /*assert.strictEqual(error, null); let result = await SimpleStorage.methods.get().call(); - assert.strictEqual(parseInt(result, 10), 150); - done(); + assert.strictEqual(parseInt(result, 10), 150);*/ + done(error); }); - SimpleStorage.methods.set2(150, 100).send(); + console.log('TEST'); + SimpleStorage.methods.set2(150, 100).send(() => { + console.log('Done'); + }); }); });