Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - cncli.sh - integration of full stakepool history load into epochdata table #1793

Merged
merged 23 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a34741d
added new function getProtocolParamsHist in env file
Jul 30, 2024
8fe2d58
added changes in getConsensus function of cncli.sh
Jul 30, 2024
448aeb5
added requested changes for epochdata
Aug 1, 2024
fa5c98d
updated getConsensus function to use getProtocolParams on next_epoch …
Aug 2, 2024
c5eb3fe
added last requested changes for epochdata process
Aug 2, 2024
bc9f59f
small syntax change
Aug 2, 2024
9fe6995
cncli epochdata: updated sql query for EPOCHS variable:
Aug 2, 2024
af949fd
added last required changes on epochdata process
Aug 3, 2024
4fcb524
removed --ledger-set param and replaced with --epoch in funcs: runCur…
Aug 3, 2024
919b529
1. EPOCH variable in echo string, replaced with $1:
Aug 3, 2024
0b89e4f
updated epochdata process to widely use ${1} as epoch number input
Aug 3, 2024
2cdd481
updated processAllEpochs function to use epoch var from epoch list lo…
Aug 3, 2024
4d78d02
Merge branch 'alpha' into cncli_epochdata_load
Scitz0 Aug 3, 2024
1b0b6b0
[Mithril] script updates (#1785)
TrevorBenson Aug 10, 2024
48a3e02
Merge branch 'alpha' into cncli_epochdata_load
rdlrt Aug 10, 2024
8fdf2a5
Merge branch 'alpha' into cncli_epochdata_load
TrevorBenson Aug 14, 2024
731efe1
Merge branch 'alpha' into cncli_epochdata_load
rdlrt Sep 4, 2024
d8dfec0
Merge branch 'alpha' into cncli_epochdata_load
rdlrt Sep 11, 2024
89abf3a
Merge branch 'alpha' into cncli_epochdata_load
rdlrt Sep 19, 2024
2d214b7
1. fixed linting issues.
Sep 19, 2024
2963267
Merge branch 'alpha' into cncli_epochdata_load
rdlrt Sep 23, 2024
cf20483
1. Added check stability window on runNextEpoch
Sep 24, 2024
8016b09
Merge branch 'alpha' into cncli_epochdata_load
Scitz0 Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 179 additions & 6 deletions scripts/cnode-helper-scripts/cncli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ usage() {
force Manually force leaderlog calculation and overwrite even if already done, exits after leaderlog is calculated
validate Continously monitor and confirm that the blocks made actually was accepted and adopted by chain (deployed as service)
all One-time re-validation of all blocks in blocklog db
epoch One-time re-validation of blocks in blocklog db for the specified epoch
epoch One-time re-validation of blocks in blocklog db for the specified epoch
epochdata Manually re-calculate leaderlog to load stakepool history into epochdata table of blocklog db. Needs completion of cncli.sh sync and cncli.sh validate processes.
all One-time re-calculation of all epochs (avg execution duration: 1hr / 50 epochs)
epoch One-time re-calculation for the specified epoch
TrevorBenson marked this conversation as resolved.
Show resolved Hide resolved
ptsendtip Send node tip to PoolTool for network analysis and to show that your node is alive and well with a green badge (deployed as service)
ptsendslots Securely sends PoolTool the number of slots you have assigned for an epoch and validates the correctness of your past epochs (deployed as service)
force Manually force pooltool sendslots submission ignoring configured time window
force Manually force pooltool sendslots submission ignoring configured time window
init One-time initialization adding all minted and confirmed blocks to blocklog
metrics Print cncli block metrics in Prometheus format
deploy Install dependencies and deploy cncli monitoring agent service (available through port specified by CNCLI_PROM_PORT)
Expand Down Expand Up @@ -126,11 +129,15 @@ getLedgerData() { # getNodeMetrics expected to have been already run
}

getConsensus() {
getProtocolParams
if isNumber "$1"; then
getProtocolParamsHist "$(( $1 - 1 ))" || return 1
else
getProtocolParams || return 1
fi
if versionCheck "9.0" "${PROT_VERSION}"; then
consensus="cpraos"
stability_window_factor=3
elif versionCheck "8.0" "${PROT_VERSION}"; then
elif versionCheck "7.0" "${PROT_VERSION}"; then
rdlrt marked this conversation as resolved.
Show resolved Hide resolved
consensus="praos"
stability_window_factor=2
else
Expand Down Expand Up @@ -794,10 +801,173 @@ cncliPTsendslots() {
done
}

#################################
# epochdata table load process #
#################################

getCurrNextEpoch() {
getNodeMetrics
curr_epoch=${epochnum}
next_epoch=$((curr_epoch+1))
}

runCurrentEpoch() {
getKoiosData
echo "Processing current epoch: ${1}"
stake_param_curr="--active-stake ${active_stake_set} --pool-stake ${pool_stake_set}"

${CNCLI} leaderlog ${cncliParams} --consensus "${consensus}" --epoch="${1}" ${stake_param_curr} |
jq -r '[.epoch, .epochNonce, .poolId, .sigma, .d, .epochSlotsIdeal, .maxPerformance, .activeStake, .totalActiveStake] | @csv' |
sed 's/"//g' >> "$tmpcsv"
}

runNextEpoch() {
Scitz0 marked this conversation as resolved.
Show resolved Hide resolved
getKoiosData
echo "Processing next epoch: ${1}"
stake_param_next="--active-stake ${active_stake_mark} --pool-stake ${pool_stake_mark}"

${CNCLI} leaderlog ${cncliParams} --consensus "${consensus}" --epoch="${1}" ${stake_param_next} |
jq -r '[.epoch, .epochNonce, .poolId, .sigma, .d, .epochSlotsIdeal, .maxPerformance, .activeStake, .totalActiveStake] | @csv' |
sed 's/"//g' >> "$tmpcsv"
}

runPreviousEpochs() {
[[ -z ${KOIOS_API} ]] && return 1
if ! pool_hist=$(curl -sSL -f "${KOIOS_API}/pool_history?_pool_bech32=${POOL_ID_BECH32}&_epoch_no=${1}" 2>&1); then
echo "ERROR: Koios pool_stake_snapshot history query failed."
return 1
fi

if ! epoch_hist=$(curl -sSL -f "${KOIOS_API}/epoch_info?_epoch_no=${1}" 2>&1); then
echo "ERROR: Koios epoch_stake_snapshot history query failed."
return 1
fi

pool_stake_hist=$(jq -r '.[].active_stake' <<< "${pool_hist}")
active_stake_hist=$(jq -r '.[].active_stake' <<< "${epoch_hist}")

echo "Processing previous epoch: ${1}"
stake_param_prev="--active-stake ${active_stake_hist} --pool-stake ${pool_stake_hist}"

${CNCLI} leaderlog ${cncliParams} --consensus "${consensus}" --epoch="${1}" ${stake_param_prev} |
jq -r '[.epoch, .epochNonce, .poolId, .sigma, .d, .epochSlotsIdeal, .maxPerformance, .activeStake, .totalActiveStake] | @csv' |
sed 's/"//g' >> "$tmpcsv"

return 0
}

processAllEpochs() {
getCurrNextEpoch
IFS=' ' read -r -a epochs_array <<< "$EPOCHS"

for epoch in "${epochs_array[@]}"; do
if ! getConsensus "${epoch}"; then echo "ERROR: Failed to fetch protocol parameters for epoch ${epoch}."; return 1; fi
if [[ "$epoch" == "$curr_epoch" ]]; then
runCurrentEpoch ${epoch}
elif [[ "$epoch" == "$next_epoch" ]]; then
runNextEpoch ${epoch}
else
runPreviousEpochs ${epoch}
Scitz0 marked this conversation as resolved.
Show resolved Hide resolved
fi
done

id=1
while IFS= read -r row; do
echo "$id,$row" >> "$csvfile"
((id++))
done < "$tmpcsv"

sqlite3 "$BLOCKLOG_DB" <<EOF
DELETE FROM epochdata;
VACUUM;
.mode csv
.import '$csvfile' epochdata
REINDEX epochdata;
EOF

row_count=$(sqlite3 "$BLOCKLOG_DB" "SELECT COUNT(*) FROM epochdata;")
echo "$row_count rows have been loaded into epochdata table in blocklog db"
echo "~ CNCLI epochdata table load completed ~"

rm $csvfile $tmpcsv
}

processSingleEpoch() {
getCurrNextEpoch
IFS=' ' read -r -a epochs_array <<< "$EPOCHS"

unset matched
for epoch in "${epochs_array[@]}"; do
[[ ${epoch} = "$1" ]] && matched=true && break
done
if [[ -z ${matched} ]]; then
echo -e "No slots found in blocklog table for epoch ${1}.\n"
echo -e "choose from epochs in list:\n $EPOCHS"; return 1
fi
if ! getConsensus "${1}"; then echo "ERROR: Failed to fetch protocol parameters for epoch ${1}."; return 1; fi
if [[ "$1" == "$curr_epoch" ]]; then
runCurrentEpoch ${1}
elif [[ "$1" == "$next_epoch" ]]; then
runNextEpoch ${1}
else
runPreviousEpochs ${1}
fi

ID=$(sqlite3 "$BLOCKLOG_DB" "SELECT max(id) + 1 FROM epochdata;")
csv_row=$(cat "$tmpcsv")
modified_csv_row="${ID},${csv_row}"
echo "$modified_csv_row" > "$onerow_csv"

sqlite3 "$BLOCKLOG_DB" "DELETE FROM epochdata WHERE epoch = ${1};"
sqlite3 "$BLOCKLOG_DB" <<EOF
.mode csv
.import "$onerow_csv" epochdata
REINDEX epochdata;
EOF
row_count=$(sqlite3 "$BLOCKLOG_DB" "SELECT COUNT(*) FROM epochdata WHERE epoch = ${1};")
echo "$row_count row has been loaded into epochdata table in blocklog db for epoch ${1}"
echo "~ CNCLI epochdata table load completed ~"
echo

rm $onerow_csv $tmpcsv
}

cncliEpochData() {
getNodeMetrics
cncliParams="--db ${CNCLI_DB} --byron-genesis ${BYRON_GENESIS_JSON} --shelley-genesis ${GENESIS_JSON} --pool-id ${POOL_ID} --pool-vrf-skey ${POOL_VRF_SKEY} --tz UTC"
EPOCHS=$(sqlite3 "$BLOCKLOG_DB" "SELECT group_concat(epoch,' ') FROM (SELECT DISTINCT epoch FROM blocklog ORDER BY epoch);")
csvdir=/tmp ; tmpcsv="${csvdir}/epochdata_tmp.csv" ; csvfile="${csvdir}/epochdata.csv" ; onerow_csv="${csvdir}/one_epochdata.csv"
true > "$tmpcsv" ; true > "$csvfile" ; true > "$onerow_csv"

proc_msg="~ CNCLI epochdata table load started ~"

if ! cncliDBinSync; then
echo ${proc_msg}
echo "CNCLI DB out of sync :( [$(printf "%2.4f %%" ${cncli_sync_prog})] ... check cncli sync service!"
exit 1
else
echo ${proc_msg}
getLedgerData

if [[ "${subcommand}" == "epochdata" ]]; then
if [[ ${subarg} == "all" ]]; then
processAllEpochs
elif isNumber "${subarg}"; then
processSingleEpoch "${subarg}"
else
echo
echo "ERROR: unknown argument passed to validate command, valid options incl the string 'all' or the epoch number to recalculate"
echo
exit 1
fi
fi
fi
}

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

case ${subcommand} in
sync )
sync )
cncliInit && cncliSync ;;
leaderlog )
cncliInit && cncliLeaderlog ;;
Expand All @@ -811,5 +981,8 @@ case ${subcommand} in
cncliInit && cncliInitBlocklogDB ;;
metrics )
cncliMetrics ;; # no cncliInit needed
* ) usage ;;
epochdata )
cncliInit && cncliEpochData ;;
* )
usage ;;
esac
18 changes: 18 additions & 0 deletions scripts/cnode-helper-scripts/env
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,24 @@ getProtocolParams() {
[[ ${PROT_MAJOR} -eq 0 ]] && return 1 || return 0
}

getProtocolParamsHist() {
Scitz0 marked this conversation as resolved.
Show resolved Hide resolved
if [[ -z ${KOIOS_API} ]]; then
return 1
fi

if ! PROT_PARAMS=$(curl -sSL -f -X GET -H "accept: application/json" "${KOIOS_API}/epoch_params?_epoch_no=${1}" 2>&1); then
return 2
fi

# Extract historical protocol major and minor versions in TSV format
read -r PROT_MAJOR PROT_MINOR <<<"$(jq -r '.[0] | [
.protocol_major //0,
.protocol_minor //0
] | @tsv' <<<"${PROT_PARAMS}" 2>/dev/null)"
PROT_VERSION="${PROT_MAJOR}.${PROT_MINOR}"

}

telegramSend() {
if [[ -z "${TG_BOT_TOKEN}" ]] || [[ -z "${TG_CHAT_ID}" ]]; then
echo "Warn: to use the telegramSend function you must first set the bot and chat id in the env file"
Expand Down