@@ -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+
5260nimbus_port=" $( config_get nimbus_port) " ;
5361exec_url=" $( config_get exec_url) " ;
5462
@@ -68,16 +76,56 @@ while [ $? -ne 0 ]; do
6876 ping -c 1 $pingServerAdr > /dev/null 2>&1
6977done
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"
7481echolog " $( date) : Connected - ${pingServerAdr} "
7582echolog " exec_url = ${exec_url} "
7683echolog " 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
0 commit comments