From dd0ae5023de787465cab05662c54d158e3fd730d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 30 Aug 2018 15:48:50 -0400 Subject: [PATCH] fix ws ping and add wait calculation --- lib/modules/blockchain_connector/index.js | 3 ++ lib/modules/transactionTracker/index.js | 27 ++++++++++ lib/utils/utils.js | 63 +++++++++++------------ 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index cacc96291..4c5087465 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -61,6 +61,9 @@ class BlockchainConnector { } let {type, host, port, accounts, protocol, coverage} = this.config.contractsConfig.deployment; + if (!protocol) { + protocol = (type === "rpc") ? 'http' : 'ws'; + } if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) { this.logger.error(__("contracts config error: unknown deployment type %s", type)); diff --git a/lib/modules/transactionTracker/index.js b/lib/modules/transactionTracker/index.js index fffa4f71e..e18174b69 100644 --- a/lib/modules/transactionTracker/index.js +++ b/lib/modules/transactionTracker/index.js @@ -16,6 +16,13 @@ class TransactionTracker { onBlockHeader(blockHeader) { this.events.request("blockchain:block:byNumber", blockHeader.hash, (err, block) => { + if (err) { + return this.logger.error('Error getting block header', err); + } + // Don't know why, but sometimes we receive nothing + if (!block || !block.transactions) { + return; + } block.transactions.forEach(transaction => { if (this.transactions[transaction.hash]) { Object.assign(this.transactions[transaction.hash], transaction, {endTimestamp: block.timestamp, wait: block.timestamp - this.transactions[transaction.hash].startTimestamp}); @@ -23,6 +30,26 @@ class TransactionTracker { }); }); } + + calculateGasPriceSpeeds() { + return Object.keys(this.transactions).reduce((acc, transactionHash) => { + const transaction = this.transactions[transactionHash]; + if (!transaction.gasPrice) { + return acc; + } + if (!acc[transaction.gasPrice]) { + acc[transaction.gasPrice] = { + nbTxs: 0, + totalWait: 0 + }; + } + acc[transaction.gasPrice].nbTxs++; + acc[transaction.gasPrice].totalWait += transaction.wait; + acc[transaction.gasPrice].averageWait = acc[transaction.gasPrice].totalWait / acc[transaction.gasPrice].nbTxs; + + return acc; + }, {}); + } } module.exports = TransactionTracker; diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 4a6bb64b9..f64bada7e 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -88,7 +88,36 @@ function getJson(url, cb) { httpGetJson(url, cb); } +function testWebSocket(wsUri, cb) { + const W3CWebSocket = require('websocket').w3cwebsocket; + const websocket = new W3CWebSocket(wsUri); + + let calledBack = false; + function callback(err) { + if (calledBack) { + return; + } + websocket.close(); + calledBack = true; + cb(err); + } + + websocket.onopen = function(_evt) { + callback(); + }; + websocket.onclose = function(_evt) { + callback('ws connection closed'); + }; + websocket.onerror = function(evt) { + callback(evt.message|| evt); + }; +} + function pingEndpoint(host, port, type, protocol, origin, callback) { + if (type === 'ws') { + port = port ? ':' + port : ''; + return testWebSocket(`${protocol}://${host}${port}`, callback); + } const options = { protocolVersion: 13, perMessageDeflate: true, @@ -96,15 +125,6 @@ function pingEndpoint(host, port, type, protocol, origin, callback) { host: host, port: port }; - if (type === 'ws') { - options.headers = { - 'Sec-WebSocket-Version': 13, - Connection: 'Upgrade', - Upgrade: 'websocket', - 'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits', - Origin: origin - }; - } let req; // remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl if (options.host.indexOf('/') > -1) { @@ -494,31 +514,6 @@ function timer(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -function interceptLogs(consoleContext, logger) { - let context = {}; - context.console = consoleContext; - - context.console.log = function() { - logger.info(normalizeInput(arguments)); - }; - context.console.warn = function() { - logger.warn(normalizeInput(arguments)); - }; - context.console.info = function() { - logger.info(normalizeInput(arguments)); - }; - context.console.debug = function() { - // TODO: ue JSON.stringify - logger.debug(normalizeInput(arguments)); - }; - context.console.trace = function() { - logger.trace(normalizeInput(arguments)); - }; - context.console.dir = function() { - logger.dir(normalizeInput(arguments)); - }; -} - module.exports = { joinPath, dirname,