Forging Failover

## auto select forging script, version for 2 nodes by Corsaro
## Requirements are: 1 fixed IP (home IP, office IP, VPN or VPS are fine) and 2 cheap VPS

## https needed
## jq is needed. install it with:

## sudo apt-get install jq

## you have to whitelist on config.json (on the API and FORGING section), the IP of the machine where the script is running

## Inside the script you have to write your SECRET seed

## where you see: ‘{“secret”:”xyz”}’

## on this version, I use https on port 2443

#!/bin/bash

SRV1="91.121.64.139"

SRV2="84.200.32.170"

PRT1=":7000"

PRT2=":7000"

# use 7000 port for testnet

# use 8000 PORT FOR MAINNET

pbk="YOUR_BUBLICK_KEY_HERE"

while true; do

HEIGHT1=$(curl --connect-timeout 3 -s "http://"$SRV1""$PRT1"/api/loader/status/sync"| jq '.height')

HEIGHT2=$(curl --connect-timeout 3 -s "http://"$SRV2""$PRT2"/api/loader/status/sync"| jq '.height')

FORGE1=$(curl --connect-timeout 3 -s "http://"$SRV1""$PRT1"/api/delegates/forging/status?publicKey="$pbk| jq '.enabled')

FORGE2=$(curl --connect-timeout 3 -s "http://"$SRV2""$PRT2"/api/delegates/forging/status?publicKey="$pbk| jq '.enabled')
echo ""

echo $SRV1 " " $HEIGHT1 " " $FORGE1

echo $SRV2 " " $HEIGHT2 " " $FORGE2

echo ""

if ! [[ "$HEIGHT1" =~ ^[0-9]+$ ]]

then

echo $SRV1 " " "is off?"
HEIGHT1="0"

fi

if ! [[ "$HEIGHT2" =~ ^[0-9]+$ ]]

then

echo $SRV3 " " "is off?"
HEIGHT2="0"

fi

if [ "$HEIGHT1" -eq "$HEIGHT2" ]

then

echo everything is fine
exit

fi

if [ "$HEIGHT1" -gt "$HEIGHT2" ]

then
diff=$(( $HEIGHT1 - $HEIGHT2 ))

if [ "$diff" -gt "3" ]

then

curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"xyz"}' https://"$SRV1":2443/api/delegates/forging/enable

curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"xyz"}' https://"$SRV2":2443/api/delegates/forging/disable

echo "$SRV1" " "

echo is greater

fi

fi

if [ "$HEIGHT2" -gt "$HEIGHT1" ]

then
diff=$(( $HEIGHT2 - $HEIGHT1 ))

if [ "$diff" -gt "3" ]

then

curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"xyz"}' https://"$SRV1":2443/api/delegates/forging/disable

curl --connect-timeout 3 -k -H "Content-Type: application/json" -X POST -d '{"secret":"xyz"}' https://"$SRV2":8443/api/delegates/forging/enable

echo "$SRV2" " "

echo is greater

fi

fi

sleep 60

done