# Configuring Slot Leader Calculations

if \[\[ ! -d "$DIRECTORY/logs" ]]; then mkdir $DIRECTORY/logs; fi

\# create a pid, this way you can ps aux | grep leaderScheduleCheck.sh to see if script is running

echo $ > "$DIRECTORY/logs/leaderScheduleCheck.pid";

\# Set your own stake pool ID

\# Set the network magic value as needed for the testnet environment that you want to use

\# For details on available testnet environments, see <https://book.world.dev.cardano.org/environments.html>

\# Edit variable with $TESTNET for Testnet and $MAINNET for Mainnet

\# check for vrf.skey presence

if \[\[ ! -f "$DIRECTORY/vrf.skey" ]]; then echo "vrf.skey not found"; exit 127; fi

CCLI=$(which cardano-cli)

if \[\[ -z $CCLI ]]; then echo "cardano-cli command cannot be found, exiting..."; exit 127; fi

if \[\[ -z $JQ ]]; then echo "jq command cannot be found, exiting..."; exit 127; fi

read -ra BYRON\_GENESIS <<< "$(jq -r '\[ .startTime, .protocolConsts.k, .blockVersionData.slotDuration ] |@tsv' < $DIRECTORY/$network-byron-genesis.json)"

if \[\[ -z $BYRON\_GENESIS ]]; then echo "BYRON GENESIS config file not loaded correctly"; exit 127; fi

if \[ $network = $TESTNET ]; then

network\_magic="--testnet-magic $MAGICNUMBER"

elif \[ $network = $MAINNET ]; then

network\_magic="--mainnet"

echo "Incorrect network selected, please use $TESTNET or $MAINNET network type"; exit 1

\# Check that node is synced

sync\_progress=$($CCLI query tip $network\_magic | jq -r ".syncProgress")

if \[\[ $sync\_progress == "100.00" ]]; then

function getCurrentEpoch(){

echo $($CCLI query tip $network\_magic | jq -r ".epoch")

\# Get epoch start time based on current one

function getEpochStartTime(){

byron\_genesis\_start\_time=${BYRON\_GENESIS\[0]}

byron\_k=${BYRON\_GENESIS\[1]}

byron\_epoch\_length=$(( 10 \* byron\_k ))

byron\_slot\_length=${BYRON\_GENESIS\[2]}

echo $(( $byron\_genesis\_start\_time + (($(getCurrentEpoch) \* $byron\_epoch\_length \* $byron\_slot\_length) / 1000) ))

\# Get epoch end time based on the current one

function getEpochEndTime(){

\#calculate currentEpoch Start time + 5 days of epoch duration - 10 minutes(600s) to not overlap with next epoch

echo $(( $(getEpochStartTime)+(5\*86400)-(600) ))

function getCurrentTime(){

echo $(printf '%(%s)T\n' -1)

\# Convert timestamps to UTC time

function timestampToUTC(){

echo $(date +"%D %T" -ud @$timestamp)

\# Find the correct time to run the leaderslot check command

function getLeaderslotCheckTime(){

epochStartTime=$(getEpochStartTime)

epochEndTime=$(getEpochEndTime)

\# epoch completion percent to check for --next epoch leaderslots

checkTimestamp=$(( $epochStartTime+($percentage\*($epochEndTime-$epochStartTime)/100) ))

\# Function to make the script sleep until check need to be executed

if \[\[ $sleepSeconds -gt 0 ]]; then

echo "Script is going to sleep for: $sleepSeconds seconds"

\# Check leaderschedule of next epoch

function checkLeadershipSchedule(){

next\_epoch=$(( $(getCurrentEpoch)+1 ))

currentTime=$(getCurrentTime)

echo "Check is running at: $(timestampToUTC $currentTime) for epoch: $next\_epoch"

$CCLI query leadership-schedule $network\_magic --genesis "$DIRECTORY/$network-shelley-genesis.json" --stake-pool-id $STAKE\_POOL\_ID --vrf-signing-key-file "$DIRECTORY/vrf.skey" --next > "$DIRECTORY/logs/leaderSchedule\_$next\_epoch.txt"

echo "Current epoch: $(getCurrentEpoch)"

epochStartTimestamp=$(getEpochStartTime)

echo "Epoch start time: $(timestampToUTC $epochStartTimestamp)"

epochEndTimestamp=$(getEpochEndTime)

echo "Epoch end time: $(timestampToUTC $epochEndTimestamp)"

currentTime=$(getCurrentTime)

echo "Current cron execution time: $(timestampToUTC $currentTime)"

timestampCheckLeaders=$(getLeaderslotCheckTime)

echo "Next check time: $(timestampToUTC $timestampCheckLeaders)"

timeDifference=$(( $timestampCheckLeaders-$currentTime ))

if \[ -f "$DIRECTORY/logs/leaderSchedule\_$(( $(getCurrentEpoch)+1 )).txt" ]; then

echo "Check already done, check logs for results"; exit 1

elif \[\[ $timeDifference -gt 86400 ]]; then

echo "Too early to run the script, wait for next cron scheduled job"; exit 1

elif \[\[ $timeDifference -gt 0 ]] && \[\[ $timeDifference -le 86400 ]]; then

sleepUntil $timeDifference

echo "Check is starting on $(timestampToUTC $(getCurrentTime))"

echo "Script ended, schedule logged inside file: leaderSchedule\_$(( $(getCurrentEpoch)+1 )).txt"

elif \[\[ $timeDifference -lt 0 ]] && \[ ! -f "$DIRECTORY/logs/leaderSchedule\_$(( $(getCurrentEpoch)+1 )).txt" ]; then

echo "Check is starting on $(timestampToUTC $(getCurrentTime))"

echo "Script ended, schedule logged inside file: leaderSchedule\_$(( $(getCurrentEpoch)+1 )).txt"

echo "There were problems on running the script, check that everything is working fine"; exit 1

echo "Node not fully synced."; exit 1
