Skip to content

Instantly share code, notes, and snippets.

@l00k
Created November 24, 2021 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l00k/3c3ecb56b851c7ba20cd1f4e485a30a9 to your computer and use it in GitHub Desktop.
Save l00k/3c3ecb56b851c7ba20cd1f4e485a30a9 to your computer and use it in GitHub Desktop.
Submit node sync progress to telegram bot
const http = require('http');
function request (method, url, body)
{
const bodyRaw = JSON.stringify(body);
const urlOptions = new URL(url);
const options = {
protocol: urlOptions.protocol,
host: urlOptions.host,
hostname: urlOptions.hostname,
port: urlOptions.port,
path: urlOptions.pathname,
method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(bodyRaw)
}
}
return new Promise((resolve, reject) => {
let rawData = '';
const req = http.request(options, (resp) => {
resp.setEncoding('utf8');
resp.on('data', (chunk) => {
rawData += chunk;
});
resp.on('end', () => {
if (resp.headers['content-type'].includes('json')) {
resolve({ response: resp, data: JSON.parse(rawData) });
}
else {
resolve({ response: resp, data: rawData });
}
});
});
req.on('error', (e) => {
reject(e);
});
req.write(bodyRaw);
req.end();
})
}
const nodeRpcRequest = async (url, rpcMethod, params = []) => {
const body = {
jsonrpc: '2.0',
id: 1,
method: rpcMethod,
params,
};
const { response, data } = await request('POST', url, body);
return data.result;
}
(async() => {
const nodeState = {
nodeKey: process.env.NODE_KEY,
relayChain: {
peers: 0,
highestBlock: 0,
finalizedBlock: 0,
},
paraChain: {
peers: 0,
highestBlock: 0,
finalizedBlock: 0,
}
};
{
const response = await nodeRpcRequest(
process.env.NODE_RELAYCHAIN_URL,
'system_health'
);
nodeState.relayChain.peers = response.peers;
}
{
const response = await nodeRpcRequest(
process.env.NODE_RELAYCHAIN_URL,
'system_syncState'
);
nodeState.relayChain.finalizedBlock = response.currentBlock;
nodeState.relayChain.highestBlock = response.highestBlock;
}
{
const response = await nodeRpcRequest(
process.env.NODE_PARACHAIN_URL,
'system_health'
);
nodeState.paraChain.peers = response.peers;
}
{
const response = await nodeRpcRequest(
process.env.NODE_PARACHAIN_URL,
'system_syncState'
);
nodeState.paraChain.finalizedBlock = response.currentBlock;
nodeState.paraChain.highestBlock = response.highestBlock;
}
const { response, data } = await request(
'POST',
process.env.API_URL,
nodeState
);
console.log(response.statusCode, response.statusMessage);
if (response.statusCode !== 200) {
console.dir(data, { depth: 10 });
}
})();
@l00k
Copy link
Author

l00k commented Nov 24, 2021

Run this script in < 15minutes frames (5 minutes should be ok)

#!/bin/bash

API_URL=http://phala-watchdog.100k.dev:4003/state/node \
NODE_KEY=<your node key> \
NODE_PARACHAIN_URL=http://<nodeHostName>:<nodeRpcParachainPort> \
NODE_RELAYCHAIN_URL=http://<nodeHostName>:<nodeRpcRelaychainPort> \
    node ./node-state-submit.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment