Skip to content

Commit

Permalink
Merge pull request #31 from Adamant-im/dev
Browse files Browse the repository at this point in the history
v1.8.0
  • Loading branch information
martiliones authored Jan 13, 2023
2 parents cac266d + c5dc8fb commit 1c19cd6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant-api",
"version": "1.7.0",
"version": "1.8.0",
"description": "REST API for ADAMANT Blockchain",
"main": "src/index.js",
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {

epochTime: new Date(Date.UTC(2017, 8, 2, 17, 0, 0, 0)),
fees: {
send: 50000000,
Expand Down Expand Up @@ -27,6 +26,7 @@ module.exports = {
STATE: 9,
},
maxVotesPerTransaction: 33,
HEALTH_CHECK_TIMEOUT: 4000, // 4 seconds
SAT: 100000000,
RE_HEX: /^[a-fA-F0-9]+$/,
RE_BASE64: /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/,
Expand All @@ -42,5 +42,4 @@ module.exports = {

RE_HTTP_URL: /^https?:\/\/(.*)$/,
RE_IP: /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/,

};
16 changes: 11 additions & 5 deletions src/helpers/healthCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ const socket = require('./wsClient');
const logger = require('./logger');
const validator = require('./validator');

const {RE_IP, RE_HTTP_URL} = require('./constants');
const {RE_IP, RE_HTTP_URL, HEALTH_CHECK_TIMEOUT} = require('./constants');

const CHECK_NODES_INTERVAL = 60 * 5 * 1000; // Update active nodes every 5 minutes
const HEIGHT_EPSILON = 5; // Used to group nodes by height and choose synced

module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallback) => {
module.exports = (
nodes,
checkHealthAtStartup = true,
timeout = HEALTH_CHECK_TIMEOUT,
checkHealthAtStartupCallback,
) => {
const nodesList = nodes;
let isCheckingNodes = false;
let startupCallback = checkHealthAtStartupCallback;
Expand Down Expand Up @@ -56,7 +61,7 @@ module.exports = (nodes, checkHealthAtStartup = true, checkHealthAtStartupCallba
try {
const start = unixTimestamp();

const req = await checkNode(`${node}/api/node/status`);
const req = await checkNode(`${node}/api/node/status`, timeout);

const [url] = node.replace(RE_HTTP_URL, '$1').split(':');
const ifIP = RE_IP.test(url);
Expand Down Expand Up @@ -215,10 +220,11 @@ async function getIP(url) {
/**
* Requests status from a single ADAMANT node
* @param {string} url Node URL to request
* @param {number} timeout Request timeout
* @return {Promise} Node's status information
*/
function checkNode(url) {
return axios.get(url)
function checkNode(url, timeout) {
return axios.get(url, {timeout})
.then((response) => ({status: response.data}))
.catch((err) => false);
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const keys = require('./helpers/keys');

/**
* Create new API instance
* @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string}} params
* @param {{ node: string, checkHealthAtStartup: boolean, logLevel: string, checkHealthTimeout: number}} params
* @param {{ error: function, warn: function, info: function, log: function }?} customLogger
* @param {function?} checkHealthAtStartupCallback callback which is called after the first health check or
* just at startup when params.checkHealthAtStartup is passed
Expand All @@ -38,6 +38,7 @@ module.exports = (params, customLogger, checkHealthAtStartupCallback) => {
const nodeManager = healthCheck(
params.node,
params.checkHealthAtStartup,
params.checkNodeTimeout,
checkHealthAtStartupCallback,
);

Expand Down

0 comments on commit 1c19cd6

Please sign in to comment.