Skip to content

Instantly share code, notes, and snippets.

@clemensgg
Last active September 14, 2022 10:48
Show Gist options
  • Save clemensgg/de8f3b36fc47a6331b6ba3d33e2de6a2 to your computer and use it in GitHub Desktop.
Save clemensgg/de8f3b36fc47a6331b6ba3d33e2de6a2 to your computer and use it in GitHub Desktop.
scan tendermint rpc, analyze block time & sig rate for a specific validator
#!bin/bash
RPC="http://localhost:2002"
STARTBLOCK=5241330
ENDBLOCK=5250000
TOTAL_VALIDATORS=135
CCHEX="72B1489EFB57A680577A838A5BAAEBE162A7C802"
BLOCK=$STARTBLOCK
NBLOCKS=0
NBLOCKS_PROPOSED=0
SUM_DIFF=0
SUM_SIGRATE=0
LAST_BT=$(date -u +"%s.%N")
echo "N, height_proposed, time_diff, av_time_diff, signatures, sign_rate, last_block_sign_rate" >> log.csv
while [ $BLOCK -le $ENDBLOCK ] ; do
echo "> walking block $BLOCK"
NBLOCKS=$((NBLOCKS+1))
# fetch block
RES=$(curl -s $RPC/block?height=$BLOCK)
BT=$(date -u -d $(echo $RES | jq -r '.result.block.header.time') +"%s.%N")
PROPOSER=$(echo $RES | jq -r '.result.block.header.proposer_address')
LASTCOMMIT_SIGS=$(echo $RES | jq -r '.result.block.last_commit.signatures')
if [[ "$NBLOCKS" -ge 2 ]] ; then
# calculate time difference to last header time
TDIFF=$(date -u -d "2000/1/1 $BT sec - $LAST_BT sec" +"%H:%M:%S.%N")
IFS=':' read -ra tdiffarray <<< $TDIFF
TDIFF_S=${tdiffarray[2]}
# calculate averages
SUM_DIFF=$(echo $SUM_DIFF $TDIFF_S | awk '{print $1 + $2}')
AV_DIFF=$(echo $SUM_DIFF $NBLOCKS | awk '{print $1 / $2}')
# if CryptoCrew proposed, scan this block + next block for signatures
if [[ "$PROPOSER" == "$CCHEX" ]] ; then
NBLOCKS_PROPOSED=$((NBLOCKS_PROPOSED+1))
CCHEIGHT=$BLOCK
# count signatures
readarray -t sigarray < <(jq -c '.[]' <<< $LASTCOMMIT_SIGS)
NUM_SIGS_LAST_BLOCK=0
for validator in ${sigarray[@]}; do
VAL_SIG=$(echo $validator | jq -r '.signature')
if [[ $VAL_SIG != "null" ]] ; then
NUM_SIGS_LAST_BLOCK=$((NUM_SIGS_LAST_BLOCK+1))
fi
done
SIG_RATE_LAST_BLOCK=$(echo $NUM_SIGS_LAST_BLOCK $TOTAL_VALIDATORS | awk '{print $1 / $2 * 100}')
echo "$BLOCK proposed by CryptoCrew"
fi
if [[ $BLOCK = $((CCHEIGHT + 1)) ]] ; then
# count signatures
readarray -t sigarray < <(jq -c '.[]' <<< $LASTCOMMIT_SIGS)
NUM_SIGS=0
for validator in ${sigarray[@]}; do
VAL_SIG=$(echo $validator | jq -r '.signature')
if [[ $VAL_SIG != "null" ]] ; then
NUM_SIGS=$((NUM_SIGS+1))
fi
done
SIG_RATE=$(echo $NUM_SIGS $TOTAL_VALIDATORS | awk '{print $1 / $2 * 100}')
# print infos, write csv
echo "diff sec: $TDIFF"
echo "av. diff sec: $AV_DIFF"
echo "signatures: $NUM_SIGS / $TOTAL_VALIDATORS - sign_rate: $SIG_RATE %"
echo "last block signatures: $NUM_SIGS_LAST_BLOCK / $TOTAL_VALIDATORS - sign_rate: $SIG_RATE_LAST_BLOCK %"
echo "$NBLOCKS_PROPOSED, $CCHEIGHT, $TDIFF s, $AV_DIFF s, $NUM_SIGS / $TOTAL_VALIDATORS, $SIG_RATE %, $SIG_RATE_LAST_BLOCK %" >> log.csv
fi
fi
LAST_BT=$BT
BLOCK=$((BLOCK+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment