Very large slot number for "cardano-cli query leadership-schedule" - why?

I’m new to this command. I just set up v1.34.1 node on testnet and ran this command:

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

And I get the following output:

 SlotNo                          UTC Time              

 60789431                   2022-06-13 22:17:27 UTC
 60803298                   2022-06-14 02:08:34 UTC
 60811493                   2022-06-14 04:25:09 UTC
 60817608                   2022-06-14 06:07:04 UTC
 60818935                   2022-06-14 06:29:11 UTC
 60824464                   2022-06-14 08:01:20 UTC
 60827224                   2022-06-14 08:47:20 UTC
 60856899                   2022-06-14 17:01:55 UTC
 60860277                   2022-06-14 17:58:13 UTC
 60862634                   2022-06-14 18:37:30 UTC
 60878191                   2022-06-14 22:56:47 UTC
 60890371                   2022-06-15 02:19:47 UTC
 60934967                   2022-06-15 14:43:03 UTC
 60965699                   2022-06-15 23:15:15 UTC
 60979778                   2022-06-16 03:09:54 UTC
 60995493                   2022-06-16 07:31:49 UTC
 61019400                   2022-06-16 14:10:16 UTC
 61082765                   2022-06-17 07:46:21 UTC
 61105585                   2022-06-17 14:06:41 UTC
 61175878                   2022-06-18 09:38:14 UTC
 61191064                   2022-06-18 13:51:20 UTC

Notice that slot number is way larger than the number of slots in an epoch. Was it wrong? Or it means something different from slotno in an epoch?

Thanks!

I suppose it’s the absolute slot number (since the beginning of Cardano), not the slot number in the epoch.

Current testnet epoch is 211. Each epoch has 432000 slots. So the number for total slots would add up to something like 90M, not 60M. Still not quite right …

Also, is there a way to just display slot number within the block?

If you look at a recent block on testnet https://testnet.cardanoscan.io/block/3631097, the “Absolute Slot” is in the range of 60 million. The slots per epoch were changed at the beginning of Shelley on mainnet. I suppose, it’s the same on testnet.

Sorry, don’t know if there is a command for slot in epoch. But should be easy to calculate knowing one correspondence.

1 Like

Ahh, that makes a lot of sense. Thanks!

Still looking for the answer to display relative slot number…

I could not tolerate the output and wrote a bash script to convert it the way I wanted. See code and new output below.

  No.	 slot/slot in epoch		PDT time
-------------------------------------------------------------
  1	61239129/24729		2022-06-18T20:12:25-07:00
  2	61248714/34314		2022-06-18T22:52:10-07:00
  3	61248716/34316		2022-06-18T22:52:12-07:00
  4	61253050/38650		2022-06-19T00:04:26-07:00
  5	61261021/46621		2022-06-19T02:17:17-07:00
  6	61271147/56747		2022-06-19T05:06:03-07:00
  7	61283462/69062		2022-06-19T08:31:18-07:00
  8	61297608/83208		2022-06-19T12:27:04-07:00
  9	61320610/106210		2022-06-19T18:50:26-07:00
  10	61326497/112097		2022-06-19T20:28:33-07:00
  11	61339737/125337		2022-06-20T00:09:13-07:00
  12	61363724/149324		2022-06-20T06:49:00-07:00
  13	61383698/169298		2022-06-20T12:21:54-07:00
  14	61428813/214413		2022-06-21T00:53:49-07:00
  15	61446344/231944		2022-06-21T05:46:00-07:00
  16	61454480/240080		2022-06-21T08:01:36-07:00
  17	61493526/279126		2022-06-21T18:52:22-07:00
  18	61500252/285852		2022-06-21T20:44:28-07:00
  19	61501421/287021		2022-06-21T21:03:57-07:00
  20	61508721/294321		2022-06-21T23:05:37-07:00
  21	61512202/297802		2022-06-22T00:03:38-07:00
  22	61552026/337626		2022-06-22T11:07:22-07:00
  23	61553416/339016		2022-06-22T11:30:32-07:00
  24	61573027/358627		2022-06-22T16:57:23-07:00
  25	61579096/364696		2022-06-22T18:38:32-07:00
  26	61584279/369879		2022-06-22T20:04:55-07:00
  27	61594018/379618		2022-06-22T22:47:14-07:00
  28	61610741/396341		2022-06-23T03:25:57-07:00
  29	61628466/414066		2022-06-23T08:21:22-07:00
# we add slot in epoch and PDT time to each block
function addMoreInfo() {
    local infile=$1
    local outfile=$2
    local count=1

    echo -e "  No.\t slot/slot in epoch\t\tPDT time" | tee $outfile
    echo "-------------------------------------------------------------" | tee -a $outfile
    while IFS="" read -r p || [ -n "$p" ]
    do
        if [[ ! "$p" =~ .*[0-9]" UTC" ]]; then
            continue;
        fi

        set $p
        local slot=$1
        shift
        local UTC_Time="$*"

        slotInEpoch=$(($slot - $SHELLEY_SLOT_START - ($epoch_no - $SHELLEY_EPOCH_START)*$SLOTS_IN_EPOCH))
        local utcTime=$(date -u -d "$UTC_Time" -I"seconds")
        local pstTime=$(TZ='America/Los_Angeles' date -d "$UTC_Time" -I"seconds")
        echo -e "  $count\t$slot/$slotInEpoch\t\t$pstTime" | tee -a $outfile

        count=$(($count + 1))
    done < $infile
}
2 Likes

This is neat!

Thank you for sharing!

By the way, would you mind to share full script?

Thank you!

Here you go

#!/bin/bash

