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

Commit

Permalink
🔨 Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Jan 3, 2024
1 parent d0ff8c4 commit 2017eee
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 766 deletions.
32 changes: 5 additions & 27 deletions services/blockchain-connector/shared/sdk/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,16 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
const {
Exceptions: { TimeoutException },
Logger,
} = require('lisk-service-framework');

const { timeoutMessage, invokeEndpoint } = require('./client');

const logger = Logger();
const { invokeEndpoint } = require('./client');

const getAuthAccount = async address => {
try {
const authAccountInfo = await invokeEndpoint('auth_getAuthAccount', { address });
return authAccountInfo;
} catch (err) {
if (err.message.includes(timeoutMessage)) {
throw new TimeoutException("Request timed out when calling 'getAuthAccount'.");
}
throw err;
}
const authAccountInfo = await invokeEndpoint('auth_getAuthAccount', { address });
return authAccountInfo;
};

const getAuthMultiSigRegMsgSchema = async () => {
try {
const multiSigRegMsgSchema = await invokeEndpoint('auth_getMultiSigRegMsgSchema');
return multiSigRegMsgSchema;
} catch (err) {
if (err.message.includes(timeoutMessage)) {
throw new TimeoutException("Request timed out when calling 'getMultiSigRegMsgSchema'.");
}
logger.warn(`Error returned when invoking 'auth_getMultiSigRegMsgSchema'.\n${err.stack}`);
throw err;
}
const multiSigRegMsgSchema = await invokeEndpoint('auth_getMultiSigRegMsgSchema');
return multiSigRegMsgSchema;
};

module.exports = {
Expand Down
39 changes: 31 additions & 8 deletions services/blockchain-connector/shared/sdk/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
const { Logger, Signals, HTTP } = require('lisk-service-framework');
const {
Logger,
Signals,
HTTP,
Exceptions: { TimeoutException },
} = require('lisk-service-framework');
const { createWSClient, createIPCClient } = require('@liskhq/lisk-api-client');

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 config = require('../../config');
Expand Down Expand Up @@ -113,13 +118,14 @@ const getApiClient = async () => {
return cachedApiClients[0];
};

const isResponse2XX = input => !!(input.status === 200);
const isResponse2XX = response => !!String(response.status).startsWith('2');

let id = 0;
let id = -1;
// eslint-disable-next-line consistent-return
const invokeEndpoint = async (endpoint, params = {}, numRetries = NUM_REQUEST_RETRIES) => {
let retries = numRetries;
do {
id++;
try {
if (config.useHttpApi) {
const rpcRequest = {
Expand All @@ -131,10 +137,7 @@ const invokeEndpoint = async (endpoint, params = {}, numRetries = NUM_REQUEST_RE

const response = await HTTP.post(`${liskAddressHttp}/rpc`, rpcRequest);
return isResponse2XX(response)
? (() => {
id++;
return response.data.result;
})()
? response.data.result
: (() => {
logger.trace(
`Error when invoking endpoint ${endpoint} with params ${JSON.stringify(params)}: ${
Expand All @@ -148,9 +151,29 @@ const invokeEndpoint = async (endpoint, params = {}, numRetries = NUM_REQUEST_RE
const response = await apiClient._channel.invoke(endpoint, params);
return response;
} catch (err) {
if (retries && err.message.includes(timeoutMessage)) {
if (err.message.includes(timeoutMessage)) {
if (!retries) {
const exceptionMsg = Object.getOwnPropertyNames(params).length
? `Request timed out when calling '${endpoint}' with params:\n${JSON.stringify(
params,
null,
'\t',
)}.`
: `Request timed out when calling '${endpoint}'.`;

throw new TimeoutException(exceptionMsg);
}
await delay(ENDPOINT_INVOKE_RETRY_DELAY);
} else {
if (Object.getOwnPropertyNames(params).length) {
logger.warn(
`Error invoking '${endpoint}' with params:\n${JSON.stringify(params, null, ' ')}.\n${
err.stack
}`,
);
}

logger.warn(`Error occurred when calling '${endpoint}' :\n${err.stack}`);
throw err;
}
}
Expand Down
69 changes: 14 additions & 55 deletions services/blockchain-connector/shared/sdk/dynamicReward.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
const {
Logger,
Exceptions: { TimeoutException },
} = require('lisk-service-framework');

const { timeoutMessage, invokeEndpoint } = require('./client');
const { invokeEndpoint } = require('./client');
const { getRegisteredModules } = require('./endpoints_1');

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 logger = Logger();

let registeredRewardModule;
let rewardTokenID;

Expand All @@ -38,60 +31,26 @@ const cacheRegisteredRewardModule = async () => {
};

const getRewardTokenID = async () => {
try {
if (!rewardTokenID) {
const response = await invokeEndpoint(`${registeredRewardModule}_getRewardTokenID`);
rewardTokenID = response.tokenID;
}
return rewardTokenID;
} catch (err) {
if (err.message.includes(timeoutMessage)) {
throw new TimeoutException("Request timed out when calling 'getRewardTokenID'.");
}
logger.warn(
`Error returned when invoking '${registeredRewardModule}_getRewardTokenID'.\n${err.stack}`,
);
throw err;
if (!rewardTokenID) {
const response = await invokeEndpoint(`${registeredRewardModule}_getRewardTokenID`);
rewardTokenID = response.tokenID;
}
return rewardTokenID;
};

const getAnnualInflation = async height => {
try {
const annualInflation = await invokeEndpoint(`${registeredRewardModule}_getAnnualInflation`, {
height,
});
return annualInflation;
} catch (err) {
if (err.message.includes(timeoutMessage)) {
throw new TimeoutException(
`Request timed out when calling 'getAnnualInflation' with block height:${height}.`,
);
}
logger.warn(
`Error returned when invoking '${registeredRewardModule}_getAnnualInflation' with block height:${height}.\n${err.stack}`,
);
throw err;
}
const annualInflation = await invokeEndpoint(`${registeredRewardModule}_getAnnualInflation`, {
height,
});
return annualInflation;
};

const getDefaultRewardAtHeight = async height => {
try {
const defaultRewardResponse = await invokeEndpoint(
`${registeredRewardModule}_getDefaultRewardAtHeight`,
{ height },
);
return defaultRewardResponse;
} catch (err) {
if (err.message.includes(timeoutMessage)) {
throw new TimeoutException(
`Request timed out when calling 'getDefaultRewardAtHeight' for block height:${height}`,
);
}
logger.warn(
`Error returned when invoking '${registeredRewardModule}_getDefaultRewardAtHeight' with height: ${height}.\n${err.stack}`,
);
throw err;
}
const defaultRewardResponse = await invokeEndpoint(
`${registeredRewardModule}_getDefaultRewardAtHeight`,
{ height },
);
return defaultRewardResponse;
};

module.exports = {
Expand Down
Loading

0 comments on commit 2017eee

Please sign in to comment.