Skip to content

Commit

Permalink
feat(hapi): save bp_json_url
Browse files Browse the repository at this point in the history
  • Loading branch information
Torresmorah committed Jan 30, 2023
1 parent 90ea00a commit 9200bd7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 56 deletions.
90 changes: 48 additions & 42 deletions hapi/src/services/eosio.service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { StatusCodes } = require('http-status-codes')

const { axiosUtil, eosUtil, sequelizeUtil, producerUtil } = require('../utils')
const { eosConfig } = require('../config')

Expand Down Expand Up @@ -81,15 +83,27 @@ const getBPJsons = async (producers = []) => {
topProducers = await Promise.all(
topProducers.map(async producer => {
let bpJson = {}
let bpJsonUrl = ''
let healthStatus = []

if (producer.url && producer.url.length > 3) {
const producerUrl = getProducerUrl(producer)
const chains = await getChains(producerUrl)
const chainUrl = chains[eosConfig.chainId]
const bpJsonUrl = getBPJsonUrl(producerUrl, chainUrl || '/bp.json')

bpJson = await getBPJson(bpJsonUrl)
bpJsonUrl = getBPJsonUrl(producerUrl, chainUrl || '/bp.json')

try {
bpJson = await getBPJson(bpJsonUrl)
} catch (error) {
if (error.code === 'ECONNABORTED') {
return {
...producer,
bp_json_url: bpJsonUrl,
health_status: healthStatus,
bp_json: bpJson
}
}
}

if (bpJson && !chainUrl && !isEosNetwork) {
const { org, producer_account_name: name } = bpJson
Expand All @@ -98,42 +112,18 @@ const getBPJsons = async (producers = []) => {
...(org && { org }),
...(name && { producer_account_name: name })
}

}

if (!Object.keys(bpJson).length && producer.total_rewards >= 100) {
healthStatus.push({ name: 'bpJson', valid: false })

try {
const { status, statusText } = await axiosUtil.instance.get(
producerUrl
)

healthStatus.push({
name: 'website',
valid: status === 200,
bpJsonUrl,
response: { status, statusText }
})
} catch (error) {
healthStatus.push({
name: 'website',
valid: false,
bpJsonUrl,
response: {
status: error.response?.status,
statusText: error.response?.statusText || 'No response'
}
})
}
} else {
healthStatus = getProducerHealthStatus(bpJson)
}
healthStatus = await getProducerHealthStatus({
producer,
producerUrl,
bpJson,
})
}

return {
...producer,
endpoints: { api: [], ssl: [], p2p: [] },
bp_json_url: bpJsonUrl,
health_status: healthStatus,
bp_json: bpJson
}
Expand All @@ -148,13 +138,8 @@ const getBPJsonUrl = (producerUrl, chainUrl) => {
}

const getBPJson = async bpJsonUrl => {
let bpJson = {}

try {
const { data: _bpJson } = await axiosUtil.instance.get(bpJsonUrl)

bpJson = !!_bpJson && typeof _bpJson === 'object' ? _bpJson : bpJson
} catch (error) {}
const { data: _bpJson } = await axiosUtil.instance.get(bpJsonUrl)
const bpJson = !!_bpJson && typeof _bpJson === 'object' ? _bpJson : bpJson

return bpJson
}
Expand Down Expand Up @@ -186,10 +171,31 @@ const getChains = async producerUrl => {
}
}

const getProducerHealthStatus = bpJson => {
if (!bpJson || !Object.keys(bpJson).length) return []
const isNonCompliant = producer => {
return !Object.keys(producer.bpJson).length && producer.total_rewards >= 100
}

const getProducerHealthStatus = async producer => {
const healthStatus = []
const {bpJson, producerUrl} = producer

if (isNonCompliant(producer)) {
const response = await producerUtil.getUrlStatus(producerUrl)

healthStatus.push({ name: 'bpJson', valid: false })
healthStatus.push({
name: 'website',
valid: response?.status === StatusCodes.OK,
response: {
status: response?.status,
statusText: response?.statusText || 'No response'
}
})

return healthStatus
}

if (!bpJson || !Object.keys(bpJson).length) return []

healthStatus.push({
name: 'bpJson',
Expand Down
7 changes: 4 additions & 3 deletions hapi/src/services/producer.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const statsService = require('./stats.service')
const updateBPJSONs = async (producers = []) => {
const upsertMutation = `
mutation ($producers: [producer_insert_input!]!) {
insert_producer(objects: $producers, on_conflict: {constraint: producer_owner_key, update_columns: [ bp_json ]}) {
insert_producer(objects: $producers, on_conflict: {constraint: producer_owner_key, update_columns: [ bp_json, health_status ]}) {
affected_rows,
}
}
Expand All @@ -21,7 +21,7 @@ const updateBPJSONs = async (producers = []) => {
const updateProducers = async (producers = []) => {
const upsertMutation = `
mutation ($producers: [producer_insert_input!]!) {
insert_producer(objects: $producers, on_conflict: {constraint: producer_owner_key, update_columns: [ producer_key, unpaid_blocks,last_claim_time, url, location, producer_authority, is_active, total_votes, total_votes_percent, total_votes_eos, vote_rewards,block_rewards, total_rewards, endpoints, rank, health_status]}) {
insert_producer(objects: $producers, on_conflict: {constraint: producer_owner_key, update_columns: [ producer_key, unpaid_blocks,last_claim_time, url, location, producer_authority, is_active, total_votes, total_votes_percent, total_votes_eos, vote_rewards,block_rewards, total_rewards, endpoints, rank, bp_json_url]}) {
affected_rows,
returning {
id,
Expand All @@ -41,7 +41,8 @@ const updateProducers = async (producers = []) => {
let topProducers = producers.slice(0, eosConfig.eosTopLimit)

topProducers = topProducers.filter(
producer => producer?.bp_json && Object.keys(producer.bp_json).length > 0
producer =>
producer?.health_status && Object.keys(producer.health_status).length > 0
)
await nodeService.clearNodes()
await updateBPJSONs(topProducers)
Expand Down
26 changes: 15 additions & 11 deletions hapi/src/utils/producer.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ const eosUtil = require('./eos.util')
const hasuraUtil = require('./hasura.util')
const { eosConfig } = require('../config')

const getNodeInfo = async (api) => {
const getUrlStatus = async (url, api = '') => {
try {
const response = await axiosUtil.instance.get(`${api}/v1/chain/get_info`)
const response = await axiosUtil.instance.get(`${url}${api}`)

return {
nodeInfo: response.data,
status: response.status,
statusText: response.statusText
}
return response
} catch (error) {
return {
status: error.response?.status || null,
statusText: error.response?.statusText || 'No response'
}
return error.response
}
}

const getNodeInfo = async url => {
const response = await getUrlStatus(url, '/v1/chain/get_info')

return {
...(response?.data && { nodeInfo: response.data }),
status: response?.status || null,
statusText: response?.statusText || 'No response'
}
}

Expand Down Expand Up @@ -261,6 +264,7 @@ module.exports = {
getEndpoints,
getExpectedRewards,
getSupportedAPIs,
getUrlStatus,
getVotes,
jsonParse
}

0 comments on commit 9200bd7

Please sign in to comment.