set -e                  # exit on error
set -o pipefail         # exit on pipeline error
set -u                  # treat unset variable as error

# current epoch or next?
if [[ $# > 0 ]] && [[ $1 == "--current" ]];then
    EPOCH="--current"
else
    EPOCH="--next"
fi

# which network are we on? 
source ~/config-inc.sh

# set network magic
# on mainnet, Byron era has 208 (0-207) epoch with 21600 slots in each epoch
# (total byron slots 4,492,800)
# on testnet, Byron era has 74 (0-73) epoch with 21600 slots ineach epoch
# (total 1598400)
# Shelley era has 432,000 slots in each epoch
if [[ $NETWORK == "mainnet" ]]; then
    NETWORK_MAGIC="--mainnet"
    SHELLEY_EPOCH_START=208
    SHELLEY_SLOT_START=4492800
elif [[ $NETWORK == "testnet" ]]; then
    NETWORK_MAGIC="--testnet-magic 1097911063"
    SHELLEY_EPOCH_START=74
    SHELLEY_SLOT_START=1598400
else
    echo "unknown network $NETWORK. quitting"
    exit 1
fi
SLOTS_IN_EPOCH=432000

# other config variables
NODE_HOME=/home/ubuntu/cardano
STAKE_POOL_ID=$(cat $NODE_HOME/stake-pool-id.txt)
export CARDANO_NODE_SOCKET_PATH=$NODE_HOME/db/socket
CCLI=cardano-cli

# create logs directory
if [[ ! -d "$NODE_HOME/logs" ]]; then 
    mkdir $NODE_HOME/logs; 
fi 

# create a pid while we are running
PID_FILE=$NODE_HOME/logs/leaderScheduleCheck.pid
echo $$ > $PID_FILE

##########################################################################################

function myerror() {
    echo -e "ERROR : $1"
    echo
    echo -e "QUITING ...."
    rm -f $PID_FILE
    exit 1
}

# 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
    echo $(( $(getEpochStartTime)+(5*86400) ))
}

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

# Check leaderschedule of next epoch
function checkLeadershipSchedule() {
    epoch_no=$(getCurrentEpoch)
    if [[ $EPOCH == "--next" ]]; then
        epoch_no=$(( $epoch_no + 1 ))
    fi

    epoch_file="$NODE_HOME/logs/leaderSchedule_$epoch_no.txt"
    if [[ -f $epoch_file ]]; then
        echo
        echo "epoch leader schedule already exist : $epoch_file"
        echo
        cat $epoch_file
    else
        echo
        echo "creating epoch leader schedule: $epoch_file"
        echo
        $CCLI query leadership-schedule $NETWORK_MAGIC \
            --genesis "$NODE_HOME/$NETWORK-shelley-genesis.json" \
            --stake-pool-id $STAKE_POOL_ID \
            --vrf-signing-key-file "$NODE_HOME/vrf.skey" \
            $EPOCH > /tmp/junk
        addMoreInfo /tmp/junk $epoch_file
    fi
}

# we add slot in epoch and PDT time to each block
function addMoreInfo() {
    local infile=$1
    local outfile=$2
    local count=1

    echo -e "  No.\t slot/slot in epoch\t\tPDT time" | tee $outfile
    echo "-------------------------------------------------------------" | tee -a $outfile
    while IFS="" read -r p || [ -n "$p" ]
    do
        if [[ ! "$p" =~ .*[0-9]" UTC" ]]; then
            continue;
        fi

        set $p
        local slot=$1
        shift
        local UTC_Time="$*"

        slotInEpoch=$(($slot - $SHELLEY_SLOT_START - ($epoch_no - $SHELLEY_EPOCH_START)*$SLOTS_IN_EPOCH))
        local utcTime=$(date -u -d "$UTC_Time" -I"seconds")
        local pstTime=$(TZ='America/Los_Angeles' date -d "$UTC_Time" -I"seconds")
        echo -e "  $count\t$slot/$slotInEpoch\t\t$pstTime" | tee -a $outfile

        count=$(($count + 1))
    done < $infile
}

##########################################################################################

# fetch byron parameters
TEMP=$(jq -r '[ .startTime, .protocolConsts.k, .blockVersionData.slotDuration ] |@tsv' < $NODE_HOME/$NETWORK-byron-genesis.json)
read -ra BYRON_GENESIS <<< $TEMP
if [[ -z $BYRON_GENESIS ]]; then 
    myerror "BYRON GENESIS config file not loaded correctly"
fi

# checking sync'ing progress
if [ ! isSynced ];then
    myerror "Node not fully synced."
fi

# now start by showing some info and times
echo "Pool name :               $POOL_NAME"
echo "Pool ticker:              $POOL_TICKER"
echo

echo "Current epoch:            $(getCurrentEpoch)"

epochStartTimestamp=$(getEpochStartTime)
echo "Epoch start time:         $(timestampToUTC $epochStartTimestamp) UTC"
epochEndTimestamp=$(getEpochEndTime)
echo "Epoch end time:           $(timestampToUTC $epochEndTimestamp) UTC"
timestampCheckLeaders=$(getLeaderslotCheckTime)
echo "Check time for next:      $(timestampToUTC $timestampCheckLeaders) UTC"
currentTime=$(getCurrentTime)
echo "Current execution time:   $(timestampToUTC $currentTime) UTC"
timeDifference=$(( $timestampCheckLeaders-$currentTime ))
if [[ $timeDifference -gt 0 ]] && [[ $EPOCH == "--next" ]]; then
    myerror "Too early to check for next epoch"
fi

# actually do the checking
checkLeadershipSchedule

# done
rm -f $PID_FILE
echo "Done!"
1 Like