Skip to content

Commit 0301e40

Browse files
committed
simplify more
1 parent 218e953 commit 0301e40

File tree

3 files changed

+54
-90
lines changed

3 files changed

+54
-90
lines changed

sequencer-migration/common-functions.sh

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ get_block_info() {
5656
return 1
5757
fi
5858

59-
local block_number=$(echo "$block_data" | jq -r '.number // empty')
59+
local block_number_hex=$(echo "$block_data" | jq -r '.number // empty')
6060
local block_hash=$(echo "$block_data" | jq -r '.hash // empty')
6161

62-
if [[ -z "$block_number" || -z "$block_hash" || "$block_number" == "null" || "$block_hash" == "null" ]]; then
62+
if [[ -z "$block_number_hex" || -z "$block_hash" || "$block_number_hex" == "null" || "$block_hash" == "null" ]]; then
6363
return 1
6464
fi
6565

66+
# Convert hex to decimal
67+
local block_number=$(echo "$block_number_hex" | xargs cast to-dec)
68+
6669
echo "$block_number $block_hash"
6770
}
6871

@@ -74,19 +77,14 @@ get_latest_block_info() {
7477
# Get only block number for a given RPC URL
7578
get_block_number() {
7679
local rpc_url="$1"
77-
local block_data
78-
79-
if ! block_data=$(cast block latest --json --rpc-url "$rpc_url" 2>/dev/null); then
80-
return 1
81-
fi
80+
local block_info
8281

83-
local block_number=$(echo "$block_data" | jq -r '.number // empty')
84-
85-
if [[ -z "$block_number" || "$block_number" == "null" ]]; then
82+
if ! block_info=$(get_block_info "$rpc_url" "latest"); then
8683
return 1
8784
fi
8885

89-
echo "$block_number"
86+
# Extract just the block number (first field)
87+
echo "$block_info" | awk '{print $1}'
9088
}
9189

9290
# Get chain ID for a given RPC URL
@@ -245,4 +243,42 @@ check_rpc_connectivity() {
245243
fi
246244

247245
log_success "Both nodes are accessible"
246+
}
247+
248+
# Common pre-flight checks for sequencer migration scripts
249+
perform_pre_flight_checks() {
250+
log_info "=== PRE-FLIGHT CHECKS ==="
251+
252+
check_rpc_connectivity
253+
254+
# Get current block states
255+
local l2geth_info=$(get_latest_block_info "$L2GETH_RPC_URL")
256+
local l2reth_info=$(get_latest_block_info "$L2RETH_RPC_URL")
257+
258+
local current_l2geth_block=$(echo "$l2geth_info" | awk '{print $1}')
259+
local l2geth_hash=$(echo "$l2geth_info" | awk '{print $2}')
260+
local current_l2reth_block=$(echo "$l2reth_info" | awk '{print $1}')
261+
local l2reth_hash=$(echo "$l2reth_info" | awk '{print $2}')
262+
263+
log_info "L2GETH current block: #$current_l2geth_block (hash: $l2geth_hash)"
264+
log_info "L2RETH current block: #$current_l2reth_block (hash: $l2reth_hash)"
265+
266+
# Verify nodes are on the same chain by comparing chain IDs
267+
local l2geth_chain_id=$(get_chain_id "$L2GETH_RPC_URL")
268+
local l2reth_chain_id=$(get_chain_id "$L2RETH_RPC_URL")
269+
270+
if [[ -z "$l2geth_chain_id" || -z "$l2reth_chain_id" ]]; then
271+
log_error "Failed to retrieve chain IDs from one or both nodes"
272+
exit 1
273+
fi
274+
275+
if [[ "$l2geth_chain_id" != "$l2reth_chain_id" ]]; then
276+
log_error "Nodes are on different chains! Chain IDs differ:"
277+
log_error " L2GETH: $l2geth_chain_id"
278+
log_error " L2RETH: $l2reth_chain_id"
279+
exit 1
280+
fi
281+
log_success "Nodes are on the same chain (Chain ID: $l2geth_chain_id)"
282+
283+
log_success "Pre-flight checks completed"
248284
}

sequencer-migration/migrate-sequencer.sh

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,18 @@ source "$(dirname "$0")/common-functions.sh"
1111

1212
# Global variables to track state
1313
START_TIME=$(date +%s)
14-
INITIAL_L2GETH_BLOCK=""
15-
INITIAL_L2RETH_BLOCK=""
1614
L2GETH_STOP_BLOCK=""
1715
L2RETH_FINAL_BLOCK=""
1816

19-
pre_flight_checks() {
20-
log_info "=== PRE-FLIGHT CHECKS ==="
21-
22-
check_rpc_connectivity
23-
24-
# Get initial block states
25-
local l2geth_info=$(get_latest_block_info "$L2GETH_RPC_URL")
26-
local l2reth_info=$(get_latest_block_info "$L2RETH_RPC_URL")
27-
28-
INITIAL_L2GETH_BLOCK=$(echo "$l2geth_info" | awk '{print $1}')
29-
local l2geth_hash=$(echo "$l2geth_info" | awk '{print $2}')
30-
INITIAL_L2RETH_BLOCK=$(echo "$l2reth_info" | awk '{print $1}')
31-
local l2reth_hash=$(echo "$l2reth_info" | awk '{print $2}')
32-
33-
log_info "L2GETH current block: #$INITIAL_L2GETH_BLOCK (hash: $l2geth_hash)"
34-
log_info "L2RETH current block: #$INITIAL_L2RETH_BLOCK (hash: $l2reth_hash)"
17+
migrate_pre_flight_checks() {
18+
perform_pre_flight_checks
3519

3620
# Check if l2geth is mining
3721
if ! is_l2geth_mining; then
3822
log_error "L2GETH is not currently mining. Please start mining first."
3923
exit 1
4024
fi
4125
log_success "L2GETH is currently mining"
42-
43-
# Verify nodes are on the same chain by comparing chain IDs
44-
local l2geth_chain_id=$(get_chain_id "$L2GETH_RPC_URL")
45-
local l2reth_chain_id=$(get_chain_id "$L2RETH_RPC_URL")
46-
47-
if [[ -z "$l2geth_chain_id" || -z "$l2reth_chain_id" ]]; then
48-
log_error "Failed to retrieve chain IDs from one or both nodes"
49-
exit 1
50-
fi
51-
52-
if [[ "$l2geth_chain_id" != "$l2reth_chain_id" ]]; then
53-
log_error "Nodes are on different chains! Chain IDs differ:"
54-
log_error " L2GETH: $l2geth_chain_id"
55-
log_error " L2RETH: $l2reth_chain_id"
56-
exit 1
57-
fi
58-
log_success "Nodes are on the same chain (Chain ID: $l2geth_chain_id)"
59-
60-
# Verify nodes are on the same chain by comparing a recent block hash
61-
local compare_block=$((INITIAL_L2RETH_BLOCK < INITIAL_L2GETH_BLOCK ? INITIAL_L2RETH_BLOCK : INITIAL_L2GETH_BLOCK))
62-
if [[ $compare_block -gt 0 ]]; then
63-
local l2geth_compare_info=$(get_block_info "$L2GETH_RPC_URL" "$compare_block")
64-
local l2reth_compare_info=$(get_block_info "$L2RETH_RPC_URL" "$compare_block")
65-
66-
if [[ -z "$l2geth_compare_info" || -z "$l2reth_compare_info" ]]; then
67-
log_error "Failed to retrieve block #$compare_block from one or both nodes"
68-
exit 1
69-
fi
70-
71-
local l2geth_compare_hash=$(echo "$l2geth_compare_info" | awk '{print $2}')
72-
local l2reth_compare_hash=$(echo "$l2reth_compare_info" | awk '{print $2}')
73-
74-
if [[ "$l2geth_compare_hash" != "$l2reth_compare_hash" ]]; then
75-
log_error "Nodes are on different chains! Block #$compare_block hashes differ:"
76-
log_error " L2GETH: $l2geth_compare_hash"
77-
log_error " L2RETH: $l2reth_compare_hash"
78-
exit 1
79-
fi
80-
log_success "Block hash verification passed at block #$compare_block"
81-
fi
82-
83-
log_success "Pre-flight checks completed"
8426
}
8527

8628
print_summary() {
@@ -89,8 +31,6 @@ print_summary() {
8931

9032
log_info "=== MIGRATION SUMMARY ==="
9133
log_info "Migration completed in ${total_time}s"
92-
log_info "Initial L2GETH block: #$INITIAL_L2GETH_BLOCK"
93-
log_info "Initial L2RETH block: #$INITIAL_L2RETH_BLOCK"
9434
log_info "L2GETH stopped at block: #$L2GETH_STOP_BLOCK"
9535
log_info "L2RETH final block: #$L2RETH_FINAL_BLOCK"
9636

@@ -122,7 +62,7 @@ main() {
12262
log_info "L2RETH will produce $blocks_to_produce blocks"
12363

12464
check_env_vars
125-
pre_flight_checks
65+
migrate_pre_flight_checks
12666

12767
# Double check if user wants to proceed
12868
read -p "Proceed with migration? (y/N): " confirm

sequencer-migration/revert-l2geth-to-block.sh

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,12 @@ reset_l2geth_to_block() {
3434

3535

3636

37-
pre_flight_checks() {
38-
log_info "=== PRE-FLIGHT CHECKS ==="
37+
revert_pre_flight_checks() {
38+
perform_pre_flight_checks
3939

40-
check_rpc_connectivity
41-
42-
# Get current block states
40+
# Get current l2geth block for validation
4341
local l2geth_info=$(get_latest_block_info "$L2GETH_RPC_URL")
44-
local l2reth_info=$(get_latest_block_info "$L2RETH_RPC_URL")
45-
4642
local current_l2geth_block=$(echo "$l2geth_info" | awk '{print $1}')
47-
local l2geth_hash=$(echo "$l2geth_info" | awk '{print $2}')
48-
local current_l2reth_block=$(echo "$l2reth_info" | awk '{print $1}')
49-
local l2reth_hash=$(echo "$l2reth_info" | awk '{print $2}')
50-
51-
log_info "L2GETH current block: #$current_l2geth_block (hash: $l2geth_hash)"
52-
log_info "L2RETH current block: #$current_l2reth_block (hash: $l2reth_hash)"
5343

5444
# Validate target block exists and is reachable
5545
if [[ $TARGET_BLOCK -gt $current_l2geth_block ]]; then
@@ -67,8 +57,6 @@ pre_flight_checks() {
6757

6858
TARGET_HASH=$(echo "$target_info" | awk '{print $2}')
6959
log_info "Target block #$TARGET_BLOCK exists (hash: $TARGET_HASH)"
70-
71-
log_success "Pre-flight checks completed"
7260
}
7361

7462
print_summary() {
@@ -106,7 +94,7 @@ main() {
10694
log_info "Starting l2geth revert to block #$TARGET_BLOCK"
10795

10896
check_env_vars
109-
pre_flight_checks
97+
revert_pre_flight_checks
11098

11199
# Phase 1: Disable sequencing on both nodes
112100
log_info "=== PHASE 1: DISABLING SEQUENCING ==="

0 commit comments

Comments
 (0)