UNPKG

ethstats-cli

Version:
159 lines (134 loc) 5.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _os = _interopRequireDefault(require("os")); var _queue = _interopRequireDefault(require("async/queue")); var _HwInfo = _interopRequireDefault(require("../HwInfo.js")); var _Usage = _interopRequireDefault(require("../Usage.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; } var Abstract = /*#__PURE__*/ function () { function Abstract(diContainer) { var _this = this; _classCallCheck(this, Abstract); this.CONNECTION_INTERVAL = 5000; this.STATS_INTERVAL = 10000; this.USAGE_INTERVAL = 5000; this.config = diContainer.config; this.lodash = diContainer.lodash; this.os = _os.default; this.pkg = diContainer.pkg; this.log = diContainer.logger; this.cli = diContainer.cli; this.server = diContainer.server; this.errorHandler = diContainer.clientErrorHandler; this.hwInfo = new _HwInfo.default(diContainer); this.usage = new _Usage.default(diContainer); this.url = this.config.client.url; this.lastSyncStatus = { currentBlock: null }; this.lastBlock = null; this.isConnected = false; this.connectionInterval = null; this.statsInterval = null; this.usageInterval = null; this.blocksQueue = (0, _queue.default)(function (hash, callback) { _this.getBlock(hash, callback); }, 1); } _createClass(Abstract, [{ key: "processBlock", value: function processBlock(block) { var lastBlockNumber = this.lastBlock === null ? null : parseInt(this.lastBlock.number, 10); var currentBlockNumber = parseInt(block.number, 10); if (lastBlockNumber === null || currentBlockNumber - lastBlockNumber === 1) { this.lastBlock = block; this.server.send('block', block); if (this.config.NETWORK_ALGO === 'clique') { this.getValidators(block); } } if (lastBlockNumber > 0 && currentBlockNumber - lastBlockNumber > 1 && currentBlockNumber - lastBlockNumber <= 1000) { this.log.info("Missing blocks detected (last block: \"".concat(lastBlockNumber, "\")")); var blocksToGet = currentBlockNumber - lastBlockNumber; while (blocksToGet > 0) { var blockNumber = currentBlockNumber - blocksToGet + 1; this.blocksQueue.push(blockNumber); this.log.info("Force get block \"".concat(blockNumber, "\"")); blocksToGet--; } } if (currentBlockNumber < lastBlockNumber) { this.log.info("Ignoring block \"".concat(currentBlockNumber, "\" because a newer block has already been sent (last block: \"").concat(lastBlockNumber, "\")")); } if (currentBlockNumber === lastBlockNumber) { if (this.lastBlock.hash === block.hash) { this.log.info("Block \"".concat(currentBlockNumber, "\" has already been sent")); } else { this.log.info("Reorg for block \"".concat(currentBlockNumber, "\"")); this.server.send('block', block); if (this.config.NETWORK_ALGO === 'clique') { this.getValidators(block); } } } } }, { key: "syncStatus", value: function syncStatus(sync) { var syncParams = { syncOperation: null, startingBlock: 0, currentBlock: 0, highestBlock: 0, progress: 0 }; if (sync === true) { syncParams.syncOperation = 'start'; } else if (sync) { var startingBlock = sync.startingBlock ? sync.startingBlock : sync.status ? sync.status.StartingBlock : 0; var currentBlock = sync.currentBlock ? sync.currentBlock : sync.status ? sync.status.CurrentBlock : 0; var highestBlock = sync.highestBlock ? sync.highestBlock : sync.status ? sync.status.HighestBlock : 0; var progress = currentBlock - startingBlock; var total = highestBlock - startingBlock; syncParams.progress = progress > 0 && total > 0 ? (progress / total * 100).toFixed(2) : 0; syncParams.syncOperation = 'progress'; syncParams.startingBlock = startingBlock; syncParams.currentBlock = currentBlock; syncParams.highestBlock = highestBlock; } else { syncParams.syncOperation = 'finish'; } if (this.protocol === 'http') { if (syncParams.syncOperation === 'start' || syncParams.syncOperation === 'progress') { this.web3.reset(true); } else { this.setLatestBlocksFilter(); } } if (this.lastSyncStatus.currentBlock !== syncParams.currentBlock) { this.lastSyncStatus = syncParams; this.server.send('sync', syncParams); this.lastBlock = null; } } }, { key: "resolveCheckChainResponse", value: function resolveCheckChainResponse(response) { if (response.success) { this.log.info('The node is on the correct network'); } else { this.log.error("Server check chain response: ".concat(JSON.stringify(response.errors)), false, true); } } }]); return Abstract; }(); exports.default = Abstract;