Skip to content

Commit

Permalink
Merge pull request #44 from eco-stake/fix-final-address-for-fallback-…
Browse files Browse the repository at this point in the history
…apis

Fix final address for fallback API path
  • Loading branch information
tombeynon authored Oct 6, 2022
2 parents bdde4c2 + dc54514 commit db8759d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
23 changes: 13 additions & 10 deletions status/healthMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ function HealthMonitor() {
const request = async () => {
try {
let address = new URL(url.address).href.replace(/\/$|$/, '/')
const response = await getLatestBlock(address, type, urlPath(type))
return buildUrl(type, chain, url, urlHealth, response);
const { path, response } = await getLatestBlock(address, type, urlPath(type))
return buildUrl({ type, chain, url, urlHealth, path, response });
} catch (error) {
return buildUrl(type, chain, url, urlHealth, undefined, error);
return buildUrl({ type, chain, url, urlHealth, error });
}
};
return queue.add(request, { identifier: url.address });
}

async function getLatestBlock(url, type, path){
try {
return await got.get(url + path, {
timeout: { request: HEALTH_TIMEOUT },
retry: { limit: 1 },
agent: agent
});
return {
path,
response: await got.get(url + path, {
timeout: { request: HEALTH_TIMEOUT },
retry: { limit: 1 },
agent: agent
})
}
} catch (error) {
const fallback = fallbackPath(type)
if (fallback && fallback !== path && error.response?.statusCode === 501) {
Expand Down Expand Up @@ -84,15 +87,15 @@ function HealthMonitor() {
}
}

function buildUrl(type, chain, url, urlHealth, response, error) {
function buildUrl({ type, chain, url, urlHealth, path, response, error }) {
let timings, body, data
let blockTime = urlHealth?.blockTime
let blockHeight = urlHealth?.blockHeight || 0;
let finalAddress = urlHealth?.finalAddress
if (!error) {
({ timings, body } = response)
data = JSON.parse(body);
const regex = new RegExp(`${urlPath(type)}/?$`)
const regex = new RegExp(`${path}/?$`)
finalAddress = new URL(response.url).href.replace(regex, '').replace(/\/$|$/, '/');
({ error, blockTime, blockHeight } = checkHeader(type, data, chain.chainId));
}else{
Expand Down
9 changes: 6 additions & 3 deletions validators/validatorsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ function ValidatorsController(chainRegistry, validatorRegistry) {

router.get('/chains/:chain/:validatorAddress', async (ctx, next) => {
const chain = await chainRegistry.getChain(ctx.params.chain);
let validatorAddress = ctx.params.validatorAddress
let registryValidator = await validatorRegistry.getRegistryValidatorFromAddress(validatorAddress)
let validator = await validatorRegistry.getChainValidator(chain, validatorAddress, registryValidator)
let validator
if(chain){
let validatorAddress = ctx.params.validatorAddress
let registryValidator = await validatorRegistry.getRegistryValidatorFromAddress(validatorAddress)
validator = await validatorRegistry.getChainValidator(chain, validatorAddress, registryValidator)
}
renderJson(ctx, validator && {
name: chain.path,
validator: validator
Expand Down

0 comments on commit db8759d

Please sign in to comment.