New Witness Management Script, even if not yet ready!

in #hive-engine3 years ago (edited)

Decided to show everyone how simple is to create some HA (high availability) using bash/shell scripting. Yes... old stuff. Don't blame me.. blame the code.

This is a simple way to get extra resilience

In Hive Engine as a witness. You can use this script on the same IP for now... I will create a new one for using it on another node as a kill switch or as a monitor. For now, this will do most of the things on the current code you are running the witness.

Disclaimer...

This script depends a lot on the performance of your node. I will try to make that more evident on comments soon, but for now, for the geeks that already know how this works (and trusting they will be laughing at me for the upload of this as bash/shell script), enjoy some of the initial insight of what might be a real thing if I have the time to finish.

image.png

The main objective is to share real experience.

The code

Check this repo (now private, moved)👉this one instead in the future.

But essentially this is:

#!/bin/bash
# Program: HIVE-Engine Auto Witness Node
# Description: Manages the sync of the node and the registration of the witness
# Author: forykw
# Date: 2021/02/27
# v1.0

# Initialise variables
SIGNING_BLOCKS="0"
REGISTER="0"

# Main loop
while [ true ]; do
# Validate if node is down
NODE_DOWN=`pm2 status prod-hivengwit |grep stopped|wc -l`
# Get number of blocks still missing from last streamer message
BLOCKS_MISSING=`tail -n 333 node_app.log |grep Streamer |grep head_block_number | tail -n 1 | cut -d" " -f 12`
# Search the log for X amount of streamer messages and count how many we were missing blocks
TIMES_MISSING=`tail -n 333 node_app.log |grep Streamer |grep head_block_number | grep -v "0\ blocks\ ahead"|wc -l`
# Update time
CURRENT_TIME=`date --iso-8601=seconds`

# Validate sync status
if [ "${NODE_DOWN}" == "1" ]; then
    echo "["${CURRENT_TIME}"] Node is DOWN."
    REGISTER="0"
elif [ "${NODE_DOWN}" == "0" ]; then
    # The order of the IFs matters!
        if [ "${TIMES_MISSING}" == "0" ] && [ "${BLOCKS_MISSING}" == "0" ]; then
                # No missed blocks, therefore we can be sure to continue signing or register
                echo "["${CURRENT_TIME}"] Witness State[${SIGNING_BLOCKS}] - Node is stable and in sync!"
                REGISTER="1"
    elif [ "${TIMES_MISSING}" == "1" ] || [ "${BLOCKS_MISSING}" == "1" ] || [ "${BLOCKS_MISSING}" == "0" ]; then
        # Let's assume for now that 1 block or one time behind is a evaluation zone (no decisions)
        echo "["${CURRENT_TIME}"] Witness State[${SIGNING_BLOCKS}] - Evaluation threshold..."
    else
                # If there are more than one message with missing blocks and still out of sync, then indicate to unregister if producing
                echo "["${CURRENT_TIME}"] Witness State[${SIGNING_BLOCKS}] - Node is unstable with: "${BLOCKS_MISSING}" blocks missing"
                REGISTER="0"
    fi
else
    echo "["${CURRENT_TIME}"] Unknown error"
fi

# (Un)Register Witness depeding on sync status
if [ "${SIGNING_BLOCKS}" == "0" ] && [ "${REGISTER}" == "1" ]; then
    echo "["${CURRENT_TIME}"] Registering Witness"
    SIGNING_BLOCKS="1"
    node witness_action.js register
    echo "["${CURRENT_TIME}"] Registration Broadcasted"
elif [ "${SIGNING_BLOCKS}" == "1" ] && [ "${REGISTER}" == "0" ]; then
    echo "["${CURRENT_TIME}"] Unregistering Witness"
    SIGNING_BLOCKS="0"
    node witness_action.js unregister
    echo "["${CURRENT_TIME}"] Unregistration Broadcasted"
fi

# Scan frequency
sleep 10
done

This is experimental stuff... so make yourself a good test environment before you decide to make this your life judgement. 😆

I will be testing and improving this one more into the future... so keep watching it if you like it.

It has been running for 8 hours already...

If you like this, feel free to vote for my witness node (@atexoras.witness) at https://tribaldex.com/witnesses

Sort:  

Gosh.. the difference between being the host of the post or not...