diff --git a/services/blockchain-connector/shared/sdk/client.js b/services/blockchain-connector/shared/sdk/client.js index 308a757dc1..33598b7587 100644 --- a/services/blockchain-connector/shared/sdk/client.js +++ b/services/blockchain-connector/shared/sdk/client.js @@ -108,7 +108,7 @@ const instantiateAndCacheClient = async () => { const getApiClient = async () => { if (cachedApiClients.length === 0) { - throw new Error(`No API client is alive!`); + await instantiateAndCacheClient(); } return cachedApiClients[0]; }; @@ -222,12 +222,14 @@ Signals.get('resetApiClient').add(resetApiClientListener); // Check periodically for client aliveness and refill cached clients pool (async () => { + if (config.useHttpApi) return; + // eslint-disable-next-line no-constant-condition while (true) { const cacheRefreshStartTime = Date.now(); await refreshClientsCache(); logger.debug( - `Refreshed API client cached in ${Date.now() - cacheRefreshStartTime}ms. There are ${ + `Refreshed API client cache in ${Date.now() - cacheRefreshStartTime}ms. There are ${ cachedApiClients.length } API client(s) in the pool.`, ); diff --git a/services/blockchain-connector/shared/sdk/events.js b/services/blockchain-connector/shared/sdk/events.js index d010463845..f4933769df 100644 --- a/services/blockchain-connector/shared/sdk/events.js +++ b/services/blockchain-connector/shared/sdk/events.js @@ -21,8 +21,15 @@ const config = require('../../config'); const { getApiClient } = require('./client'); const { formatEvent } = require('./formatter'); -const { getRegisteredEvents, getEventsByHeight, getNodeInfo } = require('./endpoints'); +const { + getRegisteredEvents, + getEventsByHeight, + getNodeInfo, + getBlockByHeight, + getGenerators, +} = require('./endpoints'); const { updateTokenInfo } = require('./token'); +const { getPosConstants } = require('./pos'); const logger = Logger(); @@ -41,6 +48,7 @@ const events = [ ]; let eventsCounter; +let lastBlockHeightEvent; const logError = (method, err) => { logger.warn(`Invocation for ${method} failed with error: ${err.message}`); @@ -48,6 +56,8 @@ const logError = (method, err) => { }; const subscribeToAllRegisteredEvents = async () => { + if (!config.useHttpApi) return; + // Reset eventsCounter first eventsCounter = 0; @@ -122,6 +132,36 @@ const genesisBlockDownloadedListener = () => { Signals.get('nodeIsSynced').add(nodeIsSyncedListener); Signals.get('genesisBlockDownloaded').add(genesisBlockDownloadedListener); +const emitNodeEvents = async nodeInfo => { + if (config.useHttpApi) { + setInterval(async () => { + const latestNodeInfo = await getNodeInfo(true); + const { syncing } = latestNodeInfo; + const isNodeSyncComplete = !syncing; + + if (isNodeSyncComplete) { + if (!lastBlockHeightEvent || latestNodeInfo.height > lastBlockHeightEvent) { + lastBlockHeightEvent = latestNodeInfo.height; + const newBlock = await getBlockByHeight(latestNodeInfo.height); + Signals.get(EVENT_CHAIN_BLOCK_NEW).dispatch({ blockHeader: newBlock.header }); + + const posConstants = await getPosConstants(); + if ( + (latestNodeInfo.height - latestNodeInfo.genesisHeight) % posConstants.roundLength === + 1 + ) { + const { list: validators } = await getGenerators(); + Signals.get(EVENT_CHAIN_VALIDATORS_CHANGE).dispatch({ nextValidators: validators }); + } + } + } + }, nodeInfo.genesis.blockTime * 1000); + } +}; + +// TODO: Add retry logic in case of failure +getNodeInfo().then(nodeInfo => emitNodeEvents(nodeInfo)); + module.exports = { events,