-
Notifications
You must be signed in to change notification settings - Fork 13
/
retention.sh
111 lines (92 loc) · 2.63 KB
/
retention.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
#!/bin/bash
genesis_repository="ephemery-testnet/ephemery-genesis"
testnet_dir=/home/ethereum/testnet
el_datadir=/home/ethereum/data-geth
cl_datadir=/home/ethereum/data-lh
cl_port=5052
start_clients() {
# start EL / CL clients
echo "start clients"
sudo /bin/systemctl start geth
sudo /bin/systemctl start beacon-chain
sudo /bin/systemctl start validator
}
stop_clients() {
# stop EL / CL clients
echo "stop clients"
sudo /bin/systemctl stop geth
sudo /bin/systemctl stop beacon-chain
sudo /bin/systemctl stop validator
}
clear_datadirs() {
if [ -d $el_datadir/geth ]; then
geth_nodekey=$(cat $el_datadir/geth/nodekey)
rm -rf $el_datadir/geth
mkdir $el_datadir/geth
echo $geth_nodekey > $el_datadir/geth/nodekey
fi
rm -rf $cl_datadir/beacon
rm -rf $cl_datadir/validators/slashing_protection.sqlite
}
setup_genesis() {
# init el genesis
~/geth/bin/geth init --datadir $el_datadir $testnet_dir/genesis.json
}
get_github_release() {
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/' |
head -n 1
}
download_genesis_release() {
genesis_release=$1
# remove old genesis
if [ -d $testnet_dir ]; then
rm -rf $testnet_dir/*
else
mkdir -p $testnet_dir
fi
# get latest genesis
wget -qO- https://github.com/$genesis_repository/releases/download/$genesis_release/testnet-all.tar.gz | tar xvz -C $testnet_dir
}
reset_testnet() {
stop_clients
clear_datadirs
download_genesis_release $1
setup_genesis
start_clients
}
check_testnet() {
current_time=$(date +%s)
genesis_time=$(curl -s http://localhost:$cl_port/eth/v1/beacon/genesis | sed 's/.*"genesis_time":"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/')
if ! [ $genesis_time -gt 0 ]; then
echo "could not get genesis time from beacon node"
return 0
fi
if ! [ -f $testnet_dir/retention.vars ]; then
echo "could not find retention.vars"
return 0
fi
source $testnet_dir/retention.vars
testnet_timeout=$(expr $genesis_time + $GENESIS_RESET_INTERVAL - 300)
echo "genesis timeout: $(expr $testnet_timeout - $current_time) sec"
if [ $testnet_timeout -le $current_time ]; then
genesis_release=$(get_github_release $genesis_repository)
if ! [ $ITERATION_RELEASE ]; then
ITERATION_RELEASE=$CHAIN_ID
fi
if [ $genesis_release = $ITERATION_RELEASE ]; then
echo "could not find new genesis release (release: $genesis_release)"
return 0
fi
reset_testnet $genesis_release
fi
}
main() {
if ! [ -f $testnet_dir/genesis.json ]; then
reset_testnet $(get_github_release $genesis_repository)
else
check_testnet
fi
}
main