-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·220 lines (185 loc) · 7.58 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·220 lines (185 loc) · 7.58 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# Script to start the Stable node with port checking before launching Docker container
# Function to check if a port is available
check_port() {
local port=$1
if netstat -tuln | grep -q ":$port "; then
return 1 # Port busy
else
return 0 # Port free
fi
}
# Function to find an available port starting from a base port
find_available_port() {
local base_port=$1
local port=$base_port
while ! check_port $port; do
echo "[~] Port $port busy, trying $((port+1))..." >&2
port=$((port+1))
# Safety to avoid infinite loop
if [ $port -gt $((base_port+1000)) ]; then
echo "[!] Unable to find available port after 1000 attempts" >&2
exit 1
fi
done
echo $port
}
# Stop and remove existing container if it exists
# This should be done BEFORE checking port availability to free up any ports in use
if docker ps -a --format '{{.Names}}' | grep -q '^stable-node$'; then
echo "[~] Stopping existing container..." >&2
docker stop stable-node >/dev/null 2>&1
echo "[~] Removing existing container..." >&2
docker rm stable-node >/dev/null 2>&1
fi
# Default ports for Stable node
DEFAULT_P2P_PORT=26656
DEFAULT_RPC_PORT=26657
DEFAULT_API_PORT=1317
DEFAULT_JSON_RPC_PORT=8545
DEFAULT_WS_PORT=8546
DEFAULT_GRPC_PORT=9090
# Find available ports
echo "[~] Checking port availability..." >&2
# Find P2P port
P2P_PORT=$(find_available_port $DEFAULT_P2P_PORT)
# Find RPC port
RPC_PORT=$P2P_PORT
while [ $RPC_PORT -eq $P2P_PORT ]; do
RPC_PORT=$(find_available_port $((RPC_PORT + 1)))
done
# Find API port (different from other ports)
API_PORT=$P2P_PORT
while [ $API_PORT -eq $P2P_PORT ] || [ $API_PORT -eq $RPC_PORT ]; do
API_PORT=$(find_available_port $((API_PORT + 1)))
done
# Find JSON-RPC port (different from other ports)
JSON_RPC_PORT=$P2P_PORT
while [ $JSON_RPC_PORT -eq $P2P_PORT ] || [ $JSON_RPC_PORT -eq $RPC_PORT ] || [ $JSON_RPC_PORT -eq $API_PORT ]; do
JSON_RPC_PORT=$(find_available_port $((JSON_RPC_PORT + 1)))
done
# Find WebSocket port (different from other ports)
WS_PORT=$P2P_PORT
while [ $WS_PORT -eq $P2P_PORT ] || [ $WS_PORT -eq $RPC_PORT ] || [ $WS_PORT -eq $API_PORT ] || [ $WS_PORT -eq $JSON_RPC_PORT ]; do
WS_PORT=$(find_available_port $((WS_PORT + 1)))
done
# Find gRPC port (different from other ports)
GRPC_PORT=$P2P_PORT
while [ $GRPC_PORT -eq $P2P_PORT ] || [ $GRPC_PORT -eq $RPC_PORT ] || [ $GRPC_PORT -eq $API_PORT ] || [ $GRPC_PORT -eq $JSON_RPC_PORT ] || [ $GRPC_PORT -eq $WS_PORT ]; do
GRPC_PORT=$(find_available_port $((GRPC_PORT + 1)))
done
# Show selected ports
echo "[✓] Using ports:" >&2
echo " P2P Port: $P2P_PORT" >&2
echo " RPC Port: $RPC_PORT" >&2
echo " API Port: $API_PORT" >&2
echo " JSON-RPC Port: $JSON_RPC_PORT" >&2
echo " WebSocket Port: $WS_PORT" >&2
echo " gRPC Port: $GRPC_PORT" >&2
# Install required dependencies
echo "[~] Installing dependencies..." >&2
apt update >/dev/null 2>&1
apt install -y wget curl jq lz4 unzip pv >/dev/null 2>&1
# Set environment variables
export MONIKER="docker-node"
export CHAIN_ID="stabletestnet_2201-1"
# Define STABLED_DIR path inside stable-node directory
STABLED_DIR="/root/Stable_Node/stabled"
# Check if config directory exists
if [ ! -d "$STABLED_DIR/config" ]; then
# Initialize node only if config doesn't exist
echo "[~] Initializing node..." >&2
mkdir -p $STABLED_DIR
stabled init $MONIKER --chain-id $CHAIN_ID >/dev/null 2>&1
# Download and configure genesis
echo "[~] Configuring genesis..." >&2
mv $STABLED_DIR/config/genesis.json $STABLED_DIR/config/genesis.json.backup
wget https://stable-testnet-data.s3.us-east-1.amazonaws.com/stable_testnet_genesis.zip >/dev/null 2>&1
unzip stable_testnet_genesis.zip >/dev/null 2>&1
cp genesis.json $STABLED_DIR/config/genesis.json
rm stable_testnet_genesis.zip genesis.json
# Download and configure optimized settings
echo "[~] Configuring optimized settings..." >&2
wget https://stable-testnet-data.s3.us-east-1.amazonaws.com/rpc_node_config.zip >/dev/null 2>&1
unzip rpc_node_config.zip >/dev/null 2>&1
cp $STABLED_DIR/config/config.toml $STABLED_DIR/config/config.toml.backup
cp $STABLED_DIR/config/app.toml $STABLED_DIR/config/app.toml.backup
cp config.toml $STABLED_DIR/config/config.toml
cp app.toml $STABLED_DIR/config/app.toml
sed -i "s/^moniker = \".*\"/moniker = \"$MONIKER\"/" $STABLED_DIR/config/config.toml
rm rpc_node_config.zip config.toml app.toml
else
echo "[~] Using existing configuration..." >&2
fi
# Essential configuration updates (applied every time to ensure proper settings)
echo "[~] Updating configuration files..." >&2
# Enable JSON-RPC in app.toml
sed -i '/\[json-rpc\]/,/^\[.*\]/ s/enable = false/enable = true/' $STABLED_DIR/config/app.toml
sed -i '/\[json-rpc\]/,/^\[.*\]/ s/address = "127.0.0.1:8545"/address = "0.0.0.0:8545"/' $STABLED_DIR/config/app.toml
sed -i '/\[json-rpc\]/,/^\[.*\]/ s/ws-address = "127.0.0.1:8546"/ws-address = "0.0.0.0:8546"/' $STABLED_DIR/config/app.toml
# Configure P2P in config.toml
sed -i 's/^laddr = "tcp:\/\/127.0.0.1:26656"/laddr = "tcp:\/\/0.0.0.0:26656"/' $STABLED_DIR/config/config.toml
sed -i 's/^persistent_peers = ""/persistent_peers = "5ed0f977a26ccf290e184e364fb04e268ef16430@37.187.147.27:26656,128accd3e8ee379bfdf54560c21345451c7048c7@37.187.147.22:26656"/' $STABLED_DIR/config/config.toml
sed -i 's/^pex = false/pex = true/' $STABLED_DIR/config/config.toml
# Configure RPC in config.toml
sed -i 's/^laddr = "tcp:\/\/127.0.0.1:26657"/laddr = "tcp:\/\/0.0.0.0:26657"/' $STABLED_DIR/config/config.toml
sed -i 's/^cors_allowed_origins = \[\]/cors_allowed_origins = \["\*"\]/' $STABLED_DIR/config/config.toml
# Fix permissions for the stabled directory
echo "[~] Fixing permissions..." >&2
chown -R 1000:1000 $STABLED_DIR
chmod -R 755 $STABLED_DIR
# Download and extract snapshot if snapshot file doesn't exist in project directory
if [ ! -f "/root/Stable_Node/snapshot.tar.lz4" ]; then
echo "[~] Downloading snapshot..." >&2
cd /root/Stable_Node
wget -c https://stable-snapshot.s3.eu-central-1.amazonaws.com/snapshot.tar.lz4 >/dev/null 2>&1
else
echo "[~] Using existing snapshot file..." >&2
cd /root/Stable_Node
fi
# Remove old data
echo "[~] Removing old data..." >&2
rm -rf $STABLED_DIR/data/*
# Extract snapshot
echo "[~] Extracting snapshot..." >&2
pv snapshot.tar.lz4 | tar -I lz4 -xf - -C $STABLED_DIR/ || tar -I lz4 -xvf snapshot.tar.lz4 -C $STABLED_DIR/
# Clean up snapshot file
rm snapshot.tar.lz4
# Update docker-compose.yml with the available ports
echo "[~] Updating docker-compose.yml with available ports..." >&2
cat > docker-compose.yml <<EOF
version: '3.8'
services:
stable-node:
build: .
container_name: stable-node
ports:
- "$P2P_PORT:26656"
- "$RPC_PORT:26657"
- "$API_PORT:1317"
- "$JSON_RPC_PORT:8545"
- "$WS_PORT:8546"
- "$GRPC_PORT:9090"
volumes:
- ./stabled:/home/stable/.stabled
environment:
- MONIKER=docker-node
- CHAIN_ID=stabletestnet_2201-1
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
EOF
# Start the container
echo "[~] Starting Stable node container..." >&2
docker compose up -d
echo "[✓] Stable node started successfully!" >&2
echo "View logs with: docker compose logs -f" >&2
echo "P2P Port: $P2P_PORT" > ports.info
echo "RPC Port: $RPC_PORT" >> ports.info
echo "API Port: $API_PORT" >> ports.info
echo "JSON-RPC Port: $JSON_RPC_PORT" >> ports.info
echo "WebSocket Port: $WS_PORT" >> ports.info
echo "gRPC Port: $GRPC_PORT" >> ports.info