forked from matter-labs/zksync-os-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_local.sh
More file actions
executable file
·244 lines (207 loc) · 7.96 KB
/
run_local.sh
File metadata and controls
executable file
·244 lines (207 loc) · 7.96 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
set -e
# Get the directory where this script is located
REPO_ROOT="$(dirname "$(realpath "$0")")"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${YELLOW}⚠️ This script is a temporary solution. Do not depend on it in production.${NC}"
echo ""
# Array to store PIDs of background processes
declare -a PIDS=()
TEMP_DIR=$(mktemp -d)
# Check if tmp dir was created
if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
echo "Could not create temporary directory via 'mkdir'"
exit 1
fi
# Cleanup function to stop all started services
cleanup() {
# Prevent re-entry when exit triggers the trap again
trap - SIGINT SIGTERM EXIT
echo -e "\n${YELLOW}Shutting down all services...${NC}"
for pid in "${PIDS[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
echo -e "${YELLOW}Stopping process $pid${NC}"
kill -TERM "$pid" 2>/dev/null || true
fi
done
# Wait for processes to terminate gracefully
sleep 2
# Force kill any remaining processes
for pid in "${PIDS[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
echo -e "${RED}Force killing process $pid${NC}"
kill -9 "$pid" 2>/dev/null || true
fi
done
echo -e "${GREEN}All services stopped${NC}"
rm -rf "$TEMP_DIR"
exit 0
}
# Set up trap for cleanup on script exit
trap cleanup SIGINT SIGTERM EXIT
# Parse command line arguments
CONFIG_DIR=""
LOGS_DIR=""
while [[ $# -gt 0 ]]; do
case $1 in
--logs-dir)
if [ -z "$2" ] || [[ "$2" == --* ]]; then
echo -e "${RED}Error: --logs-dir requires a path argument${NC}"
exit 1
fi
LOGS_DIR="$2"
shift 2
;;
-*)
echo -e "${RED}Error: Unknown option $1${NC}"
echo -e "Usage: $0 <folder-path> [--logs-dir <path>]"
exit 1
;;
*)
if [ -z "$CONFIG_DIR" ]; then
CONFIG_DIR="$1"
else
echo -e "${RED}Error: Unexpected argument $1${NC}"
exit 1
fi
shift
;;
esac
done
# Check if folder path is provided
if [ -z "$CONFIG_DIR" ]; then
echo -e "${RED}Usage: $0 <folder-path> [--logs-dir <path>]${NC}"
echo -e "Example: $0 ./local-chains/v30.2/default"
echo -e "Example: $0 ./local-chains/v30.2/multi_chain"
echo -e "Example: $0 ./local-chains/v30.2/default --logs-dir ./logs"
exit 1
fi
# Resolve to absolute path
CONFIG_DIR="$(realpath "$CONFIG_DIR")"
# Verify the directory exists
if [ ! -d "$CONFIG_DIR" ]; then
echo -e "${RED}Error: Directory '$CONFIG_DIR' does not exist${NC}"
exit 1
fi
# Check for compressed L1 state file
L1_STATE_FILE_GZ="$CONFIG_DIR/../l1-state.json.gz"
if [ ! -f "$L1_STATE_FILE_GZ" ]; then
echo -e "${RED}Error: L1 state file '$L1_STATE_FILE_GZ' not found${NC}"
exit 1
fi
# Decompress L1 state file into temporary directory
gzip -d < "$L1_STATE_FILE_GZ" > "$TEMP_DIR/l1-state.json"
# Check for L1 state file
L1_STATE_FILE="$TEMP_DIR/l1-state.json"
if [ ! -f "$L1_STATE_FILE" ]; then
echo -e "${RED}Error: decompressed L1 state file '$L1_STATE_FILE' not found${NC}"
exit 1
fi
# Generate timestamp for log files (same timestamp for all logs in this session)
LOG_TIMESTAMP=$(date +"%Y-%m-%dT%H-%M-%S")
# Setup logs directory if specified
if [ -n "$LOGS_DIR" ]; then
mkdir -p "$LOGS_DIR"
LOGS_DIR="$(realpath "$LOGS_DIR")"
echo -e "${BLUE}Logs will be written to: $LOGS_DIR${NC}"
fi
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Starting Local Development Environment${NC}"
echo -e "${BLUE}Config directory: $CONFIG_DIR${NC}"
echo -e "${BLUE}========================================${NC}"
# Build first
echo -e "\n${GREEN}Building zksync-os-server...${NC}"
if ! cargo build --release --manifest-path "$REPO_ROOT/Cargo.toml"; then
echo -e "${RED}Build failed${NC}"
exit 1
fi
echo -e "${GREEN}Build completed${NC}"
# Start Anvil
echo -e "\n${GREEN}Starting Anvil...${NC}"
if [ -n "$LOGS_DIR" ]; then
ANVIL_LOG_FILE="$LOGS_DIR/anvil-$LOG_TIMESTAMP.log"
anvil --load-state "$L1_STATE_FILE" --port 8545 --block-time 0.25 --mixed-mining --slots-in-an-epoch 10 > "$ANVIL_LOG_FILE" 2>&1 &
echo -e "${GREEN}Anvil logs: $ANVIL_LOG_FILE${NC}"
else
anvil --load-state "$L1_STATE_FILE" --port 8545 --block-time 0.25 --mixed-mining --slots-in-an-epoch 10 > /dev/null 2>&1 &
fi
ANVIL_PID=$!
PIDS+=($ANVIL_PID)
echo -e "${GREEN}Anvil started with PID $ANVIL_PID${NC}"
# Wait for Anvil to be ready
echo -e "${YELLOW}Waiting for Anvil to be ready...${NC}"
for i in {1..30}; do
if curl -s http://localhost:8545 -X POST -H "Content-Type: application/json" \
--data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' > /dev/null 2>&1; then
echo -e "${GREEN}Anvil is ready${NC}"
break
fi
if [ $i -eq 30 ]; then
echo -e "${RED}Anvil failed to start${NC}"
exit 1
fi
sleep 1
done
# Determine which chain configs to use
SINGLE_CONFIG="$CONFIG_DIR/config.yaml"
if [ -f "$SINGLE_CONFIG" ]; then
# Single chain mode
# Prompt to clean up db folder (only for single chain mode)
if [ -d "$REPO_ROOT/db" ] && [ "$(ls -A "$REPO_ROOT/db" 2>/dev/null)" ]; then
echo -e "${YELLOW}The db/ folder contains existing data.${NC}"
read -p "Do you want to clean it up? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Cleaning up db/* ...${NC}"
rm -rf "$REPO_ROOT/db"/*
echo -e "${GREEN}db/ folder cleaned${NC}"
fi
fi
echo -e "\n${GREEN}Starting single chain with config: $SINGLE_CONFIG${NC}"
if [ -n "$LOGS_DIR" ]; then
CHAIN_LOG_FILE="$LOGS_DIR/chain-$LOG_TIMESTAMP.log"
cargo run --release --manifest-path "$REPO_ROOT/Cargo.toml" -- --config "$REPO_ROOT/local-chains/local_dev.yaml" --config "$SINGLE_CONFIG" > "$CHAIN_LOG_FILE" 2>&1 &
echo -e "${GREEN}Chain logs: $CHAIN_LOG_FILE${NC}"
else
cargo run --release --manifest-path "$REPO_ROOT/Cargo.toml" -- --config "$REPO_ROOT/local-chains/local_dev.yaml" --config "$SINGLE_CONFIG" &
fi
CHAIN_PID=$!
PIDS+=($CHAIN_PID)
echo -e "${GREEN}Chain started with PID $CHAIN_PID${NC}"
else
# Multiple chains mode - look for chain_<chainid>.yaml files
CHAIN_CONFIGS=($(ls "$CONFIG_DIR"/chain_*.yaml 2>/dev/null | sort -V))
if [ ${#CHAIN_CONFIGS[@]} -eq 0 ]; then
echo -e "${RED}Error: No config.yaml or chain_*.yaml files found in '$CONFIG_DIR'${NC}"
exit 1
fi
echo -e "\n${GREEN}Starting ${#CHAIN_CONFIGS[@]} chain(s)...${NC}"
for config_file in "${CHAIN_CONFIGS[@]}"; do
echo -e "${GREEN}Starting chain with config: $config_file${NC}"
if [ -n "$LOGS_DIR" ]; then
# Extract config file name without extension for log file naming
CONFIG_NAME=$(basename "$config_file" .yaml)
CHAIN_LOG_FILE="$LOGS_DIR/${CONFIG_NAME}-$LOG_TIMESTAMP.log"
cargo run --release --manifest-path "$REPO_ROOT/Cargo.toml" -- --config "$REPO_ROOT/local-chains/local_dev.yaml" --config "$config_file" > "$CHAIN_LOG_FILE" 2>&1 &
echo -e "${GREEN}Chain logs: $CHAIN_LOG_FILE${NC}"
else
cargo run --release --manifest-path "$REPO_ROOT/Cargo.toml" -- --config "$REPO_ROOT/local-chains/local_dev.yaml" --config "$config_file" &
fi
CHAIN_PID=$!
PIDS+=($CHAIN_PID)
echo -e "${GREEN}Chain started with PID $CHAIN_PID${NC}"
# Small delay between starting chains (to make sure file locks are awaited properly)
sleep 2
done
fi
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE}All services started successfully${NC}"
echo -e "${BLUE}Press Ctrl+C to stop all services${NC}"
echo -e "${BLUE}========================================${NC}"
# Wait for all background processes
wait