-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_stats.sh
117 lines (101 loc) · 3.09 KB
/
validator_stats.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
usage() {
cat <<-EOF
Usage:
validators.sh [options] <account ID>
Displays block/chunk production stats for the given validator ID. The script
expects the MAINNET_EPOCHS_DATABASE_URL, TESTNET_EPOCHS_DATABASE_URL
and BETANET_EPOCHS_DATABASE_URL environment variables to be set.
Options:
-c <chain_id>: possible values are "mainnet", "testnet" or "betanet". Default "mainnet"
-f <timestamp>: f as in "from". Results will only include epochs that ended
at or after this timestamp
-t <timestamp>: t as in "to". Results will only include epochs that began
at or before this timestamp
Example:
$ export TESTNET_EPOCHS_DATABASE_URL=postgresql://myuser:mypassword@localhost/testnet
$ sh validator_stats.sh -c testnet -f "2022-01-01 03:04:06" -t "2022-01-06 03:04:06" node0
EOF
exit $1
}
show_stats() {
if [ "$chain_id" = "mainnet" ]; then
if [ -z "$MAINNET_EPOCHS_DATABASE_URL" ]; then
echo "please set the MAINNET_EPOCHS_DATABASE_URL environment variable"
exit 1
fi
url=$MAINNET_EPOCHS_DATABASE_URL
elif [ "$chain_id" = "testnet" ]; then
if [ -z "$TESTNET_EPOCHS_DATABASE_URL" ]; then
echo "please set the TESTNET_EPOCHS_DATABASE_URL environment variable"
exit 1
fi
url=$TESTNET_EPOCHS_DATABASE_URL
elif [ "$chain_id" = "betanet" ]; then
if [ -z "$BETANET_EPOCHS_DATABASE_URL" ]; then
echo "please set the BETANET_EPOCHS_DATABASE_URL environment variable"
exit 1
fi
url=$BETANET_EPOCHS_DATABASE_URL
else
usage 1
fi
query=$(
cat <<EOF
SELECT to_timestamp(epochs.start_timestamp / 1000000000) AS start,
to_timestamp(epochs.end_timestamp / 1000000000) AS end, epochs.height,
validator_stats.num_produced_blocks, validator_stats.num_expected_blocks,
validator_stats.num_produced_chunks, validator_stats.num_expected_chunks
FROM validator_stats LEFT OUTER JOIN epochs ON (validator_stats.epoch_id = epochs.epoch_id)
WHERE validator_stats.account_id = '$account_id'
EOF
)
if [ "$from" != "none" ]; then
query="${query} AND epochs.end_timestamp >=
DATE_PART('epoch', TIMESTAMP '${from}')::numeric::bigint * 1000000000"
fi
if [ "$to" != "none" ]; then
query="${query} AND epochs.start_timestamp <=
DATE_PART('epoch', TIMESTAMP '${to}')::numeric::bigint * 1000000000"
fi
psql -c "$query ORDER BY height DESC;" "$url"
}
run() {
if [ $# -lt 1 ]; then
usage 1
exit 1
fi
local chain_id="mainnet"
local from="none"
local to="none"
while getopts "c:f:t:" o; do
case "${o}" in
c)
chain_id=${OPTARG}
;;
f)
from=${OPTARG}
;;
t)
to=${OPTARG}
;;
*)
usage 1
;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 1 ]; then
usage 1
fi
account_id=$1
show_stats
}
case "$1" in
help | --help)
usage 0
;;
*)
run "$@"
;;
esac