-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathupdate_migration_state.sh
More file actions
executable file
·142 lines (118 loc) · 4.14 KB
/
update_migration_state.sh
File metadata and controls
executable file
·142 lines (118 loc) · 4.14 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
#!/bin/bash
# Copyright (c) RoochNetwork
# SPDX-License-Identifier: Apache-2.0
# Check if the input file is provided
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <path_to_users_file> [--batch=batch_size]"
echo "Example: $0 ./scripts/users_test.txt"
echo "Example with batch: $0 ./scripts/users_test.txt --batch=30"
echo " (default batch size is 30)"
exit 1
fi
INPUT_FILE="$1"
BATCH_SIZE=30
# Check if batch parameter is provided
if [ "$#" -eq 2 ]; then
if [[ "$2" =~ ^--batch=([0-9]+)$ ]]; then
BATCH_SIZE="${BASH_REMATCH[1]}"
echo "Running with batch size: $BATCH_SIZE"
else
echo "Error: Invalid batch parameter format. Use --batch=N where N is a number."
exit 1
fi
fi
# Check if the file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File $INPUT_FILE does not exist."
echo "Please provide the full path to the users file."
exit 1
fi
# Create a temporary file for commands
TEMP_COMMANDS_FILE=$(mktemp)
# Counter for tracking progress
total_records=$(wc -l < "$INPUT_FILE")
current=0
valid=0
skipped=0
echo "Processing migration state updates from $INPUT_FILE"
echo "Total records to process: $total_records"
echo "Will process in batches of $BATCH_SIZE addresses"
# Create a temporary addresses file
TEMP_ADDRESSES_FILE=$(mktemp)
# Process each line of the file for batch mode
while IFS=$'\t' read -r address rest_of_line || [[ -n "$address" ]]; do
# Skip header or empty lines
if [[ "$address" =~ ^[[:space:]]*$ || "$address" == "address" || "$address" == "owner" ]]; then
continue
fi
# Increment counter
((current++))
# Remove any potential whitespace
address=$(echo "$address" | tr -d ' ')
# Skip address 0x0
if [[ "$address" == "0x0" ]]; then
echo "[$current/$total_records] Skipping address 0x0..."
((skipped++))
continue
fi
# Add address to temp file
echo "$address" >> "$TEMP_ADDRESSES_FILE"
((valid++))
done < "$INPUT_FILE"
# Print summary before processing batches
echo "-------------------------------------------"
echo "Address processing completed"
echo "Total records processed: $current"
echo "Valid addresses: $valid"
echo "Skipped addresses: $skipped"
echo "-------------------------------------------"
# Count total batches
total_lines=$(wc -l < "$TEMP_ADDRESSES_FILE")
total_batches=$(( (total_lines + BATCH_SIZE - 1) / BATCH_SIZE ))
echo "Will create $total_batches batches"
echo "-------------------------------------------"
# Process in batches
batch_num=0
while read -r line; do
# Start a new batch command
if (( batch_num % BATCH_SIZE == 0 )); then
if [[ -n "$batch_cmd" ]]; then
# Finalize previous batch command
batch_cmd+="]"
echo "$batch_cmd" >> "$TEMP_COMMANDS_FILE"
fi
# Start a new batch
current_batch=$(( batch_num / BATCH_SIZE + 1 ))
batch_cmd="echo \"Processing batch $current_batch/$total_batches...\"; rooch move run --function 0x3::coin_migration::update_migration_states_batch_entry --args \"vector<address>:"
first_in_batch=true
fi
# Add separator if not the first in batch
if ! $first_in_batch; then
batch_cmd+=","
else
first_in_batch=false
fi
# Add address to batch command
batch_cmd+="$line"
((batch_num++))
# Finalize the last batch if needed
if (( batch_num == total_lines )); then
batch_cmd+="\" --sender default; echo \"-------------------------------------------\""
echo "$batch_cmd" >> "$TEMP_COMMANDS_FILE"
elif (( batch_num % BATCH_SIZE == 0 )); then
batch_cmd+="\" --sender default; echo \"-------------------------------------------\""
fi
done < "$TEMP_ADDRESSES_FILE"
# Execute commands sequentially
echo "Starting batch execution..."
echo "-------------------------------------------"
bash "$TEMP_COMMANDS_FILE"
# Clean up
rm "$TEMP_ADDRESSES_FILE"
# Clean up temporary file
echo "$TEMP_COMMANDS_FILE"
rm "$TEMP_COMMANDS_FILE"
# Print completion message
echo "-------------------------------------------"
echo "Execution completed at $(date)"
echo "-------------------------------------------"