Created
January 26, 2022 19:20
-
-
Save ib0b/c53024c634ca670ec509abc3ae646c78 to your computer and use it in GitHub Desktop.
Track BSC node sync status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//checks if local noded is sync then sends SMS message using Twillio | |
document.title = "Sync Watcher" | |
const Web3 = require("web3") | |
const net = require('net'); | |
const state = { | |
messageSent: false, | |
watcherInterval: null, | |
lastSyncStatus: null, | |
previousStatuses: [0, 0, 0], | |
lastOutOfSyncTime: null, | |
outOfSyncGaps: [] | |
} | |
const accountSid = 'xxxxxxxxxxxxxxxxx'; | |
const authToken = 'yyyyyyyyyyyyyyyyyyyy'; | |
const client = require('twilio')(accountSid, authToken); | |
//will send message only once, so you can take action and resync or rest the watcher by setting state.messageSent to false | |
async function sendMessage(message) { | |
if (state.messageSent) return | |
console.log(`Sending message ${new Date().toISOString()} `, message) | |
let response = await client.messages | |
.create({ | |
body: message, | |
messagingServiceSid: 'MGwwwwwwwwwwwwwwwwwwwwww', | |
to: '+12345678910' | |
}) | |
console.log('sent message ', message, response); | |
state.messageSent = true | |
} | |
function start() { | |
console.log(`started`) | |
state.watcherInterval = setInterval(watcher, 60000) | |
} | |
async function watcher() { | |
//initConnections | |
let web3 = new Web3(new Web3.providers.IpcProvider('/root/mainnet/geth.ipc', net)); | |
let isSyncing = await web3.eth.isSyncing() | |
state.lastSyncStatus = { time: new Date(), isSyncing } | |
let status = 0 | |
if (isSyncing !== false) { | |
console.log(`Lost sync ${new Date()} status =`, isSyncing) | |
if (state.lastOutOfSyncTime !== null) { | |
let gap = (Date.now() - state.lastOutOfSyncTime) / 60_000 | |
state.outOfSyncGaps.push(gap) | |
} | |
state.lastOutOfSyncTime = Date.now() | |
status = 1 | |
} | |
//check all three previous ==1 | |
if (state.previousStatuses.reduce((a, b) => a + b, 0) == 3) { | |
sendMessage(`Node lost sync ${new Date()}`) | |
} | |
state.previousStatuses.push(status) | |
state.previousStatuses.shift() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment