-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·139 lines (119 loc) · 5.36 KB
/
init.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
set -eo pipefail
if [[ -n $DEBUG ]]; then
set -x
fi
# script variables
ZETACHAIN_NETWORK=${ZETACHAIN_NETWORK:-"mainnet"}
ZETACHAIN_SNAPSHOT_TYPE=${ZETACHAIN_SNAPSHOT_TYPE:-"fullnode"}
ZETACORED_BINARY_URL=${ZETACORED_BINARY_URL:-}
MY_IP=${MY_IP:-$(curl -s https://checkip.amazonaws.com)}
# script constants
CURL="curl -s -L --fail --retry 5 --retry-delay 2 --retry-max-time 10 -C -"
if [[ -z $MONIKER ]]; then
echo '$MONIKER is required'
exit 1
fi
if [[ "$ZETACHAIN_NETWORK" == "mainnet" ]]; then
echo "Running mainnet"
ZETACHAIN_INIT_API_URL=${ZETACHAIN_INIT_API_URL:-"https://zetachain-mainnet.g.allthatnode.com/archive/rest"}
ZETACHAIN_SNAPSHOT_METADATA_URL=${ZETACHAIN_SNAPSHOT_METADATA_URL:-"https://snapshots.rpc.zetachain.com/mainnet/${ZETACHAIN_SNAPSHOT_TYPE}/latest.json"}
ZETACHAIN_NETWORK_CONFIG_URL_BASE=${ZETACHAIN_NETWORK_CONFIG_URL_BASE:-"https://raw.githubusercontent.com/zeta-chain/network-config/main/mainnet"}
elif [[ "$ZETACHAIN_NETWORK" == "testnet" || "$ZETACHAIN_NETWORK" == "athens3" ]]; then
echo "Running testnet"
ZETACHAIN_INIT_API_URL=${ZETACHAIN_INIT_API_URL:-"https://zetachain-athens.g.allthatnode.com/archive/rest"}
ZETACHAIN_SNAPSHOT_METADATA_URL=${ZETACHAIN_SNAPSHOT_METADATA_URL:-"https://snapshots.rpc.zetachain.com/testnet/${ZETACHAIN_SNAPSHOT_TYPE}/latest.json"}
ZETACHAIN_NETWORK_CONFIG_URL_BASE=${ZETACHAIN_NETWORK_CONFIG_URL_BASE:-"https://raw.githubusercontent.com/zeta-chain/network-config/main/athens3"}
else
echo "Invalid network"
exit 1
fi
# convert uname arch to goarch style
UNAME_ARCH=$(uname -m)
case "$UNAME_ARCH" in
x86_64) GOARCH=amd64;;
i686) GOARCH=386;;
armv7l) GOARCH=arm;;
aarch64) GOARCH=arm64;;
*) GOARCH=unknown;;
esac
download_configs() {
echo "Downloading configs if they are not present"
mkdir -p .zetacored/config/
mkdir -p .zetacored/data/
if [[ ! -f .zetacored/config/app.toml ]]; then
$CURL -o .zetacored/config/app.toml "${ZETACHAIN_NETWORK_CONFIG_URL_BASE}/app.toml"
fi
if [[ ! -f .zetacored/config/config.toml ]]; then
$CURL -o .zetacored/config/config.toml "${ZETACHAIN_NETWORK_CONFIG_URL_BASE}/config.toml"
sed -i -e "s/^moniker = .*/moniker = \"${MONIKER}\"/" .zetacored/config/config.toml
fi
if [[ ! -f .zetacored/config/client.toml ]]; then
$CURL -o .zetacored/config/client.toml "${ZETACHAIN_NETWORK_CONFIG_URL_BASE}/client.toml"
fi
if [[ ! -f .zetacored/config/genesis.json ]]; then
$CURL -o .zetacored/config/genesis.json "${ZETACHAIN_NETWORK_CONFIG_URL_BASE}/genesis.json"
fi
}
install_genesis_zetacored() {
echo "Installing genesis zetacored"
# create the genesis bin path and symlink it to the current path
genesis_path=".zetacored/cosmovisor/genesis"
mkdir -p "$genesis_path"
mkdir -p "${genesis_path}/bin"
current_path=".zetacored/cosmovisor/current"
ln -s "${HOME}/${genesis_path}" "${HOME}/${current_path}"
if [[ -z $ZETACORED_BINARY_URL ]]; then
max_height=$($CURL "${ZETACHAIN_SNAPSHOT_METADATA_URL}" | jq -r '.snapshots[0].height')
echo "Getting latest passed upgrade plan before ${max_height}"
$CURL "${ZETACHAIN_INIT_API_URL}/cosmos/gov/v1/proposals?pagination.reverse=true" | jq --arg max_height "$max_height" '
.proposals[] |
select(.status == "PROPOSAL_STATUS_PASSED") |
.messages[] |
select(."@type" == "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade" and (.plan.height |
tonumber < $max_height))' | jq -s '.[0]' | tee /tmp/init-upgrade-plan.json
ZETACORED_BINARY_URL=$(jq -r '.plan.info' /tmp/init-upgrade-plan.json | jq -r ".binaries[\"linux/$GOARCH\"]")
fi
# go-getter will verify the checksum of the downloaded binary
go-getter --mode file "$ZETACORED_BINARY_URL" .zetacored/cosmovisor/genesis/bin/zetacored
chmod +x .zetacored/cosmovisor/genesis/bin/zetacored
# run the zetacored version to ensure it's the correct architecture, glibc version, and is in PATH
zetacored version
}
restore_snapshot() {
snapshot=$($CURL "${ZETACHAIN_SNAPSHOT_METADATA_URL}" | jq -r '.snapshots[0]')
snapshot_type=$(echo "$snapshot" | jq -r '.filename' | grep -q "archive" && echo "archive" || echo "fullnode")
if [[ "$snapshot_type" == "archive" ]]; then
# handle multipart archive snapshots
links=$(echo "$snapshot" | jq -r '.links[]')
checksum=$(echo "$snapshot" | jq -r '.fullFileChecksum')
dl-pipe -progress -hash "sha256:$checksum" "$links" | tar x
else
echo "Restoring fullnode snapshot"
snapshot_link=$(echo "$snapshot" | jq -r '.link')
snapshot_md5=$(echo "$snapshot" | jq -r '.checksums.md5')
decompress_args=""
if [[ "$snapshot_link" == *"lz4"* ]]; then
decompress_args="-I lz4"
fi
echo "Downloading and extracting snapshot from ${snapshot_link}"
dl-pipe -hash "md5:${snapshot_md5}" "$snapshot_link" | tar "$decompress_args" -x -C "$HOME/.zetacored"
fi
}
cd $HOME
if [[ -f /root/init_started && ! -f /root/init_completed ]]; then
echo "Initialization interrupted, resetting node data"
rm -rf .zetacored/data/*
fi
touch /root/init_started
if [[ ! -f /root/init_completed ]]; then
echo "Starting initialization"
download_configs
install_genesis_zetacored
restore_snapshot
touch /root/init_completed
else
echo "Initialization already completed"
fi
# always set IP address as it may change after restart
sed -i -e "s/^external_address = .*/external_address = \"${MY_IP}:26656\"/" .zetacored/config/config.toml