Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check for other peers ahead and node stalled. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,41 @@ const checkNode = async (nodeUrl, types) => {
//
// get relevant chain data
//
const [block, pendingExtrinsics, health] = await Promise.all([
const [block, health, peers] = await Promise.all([
api.rpc.chain.getBlock(),
api.rpc.author.pendingExtrinsics(),
api.rpc.system.health(),
api.rpc.system.peers(),
]);
const nPeers = health.peers.toNumber();
// const bestBlock = +block.block.header.number;
const bestBlock = +block.block.header.number;
// if we have no peers we should restart
if (nPeers === 0) {
process.exit(1)
}
console.log(nPeers);
// TODO: fix broken peer request
// const nPeersAhead = peers.toArray()
// .map((p) => +p.bestNumber > bestBlock + 10)
// .filter((ahead) => ahead === true)
// .length;
console.log(`${nPeers} peers connected`);

// Calculate the number of peers more then 10 blocks ahead
const nPeersAhead = peers.toArray()
.map((p) => +p.bestNumber > bestBlock + 10)
.filter((ahead) => ahead === true)
.length;

//
// try to read the last blocknum from a temporary file
//
// if it hasn't changed since the last run, and we are behind the
// majority of peer nodes, we may be stalled and should exit with an
// error
//
// TODO: fix broken peer request
// console.log(`${bestBlock} is our best block`);
// if (nPeersAhead > nPeers / 2) {
// const storage = await readFileAsync('/tmp/nodeup.lastblock');
// const lastBlocknum = parseInt(storage.toString());
// if (lastBlocknum === bestBlock) {
// console.log(`${nPeersAhead} of ${nPeers} peers are ahead of us`);
// console.log('throwing an error since the best block has not updated recently');
// process.exit(1);
// }
// await writeFileAsync('/tmp/nodeup.lastblock', bestBlock);
// }
console.log(`${bestBlock} is our best block`);
if (nPeersAhead > nPeers / 2) {
const storage = await readFileAsync('/tmp/nodeup.lastblock');
const lastBlocknum = parseInt(storage.toString());
if (lastBlocknum === bestBlock) {
console.log(`${nPeersAhead} of ${nPeers} peers are ahead of us`);
console.log('throwing an error since the best block has not updated recently');
process.exit(1);
}
await writeFileAsync('/tmp/nodeup.lastblock', bestBlock);
}
process.exit(0);
};

Expand Down