ethstats-cli
Version:
EthStats - CLI Client
451 lines (374 loc) • 14.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _net = _interopRequireDefault(require("net"));
var _web = _interopRequireDefault(require("web3"));
var _Abstract2 = _interopRequireDefault(require("./Abstract.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var WebSocket =
/*#__PURE__*/
function (_Abstract) {
_inherits(WebSocket, _Abstract);
function WebSocket(diContainer) {
var _this;
_classCallCheck(this, WebSocket);
_this = _possibleConstructorReturn(this, _getPrototypeOf(WebSocket).call(this, diContainer));
if (_this.protocol === 'ipc') {
_this.web3 = new _web.default(new _web.default.providers.IpcProvider(_this.url, _net.default));
} else {
_this.web3 = new _web.default(new _web.default.providers.WebsocketProvider(_this.url));
}
_this.web3.isConnected = function () {
var result = false;
return _this.web3.eth.getProtocolVersion().then(function (data) {
if (data) {
result = true;
}
return result;
}).catch(function (error) {
if (error.message !== 'connection not open') {
_this.log.error(_this.errorHandler.resolve(error));
}
return result;
});
};
return _this;
}
_createClass(WebSocket, [{
key: "setProvider",
value: function setProvider() {
this.log.debug("Setting Web3 provider to \"".concat(this.url, "\""));
if (this.protocol === 'ipc') {
this.web3.setProvider(new _web.default.providers.IpcProvider(this.url, _net.default));
} else {
this.web3.setProvider(new _web.default.providers.WebsocketProvider(this.url));
}
}
}, {
key: "connect",
value: function connect() {
var _this2 = this;
this.web3.isConnected().then(function (result) {
if (!result) {
_this2.log.echo("Connecting to node \"".concat(_this2.url, "\""));
_this2.setProvider();
_this2.checkConnection();
}
});
if (this.connectionInterval === null) {
this.connectionInterval = setInterval(function () {
_this2.checkConnection();
}, this.CONNECTION_INTERVAL);
}
}
}, {
key: "checkConnection",
value: function checkConnection() {
var _this3 = this;
this.web3.isConnected().then(function (isConnected) {
_this3.log.debug("Check node connection => ".concat(isConnected));
if (isConnected) {
if (!_this3.isConnected) {
_this3.isConnected = true;
_this3.lastBlock = null;
_this3.start();
_this3.log.echo('Connection established with the node');
}
if (!_this3.server.isLoggedIn) {
_this3.getLoginInfo().then(function (loginInfos) {
_this3.usage.setNodeProcessName(loginInfos.node);
_this3.server.login(loginInfos);
});
}
} else {
if (_this3.isConnected) {
_this3.isConnected = false;
_this3.stop();
_this3.log.warning('Connection lost with the node. Waiting to reconnect...');
} else {
_this3.setProvider();
}
if (_this3.server.isLoggedIn) {
_this3.server.logout();
}
}
});
}
}, {
key: "getLoginInfo",
value: function getLoginInfo() {
var _this4 = this;
var result = {
nodeName: this.config.configStore.get('nodeName'),
secretKey: this.config.configStore.get('secretKey'),
coinbase: null,
node: null,
net: null,
protocol: null,
api: this.web3.version,
os: this.os.type(),
osVersion: this.os.release(),
client: this.pkg.version,
cpu: null,
memory: null,
disk: null
};
var allPromises = [];
var coinbase = this.web3.eth.getCoinbase().then(function (data) {
if (data) {
return data.toString();
}
return null;
}).catch(function (error) {
_this4.log.error(_this4.errorHandler.resolve(error));
return null;
});
allPromises.push(coinbase);
var nodeInfo = this.web3.eth.getNodeInfo().then(function (data) {
if (data) {
return data.toString();
}
return null;
}).catch(function (error) {
_this4.log.error(_this4.errorHandler.resolve(error));
return null;
});
allPromises.push(nodeInfo);
var networkId = this.web3.eth.net.getId().then(function (data) {
if (data) {
return data.toString();
}
return null;
}).catch(function (error) {
_this4.log.error(_this4.errorHandler.resolve(error));
return null;
});
allPromises.push(networkId);
var protocolVersion = this.web3.eth.getProtocolVersion().then(function (data) {
if (data) {
return data.toString();
}
return null;
}).catch(function (error) {
_this4.log.error(_this4.errorHandler.resolve(error));
return null;
});
allPromises.push(protocolVersion);
allPromises.push(this.hwInfo.getCpuInfo());
allPromises.push(this.hwInfo.getMemoryInfo());
allPromises.push(this.hwInfo.getDiskInfo());
return Promise.all(allPromises).then(function (promiseResults) {
result.coinbase = promiseResults[0];
result.node = promiseResults[1];
result.net = promiseResults[2];
result.protocol = promiseResults[3];
result.cpu = promiseResults[4];
result.memory = promiseResults[5];
result.disk = promiseResults[6];
return result;
});
}
}, {
key: "start",
value: function start() {
var _this5 = this;
this.log.debug('Start client');
this.statsInterval = setInterval(function () {
_this5.getStats();
}, this.STATS_INTERVAL);
this.usageInterval = setInterval(function () {
_this5.usage.getStats();
}, this.USAGE_INTERVAL);
this.web3.eth.subscribe('newBlockHeaders').on('data', function (blockHeader) {
if (blockHeader) {
_this5.log.debug("Got block: \"".concat(blockHeader.number, "\""));
_this5.blocksQueue.push(blockHeader.hash);
}
}).on('error', function (error) {
_this5.log.error(_this5.errorHandler.resolve(error));
});
this.web3.eth.subscribe('syncing').on('changed', function (isSyncing) {
_this5.syncStatus(isSyncing);
}).on('data', function (syncInfo) {
if (syncInfo) {
_this5.syncStatus(syncInfo);
}
}).on('error', function (error) {
_this5.log.error(_this5.errorHandler.resolve(error));
});
}
}, {
key: "stop",
value: function stop() {
var stopConnectionInterval = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
this.log.debug('Stop client');
if (stopConnectionInterval) {
clearInterval(this.connectionInterval);
}
clearInterval(this.statsInterval);
clearInterval(this.usageInterval);
}
}, {
key: "getStats",
value: function getStats() {
var _this6 = this;
this.log.debug('Get stats');
var result = {
peers: 0,
gasPrice: 0,
mining: false,
hashrate: 0,
pendingTXs: 0
};
var allPromises = [];
var peers = this.web3.eth.net.getPeerCount().then(function (data) {
if (data) {
return data;
}
return 0;
}).catch(function (error) {
_this6.log.error(_this6.errorHandler.resolve(error));
return 0;
});
allPromises.push(peers);
var gasPrice = this.web3.eth.getGasPrice().then(function (data) {
if (data) {
return data.toString();
}
return 0;
}).catch(function (error) {
_this6.log.error(_this6.errorHandler.resolve(error));
return 0;
});
allPromises.push(gasPrice);
var mining = this.web3.eth.isMining().then(function (data) {
if (data) {
return data;
}
return false;
}).catch(function (error) {
_this6.log.error(_this6.errorHandler.resolve(error));
return false;
});
allPromises.push(mining);
var hashrate = this.web3.eth.getHashrate().then(function (data) {
if (data) {
return data;
}
return 0;
}).catch(function (error) {
_this6.log.error(_this6.errorHandler.resolve(error));
return 0;
});
allPromises.push(hashrate);
var pendingTXs = this.web3.eth.getBlockTransactionCount('pending').then(function (data) {
if (data) {
return data;
}
return 0;
}).catch(function (error) {
_this6.log.error(_this6.errorHandler.resolve(error));
return 0;
});
allPromises.push(pendingTXs);
return Promise.all(allPromises).then(function (promiseResults) {
result.peers = promiseResults[0];
result.gasPrice = promiseResults[1];
result.mining = promiseResults[2];
result.hashrate = promiseResults[3];
result.pendingTXs = promiseResults[4];
_this6.server.send('stats', result);
return result;
});
}
}, {
key: "getBlock",
value: function getBlock(number, asyncCallback) {
var _this7 = this;
this.web3.eth.getBlock(number, false).then(function (block) {
if (block) {
_this7.processBlock(block);
}
if (asyncCallback) {
asyncCallback();
}
}).catch(function (error) {
_this7.log.error(_this7.errorHandler.resolve(error));
if (asyncCallback) {
asyncCallback();
}
});
}
}, {
key: "getBlockHashes",
value: function getBlockHashes(blockNumber) {
var _this8 = this;
var result = {
blockNumber: null,
blockHash: null,
blockParentHash: null
};
this.web3.eth.getBlock(blockNumber, false).then(function (block) {
if (block === null) {
_this8.log.error("Could not get block \"".concat(blockNumber, "\". Your node might be not fully synced."), false, true);
} else {
result.blockNumber = parseInt(block.number, 10);
result.blockHash = block.hash.toString();
result.blockParentHash = block.parentHash.toString();
}
_this8.server.send('checkChainData', result);
}).catch(function (error) {
_this8.log.error(_this8.errorHandler.resolve(error));
});
}
}, {
key: "getBlocks",
value: function getBlocks(range) {
var _this9 = this;
var allPromises = range.map(function (blockNumber) {
_this9.log.debug("History get block: \"".concat(blockNumber, "\""));
return _this9.web3.eth.getBlock(blockNumber, false);
});
Promise.all(allPromises).then(function (results) {
_this9.server.send('getBlocksData', results);
}).catch(function (error) {
_this9.log.error("Error getting block history: ".concat(error));
_this9.server.send('getBlocksData', []);
});
}
}, {
key: "getValidators",
value: function getValidators(block) {
var _this10 = this;
var result = {
blockNumber: block.number,
blockHash: block.hash,
validators: []
};
this.web3._requestManager.send({
method: 'clique_getSignersAtHash',
params: [block.hash]
}, function (error, validators) {
if (error) {
_this10.log.error("Could not get validators for block ".concat(block.number, "::").concat(block.hash, " => ").concat(error.message));
} else {
result.validators = validators;
_this10.server.send('validators', result);
}
});
}
}]);
return WebSocket;
}(_Abstract2.default);
exports.default = WebSocket;