Configuring Slot Leader Calculations

๐Ÿ”ฅ Hot tip: You can calculate your slot leader schedule, which tells you when it's your stake pools turn to mint a block. This can help you know what time is best to schedule maintenance on your stake pool. It can also help verify your pool is minting blocks correctly when it is your pool's turn. This is to be setup and run on the block producer node.

Since version 1.34, it is possible to check the slot leadership schedule for the current and next epoch using cardano-cli.

Next epoch's leadership schedule becomes available 1.5 days (36 hours) before the end of the current epoch.


Next epoch's leadership schedule is obtained with the following:

cardano-cli query leadership-schedule \
--mainnet \
--genesis $NODE_HOME/shelley-genesis.json \
--stake-pool-id $(cat $NODE_HOME/stakepoolid.txt) \
--vrf-signing-key-file $NODE_HOME/vrf.skey \
--next

Current epoch's leadership schedule is obtained with the following:

cardano-cli query leadership-schedule \
--mainnet \
--genesis $NODE_HOME/shelley-genesis.json \
--stake-pool-id $(cat $NODE_HOME/stakepoolid.txt) \
--vrf-signing-key-file $NODE_HOME/vrf.skey \
--current

Example leadership schedule output:

SlotNo                          UTC Time
-------------------------------------------------------------
     4073                   2021-12-29 17:26:54.998001755 UTC
     4126                   2021-12-29 17:27:00.298001755 UTC
     4206                   2021-12-29 17:27:08.298001755 UTC
     4256                   2021-12-29 17:27:13.298001755 UTC
     4309                   2021-12-29 17:27:18.598001755 UTC
     4376                   2021-12-29 17:27:25.298001755 UTC
     4423                   2021-12-29 17:27:29.998001755 UTC
     4433                   2021-12-29 17:27:30.998001755 UTC

๐Ÿ” Automate the process with Cronjob:

The automation of this process will work with the following method, as said, next epoch blocks can be checked 1.5 days before the start of the next epoch or at the 75% of the current epoch's completion. What the script will do, is to calculate the the correct day and hour to run the command, then wait until it is possible to do that and once the selected time comes, run the check listed below. Once finished, it will redirect the output into a log file that can be analyzed.

Keep in mind that running the leadership-schedule command, listed below and used by the script, with the cardano-node at the same time, will use approximately 17GB of RAM at the time of writing this guide (April 2022).

The possible solutions to avoid a node crash are:

Credits to Techs2help for developing the script.

Create the leaderScheduleCheck.sh script file in the block producer (script can also be run on a relay node but vrf.skey needs to be exported there) and paste the following code inside of it:

#!/bin/bash

# cardano node directory
DIRECTORY=

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
STAKE_POOL_ID=""

TESTNET="testnet"
MAINNET="mainnet"

# 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
MAGICNUMBER="1"

# Edit variable with $TESTNET for Testnet and $MAINNET for Mainnet
network=$TESTNET

# 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

JQ=$(which jq)
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

network_magic=""
if [ $network = $TESTNET ]; then
    network_magic="--testnet-magic $MAGICNUMBER"
elif [ $network = $MAINNET ]; then
    network_magic="--mainnet"
else
    echo "Incorrect network selected, please use $TESTNET or $MAINNET network type"; exit 1
fi


# Check that node is synced
function isSynced(){
    isSynced=false

    sync_progress=$($CCLI query tip $network_magic | jq -r ".syncProgress")
    if [[ $sync_progress == "100.00" ]]; then
        isSynced=true
    fi

    echo $isSynced
}

# Get current epoch
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) ))
}

# Get current timestamp
function getCurrentTime(){
    echo $(printf '%(%s)T\n' -1)
}

# Convert timestamps to UTC time
function timestampToUTC(){
    timestamp=$1
    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
    percentage=75
    checkTimestamp=$(( $epochStartTime+($percentage*($epochEndTime-$epochStartTime)/100) ))

    echo $checkTimestamp
}

# Function to make the script sleep until check need to be executed
function sleepUntil(){
    sleepSeconds=$1
    if [[ $sleepSeconds -gt 0 ]]; then
        echo "Script is going to sleep for: $sleepSeconds seconds"
        sleep $sleepSeconds
    fi
}

# 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"
}

if [ isSynced ];then
    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))"
            checkLeadershipSchedule
        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))"
                checkLeadershipSchedule
                echo "Script ended, schedule logged inside file: leaderSchedule_$(( $(getCurrentEpoch)+1 )).txt"
    else
        echo "There were problems on running the script, check that everything is working fine"; exit 1
    fi
else
    echo "Node not fully synced."; exit 1
fi

Set the following variables with your data:

# cardano node directory, directory where all files needed for running a cardano-node are located
DIRECTORY=

# Set your own stake pool ID
STAKE_POOL_ID=""

# Set variable with $TESTNET for Testnet and $MAINNET for Mainnet
network=

Add execution permissions and test that the script is running without errors:

chmod +x leaderScheduleCheck.sh
./leaderScheduleCheck.sh

If everything is working correctly, an output as the follow will be presented:

Current epoch: 199 Epoch start time: 04/14/22 20:20:16 Epoch end time: 04/19/22 20:10:16 Current cron execution time: 04/18/22 15:37:51 Next check time: 04/18/22 14:12:46 [...] Cutted output cause it can vary based on time when the script is ran

Configure Cronjob to make the script run automatically:

To configure the job at the start of an epoch, keep in mind the following information:

  • Epoch in MAINNET starts at 21:45 UTC

Find the time when the cronjob should start:

Cronjobs run based on local timezone, not on UTC hours. \

Find timezone:

timedatectl | grep "Time zone"

Once you found your timezone, you need to understand when run the job (It isn't mandatory to run it at epoch's starting hour). Here is an example with a UTC+2 timezone for Mainnet:

Epoch starting hour UTC: 21:45 Epoch starting hour for requested timezone: 23:45 Cronjob will be set to run at 23:45

Add cronjob and edit parameters based on your needs, PATH, NODE_HOME, NODE_CONFIG, CARDANO_NODE_SOCKET_PATH, MM, HH, path_to_script and desired_log_folder:

cat > $NODE_HOME/crontab-fragment.txt << EOF
# disable MTA use
MAILTO=""
# linux path, needed because cron doesn't know where to find cardano-cli
PATH=
# folder with cardano-node files
NODE_HOME=
# testnet or mainnet
NODE_CONFIG=
# path to the socket of cardano node, should be under db/ folder under NODE_HOME
CARDANO_NODE_SOCKET_PATH=

MM HH * * * path_to_script/leaderScheduleCheck.sh > desired_log_folder/leaderSchedule_logs.txt 2>&1
EOF
crontab -l | cat - ${NODE_HOME}/crontab-fragment.txt > ${NODE_HOME}/crontab.txt && crontab ${NODE_HOME}/crontab.txt
rm ${NODE_HOME}/crontab-fragment.txt

Once the cronjob is set, the script will be run every day and it will check if in the next 24H, it will be the correct time to run the command and see if there are scheduled blocks in the next epoch. For every epoch, there will be a file called leaderSchedule_epoch.txt

Your slot leader log should remain confidential. If you share this information publicly, an attacker could use this information to attack your stake pool.

Last updated