Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Vagabond/4e8608263b102463ae4be50e0bac6358 to your computer and use it in GitHub Desktop.
Save Vagabond/4e8608263b102463ae4be50e0bac6358 to your computer and use it in GitHub Desktop.
Helium Validator GRPC Port Checker
#!/bin/bash
# For each staked validator, get the release version
# command to call miner of the desired validator to use
MINER=./_build/validator/rel/miner/bin/miner
VALS=$(${MINER} ledger validators --format csv -v | awk -F, '$7 == "staked" {print $2}')
${MINER} ledger validators --format csv -v | awk -F, '$7 == "staked" {print $2}' | xargs -P 0 -I{} ${MINER} peer connect /p2p/{}
# clear csv. format is: validator address, grpc address, port reachable status
echo "address,ip,grpc_port,port_status" > validatorGRPCPorts.csv
# for each validator, get the grpc address from the peerbook, then check it for connectivity
for val in $(${MINER} ledger validators --format csv -v | awk -F, '$7 == "staked" {print $2}');
do
${MINER} peer connect /p2p/${val}
# use miner eval to get the signed metadata which contains the grpc_address
grpc_address=$(${MINER} eval 'SM=libp2p_peer:signed_metadata(element(2,libp2p_peerbook:get(libp2p_swarm:peerbook(blockchain_swarm:tid()), libp2p_crypto:pubkey_to_bin(libp2p_crypto:b58_to_pubkey("'${val}'"))))),binary_to_list(maps:get(<<"grpc_address">>,SM)).')
#echo $grpc_address
if [[ "$grpc_address" =~ .*"badkey".* ]]; then
echo "${val}: not_found"
echo "${val},not_found,not_found,no_port_not_upgraded" >> validatorGRPCPorts.csv
elif [[ "$grpc_address" =~ .*"not_found".* ]]; then
echo "${val}: not_found"
echo "${val},not_found,not_found,not_found_in_peerbook" >> validatorGRPCPorts.csv
else
ip=$(echo ${grpc_address} | sed 's/http:\/\///' | sed 's/"//' | sed 's/:/ /' | awk '{print $1}' )
port=$(echo ${grpc_address} | sed 's/"//' | sed 's/"//' | sed 's/http:\/\///' | sed 's/:/ /' | awk '{print $2}' )
nc -z -w 2 $ip $port
if [[ $? == 0 ]]; then
CONFIG=$(grpcurl --import-path _build/default/lib/helium_proto/src --import-path _build/default/lib/helium_proto/src/service --proto gateway.proto --plaintext -d '{"keys": []}' $ip:$port helium.gateway/config)
if [ $? -ne 0 ]; then
echo "${val}: ${grpc_address} is not responding to gRPC"
echo "${val},${ip},${port},error," >> validatorGRPCPorts.csv
else
AGE=$(echo $CONFIG | jq .blockAge | bc)
if [ $AGE -gt 900 ]; then
echo "${val}: ${grpc_address} is stale (age $AGE)"
echo "${val},${ip},${port},stale," >> validatorGRPCPorts.csv
else
echo "${val}: ${grpc_address} is open"
echo "${val},${ip},${port},open," >> validatorGRPCPorts.csv
fi
fi
else
echo "${val}: ${grpc_address} timeout"
echo "${val},${ip},${port},timeout," >> validatorGRPCPorts.csv
fi
fi
done
echo "\n---SUMMARY---\n"
cat validatorGRPCPorts.csv | awk -F, '{count[$4]++} END {for (word in count) print word, "\t" count[word]}' | sort > validatorGRPCSummary
cat validatorGRPCSummary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment