|
| 1 | +#!/bin/bash |
| 2 | +set -eu |
| 3 | + |
| 4 | +# Source environment variables from .env file |
| 5 | +if [ -f /opt/.env ]; then |
| 6 | + stdbuf -oL echo "Sourcing environment variables from .env file" |
| 7 | + . /opt/.env |
| 8 | +fi |
| 9 | + |
| 10 | +# Extract TAPVerifier address from contracts.json |
| 11 | +VERIFIER_ADDRESS=$(jq -r '."1337".TAPVerifier.address' /opt/contracts.json) |
| 12 | +ALLOCATION_ID="0xfa44c72b753a66591f241c7dc04e8178c30e13af" # ALLOCATION_ID_0 |
| 13 | + |
| 14 | +# Wait for postgres to be ready |
| 15 | +until pg_isready -h postgres -U postgres -d indexer_components_1; do |
| 16 | + stdbuf -oL echo "Waiting for postgres..." |
| 17 | + sleep 2 |
| 18 | +done |
| 19 | + |
| 20 | +stdbuf -oL echo "Checking if required services are available..." |
| 21 | +for service in postgres graph-node tap-aggregator; do |
| 22 | + if getent hosts $service >/dev/null 2>&1; then |
| 23 | + IP=$(getent hosts $service | awk '{ print $1 }') |
| 24 | + stdbuf -oL echo "✅ $service resolves to $IP" |
| 25 | + else |
| 26 | + stdbuf -oL echo "❌ Cannot resolve $service hostname" |
| 27 | + fi |
| 28 | +done |
| 29 | + |
| 30 | +# Get network subgraph deployment ID with retries |
| 31 | +stdbuf -oL echo "Getting network subgraph deployment ID..." |
| 32 | +MAX_ATTEMPTS=30 |
| 33 | +ATTEMPT=0 |
| 34 | +NETWORK_DEPLOYMENT="" |
| 35 | + |
| 36 | +while [ -z "$NETWORK_DEPLOYMENT" ] || [ "$NETWORK_DEPLOYMENT" = "null" ] && [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do |
| 37 | + NETWORK_DEPLOYMENT=$(curl -s "http://graph-node:8000/subgraphs/name/graph-network" \ |
| 38 | + -H 'content-type: application/json' \ |
| 39 | + -d '{"query": "{ _meta { deployment } }"}' | jq -r '.data._meta.deployment' 2>/dev/null) |
| 40 | + |
| 41 | + if [ -z "$NETWORK_DEPLOYMENT" ] || [ "$NETWORK_DEPLOYMENT" = "null" ]; then |
| 42 | + ATTEMPT=$((ATTEMPT + 1)) |
| 43 | + echo "Waiting for network subgraph to be deployed... Attempt $ATTEMPT/$MAX_ATTEMPTS" |
| 44 | + sleep 5 |
| 45 | + fi |
| 46 | +done |
| 47 | + |
| 48 | +if [ -z "$NETWORK_DEPLOYMENT" ] || [ "$NETWORK_DEPLOYMENT" = "null" ]; then |
| 49 | + echo "ERROR: Failed to get network subgraph deployment ID after $MAX_ATTEMPTS attempts" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +stdbuf -oL echo "Network subgraph deployment ID: $NETWORK_DEPLOYMENT" |
| 54 | + |
| 55 | +# Get escrow subgraph deployment ID with retries |
| 56 | +stdbuf -oL echo "Getting escrow subgraph deployment ID..." |
| 57 | +MAX_ATTEMPTS=30 |
| 58 | +ATTEMPT=0 |
| 59 | +ESCROW_DEPLOYMENT="" |
| 60 | + |
| 61 | +while [ -z "$ESCROW_DEPLOYMENT" ] || [ "$ESCROW_DEPLOYMENT" = "null" ] && [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do |
| 62 | + ESCROW_DEPLOYMENT=$(curl -s "http://graph-node:8000/subgraphs/name/semiotic/tap" \ |
| 63 | + -H 'content-type: application/json' \ |
| 64 | + -d '{"query": "{ _meta { deployment } }"}' | jq -r '.data._meta.deployment' 2>/dev/null) |
| 65 | + |
| 66 | + if [ -z "$ESCROW_DEPLOYMENT" ] || [ "$ESCROW_DEPLOYMENT" = "null" ]; then |
| 67 | + ATTEMPT=$((ATTEMPT + 1)) |
| 68 | + echo "Waiting for escrow subgraph to be deployed... Attempt $ATTEMPT/$MAX_ATTEMPTS" |
| 69 | + sleep 5 |
| 70 | + fi |
| 71 | +done |
| 72 | + |
| 73 | +if [ -z "$ESCROW_DEPLOYMENT" ] || [ "$ESCROW_DEPLOYMENT" = "null" ]; then |
| 74 | + stdbuf -oL echo "ERROR: Failed to get escrow subgraph deployment ID after $MAX_ATTEMPTS attempts" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +stdbuf -oL echo "Escrow subgraph deployment ID: $ESCROW_DEPLOYMENT" |
| 79 | + |
| 80 | +# Copy the config template |
| 81 | +cp /opt/config/config.toml /opt/config.toml |
| 82 | + |
| 83 | +# Replace the placeholders with actual values |
| 84 | +sed -i "s/NETWORK_DEPLOYMENT_PLACEHOLDER/$NETWORK_DEPLOYMENT/g" /opt/config.toml |
| 85 | +sed -i "s/ESCROW_DEPLOYMENT_PLACEHOLDER/$ESCROW_DEPLOYMENT/g" /opt/config.toml |
| 86 | +sed -i "s/VERIFIER_ADDRESS_PLACEHOLDER/$VERIFIER_ADDRESS/g" /opt/config.toml |
| 87 | +sed -i "s/INDEXER_ADDRESS_PLACEHOLDER/$RECEIVER_ADDRESS/g" /opt/config.toml |
| 88 | +sed -i "s/INDEXER_MNEMONIC_PLACEHOLDER/$INDEXER_MNEMONIC/g" /opt/config.toml |
| 89 | +sed -i "s/ACCOUNT0_ADDRESS_PLACEHOLDER/$ACCOUNT0_ADDRESS/g" /opt/config.toml |
| 90 | +sed -i "s/TAP_AGGREGATOR_PORT_PLACEHOLDER/$TAP_AGGREGATOR/g" /opt/config.toml |
| 91 | +sed -i "s/POSTGRES_PORT_PLACEHOLDER/$POSTGRES/g" /opt/config.toml |
| 92 | +sed -i "s/GRAPH_NODE_GRAPHQL_PORT_PLACEHOLDER/$GRAPH_NODE_GRAPHQL/g" /opt/config.toml |
| 93 | +sed -i "s/GRAPH_NODE_STATUS_PORT_PLACEHOLDER/$GRAPH_NODE_STATUS/g" /opt/config.toml |
| 94 | +sed -i "s/INDEXER_SERVICE_PORT_PLACEHOLDER/$INDEXER_SERVICE/g" /opt/config.toml |
| 95 | + |
| 96 | +stdbuf -oL echo "Starting tap-agent with config:" |
| 97 | +cat /opt/config.toml |
| 98 | + |
| 99 | +# Set profiling tool based on environment variable |
| 100 | +# Default is no profiling |
| 101 | +PROFILER="${PROFILER:-none}" |
| 102 | +stdbuf -oL echo "🔍 DEBUG: Profiling with: $PROFILER" |
| 103 | + |
| 104 | +# Run agent with enhanced logging |
| 105 | +stdbuf -oL echo "Starting tap-agent..." |
| 106 | +export RUST_BACKTRACE=full |
| 107 | +export RUST_LOG=debug |
| 108 | + |
| 109 | +# Create output directory if it doesn't exist |
| 110 | +mkdir -p /opt/profiling/tap-agent |
| 111 | +chmod 777 /opt/profiling |
| 112 | +chmod 777 /opt/profiling/tap-agent |
| 113 | + |
| 114 | +case "$PROFILER" in |
| 115 | +flamegraph) |
| 116 | + stdbuf -oL echo "🔥 Starting with profiler..." |
| 117 | + |
| 118 | + # Start the service in the background with output redirection |
| 119 | + stdbuf -oL echo "🚀 Starting service..." |
| 120 | + exec /usr/local/bin/indexer-tap-agent --config /opt/config.toml |
| 121 | + ;; |
| 122 | +strace) |
| 123 | + stdbuf -oL echo "🔍 Starting with strace..." |
| 124 | + # -f: follow child processes |
| 125 | + # -tt: print timestamps with microsecond precision |
| 126 | + # -T: show time spent in each syscall |
| 127 | + # -e trace=all: trace all system calls |
| 128 | + # -s 256: show up to 256 characters per string |
| 129 | + # -o: output file |
| 130 | + exec strace -f -tt -T -e trace=all -s 256 -o /opt/profiling/tap-agent/strace.log /usr/local/bin/indexer-tap-agent --config /opt/config.toml |
| 131 | + ;; |
| 132 | +valgrind) |
| 133 | + stdbuf -oL echo "🔍 Starting with Valgrind profiling..." |
| 134 | + |
| 135 | + # Start with Massif memory profiler |
| 136 | + stdbuf -oL echo "🔄 Starting Valgrind Massif memory profiling..." |
| 137 | + exec valgrind --tool=massif \ |
| 138 | + --massif-out-file=/opt/profiling/tap-agent/massif.out \ |
| 139 | + --time-unit=B \ |
| 140 | + --detailed-freq=10 \ |
| 141 | + --max-snapshots=100 \ |
| 142 | + --threshold=0.5 \ |
| 143 | + /usr/local/bin/indexer-tap-agent --config /opt/config.toml |
| 144 | + ;; |
| 145 | +# Use sudo callgrind_annotate indexer-service.callgrind.out |
| 146 | +# for humand friendly report of callgrind output |
| 147 | +# Ideally you should set: |
| 148 | +# [profile.release.package."*"] |
| 149 | +# debug = true |
| 150 | +# force-frame-pointers = true |
| 151 | +# in the Cargo.toml |
| 152 | +callgrind) |
| 153 | + stdbuf -oL echo "🔍 Starting with Callgrind CPU profiling..." |
| 154 | + exec valgrind --tool=callgrind \ |
| 155 | + --callgrind-out-file=/opt/profiling/tap-agent/callgrind.out \ |
| 156 | + --cache-sim=yes \ |
| 157 | + --branch-sim=yes \ |
| 158 | + /usr/local/bin/indexer-tap-agent --config /opt/config.toml |
| 159 | + ;; |
| 160 | +none) |
| 161 | + stdbuf -oL echo "🔍 Starting without profiling..." |
| 162 | + exec /usr/local/bin/indexer-tap-agent --config /opt/config.toml |
| 163 | + ;; |
| 164 | +esac |
0 commit comments