Skip to content

Commit

Permalink
Merge pull request #324 from cardano-community/cntools-kes
Browse files Browse the repository at this point in the history
[2.1.1] - KES calculation and basic health data
  • Loading branch information
dmitrystas committed Jul 12, 2020
2 parents d0bd591 + ca7a2f6 commit 7576d4c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 54 deletions.
11 changes: 11 additions & 0 deletions docs/Scripts/cntools-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this tool will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.1] - 2020-07-12
### Added
- Basic health check data on main menu
- Epoch, time until next epoch, node tip vs calculated reference tip and a warning if node is lagging behind.
- Address era and encoding to `Wallet >> Show`

### Changed
- KES calculation, now take into account the byron era and the transition period until shelley start
- Credit to Martin @ ATADA for inspiration on how to calculate this


## [2.0.1] - 2020-07-12
### Fixed
- Version fix to include patch version
Expand Down
145 changes: 102 additions & 43 deletions scripts/cnode-helper-scripts/cntools.library
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Any breaking changes (eg: that requires change of cntools.config, env or a change in priv folder would be considered breaking and will be exempt from auto-update)
CNTOOLS_MAJOR_VERSION=2
# Changes that can be applied without breaking existing functionality that can be applied from within CNTools
CNTOOLS_MINOR_VERSION=0
CNTOOLS_MINOR_VERSION=1
# Backwards compatible bug fixes. No additional functionality or major changes and can be applied from within CNTools
CNTOOLS_PATCH_VERSION=1
CNTOOLS_VERSION="${CNTOOLS_MAJOR_VERSION}.${CNTOOLS_MINOR_VERSION}.${CNTOOLS_PATCH_VERSION}"
Expand Down Expand Up @@ -359,24 +359,109 @@ getBlockTip() {
getSlotTip() {
$CCLI shelley query tip --cardano-mode --testnet-magic ${NWMAGIC} | jq -r '.slotNo'
}
# Command : getSlotTipRef
# Description: Get calculated slot number tip
# Return : string with tip on STDOUT
getSlotTipRef() {
shelley_genesis_start_time_sec=$(date --date=$(jq -r .systemStart "${GENESIS_JSON}") +%s)
byron_genesis_start_time_sec=$(jq -r .startTime "${BYRON_GENESIS_JSON}")
epoch_length=$(jq -r .epochLength "${GENESIS_JSON}")
slot_length=$(jq -r .slotLength "${GENESIS_JSON}")
byron_slots=$(( ( shelley_genesis_start_time_sec - byron_genesis_start_time_sec ) / 20 ))
byron_shelley_trans_slots=$(( ( 2 * epoch_length ) / 20 ))
byron_shelley_trans_end=$(( shelley_genesis_start_time_sec + ( 2 * epoch_length ) ))
current_time_sec=$(date -u +%s)

if [[ "${current_time_sec}" -lt "${transTimeEnd}" ]]; then
# In Transistion Phase between Shelley and Byron
echo $(( byron_slots + ( current_time_sec - shelley_genesis_start_time_sec ) / 20 ))
else
# In Shelley Phase
echo $(( byron_slots + byron_shelley_trans_slots + (( current_time_sec - byron_shelley_trans_end ) / slot_length ) ))
fi
}
# Command : getSlotTipDiff
# Description: Get difference between current node slot tip and calculated tip based on genesis file
# Return : diff on STDOUT
getSlotTipDiff() {
tip_ref=$(getSlotTipRef)
tip_node=$(getSlotTip)
echo $(( tip_ref - tip_node ))
}

slotInterval() {
slot_length=$(jq -r .slotLength "${GENESIS_JSON}")
active_slots_coeff=$(jq -r .activeSlotsCoeff "${GENESIS_JSON}")
echo "${slot_length} / ${active_slots_coeff}" | bc
}

# Command : getEpoch
# Description: Calculates current epoch based on slot tip
# Parameters : null
# Return : epoch on STDOUT
# Examples of Usage:
# >> epoch=$(getEpoch)
# Description: Offline calculation of current epoch based on genesis file
# Return : current epoch on STDOUT
getEpoch() {
echo $(( $(getSlotTip) / $(jq -r .epochLength ${GENESIS_JSON}) ))
genesis_start_time_sec=$(date --date=$(jq -r .systemStart "${GENESIS_JSON}") +%s)
current_time_sec=$(date -u +%s)
epoch_length=$(jq -r .epochLength "${GENESIS_JSON}")
current_epoch=$(( ( current_time_sec - genesis_start_time_sec ) / epoch_length ))
echo "${current_epoch}"
}
# Command : getTimeUntilNextEpoch
# Description: Offline calculation of time until next epoch
# Return : time left in hh:mm:ss
timeUntilNextEpoch() {
getEpoch >/dev/null
showTimeLeft $(( epoch_length - ( current_time_sec - genesis_start_time_sec ) + ( current_epoch * epoch_length ) ))
}

# Command : getCurrentKESperiod
# Description: Offline calculation of the current KES period
# Return : current KES period on STDOUT
getCurrentKESperiod() {
shelley_genesis_start_time_sec=$(date --date=$(jq -r .systemStart "${GENESIS_JSON}") +%s)
epoch_length=$(jq -r .epochLength "${GENESIS_JSON}")
slot_length=$(jq -r .slotLength "${GENESIS_JSON}")
byron_shelley_trans_end=$(( shelley_genesis_start_time_sec + ( 2 * epoch_length ) ))
slots_per_kes_period=$(jq -r .slotsPerKESPeriod "${GENESIS_JSON}")
current_time_sec=$(date -u +%s)
current_kes_period=$(( ( current_time_sec - byron_shelley_trans_end ) / ( slots_per_kes_period * slot_length ) ))
echo "${current_kes_period}"
}

# Command : kesExpiration [current KES period]
#
# Description: Calculate KES expiration
# Parameters : current KES period > KES start stored in POOL_CURRENT_KES_START file for pool in question
# Return : expiration date can be accessed through variable ${expiration_date} after function has been executed
kesExpiration() {
if [[ -z "${1##*[!0-9]*}" ]]; then
say "${RED}ERROR${NC}: current KES period must be an integer number [$1]"
return 1
fi
max_kes_evolutions=$(jq -r .maxKESEvolutions "${GENESIS_JSON}")
kes_expiration_period=$(( ${1} + ${max_kes_evolutions} - 1 ))
shelley_genesis_start_time_sec=$(date --date=$(jq -r .systemStart "${GENESIS_JSON}") +%s)
epoch_length=$(jq -r .epochLength "${GENESIS_JSON}")
slot_length=$(jq -r .slotLength "${GENESIS_JSON}")
slots_per_kes_period=$(jq -r .slotsPerKESPeriod "${GENESIS_JSON}")
byron_shelley_trans_end=$(( shelley_genesis_start_time_sec + ( 2 * epoch_length ) ))
expiration_time_sec=$(( ${byron_shelley_trans_end} + ( ${slot_length} * ${kes_expiration_period} * ${slots_per_kes_period} ) ))
current_time_sec=$(date -u +%s)
expiration_time_sec_diff=$(( expiration_time_sec - current_time_sec ))
expiration_date=$(date --date=@${expiration_time_sec})
}

showTimeLeft() {
if [[ -z "${1##*[!0-9]*}" ]]; then
say "${RED}ERROR${NC}: time must be an integer number [$1]"
return 1
fi
printf '%02dh:%02dm:%02ds\n' $(($1/3600)) $(($1%3600/60)) $(($1%60))
}


# Command : waitNewBlockCreated
# Description: Wait for a new block to be created
# Parameters : null
# Return : prints progress on STDOUT
# Examples of Usage:
# >> waitNewBlockCreated
waitNewBlockCreated() {
SLOT_DURATION=$(jq -r .slotLength "$GENESIS_JSON")
COUNTER=${TIMEOUT_NO_OF_SLOTS}
Expand Down Expand Up @@ -539,6 +624,13 @@ getRewardAddress() {
fi
}

# Command : getAddressInfo [address]
# Description: get address info from from node
# Return : populates $address_info
getAddressInfo() {
address_info=$(${CCLI} shelley address info --address $1)
}

# Command : getBalance [address]
# Description: check balance for provided address
# Return : populates $lovelace & $utx0_count
Expand Down Expand Up @@ -1507,39 +1599,6 @@ deRegisterPool() {
}


# Command : kesExpiration [current KES period]
#
# Description: Calculate KES expiration
# Parameters : current KES period > KES start stored in POOL_CURRENT_KES_START file for pool in question
# Return : expiration date can be accessed through variable ${expiration_date} after function has been executed
# Examples of Usage:
# >> kesExpiration 1234
#
kesExpiration() {
if [[ -z "${1##*[!0-9]*}" ]]; then
say "${RED}ERROR${NC}: current KES period must be an integer number [$1]"
return 1
fi

max_kes_evolutions=$(jq -r .maxKESEvolutions "${GENESIS_JSON}")
genesis_start_time_sec=$(date --date=$(jq -r .systemStart "${GENESIS_JSON}") +%s) # UTC
slot_duration=$(jq -r .slotLength "${GENESIS_JSON}")
slots_per_kes_period=$(jq -r .slotsPerKESPeriod "${GENESIS_JSON}")
kes_expiration_period=$(( ${1} + ${max_kes_evolutions} - 1 ))
expiration_time_sec=$(( ${genesis_start_time_sec} + ( ${slot_duration} * ${kes_expiration_period} * ${slots_per_kes_period} ) ))
expiration_time_sec_diff=$(( expiration_time_sec - $(date +%s) ))
expiration_date=$(date --date=@${expiration_time_sec})
}

showTimeLeft() {
if [[ -z "${1##*[!0-9]*}" ]]; then
say "${RED}ERROR${NC}: time must be an integer number [$1]"
return 1
fi
printf '%02dh:%02dm:%02ds\n' $(($1/3600)) $(($1%3600/60)) $(($1%60))
}


function printTable() {
local -r delimiter="${1}"
local -r data="$(removeEmptyLines "${2}")"
Expand Down
27 changes: 16 additions & 11 deletions scripts/cnode-helper-scripts/cntools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,17 @@ say " ) Pool - pool creation and management"
say " ) Blocks - show core node leader slots"
say " ) Update - update cntools script and library config files"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

say " What would you like to do?\n"
say "$(printf "%84s" "Epoch $(getEpoch) - $(timeUntilNextEpoch) until next")"
tip_diff=$(getSlotTipDiff)
slot_interval=$(slotInterval)
if [[ ${tip_diff} -le ${slot_interval} ]]; then
say "$(printf " %-20s %73s" "What would you like to do?" "Ref/Node Tip DIFF: ${GREEN}${tip_diff}${NC} slots")"
elif [[ ${tip_diff} -le $(( slot_interval * 3 )) ]]; then
say "$(printf " %-20s %73s" "What would you like to do?" "Ref/Node Tip DIFF: ${ORANGE}${tip_diff}${NC} slots")"
else
say "$(printf " %-20s %73s" "What would you like to do?" "Ref/Node Tip DIFF: ${RED}${tip_diff}${NC} slots **WARNING**")"
fi
say ""
case $(select_opt "[w] Wallet" "[f] Funds" "[p] Pool" "[b] Blocks" "[u] Update" "[q] Quit") in
0) OPERATION="wallet" ;;
1) OPERATION="funds" ;;
Expand Down Expand Up @@ -309,6 +318,9 @@ case $OPERATION in
say ""
say "$(printf "%-19s : %s" "Address" "${base_addr}")" "log"
say "$(printf "%-19s : ${CYAN}%s${NC} ADA" "Funds" "$(formatLovelace ${base_lovelace})")" "log"
getAddressInfo "${base_addr}"
say "$(printf "%-19s : %s" "Era" "$(jq -r '.era' <<< ${address_info})")" "log"
say "$(printf "%-19s : %s" "Encoding" "$(jq -r '.encoding' <<< ${address_info})")" "log"
say "$(printf "%-19s : %s" "Enterprise Address" "${pay_addr}")" "log"
say "$(printf "%-19s : ${CYAN}%s${NC} ADA" "Enterprise Funds" "$(formatLovelace ${pay_lovelace})")" "log"
getRewards ${wallet_name}
Expand Down Expand Up @@ -1441,10 +1453,7 @@ case $OPERATION in
say "\n"
say "# Register Stake Pool" "log"

#Calculate appropriate KES period
currSlot=$(getSlotTip)
slotsPerKESPeriod=$(jq -r .slotsPerKESPeriod $GENESIS_JSON)
start_kes_period=$(( currSlot / slotsPerKESPeriod ))
start_kes_period=$(getCurrentKESperiod)
echo "${start_kes_period}" > ${pool_saved_kes_start}
say "creating operational certificate" 1 "log"
${CCLI} shelley node issue-op-cert --kes-verification-key-file "${pool_hotkey_vk_file}" --cold-signing-key-file "${pool_coldkey_sk_file}" --operational-certificate-issue-counter-file "${pool_opcert_counter_file}" --kes-period "${start_kes_period}" --out-file "${pool_opcert_file}"
Expand Down Expand Up @@ -2240,11 +2249,7 @@ case $OPERATION in
pool_saved_kes_start="${POOL_FOLDER}/${pool_name}/${POOL_CURRENT_KES_START}"
pool_opcert_file="${POOL_FOLDER}/${pool_name}/${POOL_OPCERT_FILENAME}"

#Calculate appropriate KES period
currSlot=$(getSlotTip)
slotsPerKESPeriod=$(jq -r .slotsPerKESPeriod $GENESIS_JSON)
start_kes_period=$(( currSlot / slotsPerKESPeriod ))

start_kes_period=$(getCurrentKESperiod)
echo "${start_kes_period}" > ${pool_saved_kes_start}

say "creating new hot keys and certificate" 1
Expand Down
1 change: 1 addition & 0 deletions scripts/cnode-helper-scripts/env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CCLI=$(which cardano-cli)
CNODE_HOME=/opt/cardano/cnode
CONFIG=$CNODE_HOME/files/ptn0.yaml
GENESIS_JSON=$CNODE_HOME/files/genesis.json
BYRON_GENESIS_JSON=$CNODE_HOME/files/byron_genesis.json
NODE_SOCKET_PATH=$CNODE_HOME/sockets/node0.socket
export CARDANO_NODE_SOCKET_PATH=$CNODE_HOME/sockets/node0.socket
MAGIC=$(jq -r .protocolMagicId < $GENESIS_JSON)
Expand Down

0 comments on commit 7576d4c

Please sign in to comment.