fix ws ping and add wait calculation

This commit is contained in:
Jonathan Rainville 2018-08-30 15:48:50 -04:00 committed by Iuri Matias
parent d83bced73e
commit 23a44f614b
3 changed files with 60 additions and 35 deletions

View File

@ -60,7 +60,10 @@ class BlockchainConnector {
this.logger.error(message);
}
const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws';
let protocol = this.contractsConfig.deployment.protocol;
if (!protocol) {
protocol = (this.contractsConfig.deployment.type === "rpc") ? 'http' : 'ws';
}
this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`;

View File

@ -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;

View File

@ -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,