Skip to content

Commit 9299d71

Browse files
committed
Nimbus run script with succes chceck and retry
1 parent 067f49d commit 9299d71

3 files changed

Lines changed: 109 additions & 14 deletions

File tree

distros/raspberry_pi/nimbus/nimbus.sh

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ echolog(){
2222
done
2323
else
2424
echo -n "$(date +'[%F %T %Z]') - " | tee -a $LOGI
25-
echo $* | tee -a $LOGI
25+
echo "$*" | tee -a $LOGI
2626
fi
2727
}
2828

@@ -32,7 +32,7 @@ get_install_stage() {
3232
local file_path=$1
3333
if [ -f "/root/.install_stage" ]; then
3434
local number=$(cat "/root/.install_stage")
35-
echo $number
35+
echo "$number"
3636
else
3737
echolog "File /root/.install_stage does not exist."
3838
return 0
@@ -49,6 +49,14 @@ set_status() {
4949
echolog " "
5050
}
5151

52+
# Function to calculate average ping time
53+
calculate_average_ping() {
54+
local server=$1
55+
local server_address=$(echo "$server" | sed -E 's#^https?://##')
56+
local avg_ping=$(ping -c 3 -q "$server_address" 2>/dev/null | grep -oP '(?<=rtt min/avg/max/mdev = )[0-9.]+(?=/)')
57+
echo "$avg_ping"
58+
}
59+
5260
nimbus_port="$(config_get nimbus_port)";
5361
exec_url="$(config_get exec_url)";
5462

@@ -68,16 +76,56 @@ while [ $? -ne 0 ]; do
6876
ping -c 1 $pingServerAdr > /dev/null 2>&1
6977
done
7078

71-
# Script for finding the best server
72-
source /opt/web3pi/Ethereum-On-Raspberry-Pi/distros/raspberry_pi/scripts/pingServers.sh
7379

80+
nimbus_dir="/mnt/storage/.nimbus/data/shared_mainnet_0"
7481
echolog "$(date): Connected - ${pingServerAdr}"
7582
echolog "exec_url = ${exec_url}"
7683
echolog "nimbus_port = ${nimbus_port}"
77-
echolog "best_server = ${best_server} ($best_ping ms)"
84+
echolog "nimbus_dir = ${nimbus_dir}"
85+
86+
bash /opt/web3pi/Ethereum-On-Raspberry-Pi/distros/raspberry_pi/scripts/sort_servers.sh
87+
88+
# File with the list of servers
89+
SERVERS_FILE="/opt/web3pi/Ethereum-On-Raspberry-Pi/distros/raspberry_pi/scripts/serversList.txt"
90+
91+
# Directory for Nimbus
92+
nimbus_dir="/mnt/storage/.nimbus/data/shared_mainnet_0"
93+
94+
# Iterate through each server from the list
95+
success=false
96+
while read -r server; do
97+
if [[ -n "$server" ]]; then
98+
echolog "Attempting to sync with server: $server"
99+
avg_ping=$(calculate_average_ping "$server")
100+
echolog "Average ping = $avg_ping ms"
101+
102+
# Run the Nimbus beacon node command and display output in real time
103+
output=$(nimbus_beacon_node trustedNodeSync --network=mainnet --data-dir="$nimbus_dir" --trusted-node-url="$server" --backfill=false 2>&1 | tee /dev/tty)
104+
105+
# Searching for a line containing 'horizon' and extracting the value.
106+
horizon_value=$(echo "$output" | grep -oP 'horizon=\K\d+')
107+
echolog "horizon=$horizon_value "
108+
109+
# Check the output for success messages
110+
if [ "$horizon_value" -gt 0 ]; then
111+
echolog "Sync successful with server: $server "
112+
success=true
113+
break
114+
else
115+
echolog "Sync failed with server: $server, trying next server..."
116+
echolog "Removing $nimbus_dir "
117+
rm -r $nimbus_dir
118+
fi
119+
fi
120+
done < "$SERVERS_FILE"
78121

79-
echolog "Run Nimbus beacon node - quick sync"
80-
nimbus_beacon_node trustedNodeSync --network:mainnet --data-dir=/mnt/storage/.nimbus/data/shared_mainnet_0 --trusted-node-url=${best_server} --backfill=false
81122

82-
echolog "Run Nimbus beacon node"
83-
nimbus_beacon_node --non-interactive --tcp-port=${nimbus_port} --udp-port=${nimbus_port} --el=${exec_url} --network:mainnet --data-dir=/mnt/storage/.nimbus/data/shared_mainnet_0 --jwt-secret=/home/ethereum/clients/secrets/jwt.hex --rest=true --rest-port=5052 --rest-address=0.0.0.0 --rest-allow-origin='*' --enr-auto-update
123+
# If the trustedNodeSync was successful
124+
if [ "$success" = true ]; then
125+
echolog "Run Nimbus beacon node"
126+
nimbus_beacon_node --non-interactive --tcp-port=${nimbus_port} --udp-port=${nimbus_port} --el=${exec_url} --network:mainnet --data-dir=${nimbus_dir} --jwt-secret=/home/ethereum/clients/secrets/jwt.hex --rest=true --rest-port=5052 --rest-address=0.0.0.0 --rest-allow-origin='*' --enr-auto-update
127+
else
128+
# If no server was successful
129+
echolog "All servers failed to complete the trustedNodeSync."
130+
exit 1
131+
fi
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
https://sync.invis.tools
2+
https://mainnet-checkpoint-sync.stakely.io
13
https://beaconstate.info
24
https://sync-mainnet.beaconcha.in
3-
https://mainnet-checkpoint-sync.stakely.io
4-
https://mainnet.checkpoint.sigp.io
5+
https://mainnet-checkpoint-sync.attestant.io
56
https://beaconstate-mainnet.chainsafe.io
67
https://checkpointz.pietjepuk.net
7-
https://mainnet-checkpoint-sync.attestant.io
8-
https://sync.invis.tools
9-
https://beaconstate.ethstaker.cc
8+
https://mainnet.checkpoint.sigp.io
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
echo "Start sorting server list..."
4+
5+
# File with the list of servers
6+
SERVERS_FILE="/opt/web3pi/Ethereum-On-Raspberry-Pi/distros/raspberry_pi/scripts/serversList.txt"
7+
8+
# Check if the file exists
9+
if [ ! -f "$SERVERS_FILE" ]; then
10+
echo "File $SERVERS_FILE does not exist."
11+
exit 1
12+
fi
13+
14+
# Temporary file for results
15+
TEMP_FILE="/tmp/sorted_servers.txt"
16+
17+
# Function to calculate average ping time
18+
calculate_average_ping() {
19+
local server=$1
20+
local server_address=$(echo "$server" | sed -E 's#^https?://##')
21+
local avg_ping=$(ping -c 3 -q "$server_address" 2>/dev/null | grep -oP '(?<=rtt min/avg/max/mdev = )[0-9.]+(?=/)')
22+
echo "$avg_ping"
23+
}
24+
25+
# Read servers, ping them, and sort
26+
{
27+
while read -r server; do
28+
if [[ -n "$server" ]]; then
29+
avg_ping=$(calculate_average_ping "$server")
30+
if [[ -n "$avg_ping" ]]; then
31+
echo "$avg_ping $server"
32+
else
33+
echo "9999 $server" # Large value if the server is unreachable
34+
fi
35+
fi
36+
done < "$SERVERS_FILE"
37+
} | sort -n > "$TEMP_FILE"
38+
39+
echo -e "\nAfter sort"
40+
cat $TEMP_FILE
41+
42+
# Update the server list file (only server names)
43+
awk '{print $2}' "$TEMP_FILE" > "$SERVERS_FILE"
44+
45+
# Remove the temporary file
46+
rm "$TEMP_FILE"
47+
48+
echo "The server list has been sorted based on average ping times."

0 commit comments

Comments
 (0)