Skip to content

Commit

Permalink
Merge branch 'staging' into batcher-gas-cost-metric
Browse files Browse the repository at this point in the history
  • Loading branch information
avilagaston9 committed Dec 30, 2024
2 parents 47476ba + f73d455 commit 9882ace
Show file tree
Hide file tree
Showing 68 changed files with 893,926 additions and 8,911 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CONFIG_FILE?=config-files/config.yaml
export OPERATOR_ADDRESS ?= $(shell yq -r '.operator.address' $(CONFIG_FILE))
AGG_CONFIG_FILE?=config-files/config-aggregator.yaml

OPERATOR_VERSION=v0.12.1
OPERATOR_VERSION=v0.12.2

ifeq ($(OS),Linux)
BUILD_ALL_FFI = $(MAKE) build_all_ffi_linux
Expand Down Expand Up @@ -52,7 +52,7 @@ deps: submodules go_deps build_all_ffi ## Install deps

go_deps:
@echo "Installing Go dependencies..."
go install github.com/maoueh/zap-pretty@latest
go install github.com/maoueh/zap-pretty@v0.3.0
go install github.com/ethereum/go-ethereum/cmd/abigen@latest
go install github.com/Layr-Labs/eigenlayer-cli/cmd/eigenlayer@latest

Expand Down
13 changes: 7 additions & 6 deletions aggregator/pkg/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,12 @@ func (agg *Aggregator) handleBlsAggServiceResponse(blsAggServiceResp blsagg.BlsA
if err == nil {
// In some cases, we may fail to retrieve the receipt for the transaction.
txHash := "Unknown"
effectiveGasPrice := "Unknown"
if receipt != nil {
txHash = receipt.TxHash.String()
effectiveGasPrice = receipt.EffectiveGasPrice.String()
}
agg.telemetry.TaskSentToEthereum(batchData.BatchMerkleRoot, txHash)
agg.telemetry.TaskSentToEthereum(batchData.BatchMerkleRoot, txHash, effectiveGasPrice)
agg.logger.Info("Aggregator successfully responded to task",
"taskIndex", blsAggServiceResp.TaskIndex,
"batchIdentifierHash", "0x"+hex.EncodeToString(batchIdentifierHash[:]))
Expand Down Expand Up @@ -326,9 +328,8 @@ func (agg *Aggregator) sendAggregatedResponse(batchIdentifierHash [32]byte, batc
"batchIdentifierHash", hex.EncodeToString(batchIdentifierHash[:]))

// This function is a callback that is called when the gas price is bumped on the avsWriter.SendAggregatedResponse
onGasPriceBumped := func(bumpedGasPrice *big.Int) {
agg.metrics.IncBumpedGasPriceForAggregatedResponse()
agg.telemetry.BumpedTaskGasPrice(batchMerkleRoot, bumpedGasPrice.String())
onSetGasPrice := func(gasPrice *big.Int) {
agg.telemetry.TaskSetGasPrice(batchMerkleRoot, gasPrice.String())
}

startTime := time.Now()
Expand All @@ -341,12 +342,12 @@ func (agg *Aggregator) sendAggregatedResponse(batchIdentifierHash [32]byte, batc
agg.AggregatorConfig.Aggregator.GasBumpIncrementalPercentage,
agg.AggregatorConfig.Aggregator.GasBumpPercentageLimit,
agg.AggregatorConfig.Aggregator.TimeToWaitBeforeBump,
onGasPriceBumped,
agg.metrics,
onSetGasPrice,
)
if err != nil {
agg.walletMutex.Unlock()
agg.logger.Infof("- Unlocked Wallet Resources: Error sending aggregated response for batch %s. Error: %s", hex.EncodeToString(batchIdentifierHash[:]), err)
agg.telemetry.LogTaskError(batchMerkleRoot, err)
return nil, err
}

Expand Down
28 changes: 15 additions & 13 deletions aggregator/pkg/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ type TaskErrorMessage struct {
TaskError string `json:"error"`
}

type TaskGasPriceBumpMessage struct {
MerkleRoot string `json:"merkle_root"`
BumpedGasPrice string `json:"bumped_gas_price"`
type TaskSetGasPriceMessage struct {
MerkleRoot string `json:"merkle_root"`
GasPrice string `json:"gas_price"`
}

type TaskSentToEthereumMessage struct {
MerkleRoot string `json:"merkle_root"`
TxHash string `json:"tx_hash"`
MerkleRoot string `json:"merkle_root"`
TxHash string `json:"tx_hash"`
EffectiveGasPrice string `json:"effective_gas_price"`
}

type Telemetry struct {
Expand Down Expand Up @@ -101,20 +102,21 @@ func (t *Telemetry) LogTaskError(batchMerkleRoot [32]byte, taskError error) {
}
}

func (t *Telemetry) BumpedTaskGasPrice(batchMerkleRoot [32]byte, bumpedGasPrice string) {
body := TaskGasPriceBumpMessage{
MerkleRoot: fmt.Sprintf("0x%s", hex.EncodeToString(batchMerkleRoot[:])),
BumpedGasPrice: bumpedGasPrice,
func (t *Telemetry) TaskSetGasPrice(batchMerkleRoot [32]byte, gasPrice string) {
body := TaskSetGasPriceMessage{
MerkleRoot: fmt.Sprintf("0x%s", hex.EncodeToString(batchMerkleRoot[:])),
GasPrice: gasPrice,
}
if err := t.sendTelemetryMessage("/api/aggregatorTaskGasPriceBump", body); err != nil {
if err := t.sendTelemetryMessage("/api/aggregatorTaskSetGasPrice", body); err != nil {
t.logger.Warn("[Telemetry] Error in LogOperatorResponse", "error", err)
}
}

func (t *Telemetry) TaskSentToEthereum(batchMerkleRoot [32]byte, txHash string) {
func (t *Telemetry) TaskSentToEthereum(batchMerkleRoot [32]byte, txHash string, effectiveGasPrice string) {
body := TaskSentToEthereumMessage{
MerkleRoot: fmt.Sprintf("0x%s", hex.EncodeToString(batchMerkleRoot[:])),
TxHash: txHash,
MerkleRoot: fmt.Sprintf("0x%s", hex.EncodeToString(batchMerkleRoot[:])),
TxHash: txHash,
EffectiveGasPrice: effectiveGasPrice,
}
if err := t.sendTelemetryMessage("/api/aggregatorTaskSent", body); err != nil {
t.logger.Warn("[Telemetry] Error in TaskSentToEthereum", "error", err)
Expand Down
33 changes: 33 additions & 0 deletions alerts/.env.devnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SLACK_WEBHOOK_URL=<YOUR_SLACK_WEBHOOK_URL>
# TELEGRAM_BOT_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN>
# TELEGRAM_CHAT_ID=<YOUR_TELEGRAM_CHAT_ID>

# # Variables for contract_alerts.sh
# RPC_URL=<YOUR_RPC_URL>
# CONTRACT_ADDRESS=<YOUR_CONTRACT_ADDRESS>
# NEW_BATCH_TOPIC=<YOUR_NEW_BATCH_TOPIC>
# VERIFIED_BATCH_TOPIC=<YOUR_VERIFIED_BATCH_TOPIC>
# PAGER_DUTY_KEY=<YOUR_PAGER_DUTY_KEY>
# PAGER_DUTY_EMAIL=<YOUR_PAGER_DUTY_EMAIL>
# PAGER_DUTY_SERVICE_ID=<YOUR_PAGER_DUTY_SERVICE_ID>

# # Variables for process_errors_alerts.sh
# SERVICE=<YOUR_SERVICE>
# EXPRESSION=<GREP_EXPRESSION>

# # Variables for balance_alerts.sh
# RPC_URL=<YOUR_RPC_URL>
# PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
# BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
# WALLET_ADDRESS=<YOUR_WALLET_ADDRESS>

# Variables for sender_with_alert.sh
REPETITIONS=8
SENDER_ADDRESS=0x14dC79964da2C08b23698B3D3cc7Ca32193d9955
BATCHER_URL=ws://localhost:8080
RPC_URL=http://localhost:8545
EXPLORER_URL=http://localhost:3000
NETWORK=devnet
PRIVATE_KEY=0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356
VERIFICATION_WAIT_TIME=30
LOGS_BLOCK_RANGE=100
11 changes: 11 additions & 0 deletions alerts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ RPC_URL=<YOUR_RPC_URL>
PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
WALLET_ADDRESS=<YOUR_WALLET_ADDRESS>

# Variables for sender_with_alert.sh
REPETITIONS=<REPETITIONS>
SENDER_ADDRESS=<YOUR_SENDER_ADDRESS>
BATCHER_URL=<BATCHER_URL>
RPC_URL=<RPC_BASE_URL>
EXPLORER_URL=<EXPLORER_BASE_URL>
NETWORK=<NETWORK>
PRIVATE_KEY=<SENDER_PRIVATE_KEY>
VERIFICATION_WAIT_TIME=<TIME_TO_WAIT_FOR_VERIFICATION>
LOGS_BLOCK_RANGE=<LOGS_BLOCK_RANGE>
186 changes: 186 additions & 0 deletions alerts/sender_with_alert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/bin/bash

# ENV VARIABLES:
# - REPETITIONS
# - EXPLORER_URL
# - SENDER_ADDRESS
# - BATCHER_URL
# - RPC_URL
# - EXPLORER_URL
# - NETWORK
# - PRIVATE_KEY
# - VERIFICATION_WAIT_TIME
# - LOGS_BLOCK_RANGE
# - SLEEP_TIME
# - PAGER_DUTY_KEY
# - PAGER_DUTY_EMAIL
# - PAGER_DUTY_SERVICE_ID
# - SLACK_WEBHOOK_URL

# TODO (Improvement):
# 1. This script waits VERIFICATION_WAIT_TIME seconds before fetching the explorer for the response tx hash.
# We should instead poll in a loop until the batch is marked as verified

# ACKNOWLEDGMENTS
#
# Special thanks to AniV for their contribution on StackExchange regarding AWK formatting with floating point operations:
# https://unix.stackexchange.com/questions/292087/how-do-i-get-bc-to-start-decimal-fractions-with-a-leading-zero


# Load env file from $1 path
source "$1"

# Just for debugging
#set -ex

### FUNCTIONS ###

# Function to get the tx cost from the tx hash
# @param tx_hash
function fetch_tx_cost() {
if [[ -z "$1" ]]; then
echo 0
else
# Get the tx receipt from the blockchain
receipt=$(cast receipt --rpc-url $RPC_URL $1)
# Parse the gas used and gas price
gas_price=$(echo "$receipt" | grep "effectiveGasPrice" | awk '{ print $2 }')
gas_used=$(echo "$receipt" | grep "gasUsed" | awk '{ print $2 }')
# Calculate fee in wei
fee_in_wei=$(($gas_price * $gas_used))

echo $fee_in_wei
fi
}

# Function to send PagerDuty alert
# @param message
function send_pagerduty_alert() {
. alerts/pagerduty.sh "$1"
}

# Function so send Slack message
# @param message
function send_slack_message() {
. alerts/slack.sh "$1"
}

#################
while true
do

## Remove Proof Data
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
rm -rf ./aligned_verification_data/*

mkdir -p ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs

## Generate Proof
nonce=$(aligned get-user-nonce --batcher_url $BATCHER_URL --user_addr $SENDER_ADDRESS 2>&1 | awk '{print $9}')
x=$((nonce + 1)) # So we don't have any issues with nonce = 0
echo "Generating proof $x != 0"
go run ./scripts/test_files/gnark_groth16_bn254_infinite_script/cmd/main.go $x

## Send Proof
echo "Submitting $REPETITIONS proofs $x != 0"
submit=$(aligned submit \
--proving_system Groth16Bn254 \
--repetitions $REPETITIONS \
--proof "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.proof" \
--public_input "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.pub" \
--vk "./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/ineq_${x}_groth16.vk" \
--proof_generator_addr $SENDER_ADDRESS \
--private_key $PRIVATE_KEY \
--rpc_url $RPC_URL \
--batcher_url $BATCHER_URL \
--network $NETWORK \
--max_fee 4000000000000000 \
2>&1)

echo "$submit"

echo "Waiting $VERIFICATION_WAIT_TIME seconds for verification"
sleep $VERIFICATION_WAIT_TIME

# Get all the batches merkle roots
batch_merkle_roots=$(echo "$submit" | grep "Batch merkle root: " | grep -oE "0x[[:alnum:]]{64}" | uniq)

# Fetch the logs of both submission and response
current_block_number=$(cast block-number --rpc-url $RPC_URL)
from_block_number=$(($current_block_number - LOGS_BLOCK_RANGE))
if [ $from_block_number -lt 0 ]; then
from_block_number=0
fi

total_fee_in_wei=0
batch_explorer_urls=()
for batch_merkle_root in $batch_merkle_roots
do
# Construct the batcher explorer url
batch_explorer_url="$EXPLORER_URL/batches/$batch_merkle_root"
batch_explorer_urls+=($batch_explorer_url)

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)
submission_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')

log=$(cast logs --rpc-url $RPC_URL --from-block $from_block_number --to-block latest 'BatchVerified (bytes32 indexed batchMerkleRoot, address senderAddress)' $batch_merkle_root)
response_tx_hash=$(echo "$log" | grep -oE "transactionHash: 0x[[:alnum:]]{64}" | awk '{ print $2 }')

# Calculate fees for transactions
submission_fee_in_wei=$(fetch_tx_cost $submission_tx_hash)
response_fee_in_wei=$(fetch_tx_cost $response_tx_hash)
batch_fee_in_wei=$((submission_fee_in_wei + response_fee_in_wei))

# Accumulate the fee
total_fee_in_wei=$(($total_fee_in_wei + $batch_fee_in_wei))
done

# Calculate the spent amount by converting the fee to ETH
wei_to_eth_division_factor=$((10**18))
spent_amount=$(echo "scale=30; $total_fee_in_wei / (10^18)" | bc -l | awk '{printf "%.15f", $0}')

eth_usd=$(curl -s https://cryptoprices.cc/ETH/)
spent_amount_usd=$(echo "$spent_amount * $eth_usd" | bc | awk '{printf "%.2f", $1}')

slack_messsage=""
verified=1

## Verify Proofs
echo "Verifying $REPETITIONS proofs $x != 0"
for proof in ./aligned_verification_data/*.cbor; do
## Validate Proof on Chain
verification=$(aligned verify-proof-onchain \
--aligned-verification-data $proof \
--rpc_url $RPC_URL \
--network $NETWORK \
2>&1)

## Send Alert if Verification Fails
if echo "$verification" | grep -q not; then
message="Proof verification failed for $proof [ ${batch_explorer_urls[@]} ]"
echo "$message"
send_pagerduty_alert "$message"
verified=0 # Some proofs failed, so we should not send the success message
break
elif echo "$verification" | grep -q verified; then
echo "Proof verification succeeded for $proof"
fi
done

if [ $verified -eq 1 ]; then
slack_message="$REPETITIONS Proofs submitted and verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
else
slack_message="$REPETITIONS Proofs submitted but not verified. Spent amount: $spent_amount ETH ($ $spent_amount_usd) [ ${batch_explorer_urls[@]} ]"
fi

## Send Update to Slack
echo "$slack_message"
send_slack_message "$slack_message"

## Remove Proof Data
rm -rf ./scripts/test_files/gnark_groth16_bn254_infinite_script/infinite_proofs/*
rm -rf ./aligned_verification_data/*

echo "Sleeping $SLEEP_TIME seconds"
sleep $SLEEP_TIME
done
5 changes: 5 additions & 0 deletions alerts/slack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Function to send slack message
# @param message
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$1\"}" \
$SLACK_WEBHOOK_URL
Binary file added audits/aligned_v0.4/least_authority.pdf
Binary file not shown.
Binary file added audits/aligned_v0.4/trail_of_bits.pdf
Binary file not shown.
Binary file added audits/erc20_contracts/creed.pdf
Binary file not shown.
Binary file added audits/erc20_contracts/fuzzing_labs.pdf
Binary file not shown.
Binary file added audits/erc20_contracts/least_authority.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion batcher/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion batcher/aligned/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aligned"
version = "0.12.1"
version = "0.12.2"
edition = "2021"

[dependencies]
Expand Down
Loading

0 comments on commit 9882ace

Please sign in to comment.