Skip to content

Commit 218e953

Browse files
committed
address review comments
1 parent bc8e650 commit 218e953

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

sequencer-migration/common-functions.sh

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,45 +43,59 @@ check_env_vars() {
4343
log_info "L2RETH_RPC_URL: $L2RETH_RPC_URL"
4444
}
4545

46-
# Get block number and hash for a given RPC URL
47-
get_latest_block_info() {
46+
# Get block number and hash for a given RPC URL and block identifier
47+
# Parameters:
48+
# $1: rpc_url - The RPC endpoint URL
49+
# $2: block_identifier - Block number or "latest" (default: "latest")
50+
get_block_info() {
4851
local rpc_url="$1"
49-
local temp_file=$(mktemp)
52+
local block_identifier="${2:-latest}"
53+
local block_data
5054

51-
if ! cast block latest --rpc-url "$rpc_url" > "$temp_file" 2>/dev/null; then
52-
rm -f "$temp_file"
55+
if ! block_data=$(cast block "$block_identifier" --json --rpc-url "$rpc_url" 2>/dev/null); then
5356
return 1
5457
fi
5558

56-
local block_number=$(grep "^number" "$temp_file" | awk '{print $2}')
57-
local block_hash=$(grep "^hash" "$temp_file" | awk '{print $2}')
59+
local block_number=$(echo "$block_data" | jq -r '.number // empty')
60+
local block_hash=$(echo "$block_data" | jq -r '.hash // empty')
61+
62+
if [[ -z "$block_number" || -z "$block_hash" || "$block_number" == "null" || "$block_hash" == "null" ]]; then
63+
return 1
64+
fi
5865

59-
rm -f "$temp_file"
6066
echo "$block_number $block_hash"
6167
}
6268

63-
# Get block info for a specific block number
64-
get_block_info() {
69+
# Get latest block info (convenience function)
70+
get_latest_block_info() {
71+
get_block_info "$1" "latest"
72+
}
73+
74+
# Get only block number for a given RPC URL
75+
get_block_number() {
6576
local rpc_url="$1"
66-
local block_number="$2"
67-
local temp_file=$(mktemp)
77+
local block_data
6878

69-
if ! cast block "$block_number" --rpc-url "$rpc_url" > "$temp_file" 2>/dev/null; then
70-
rm -f "$temp_file"
79+
if ! block_data=$(cast block latest --json --rpc-url "$rpc_url" 2>/dev/null); then
7180
return 1
7281
fi
7382

74-
local block_num=$(grep "^number" "$temp_file" | awk '{print $2}')
75-
local block_hash=$(grep "^hash" "$temp_file" | awk '{print $2}')
83+
local block_number=$(echo "$block_data" | jq -r '.number // empty')
7684

77-
rm -f "$temp_file"
78-
echo "$block_num $block_hash"
85+
if [[ -z "$block_number" || "$block_number" == "null" ]]; then
86+
return 1
87+
fi
88+
89+
echo "$block_number"
7990
}
8091

81-
# Get only block number for a given RPC URL
82-
get_block_number() {
92+
# Get chain ID for a given RPC URL
93+
get_chain_id() {
8394
local rpc_url="$1"
84-
cast block latest --rpc-url "$rpc_url" 2>/dev/null | grep "^number" | awk '{print $2}'
95+
96+
if ! cast chain-id --rpc-url "$rpc_url" 2>/dev/null; then
97+
return 1
98+
fi
8599
}
86100

87101
# Check if l2geth is mining
@@ -186,8 +200,21 @@ wait_for_block() {
186200
log_success "$node_name reached target block #$target_block (hash: $target_hash)"
187201
return 0
188202
elif [[ "$current_block" -gt "$target_block" ]]; then
189-
log_success "$node_name surpassed target, now at block #$current_block (hash: $current_hash)"
190-
return 0
203+
# Verify the target block hash even though we surpassed it
204+
local target_block_info=$(get_block_info "$rpc_url" "$target_block")
205+
if [[ -n "$target_block_info" ]]; then
206+
local actual_target_hash=$(echo "$target_block_info" | awk '{print $2}')
207+
if [[ "$actual_target_hash" == "$target_hash" ]]; then
208+
log_success "$node_name surpassed target, now at block #$current_block (hash: $current_hash), target block verified"
209+
return 0
210+
else
211+
log_error "$node_name surpassed target but target block #$target_block hash mismatch: expected $target_hash, got $actual_target_hash"
212+
return 1
213+
fi
214+
else
215+
log_error "$node_name surpassed target but failed to verify target block #$target_block"
216+
return 1
217+
fi
191218
else
192219
log_warning "$node_name at block #$current_block but hash mismatch: expected $target_hash, got $current_hash"
193220
fi

sequencer-migration/migrate-sequencer.sh

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,44 @@ pre_flight_checks() {
4040
fi
4141
log_success "L2GETH is currently mining"
4242

43-
# Verify nodes are on the same chain by comparing a recent block
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
4461
local compare_block=$((INITIAL_L2RETH_BLOCK < INITIAL_L2GETH_BLOCK ? INITIAL_L2RETH_BLOCK : INITIAL_L2GETH_BLOCK))
4562
if [[ $compare_block -gt 0 ]]; then
46-
local l2geth_compare_hash=$(cast block "$compare_block" --rpc-url "$L2GETH_RPC_URL" 2>/dev/null | grep "^hash" | awk '{print $2}')
47-
local l2reth_compare_hash=$(cast block "$compare_block" --rpc-url "$L2RETH_RPC_URL" 2>/dev/null | grep "^hash" | awk '{print $2}')
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}')
4873

4974
if [[ "$l2geth_compare_hash" != "$l2reth_compare_hash" ]]; then
5075
log_error "Nodes are on different chains! Block #$compare_block hashes differ:"
5176
log_error " L2GETH: $l2geth_compare_hash"
5277
log_error " L2RETH: $l2reth_compare_hash"
5378
exit 1
5479
fi
55-
log_success "Nodes are on the same chain (verified at block #$compare_block)"
80+
log_success "Block hash verification passed at block #$compare_block"
5681
fi
5782

5883
log_success "Pre-flight checks completed"

0 commit comments

Comments
 (0)