Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
okdas committed Jan 13, 2022
1 parent f7a5dc8 commit d4a459b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ app.get("/metrics", async (req, res) => {

init();

app.listen(9000);
const server = app.listen(9000);

console.log("Listening on :9000/metrics");

process.on("SIGTERM", () => {
console.log("SIGTERM signal received: closing HTTP server");
server.close(() => {
console.log("HTTP server closed");
});
});
39 changes: 28 additions & 11 deletions src/pokt-poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const {
const POCKET_DISPATCHER = new URL(RPC_ADDRESS || "http://localhost:8081");
const rpcProvider = new HttpRpcProvider(POCKET_DISPATCHER);
const pocket = new Pocket([POCKET_DISPATCHER], rpcProvider);
const status = {
RPCAvailable: false,
};

export const isRPCAvailable = () => status.RPCAvailable;

const height = new Gauge({
name: "pocket_node_height",
Expand Down Expand Up @@ -52,9 +57,15 @@ const isJailed = new Gauge({
const nodeStatus = new Gauge({
name: "pocket_node_status",
help: "0 - Unstaked, 1 - Unstaking, 2 - Staked",
labelNames: ["address"],
labelNames: ["address", "status"],
});

const statuses = {
0: "unstaked",
1: "unstaking",
2: "staked",
};

const stakedBalance = new Gauge({
name: "pocket_node_staked_balance_upokt",
help: "Balance of the staked node",
Expand All @@ -65,6 +76,10 @@ const updateHeight = async () => {
try {
const resp = await pocket.rpc().query.getHeight();
if (resp instanceof QueryHeightResponse) {
if (isRPCAvailable() === false) {
status.RPCAvailable = true;
}

height.set(Number(resp.height));
} else {
// console.log(resp);
Expand Down Expand Up @@ -92,7 +107,9 @@ const updateNode = async (address: string) => {
const resp = await pocket.rpc().query.getNode(address);
if (resp instanceof QueryNodeResponse) {
isJailed.labels({ address }).set(resp.node.jailed ? 1 : 0);
nodeStatus.labels({ address }).set(Number(resp.node.status));
nodeStatus
.labels({ address, status: statuses[Number(resp.node.status)] })
.set(1);
stakedBalance.labels({ address }).set(Number(resp.node.stakedTokens));
} else {
// console.log(resp);
Expand All @@ -103,18 +120,22 @@ const updateNode = async (address: string) => {
};

const performChecks = async () => {
updateHeight();

const addresses = (CHECK_ADDRESSES || "").split(",");
if (VALIDATOR_ADDRESS) {
addresses.push(VALIDATOR_ADDRESS);
}

CHECK_HEIGHT != "false" ? updateHeight() : null;

addresses.forEach((addr) => {
[...new Set(addresses)].forEach((addr) => {
updateAccount(addr);
updateNode(addr);
});

if (!(PERFORM_RELAY_SIMULATIONS === "false")) {
simulateRelaysForChains(configuredChains);
}

if (!(CHECK_VALIDATOR === "false")) {
const address = getValidator();
const node = await pocket.rpc().query.getNode(address);
Expand All @@ -123,17 +144,11 @@ const performChecks = async () => {
}

const stakedChains = node.node.chains;
const configuredChains = getConfiguredChains();

stakedChains.forEach(function (chain) {
if (!configuredChains.includes(chain)) {
stakedChainMissingConfig.set({ chain }, 1);
}
});

if (!(PERFORM_RELAY_SIMULATIONS === "false")) {
simulateRelaysForChains(configuredChains);
}
}
};

Expand All @@ -158,6 +173,8 @@ const getConfiguredChains = () => {
return JSON.parse(chainsJson).map((el) => el.id) as string[];
};

const configuredChains = getConfiguredChains();

export function init() {
(async () => {
setInterval(async () => {
Expand Down
6 changes: 6 additions & 0 deletions src/relay-simulations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import { Gauge, Counter } from "prom-client";
import { isRPCAvailable } from "./pokt-poller";

const { RPC_ADDRESS } = process.env;
const rpcAddress = RPC_ADDRESS || "http://localhost:8081";
Expand Down Expand Up @@ -150,6 +151,11 @@ const supportedChains = {
} as Chains;

export const simulateRelaysForChains = async (chains: string[]) => {
if (isRPCAvailable() === false) {
console.log("RPC is not available yet");
return;
}

chains.forEach(async (chain) => {
if (!supportedChains[chain]) {
simRelayChainNotImplemented.inc({ chain });
Expand Down

0 comments on commit d4a459b

Please sign in to comment.