-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patherrorChecker.js
76 lines (67 loc) · 3.08 KB
/
errorChecker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const axios = require('axios');
const addressVoteMap = new Map();
const FULL_NODE_IP = "34.125.96.129"
const proofsInEpochMap = new Map()
const THREE_HRS_IN_MS = 10800000
async function checkForErrors(validator, response) {
let errors = []
const notiFyForArray = validator.notifyFor;
let validatorIpAddress = validator.ipAddress
if ((notiFyForArray.includes('NOT_IN_SYNC') || notiFyForArray.includes('ALL')) && !response.items.is_synced) {
errors.push('not in sync');
}
if ((notiFyForArray.includes('NOT_IN_VALIDATOR_SET') || notiFyForArray.includes('ALL')) && !response.items.validator_set) {
errors.push('not in validator set');
}
if ((notiFyForArray.includes('NOT_RUNNING') || notiFyForArray.includes('ALL')) && !response.items.node_running) {
errors.push('not running');
}
if(notiFyForArray.includes('VOTES_IN_EPOCH_UNCHANGED') || notiFyForArray.includes('ALL')){
let validatorAddress = response.account_view.address
let voteKey = validatorAddress + validator.description
for(val of response.chain_view.validator_view){
if(val.account_address == validatorAddress.toUpperCase()){
let voteCount = val.vote_count_in_epoch
let mapVal = addressVoteMap.get(voteKey)
if(addressVoteMap.has(voteKey) &&
mapVal.votes === voteCount){
errors.push(`votes in epoch: ${voteCount}, unchanged in > 5 min`) }
else {
addressVoteMap.set(voteKey, {votes: voteCount, timestamp: Date.now()})
}
}
}
}
if(notiFyForArray.includes('PROOFS_IN_EPOCH_UNCHANGED') || notiFyForArray.includes('ALL')){
let proofsInEpoch = await getProofsInEpoch(response.account_view.address)
let proofKey = validatorIpAddress + validator.description
if(proofsInEpochMap.has(proofKey)){
let oldProofs = proofsInEpochMap.get(proofKey).proofs
let lastProofChangeTimestamp = proofsInEpochMap.get(proofKey).timestamp
if(oldProofs < 8 && oldProofs == proofsInEpoch &&
Date.now() - lastProofChangeTimestamp > THREE_HRS_IN_MS ){
errors.push(`proofs in epoch: ${proofsInEpoch}. No new proofs in last 3 hours`)
}
if(oldProofs != proofsInEpoch){
proofsInEpochMap.set(proofKey, {proofs : proofsInEpoch, timestamp: Date.now() })
}
}
else{
proofsInEpochMap.set(proofKey, {proofs : proofsInEpoch, timestamp: Date.now() })
}
}
return errors
}
async function getProofsInEpoch(account){
try{
const response = await axios.post(`http://${FULL_NODE_IP}:8080`,
{"jsonrpc": "2.0", "id": 1, "method": "get_tower_state_view", "params": [account] },
{timeout: 500})
return response.data.result.actual_count_proofs_in_epoch
}
catch(error){
console.log(`Can't connect to ${FULL_NODE_IP}`)
return Number.MAX_VALUE
}
}
module.exports = {checkForErrors}