[GUIDE/SCRIPT] Automagically stops snark work prior of getting a block proposal

Very useful for people with a single node to make sure you have enough CPU resource to hash the block


Logic work as follow
-Look at next proposal time
-Calculate time left before it happens
-IF block is within 3 minutes, stop snark works for 10 minutes
-ELSE sleep for HALF the remaining time and check again if its within 3 minutes

1-Prerequisites

sudo apt install jq screen -y
wget https://cdn.discordapp.com/attachments/601215883939020852/671457151788777473/snarkstopper.sh

2-Makes script executable

chmod +x snarkstopper.sh

3-Edit your public key

line 36
coda client set-snark-worker -address YOURBPUBLICKEY

4-Start the script

screen -dmS SnarkStopper bash snarkstopper.sh
CTRL A+D to exit screen to normal view
screen -r SnarkStopper to bring back the view

Full script below, feel free to use and improve!

#!/bin/bash

# Set readonly variables
readonly SECONDS_PER_MINUTE=60
readonly SECONDS_PER_HOUR=3600

while true
do 
# Get next proposal time and remove "
NEXTPROP="$(coda client status -json | jq .next_proposal[1])"
NEXTPROP="${NEXTPROP:1}"
NEXTPROP="${NEXTPROP:0:-1}"
echo "Next prop is at  $NEXTPROP"

# Get current time and calculate time left before next proposal
NOW="$(date +%s%N | cut -b1-13)"
echo "Where are now at $NOW"

TIMEBEFORENEXT="$(($NEXTPROP-$NOW))"
echo "Time before next block $TIMEBEFORENEXT"

TIMEBEFORENEXTSEC="${TIMEBEFORENEXT:0:-3}"
echo "in seconds $TIMEBEFORENEXTSEC "

TIMEBEFORENEXTMIN="$((${TIMEBEFORENEXTSEC} / ${SECONDS_PER_MINUTE}))"
echo "in minutes $TIMEBEFORENEXTMIN"

# Check if the next proposal is within 3 minutes
if [ $TIMEBEFORENEXTMIN -lt 3 ]
then
    echo "Stop snarking"
    coda client set-snark-worker
    echo "Sleep 600"
    sleep 600
    echo "Start snarking"
    coda client set-snark-worker -address YOURBPUBLICKEY
else
    # Do nothing
    echo "Block too far away"
fi

SLEEP="$((${TIMEBEFORENEXTSEC} / 2))"
    echo "Sleep for $SLEEP"
    sleep $SLEEP
done
4 Likes

Thanks to this script, after I ran it, the node I was running successfully added minted a new block.

1 Like

now coda client status -json | jq .next_proposal[1] is not working, because the status value in json is returned in text, although in the previous release it was in milliseconds
Check it - coda client status -json | jq .next_proposal
OUTPUT: "None this epoch… checking at in 7.027h"

I made python script. Now it works through API - https://github.com/c29r3/coda-snark-stopper