fix ws ping and add wait calculation

This commit is contained in:
Jonathan Rainville 2018-08-30 15:48:50 -04:00 committed by Pascal Precht
parent 3a6ed745f6
commit dd0ae5023d
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 59 additions and 34 deletions

View File

@ -61,6 +61,9 @@ class BlockchainConnector {
} }
let {type, host, port, accounts, protocol, coverage} = this.config.contractsConfig.deployment; let {type, host, port, accounts, protocol, coverage} = this.config.contractsConfig.deployment;
if (!protocol) {
protocol = (type === "rpc") ? 'http' : 'ws';
}
if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) { if (!BlockchainConnector.ACCEPTED_TYPES.includes(type)) {
this.logger.error(__("contracts config error: unknown deployment type %s", type)); this.logger.error(__("contracts config error: unknown deployment type %s", type));

View File

@ -16,6 +16,13 @@ class TransactionTracker {
onBlockHeader(blockHeader) { onBlockHeader(blockHeader) {
this.events.request("blockchain:block:byNumber", blockHeader.hash, (err, block) => { 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 => { block.transactions.forEach(transaction => {
if (this.transactions[transaction.hash]) { if (this.transactions[transaction.hash]) {
Object.assign(this.transactions[transaction.hash], transaction, {endTimestamp: block.timestamp, wait: block.timestamp - this.transactions[transaction.hash].startTimestamp}); 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; module.exports = TransactionTracker;

View File

@ -88,7 +88,36 @@ function getJson(url, cb) {
httpGetJson(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) { function pingEndpoint(host, port, type, protocol, origin, callback) {
if (type === 'ws') {
port = port ? ':' + port : '';
return testWebSocket(`${protocol}://${host}${port}`, callback);
}
const options = { const options = {
protocolVersion: 13, protocolVersion: 13,
perMessageDeflate: true, perMessageDeflate: true,
@ -96,15 +125,6 @@ function pingEndpoint(host, port, type, protocol, origin, callback) {
host: host, host: host,
port: port 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; let req;
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl // remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
if (options.host.indexOf('/') > -1) { if (options.host.indexOf('/') > -1) {
@ -494,31 +514,6 @@ function timer(ms) {
return new Promise(resolve => setTimeout(resolve, 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 = { module.exports = {
joinPath, joinPath,
dirname, dirname,