Skip to content

Commit

Permalink
fix(validator): Correct uptime calculation to prevent 10000% display
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayagoumi committed Nov 7, 2023
1 parent e09d233 commit 8772776
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
88 changes: 84 additions & 4 deletions src/components/wallet/earn/Validate/ValidatorInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<div>
<Tooltip style="display: inline-block" :text="$t('validator.info.up_time')">
<v-icon class="icon-mdi-camino">mdi-arrow-up-bold</v-icon>
<label>{{ upTime.toFixed() }} %</label>
<label v-if="initialized">{{ upTime.toFixed() }} %</label>
<Spinner v-else />
</Tooltip>
</div>
<div>
Expand Down Expand Up @@ -77,14 +78,16 @@
</template>
<script lang="ts">
import 'reflect-metadata'
import { Component, Prop, Vue } from 'vue-property-decorator'
import { Component, Prop, Vue, Watch } from 'vue-property-decorator'
import Spinner from '@/components/misc/Spinner.vue'
import moment from 'moment'
import { ava } from '@/AVA'
import { BN } from '@c4tplatform/caminojs'
import AvaxInput from '@/components/misc/AvaxInput.vue'
import Tooltip from '@/components/misc/Tooltip.vue'
import { ValidatorRaw } from '@/components/misc/ValidatorList/types'
import { AvaNetwork } from '@/js/AvaNetwork'
import axios from 'axios'
@Component({
name: 'validator_info',
Expand All @@ -106,6 +109,8 @@ export default class ValidatorInfo extends Vue {
txID: string = ''
loading: boolean = true
nodeVersion: string = ''
initialized: boolean = false
mounted() {
this.getInformationValidator()
Expand All @@ -120,8 +125,83 @@ export default class ValidatorInfo extends Vue {
return ava.getNetwork().P.minStake
}
getInformationValidator() {
get activeNetwork(): null | AvaNetwork {
return this.$store?.state?.Network?.selectedNetwork
}
async fetchNodeVersion() {
if (this.activeNetwork && this.activeNetwork.url) {
await axios
.post(this.activeNetwork.url + '/ext/info', {
jsonrpc: '2.0',
id: 1,
method: 'info.getNodeVersion',
})
.then((res) => {
const data = res.data
if (data && data.result && data.result.gitVersion) {
this.nodeVersion = data.result.gitVersion.slice(1) // remove v
}
})
.finally(() => {
this.initialized = true
})
}
}
checkNodeVersionFlag(targetVersion: string): boolean {
if (!this.initialized) {
throw new Error('Provider not initialized yet')
}
if (!this.nodeVersion) {
throw new Error('Node version not exists, function uncallable')
}
const versionRegex = /^\d+\.\d+\.\d+(-rc\d+)?$/
if (!versionRegex.test(targetVersion)) {
throw new Error(
`Invalid version format: ${targetVersion}. Correct version is of type major.minor.path e.g 1.2.3-rc2`
)
}
const [coreTargetVersion, targetVariant] = targetVersion.split('-')
const [coreNodeVersion, nodeVariant] = this.nodeVersion.split('-')
const [targetMajor, targetMinor, targetPatch] = coreTargetVersion.split('.').map(Number)
const [nodeMajor, nodeMinor, nodePatch] = coreNodeVersion.split('.').map(Number)
if (targetMajor !== nodeMajor) {
return targetMajor < nodeMajor
}
if (targetMinor !== nodeMinor) {
return targetMinor < nodeMinor
}
if (targetPatch !== nodePatch) {
return targetPatch < nodePatch
}
if (nodeVariant) {
return targetVariant <= nodeVariant
}
return true
}
formatUptime(uptime: string): number {
const versionFlag = this.checkNodeVersionFlag('0.4.10-rc3')
const value = versionFlag
? Math.round(parseFloat(uptime))
: Math.round(parseFloat(uptime) * 100)
return value
}
async getInformationValidator() {
try {
await this.fetchNodeVersion()
this.loading = true
let today = moment()
Expand All @@ -131,7 +211,7 @@ export default class ValidatorInfo extends Vue {
this.endTime = moment(new Date(parseInt(this.nodeInfo.endTime) * 1000)).format(
'MMMM Do YYYY, h:mm:ss a'
)
this.upTime = parseFloat(this.nodeInfo.uptime) * 100
this.upTime = this.formatUptime(this.nodeInfo.uptime)
var reaminingValidationDuration = moment.duration(
moment(new Date(parseInt(this.nodeInfo.endTime) * 1000)).diff(today)
Expand Down
6 changes: 6 additions & 0 deletions src/views/wallet/Validator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ import { AddressState } from '@c4tplatform/caminojs/dist/apis/platformvm'
import Big from 'big.js'
import 'reflect-metadata'
import { Component, Vue, Watch } from 'vue-property-decorator'
import { AvaNetwork } from '@/js/AvaNetwork'
@Component({
name: 'validator',
Expand Down Expand Up @@ -157,6 +158,10 @@ export default class Validator extends Vue {
)
}
get activeNetwork(): null | AvaNetwork {
return this.$store?.state?.Network?.selectedNetwork
}
verifyValidatorIsReady(val: ValidatorRaw) {
this.nodeInfo = val
}
Expand All @@ -182,6 +187,7 @@ export default class Validator extends Vue {
clearInterval(this.intervalID)
}
@Watch('activeNetwork')
@Watch('$store.state.networkName')
@Watch('$store.state.activeWallet')
async evaluateCanRegisterNode() {
Expand Down

0 comments on commit 8772776

Please sign in to comment.