Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
:WIP: Draft implementation to support events when http-api enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Jan 2, 2024
1 parent 2ebf600 commit 8c43bc8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 4 additions & 2 deletions services/blockchain-connector/shared/sdk/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand Down Expand Up @@ -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.`,
);
Expand Down
42 changes: 41 additions & 1 deletion services/blockchain-connector/shared/sdk/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Check notice

Code scanning / Semgrep

Semgrep Finding: javascript.lang.correctness.useless-assign.useless-assignment Note

const is assigned twice; the first assignment is useless
const { getPosConstants } = require('./pos');

const logger = Logger();

Expand All @@ -41,13 +48,16 @@ const events = [
];

let eventsCounter;
let lastBlockHeightEvent;

const logError = (method, err) => {
logger.warn(`Invocation for ${method} failed with error: ${err.message}`);
logger.debug(err.stack);
};

const subscribeToAllRegisteredEvents = async () => {
if (!config.useHttpApi) return;

// Reset eventsCounter first
eventsCounter = 0;

Expand Down Expand Up @@ -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,

Expand Down

0 comments on commit 8c43bc8

Please sign in to comment.