Skip to content

Commit f040112

Browse files
committed
feat: modify the sync snapshot script
- improved the speed of make snapshot, sync sync using aria2c, pigz libraries. - increased reliability by using docker inspect for result lookup for docker execution requests.
1 parent 6297446 commit f040112

1 file changed

Lines changed: 121 additions & 10 deletions

File tree

sync_block.sh

Lines changed: 121 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,136 @@
11
#!/bin/bash
2+
set -e # enable exit on error
3+
24
NETWORK_NAME=$1
35

4-
# Check if proper argument is provided for network
56
if [[ -z $NETWORK_NAME ]]; then
6-
echo "Error: Argument not provided. Usage: $0 <network>. Allowed values are 'sepolia' or 'mainnet'."
7+
echo "Error: Argument not provided. Usage: $0 <network>."
78
exit 1
89
elif [[ $NETWORK_NAME == "sepolia" ]]; then
910
SNAPSHOT_ORIGIN=https://snapshot.sepolia.kroma.network/latest/snapshot.tar.gz
1011
elif [[ $NETWORK_NAME == "mainnet" ]]; then
1112
SNAPSHOT_ORIGIN=https://snapshot.kroma.network/latest/snapshot.tar.gz
1213
else
13-
echo "Error: Invalid network. Allowed values are 'sepolia' or 'mainnet'."
14+
echo "Error: Invalid network. Allowed values are 'holesky1', 'sepolia' or 'mainnet'."
1415
exit 1
1516
fi
1617

17-
KROMA_DB_PATH=/.kroma/db/geth
18+
# function to check if dependencies are installed
19+
check_dependencies() {
20+
# check if aria2c is installed
21+
if ! command -v aria2c &> /dev/null; then
22+
echo "Error: aria2c is not installed."
23+
echo "To install aria2c, run: sudo apt-get install aria2 # For Debian/Ubuntu"
24+
echo "To install aria2c, run: brew install aria2 # For macOS"
25+
exit 1
26+
fi
27+
28+
# check if pigz is installed
29+
if ! command -v pigz &> /dev/null; then
30+
echo "Error: pigz is not installed."
31+
echo "To install pigz, run: sudo apt-get install pigz # For Debian/Ubuntu"
32+
echo "To install pigz, run: brew install pigz # For macOS"
33+
exit 1
34+
fi
35+
}
36+
37+
# function to stop the container and wait for it to stop
38+
stop_and_wait_container() {
39+
CONTAINER_NAME=$1
40+
echo "Stopping container: $CONTAINER_NAME"
41+
42+
# stop the container
43+
docker stop "$CONTAINER_NAME"
44+
if [ $? -ne 0 ]; then
45+
echo "Error: Failed to stop container $CONTAINER_NAME."
46+
exit 1
47+
fi
48+
49+
# wait for the container to stop by inspecting its status
50+
echo "Waiting for container $CONTAINER_NAME to stop..."
51+
while true; do
52+
container_status=$(docker inspect --format '{{.State.Status}}' "$CONTAINER_NAME")
53+
54+
# check if the container has exited
55+
if [ "$container_status" == "exited" ]; then
56+
exit_code=$(docker inspect --format '{{.State.ExitCode}}' "$CONTAINER_NAME")
57+
58+
if [ "$exit_code" -eq 0 ]; then
59+
echo "Container $CONTAINER_NAME stopped successfully with exit code: $exit_code"
60+
else
61+
echo "Error: Container $CONTAINER_NAME stopped with an error (Exit code: $exit_code)"
62+
fi
63+
break
64+
else
65+
# wait for 1 second before checking the status again
66+
sleep 1
67+
fi
68+
done
69+
}
70+
71+
# function to start the container and wait for it to be running
72+
start_and_wait_container() {
73+
CONTAINER_NAME=$1
74+
echo "Starting container: $CONTAINER_NAME"
75+
76+
# start the container
77+
docker start "$CONTAINER_NAME"
78+
if [ $? -ne 0 ]; then
79+
echo "Error: Failed to start container $CONTAINER_NAME."
80+
exit 1
81+
fi
82+
83+
# wait for the container to be running by inspecting its status
84+
echo "Waiting for container $CONTAINER_NAME to start..."
85+
while true; do
86+
container_status=$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME")
87+
88+
# check if the container is running
89+
if [ "$container_status" == "true" ]; then
90+
echo "Container $CONTAINER_NAME started successfully and is running."
91+
break
92+
else
93+
# wait for 1 second before checking the status again
94+
sleep 1
95+
fi
96+
done
97+
}
98+
99+
# set database path
100+
KROMA_DB_PATH="/.kroma/db/geth"
101+
TEMP_FILE="snapshot.tar.gz"
102+
103+
# call the function to check dependencies
104+
check_dependencies
105+
106+
echo "Deleting all .gz files in ${KROMA_DB_PATH}..."
107+
find ${KROMA_DB_PATH} -type f -name "*.gz" -exec rm -f {} \;
108+
echo "All .gz files in ${KROMA_DB_PATH} have been deleted."
109+
110+
# stop and wait for each container: kroma-node, kroma-geth
111+
stop_and_wait_container "kroma-node"
112+
stop_and_wait_container "kroma-geth"
113+
echo "All containers have been stopped successfully."
114+
115+
# remove & get chaindata
116+
echo "Removing chaindata..."
117+
rm -rf ${KROMA_DB_PATH}/chaindata
118+
119+
# change directory to the database path
120+
echo "Changing directory to ${KROMA_DB_PATH}..."
121+
cd "${KROMA_DB_PATH}" || { echo "Error: Failed to change directory to ${KROMA_DB_PATH}"; exit 1; }
122+
123+
# download the snapshot using aria2c and extract using pigz and tar
124+
echo "Downloading and extracting snapshot from ${SNAPSHOT_ORIGIN} to temporary file ${TEMP_FILE}..."
125+
aria2c -x 16 -s 16 --console-log-level=notice -o "${TEMP_FILE}" "${SNAPSHOT_ORIGIN}"
126+
echo "Extracting snapshot to ${KROMA_DB_PATH}..."
127+
pigz -dcv "${KROMA_DB_PATH}/${TEMP_FILE}" | tar -xv -C "${KROMA_DB_PATH}"
128+
echo "Download and extraction completed successfully."
18129

19-
docker stop kroma-node
20-
docker exec -it kroma-geth sh -c "rm -rf ${KROMA_DB_PATH}/chaindata"
21-
docker exec -it kroma-geth sh -c "cd /.kroma/db/geth && wget -O - ${SNAPSHOT_ORIGIN} | tar -xvz"
130+
echo "Removing temporary file ${KROMA_DB_PATH}/${TEMP_FILE}..."
131+
rm -rf "${KROMA_DB_PATH}/${TEMP_FILE}"
22132

23-
docker restart kroma-geth
24-
sleep 5
25-
docker start kroma-node
133+
# start and wait for each container: kroma-node, kroma-geth
134+
start_and_wait_container "kroma-geth"
135+
start_and_wait_container "kroma-node"
136+
echo "All containers have been started successfully."

0 commit comments

Comments
 (0)