let async = require('async'); let apiInterfaces = require('./apiInterfaces.js')(config.daemon, config.wallet, config.api); let lastHash; let observerConfig = config.chainObserver || config.daemon; let logSystem = 'chainObserver'; let blockData = JSON.stringify({ id: "0", jsonrpc: "2.0", method: 'getlastblockheader', params: {} }); require('./exceptionWriter.js')(logSystem); function runInterval () { async.waterfall([ function (callback) { apiInterfaces.jsonHttpRequest(observerConfig.host, observerConfig.port, blockData, function (err, res) { if (err) { log('error', logSystem, '%s error from chain observer', [config.coin]); setTimeout(runInterval, 3000); return; } if (res && res.result && res.result.status === "OK" && res.result.hasOwnProperty('block_header')) { let blockHeader = res.result.block_header; let hash = blockHeader.hash.toString('hex'); if (!lastHash || lastHash !== hash) { lastHash = hash; log('info', logSystem, '%s observed new chain tip %s at height %d', [config.coin, hash, blockHeader.height]); process.send({ type: 'ChainState', block_header: blockHeader }); } callback(null); return; } log('error', logSystem, 'bad response from chain observer'); setTimeout(runInterval, 3000); }); } ], function () { setTimeout(function () { runInterval(); }, observerConfig.refreshInterval || config.poolServer.blockRefreshInterval); }); } runInterval();