Skip to content

Commit 1037d7f

Browse files
Initial version
1 parent 129a539 commit 1037d7f

File tree

2 files changed

+193
-138
lines changed

2 files changed

+193
-138
lines changed

alerts/periodic_sender.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
# This script is responsible for the infinite loop and the sleep between passes.
3+
# It forwards the path of the .env file as the first argument to action.sh
4+
5+
ENV_FILE="$1"
6+
7+
if [[ -z "$ENV_FILE" ]]; then
8+
echo "Usage: $0 path/to/.env"
9+
exit 1
10+
fi
11+
12+
# Fetches the current ETH gas price
13+
function fetch_gas_price() {
14+
# TODO: We should have a second RPC_URL for fetching gas price to avoid being rate limited
15+
gas_price=$(cast gas-price --rpc-url $RPC_URL)
16+
echo $gas_price
17+
}
18+
19+
source "$ENV_FILE"
20+
21+
# Each tic lasts for 30 minutes
22+
sleep_time=1800
23+
tic=0
24+
25+
while true; do
26+
echo "Starting pass #$tic"
27+
28+
current_gas_price=$(fetch_gas_price)
29+
echo "Current gas price: $current_gas_price wei"
30+
31+
# Conditions for sending proofs:
32+
if { [ $tic -ge 10 ] && [ $tic -lt 14 ] && [ $current_gas_price -lt 2000000000 ]; }; then
33+
# - Between 10 and 14 tics (5 to 7 hours), if gas price is below 2 gwei, send a proof
34+
message="Sending proof at tic $tic with gas price $current_gas_price wei"
35+
echo "$message"
36+
./sender_with_alert.sh "$ENV_FILE"
37+
elif { [ $tic -ge 14 ] && [ $tic -lt 16 ] && [ $current_gas_price -lt 5000000000 ]; }; then
38+
# - Between 14 and 16 tics (7 to 8 hours), if gas price is below 5 gwei, send a proof
39+
message="Sending proof at tic $tic with gas price $current_gas_price wei"
40+
echo "$message"
41+
./sender_with_alert.sh "$ENV_FILE"
42+
elif { [ $tic -ge 16 ] && [ $tic -lt 24 ] && [ $current_gas_price -lt 15000000000 ]; }; then
43+
# - Between 16 and 24 tics (8 to 12 hours), if gas price is below 15 gwei, send a proof
44+
message="Sending proof at tic $tic with gas price $current_gas_price wei"
45+
echo "$message"
46+
./sender_with_alert.sh "$ENV_FILE"
47+
elif { [ $tic -ge 50 ] && [ $current_gas_price -lt 50000000000 ]; }; then
48+
# - After 50 tics (25 hours), if gas price is below 50 gwei, send a proof
49+
message="Sending proof at tic $tic with gas price $current_gas_price wei"
50+
echo "$message"
51+
./sender_with_alert.sh "$ENV_FILE"
52+
fi
53+
54+
tic=$((tic + 1))
55+
if [ $tic -ge 96 ]; then
56+
tic=0
57+
fi
58+
59+
echo "Sleeping $sleep_time seconds (($((sleep_time / 60)) minutes))"
60+
sleep "$sleep_time"
61+
done

alerts/sender_with_alert.sh

Lines changed: 132 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - PRIVATE_KEY
1111
# - VERIFICATION_WAIT_TIME
1212
# - LOGS_BLOCK_RANGE
13-
# - SLEEP_TIME
1413
# - PAGER_DUTY_KEY
1514
# - PAGER_DUTY_EMAIL
1615
# - PAGER_DUTY_SERVICE_ID
@@ -30,7 +29,7 @@
3029
source "$1"
3130

3231
# Just for debugging
33-
#set -ex
32+
# set -ex
3433

3534
### FUNCTIONS ###
3635

@@ -52,7 +51,7 @@ function fetch_tx_cost() {
5251
fi
5352
}
5453

