|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ENV VARIABLES: |
| 4 | +# - REPETITIONS |
| 5 | +# - EXPLORER_URL |
| 6 | +# - SENDER_ADDRESS |
| 7 | +# - BATCHER_URL |
| 8 | +# - RPC_URL |
| 9 | +# - EXPLORER_URL |
| 10 | +# - NETWORK |
| 11 | +# - PRIVATE_KEY |
| 12 | +# - VERIFICATION_WAIT_TIME |
| 13 | +# - LOGS_BLOCK_RANGE |
| 14 | +# - SLEEP_TIME |
| 15 | +# - PAGER_DUTY_KEY |
| 16 | +# - PAGER_DUTY_EMAIL |
| 17 | +# - PAGER_DUTY_SERVICE_ID |
| 18 | +# - SLACK_WEBHOOK_URL |
| 19 | + |
| 20 | +# TODO (Improvement): |
| 21 | +# 1. This script waits VERIFICATION_WAIT_TIME seconds before fetching the explorer for the response tx hash. |
| 22 | +# We should instead poll in a loop until the batch is marked as verified |
| 23 | + |
| 24 | +# ACKNOWLEDGMENTS |
| 25 | +# |
| 26 | +# Special thanks to AniV for their contribution on StackExchange regarding AWK formatting with floating point operations: |
| 27 | +# https://unix.stackexchange.com/questions/292087/how-do-i-get-bc-to-start-decimal-fractions-with-a-leading-zero |
| 28 | + |
| 29 | + |
| 30 | +# Load env file from $1 path |
| 31 | +source "$1" |
| 32 | + |
| 33 | +# Just for debugging |
| 34 | +#set -ex |
| 35 | + |
| 36 | +### FUNCTIONS ### |
| 37 | + |
| 38 | +# Function to get the tx cost from the tx hash |
| 39 | +# @param tx_hash |
| 40 | +function fetch_tx_cost() { |
| 41 | + if [[ -z "$1" ]]; then |
| 42 | + echo 0 |
| 43 | + else |
| 44 | + # Get the tx receipt from the blockchain |
| 45 | + receipt=$(cast receipt --rpc-url $RPC_URL $1) |
| 46 | + # Parse the gas used and gas price |
| 47 | + gas_price=$(echo "$receipt" | grep "effectiveGasPrice" | awk '{ print $2 }') |
| 48 | + gas_used=$(echo "$receipt" | grep "gasUsed" | awk '{ print $2 }') |
| 49 | + # Calculate fee in wei |
| 50 | + fee_in_wei=$(($gas_price * $gas_used)) |
| 51 | + |
| 52 | + echo $fee_in_wei |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# Function to send PagerDuty alert |
| 57 | +# @param message |
| 58 | +function send_pagerduty_alert() { |
| 59 | + . alerts/pagerduty.sh "$1" |
| 60 | +} |
| 61 | + |
| 62 | +# Function so send Slack message |
| 63 | +# @param message |
| 64 | +function send_slack_message() { |
| 65 | + . alerts/slack.sh "$1" |
| 66 | +} |
| 67 | + |
| 68 | +################# |
| 69 | +while true |
| 70 | +do |
| 71 | + |
| 72 | + ## Remove Proof Data |
| 73 | + rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/* |
| 74 | + rm -rf ./aligned_verification_data/* |
| 75 | + |
| 76 | + mkdir -p ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs |
| 77 | + |
| 78 | + ## Generate Proof |
| 79 | + nonce=$(aligned get-user-nonce --batcher_url $BATCHER_URL --user_addr $SENDER_ADDRESS 2>&1 | awk '{print $9}') |
| 80 | + x=$((nonce + 1)) # So we don't have any issues with nonce = 0 |
| 81 | + echo "Generating proof $x != 0" |
| 82 | + go run ./scripts/test_files/gnark_groth16_bn254_infinite_script/cmd/main.go $x |
| 83 | + |
| 84 | + ## Send Proof |
| 85 | + echo "Submitting $REPETITIONS proofs $x != 0" |
| 86 | + submit=$(aligned submit \ |
| 87 | + --proving_system Groth16Bn254 \ |
| 88 | + --repetitions $REPETITIONS \ |
| 89 | + --proof "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.proof" \ |
| 90 | + --public_input "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.pub" \ |
| 91 | + --vk "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.vk" \ |
| 92 | + --proof_generator_addr $SENDER_ADDRESS \ |
| 93 | + --private_key $PRIVATE_KEY \ |
| 94 | + --rpc_url $RPC_URL \ |
| 95 | + --batcher_url $BATCHER_URL \ |
| 96 | + --network $NETWORK \ |
| 97 | + --max_fee 4000000000000000 \ |
| 98 | + 2>&1) |
| 99 | + |
| 100 | + echo "$submit" |
| 101 | + |
| 102 | + echo "Waiting $VERIFICATION_WAIT_TIME seconds for verification" |
| 103 | + sleep $VERIFICATION_WAIT_TIME |
| 104 | + |
| 105 | + # Get all the batches merkle roots |
| 106 | + batch_merkle_roots=$(echo "$submit" | grep "Batch merkle root: " | grep -oE "0x[[:alnum:]]{64}" | uniq) |
| 107 | + |
| 108 | + # Fetch the logs of both submission and response |
| 109 | + current_block_number=$(cast block-number --rpc-url $RPC_URL) |
| 110 | + from_block_number=$(($current_block_number - LOGS_BLOCK_RANGE)) |
| 111 | + if [ $from_block_number -lt 0 ]; then |
| 112 | + from_block_number=0 |
| 113 | + fi |
| 114 | + |
| 115 | + total_fee_in_wei=0 |
| 116 | + batch_explorer_urls=() |
| 117 | + for batch_merkle_root in $batch_merkle_roots |
| 118 | + do |
| 119 | + # Construct the batcher explorer url |
| 120 | + batch_explorer_url="$EXPLORER_URL/batches/$batch_merkle_root" |
| 121 | + batch_explorer_urls+=($batch_explorer_url) |
| 122 | + |
| 123 | + 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) |
| 124 | + submission_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }') |
| 125 | + |
| 126 | + log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'BatchVerified (bytes32 indexed batchMerkleRoot, address senderAddress)' $batch_merkle_root) |
| 127 | + response_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }') |
| 128 | + |
| 129 | + # Calculate fees for transactions |
| 130 | + submission_fee_in_wei=$(fetch_tx_cost $submission_tx_hash) |
| 131 | + response_fee_in_wei=$(fetch_tx_cost $response_tx_hash) |
| 132 | + batch_fee_in_wei=$((submission_fee_in_wei + response_fee_in_wei)) |
| 133 | + |
| 134 | + # Accumulate the fee |
| 135 | + total_fee_in_wei=$(($total_fee_in_wei + $batch_fee_in_wei)) |
| 136 | + done |
| 137 | + |
| 138 | + # Calculate the spent amount by converting the fee to ETH |
| 139 | + wei_to_eth_division_factor=$((10**18)) |
| 140 | + spent_amount=$(echo "scale=30; $total_fee_in_wei / (10^18)" | bc -l | awk '{printf "%.15f", $0}') |
| 141 | + |
| 142 | + eth_usd=$(curl -s https://cryptoprices.cc/ETH/) |
| 143 | + spent_amount_usd=$(echo "$spent_amount * $eth_usd" | bc | awk '{printf "%.2f", $1}') |
| 144 | + |
| 145 | + slack_messsage="" |
| 146 | + verified=1 |
| 147 | + |
| 148 | + ## Verify Proofs |
| 149 | + echo "Verifying $REPETITIONS proofs $x != 0" |
| 150 | + for proof in ./aligned_verification_data/*.cbor; do |
| 151 | + ## Validate Proof on Chain |
| 152 | + verification=$(aligned verify-proof-onchain \ |
| 153 | + --aligned-verification-data $proof \ |
| 154 | + --rpc_url $RPC_URL \ |
| 155 | + --network $NETWORK \ |
| 156 | + 2>&1) |
| 157 | + |
| 158 | + ## Send Alert if Verification Fails |
| 159 | + if echo "$verification" | grep -q not; then |
| 160 | + message="Proof verification failed for $proof [ ${batch_explorer_urls[@]} ]" |
| 161 | + echo "$message" |
| 162 | + send_pagerduty_alert "$message" |
| 163 | + verified=0 # Some proofs failed, so we should not send the success message |
| 164 | + break |
| 165 | + elif echo "$verification" | grep -q verified; then |
| 166 | + echo "Proof verification succeeded for $proof" |
| 167 | + fi |
| 168 | + done |
| 169 | + |
| 170 | + if [ $verified -eq 1 ]; then |
| 171 | + slack_message="$REPETITIONS Proofs submitted and verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]" |
| 172 | + else |
| 173 | + slack_message="$REPETITIONS Proofs submitted but not verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]" |
| 174 | + fi |
| 175 | + |
| 176 | + ## Send Update to Slack |
| 177 | + echo "$slack_message" |
| 178 | + send_slack_message "$slack_message" |
| 179 | + |
| 180 | + ## Remove Proof Data |
| 181 | + rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/* |
| 182 | + rm -rf ./aligned_verification_data/* |
| 183 | + |
| 184 | + echo "Sleeping $SLEEP_TIME seconds" |
| 185 | + sleep $SLEEP_TIME |
| 186 | +done |
0 commit comments