55-
# Function to get the tx cost from the tx hash
54+
# Function to get the number of proofs in the batch (from create task tx)
5655
# @param tx_hash
5756
function get_number_proofs_in_batch_from_create_task_tx() {
5857
if [[ -z "$1" ]]; then
@@ -80,152 +79,147 @@ function send_slack_message() {
8079
. alerts/slack.sh "$1"
8180
}
8281

83-
#################
84-
while true
82+
################# SEND LOGIC #################
83+
84+
## Remove Proof Data
85+
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
86+
rm -rf ./aligned_verification_data/*
87+
88+
mkdir -p ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs
89+
90+
## Generate Proof
91+
nonce=$(aligned get-user-nonce --network $NETWORK --user_addr $SENDER_ADDRESS 2>&1 | awk '{print $9}')
92+
echo $nonce
93+
if ! [[ "$nonce" =~ ^[0-9]+$ ]]; then
94+
echo "Failed getting user nonce, retrying in 10 seconds"
95+
sleep 10
96+
exit 0
97+
fi
98+
99+
x=$((nonce + 1)) # So we don't have any issues with nonce = 0
100+
echo "Generating proof $x != 0, nonce: $nonce"
101+
go run ./scripts/test_files/gnark_groth16_bn254_infinite_script/cmd/main.go $x
102+
103+
## Send Proof
104+
echo "Submitting $REPETITIONS proofs $x != 0"
105+
submit=$(aligned submit \
106+
--proving_system GnarkGroth16Bn254 \
107+
--repetitions $REPETITIONS \
108+
--proof "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.proof" \
109+
--public_input "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.pub" \
110+
--vk "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.vk" \
111+
--private_key $PRIVATE_KEY \
112+
--rpc_url $RPC_URL \
113+
--network $NETWORK \
114+
--max_fee 0.004ether \
115+
--random_address \
116+
2>&1)
117+
118+
echo "$submit"
119+
120+
submit_errors=$(echo "$submit" | grep -oE 'ERROR[^]]*]([^[]*)' | sed 's/^[^]]*]//;s/[[:space:]]*$//')
121+
122+
# Loop through each error found and print with the custom message
123+
is_error=0
124+
while IFS= read -r error; do
125+
if [[ -n "$error" ]]; then
126+
slack_error_message="Error submitting proof to $NETWORK: $error"
127+
send_slack_message "$slack_error_message"
128+
is_error=1
129+
fi
130+
done <<< "$submit_errors"
131+
132+
if [ $is_error -eq 1 ]; then
133+
echo "Error submitting proofs to $NETWORK, retrying in 60 seconds"
134+
send_slack_message "Error submitting proofs to $NETWORK, retrying in 60 seconds"
135+
sleep 60
136+
exit 0
137+
fi
138+
139+
echo "Waiting $VERIFICATION_WAIT_TIME seconds for verification"
140+
sleep $VERIFICATION_WAIT_TIME
141+
142+
# Get all the batches merkle roots
143+
batch_merkle_roots=$(echo "$submit" | grep "Batch merkle root: " | grep -oE "0x[[:alnum:]]{64}" | uniq)
144+
145+
# Fetch the logs of both submission and response
146+
current_block_number=$(cast block-number --rpc-url $RPC_URL)
147+
from_block_number=$(($current_block_number - LOGS_BLOCK_RANGE))
148+
if [ $from_block_number -lt 0 ]; then
149+
from_block_number=0
150+
fi
151+
152+
total_fee_in_wei=0
153+
total_number_proofs=0
154+
batch_explorer_urls=()
155+
for batch_merkle_root in $batch_merkle_roots
85156
do
157+
# Construct the batcher explorer url
158+
batch_explorer_url="$EXPLORER_URL/batches/$batch_merkle_root"
159+
batch_explorer_urls+=($batch_explorer_url)
86160

87-
## Remove Proof Data
88-
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
89-
rm -rf ./aligned_verification_data/*
161+
log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'NewBatchV3 (bytes32 indexed batchMerkleRoot, address senderAddress, uint32 taskCreatedBlock, string batchDataPointer, uint256 respondToTaskFeeLimit)' $batch_merkle_root)
162+
submission_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')
90163

91-
mkdir -p ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs
164+
log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'BatchVerified (bytes32 indexed batchMerkleRoot, address senderAddress)' $batch_merkle_root)
165+
response_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')
92166

93-
## Generate Proof
94-
nonce=$(aligned get-user-nonce --network $NETWORK --user_addr $SENDER_ADDRESS 2>&1 | awk '{print $9}')
95-
echo $nonce
96-
if ! [[ "$nonce" =~ ^[0-9]+$ ]]; then
97-
echo "Failed getting user nonce, retrying in 10 seconds"
98-
sleep 10
99-
continue
100-
fi
167+
# Calculate fees for transactions
168+
number_proofs_in_batch=$(get_number_proofs_in_batch_from_create_task_tx $submission_tx_hash)
169+
submission_fee_in_wei=$(fetch_tx_cost $submission_tx_hash)
170+
response_fee_in_wei=$(fetch_tx_cost $response_tx_hash)
171+
batch_fee_in_wei=$((submission_fee_in_wei + response_fee_in_wei))
101172

102-
x=$((nonce + 1)) # So we don't have any issues with nonce = 0
103-
echo "Generating proof $x != 0, nonce: $nonce"
104-
go run ./scripts/test_files/gnark_groth16_bn254_infinite_script/cmd/main.go $x
105-
106-
## Send Proof
107-
echo "Submitting $REPETITIONS proofs $x != 0"
108-
submit=$(aligned submit \
109-
--proving_system GnarkGroth16Bn254 \
110-
--repetitions $REPETITIONS \
111-
--proof "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.proof" \
112-
--public_input "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.pub" \
113-
--vk "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_gnark_groth16_0_12_0.vk" \
114-
--private_key $PRIVATE_KEY \
115-
--rpc_url $RPC_URL \
116-
--network $NETWORK \
117-
--max_fee 0.004ether \
118-
--random_address \
119-
2>&1)
173+
# Accumulate the fee
174+
total_fee_in_wei=$(($total_fee_in_wei + $batch_fee_in_wei))
120175

121-
echo "$submit"
122-
123-
submit_errors=$(echo "$submit" | grep -oE 'ERROR[^]]*]([^[]*)' | sed 's/^[^]]*]//;s/[[:space:]]*$//')
124-
125-
# Loop through each error found and print with the custom message
126-
is_error=0
127-
while IFS= read -r error; do
128-
if [[ -n "$error" ]]; then
129-
slack_error_message="Error submitting proof to $NETWORK: $error"
130-
send_slack_message "$slack_error_message"
131-
is_error=1
132-
fi
133-
done <<< "$submit_errors"
134-
135-
if [ $is_error -eq 1 ]; then
136-
echo "Error submitting proofs to $NETWORK, retrying in 60 seconds"
137-
send_slack_message "Error submitting proofs to $NETWORK, retrying in 60 seconds"
138-
sleep 60
139-
continue
140-
fi
176+
# Accumulate proofs in batch
177+
total_number_proofs=$(($total_number_proofs + $number_proofs_in_batch))
178+
done
141179

142-
echo "Waiting $VERIFICATION_WAIT_TIME seconds for verification"
143-
sleep $VERIFICATION_WAIT_TIME
180+
# Calculate the spent amount by converting the fee to ETH
181+
wei_to_eth_division_factor=$((10**18))
182+
spent_amount=$(echo "scale=30; $total_fee_in_wei / (10^18)" | bc -l | awk '{printf "%.15f", $0}')
144183

145-
# Get all the batches merkle roots
146-
batch_merkle_roots=$(echo "$submit" | grep "Batch merkle root: " | grep -oE "0x[[:alnum:]]{64}" | uniq)
184+
eth_usd=$(curl -s https://cryptoprices.cc/ETH/)
185+
spent_amount_usd=$(echo "$spent_amount * $eth_usd" | bc | awk '{printf "%.2f", $1}')
147186

148-
# Fetch the logs of both submission and response
149-
current_block_number=$(cast block-number --rpc-url $RPC_URL)
150-
from_block_number=$(($current_block_number - LOGS_BLOCK_RANGE))
151-
if [ $from_block_number -lt 0 ]; then
152-
from_block_number=0
153-
fi
187+
verified=1
154188

155-
total_fee_in_wei=0
156-
total_number_proofs=0
157-
batch_explorer_urls=()
158-
for batch_merkle_root in $batch_merkle_roots
159-
do
160-
# Construct the batcher explorer url
161-
batch_explorer_url="$EXPLORER_URL/batches/$batch_merkle_root"
162-
batch_explorer_urls+=($batch_explorer_url)
163-
164-
log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'NewBatchV3 (bytes32 indexed batchMerkleRoot, address senderAddress, uint32 taskCreatedBlock, string batchDataPointer, uint256 respondToTaskFeeLimit)' $batch_merkle_root)
165-
submission_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')
166-
167-
log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'BatchVerified (bytes32 indexed batchMerkleRoot, address senderAddress)' $batch_merkle_root)
168-
response_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')
169-
170-
# Calculate fees for transactions
171-
number_proofs_in_batch=$(get_number_proofs_in_batch_from_create_task_tx $submission_tx_hash)
172-
submission_fee_in_wei=$(fetch_tx_cost $submission_tx_hash)
173-
response_fee_in_wei=$(fetch_tx_cost $response_tx_hash)
174-
batch_fee_in_wei=$((submission_fee_in_wei + response_fee_in_wei))
175-
176-
# Accumulate the fee
177-
total_fee_in_wei=$(($total_fee_in_wei + $batch_fee_in_wei))
178-
179-
# Accumulate proofs in batch
180-
total_number_proofs=$(($total_number_proofs + $number_proofs_in_batch))
181-
done
182-
183-
# Calculate the spent amount by converting the fee to ETH
184-
wei_to_eth_division_factor=$((10**18))
185-
spent_amount=$(echo "scale=30; $total_fee_in_wei / (10^18)" | bc -l | awk '{printf "%.15f", $0}')
186-
187-
eth_usd=$(curl -s https://cryptoprices.cc/ETH/)
188-
spent_amount_usd=$(echo "$spent_amount * $eth_usd" | bc | awk '{printf "%.2f", $1}')
189-
190-
slack_messsage=""
191-
verified=1
192-
193-
## Verify Proofs
194-
echo "Verifying $REPETITIONS proofs $x != 0"
195-
for proof in ./aligned_verification_data/*.cbor; do
196-
## Validate Proof on Chain
197-
verification=$(aligned verify-proof-onchain \
198-
--aligned-verification-data $proof \
199-
--rpc_url $RPC_URL \
200-
--network $NETWORK \
201-
2>&1)
202-
203-
## Send Alert if Verification Fails
204-
if echo "$verification" | grep -q not; then
205-
message="Proof verification failed for $proof [ ${batch_explorer_urls[@]} ]"
206-
echo "$message"
207-
send_pagerduty_alert "$message"
208-
verified=0 # Some proofs failed, so we should not send the success message
209-
break
210-
elif echo "$verification" | grep -q verified; then
211-
echo "Proof verification succeeded for $proof"
212-
fi
213-
done
214-
215-
if [ $verified -eq 1 ]; then
216-
slack_message="$total_number_proofs proofs submitted and verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
217-
else
218-
slack_message="$total_number_proofs proofs submitted but not verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
189+
## Verify Proofs
190+
echo "Verifying $REPETITIONS proofs $x != 0"
191+
for proof in ./aligned_verification_data/*.cbor; do
192+
## Validate Proof on Chain
193+
verification=$(aligned verify-proof-onchain \
194+
--aligned-verification-data $proof \
195+
--rpc_url $RPC_URL \
196+
--network $NETWORK \
197+
2>&1)
198+
199+
## Send Alert if Verification Fails
200+
if echo "$verification" | grep -q not; then
201+
message="Proof verification failed for $proof [ ${batch_explorer_urls[@]} ]"
202+
echo "$message"
203+
send_pagerduty_alert "$message"
204+
verified=0 # Some proofs failed, so we should not send the success message
205+
break
206+
elif echo "$verification" | grep -q verified; then
207+
echo "Proof verification succeeded for $proof"
219208
fi
209+
done
220210

221-
## Send Update to Slack
222-
echo "$slack_message"
223-
send_slack_message "$slack_message"
211+
if [ $verified -eq 1 ]; then
212+
slack_message="$total_number_proofs proofs submitted and verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
213+
else
214+
slack_message="$total_number_proofs proofs submitted but not verified. We sent $REPETITIONS proofs. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
215+
fi
224216

225-
## Remove Proof Data
226-
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
227-
rm -rf ./aligned_verification_data/*
217+
## Send Update to Slack
218+
echo "$slack_message"
219+
send_slack_message "$slack_message"
228220

229-
echo "Sleeping $SLEEP_TIME seconds"
230-
sleep $SLEEP_TIME
231-
done
221+
## Remove Proof Data
222+
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
223+
rm -rf ./aligned_verification_data/*
224+
225+
exit 0

0 commit comments

Comments
 (0)