Skip to content

Commit 02f3f60

Browse files
committed
test: add additional script to deploy services
This script leverages local-network setup by adding some extra overrides to compile from source
1 parent f7ce177 commit 02f3f60

File tree

1 file changed

+317
-0
lines changed

1 file changed

+317
-0
lines changed

run_network.sh

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
#!/bin/bash
2+
3+
# Interruptible timeout function
4+
interruptible_wait() {
5+
local timeout_seconds=$1
6+
local condition_command="$2"
7+
local description="${3:-Waiting for condition}"
8+
9+
echo "$description (timeout: ${timeout_seconds}s, press Ctrl+C to cancel)..."
10+
11+
local elapsed=0
12+
local interval=5
13+
14+
while [ $elapsed -lt $timeout_seconds ]; do
15+
if eval "$condition_command"; then
16+
return 0
17+
fi
18+
19+
# Check for interrupt signal
20+
if ! sleep $interval; then
21+
echo "Interrupted by user"
22+
return 130 # Standard interrupt exit code
23+
fi
24+
25+
elapsed=$((elapsed + interval))
26+
echo "Still waiting... (${elapsed}/${timeout_seconds}s elapsed)"
27+
done
28+
29+
echo "Timeout after ${timeout_seconds}s waiting for: $description"
30+
return 1
31+
}
32+
33+
# ==============================================================================
34+
# SETUP LOCAL GRAPH NETWORK FOR TESTING (HORIZON VERSION)
35+
# ==============================================================================
36+
# This script sets up a local Graph network for testing with horizon upgrade.
37+
#
38+
# NOTES:
39+
# - If you encounter container conflicts, run: docker compose down
40+
# to stop all services before running this script again
41+
#
42+
# - To test changes to your indexer code without restarting everything:
43+
# just reload
44+
#
45+
# - The script checks for existing services and skips those already running
46+
# ==============================================================================
47+
48+
get_docker_sizes() {
49+
local df_output=$(docker system df 2>/dev/null)
50+
51+
# Extract sizes using awk (more reliable)
52+
local images_size=$(echo "$df_output" | awk '/Images/ {print $4}' | head -1)
53+
local containers_size=$(echo "$df_output" | awk '/Containers/ {print $4}' | head -1)
54+
local volumes_size=$(echo "$df_output" | awk '/Local Volumes/ {print $5}' | head -1)
55+
56+
# If awk fails, try alternative method
57+
if [ -z "$images_size" ] || [ -z "$containers_size" ] || [ -z "$volumes_size" ]; then
58+
# Method 2: Use docker system df --format table and parse
59+
images_size=$(docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}" 2>/dev/null | grep "Images" | awk '{print $4}' || echo "N/A")
60+
containers_size=$(docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}" 2>/dev/null | grep "Containers" | awk '{print $4}' || echo "N/A")
61+
volumes_size=$(docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}" 2>/dev/null | grep "Local Volumes" | awk '{print $5}' || echo "N/A")
62+
fi
63+
64+
# Set defaults if still empty
65+
images_size=${images_size:-"N/A"}
66+
containers_size=${containers_size:-"N/A"}
67+
volumes_size=${volumes_size:-"N/A"}
68+
69+
echo "$images_size $containers_size $volumes_size"
70+
}
71+
72+
# Save the root directory path for source mounts
73+
INDEXER_RS_ROOT="$(pwd)"
74+
75+
# Set source roots for development if not already set
76+
# This enables hot-reload development mode for the Rust services
77+
if [[ -z "${INDEXER_SERVICE_SOURCE_ROOT:-}" ]]; then
78+
export INDEXER_SERVICE_SOURCE_ROOT="$INDEXER_RS_ROOT"
79+
echo "🔧 Setting INDEXER_SERVICE_SOURCE_ROOT to: $INDEXER_SERVICE_SOURCE_ROOT"
80+
fi
81+
82+
if [[ -z "${TAP_AGENT_SOURCE_ROOT:-}" ]]; then
83+
export TAP_AGENT_SOURCE_ROOT="$INDEXER_RS_ROOT"
84+
echo "🔧 Setting TAP_AGENT_SOURCE_ROOT to: $TAP_AGENT_SOURCE_ROOT"
85+
fi
86+
87+
# Optionally set INDEXER_AGENT_SOURCE_ROOT if you have the TypeScript indexer checked out
88+
# export INDEXER_AGENT_SOURCE_ROOT="/path/to/graph-protocol/indexer"
89+
echo $"🔧 INDEXER_SERVICE_SOURCE_ROOT is: ${INDEXER_SERVICE_SOURCE_ROOT:-<not set>}"
90+
echo $"🔧 TAP_AGENT_SOURCE_ROOT is: ${TAP_AGENT_SOURCE_ROOT:-<not set>}"
91+
92+
# Track build times
93+
SCRIPT_START_TIME=$(date +%s)
94+
# Save the starting disk usage
95+
START_SPACE=$(df -h --output=used /var/lib/docker | tail -1)
96+
START_SIZES=($(get_docker_sizes))
97+
START_IMAGES_SIZE=${START_SIZES[0]}
98+
START_CONTAINERS_SIZE=${START_SIZES[1]}
99+
START_VOLUMES_SIZE=${START_SIZES[2]}
100+
101+
echo "============ STARTING DISK USAGE ============"
102+
echo "Docker directory usage: $START_SPACE"
103+
echo "Images size: $START_IMAGES_SIZE"
104+
echo "Containers size: $START_CONTAINERS_SIZE"
105+
echo "Volumes size: $START_VOLUMES_SIZE"
106+
echo "=============================================="
107+
108+
container_running() {
109+
docker ps --format '{{.Names}}' | grep -q "^$1$"
110+
return $?
111+
}
112+
113+
# Function to fund the escrow smart contract for horizon
114+
# Uses L2GraphToken and TAPEscrow from the horizon structure
115+
fund_escrow() {
116+
echo "Funding escrow for sender..."
117+
118+
if [ -f "local-network/.env" ]; then
119+
source local-network/.env
120+
else
121+
echo "Error: local-network/.env file not found"
122+
return 1
123+
fi
124+
125+
# Use L2GraphToken from horizon.json for horizon upgrade
126+
GRAPH_TOKEN=$(jq -r '."1337".L2GraphToken.address' local-network/horizon.json)
127+
TAP_ESCROW=$(jq -r '."1337".Escrow' local-network/tap-contracts.json)
128+
129+
# Override with test values taken from test-assets/src/lib.rs
130+
ALLOCATION_ID="0xfa44c72b753a66591f241c7dc04e8178c30e13af" # ALLOCATION_ID_0
131+
132+
if [ -z "$GRAPH_TOKEN" ] || [ -z "$TAP_ESCROW" ] || [ "$GRAPH_TOKEN" == "null" ] || [ "$TAP_ESCROW" == "null" ]; then
133+
echo "Error: Could not read contract addresses from horizon.json or tap-contracts.json"
134+
echo "GRAPH_TOKEN: $GRAPH_TOKEN"
135+
echo "TAP_ESCROW: $TAP_ESCROW"
136+
return 1
137+
fi
138+
139+
# Use constants from .env
140+
SENDER_ADDRESS="$ACCOUNT0_ADDRESS"
141+
SENDER_KEY="$ACCOUNT0_SECRET"
142+
AMOUNT="10000000000000000000"
143+
144+
echo "Using L2GraphToken at: $GRAPH_TOKEN"
145+
echo "Using TapEscrow at: $TAP_ESCROW"
146+
echo "Using sender address: $SENDER_ADDRESS"
147+
148+
# Approve GRT for escrow
149+
echo "Approving GRT..."
150+
docker exec chain cast send \
151+
--rpc-url http://localhost:8545 \
152+
--private-key $SENDER_KEY \
153+
$GRAPH_TOKEN "approve(address,uint256)" $TAP_ESCROW $AMOUNT
154+
155+
# Deposit to escrow
156+
echo "Depositing to escrow..."
157+
docker exec chain cast send \
158+
--rpc-url http://localhost:8545 \
159+
--private-key $SENDER_KEY \
160+
$TAP_ESCROW "deposit(address,uint256)" $SENDER_ADDRESS $AMOUNT
161+
162+
# Verify deposit
163+
echo "Verifying deposit..."
164+
ESCROW_BALANCE=$(docker exec chain cast call \
165+
--rpc-url http://localhost:8545 \
166+
$TAP_ESCROW "getEscrowAmount(address,address)(uint256)" $SENDER_ADDRESS $SENDER_ADDRESS)
167+
echo "Escrow balance: $ESCROW_BALANCE"
168+
if [[ "$ESCROW_BALANCE" == "0" ]]; then
169+
echo "Error: Failed to fund escrow"
170+
return 1
171+
fi
172+
echo "Successfully funded escrow"
173+
return 0
174+
}
175+
176+
if container_running "indexer-service" && container_running "tap-agent" && container_running "gateway" && container_running "indexer-cli"; then
177+
echo "====================================================================================="
178+
echo "All services are already running. To test changes to your indexer code, you can use:"
179+
echo " just reload - To rebuild and restart just indexer-service tap-agent services"
180+
echo ""
181+
echo "If you need to start from scratch, first stop all services with:"
182+
echo " just down"
183+
echo " docker rm -f indexer-service tap-agent gateway indexer-cli"
184+
echo "====================================================================================="
185+
exit 0
186+
fi
187+
188+
cd contrib/
189+
190+
# Clone local-network repo if it doesn't exist
191+
if [ ! -d "local-network" ]; then
192+
git clone https://github.com/semiotic-ai/local-network.git
193+
# git clone https://github.com/edgeandnode/local-network.git
194+
cd local-network
195+
# Checkout to the horizon branch
196+
git checkout semiotic/horizon
197+
cd ..
198+
fi
199+
200+
# Start the required services from local-network
201+
cd local-network
202+
203+
# Build the list of compose files to use
204+
COMPOSE_BASE="-f docker-compose.yaml"
205+
206+
# Check for dev overrides
207+
COMPOSE_DEV_FILES=""
208+
if [[ -n "${INDEXER_SERVICE_SOURCE_ROOT:-}" ]]; then
209+
echo "📦 INDEXER_SERVICE_SOURCE_ROOT detected - will use dev override for indexer-service"
210+
COMPOSE_DEV_FILES="$COMPOSE_DEV_FILES -f overrides/indexer-service-dev/indexer-service-dev.yaml"
211+
fi
212+
if [[ -n "${TAP_AGENT_SOURCE_ROOT:-}" ]]; then
213+
echo "📦 TAP_AGENT_SOURCE_ROOT detected - will use dev override for tap-agent"
214+
COMPOSE_DEV_FILES="$COMPOSE_DEV_FILES -f overrides/tap-agent-dev/tap-agent-dev.yaml"
215+
fi
216+
217+
echo "Starting services with overrides..."
218+
# Show all compose files being used
219+
echo "Using compose files:"
220+
echo " - Base: $COMPOSE_BASE"
221+
[ -n "$COMPOSE_DEV_FILES" ] && echo " - Dev: $COMPOSE_DEV_FILES"
222+
223+
# Build strategy (defaults favor clean rebuilds to avoid stale images)
224+
FORCE_REBUILD=${FORCE_REBUILD:-true}
225+
PULL_BASE=${PULL_BASE:-false}
226+
REMOVE_ORPHANS=${REMOVE_ORPHANS:-true}
227+
228+
echo "Build options -> FORCE_REBUILD=${FORCE_REBUILD} PULL_BASE=${PULL_BASE} REMOVE_ORPHANS=${REMOVE_ORPHANS}"
229+
230+
# Optionally force a clean image rebuild before starting containers
231+
if [[ "${FORCE_REBUILD}" == "true" ]]; then
232+
echo "🛠 Running docker compose build with --no-cache${PULL_BASE:+ and --pull}..."
233+
if [[ "${PULL_BASE}" == "true" ]]; then
234+
docker compose $COMPOSE_BASE $COMPOSE_DEV_FILES build --no-cache --pull
235+
else
236+
docker compose $COMPOSE_BASE $COMPOSE_DEV_FILES build --no-cache
237+
fi
238+
fi
239+
240+
# Start all services (optionally remove any orphaned containers)
241+
if [[ "${REMOVE_ORPHANS}" == "true" ]]; then
242+
docker compose $COMPOSE_BASE $COMPOSE_DEV_FILES up -d --remove-orphans
243+
else
244+
docker compose $COMPOSE_BASE $COMPOSE_DEV_FILES up -d
245+
fi
246+
247+
echo "=== DEV MODE SETUP COMPLETE ==="
248+
echo "Services starting with your dev binaries mounted."
249+
echo "To rebuild and restart: cargo build --release && docker restart indexer-service tap-agent"
250+
251+
# Ensure gateway is ready before testing
252+
interruptible_wait 100 'curl -f http://localhost:7700/ > /dev/null 2>&1' "Waiting for gateway service"
253+
254+
cd ..
255+
256+
# Build and start indexer-cli for integration testing (last container)
257+
echo "Building and starting indexer-cli container for integration testing..."
258+
docker compose -f docker-compose.yml -f docker-compose.override.yml up --build -d indexer-cli
259+
rm -f docker-compose.override.yml
260+
261+
# Wait for indexer-cli to be ready
262+
echo "Waiting for indexer-cli to be ready..."
263+
sleep 10 # Give time for the CLI to initialize
264+
265+
# Connect the CLI to the indexer-agent
266+
echo "Connecting indexer-cli to indexer-agent..."
267+
docker exec indexer-cli graph indexer connect http://indexer-agent:7600 || true
268+
269+
echo "============================================"
270+
echo "Indexer CLI is ready for integration testing!"
271+
echo "Example commands:"
272+
echo " List allocations: docker exec indexer-cli graph indexer allocations get --network hardhat"
273+
# FIXME: Provided by edge&node team, this does not work tho
274+
echo " Close allocation: docker exec indexer-cli graph indexer allocations close 0x0a067bd57ad79716c2133ae414b8f6bb47aaa22d 0x0000000000000000000000000000000000000000000000000000000000000000 100 0x0000000000000000000000000000000000000000000000000000000000000000 --network hardhat --force"
275+
echo "============================================"
276+
277+
# Calculate timing and final reports
278+
SCRIPT_END_TIME=$(date +%s)
279+
TOTAL_DURATION=$((SCRIPT_END_TIME - SCRIPT_START_TIME))
280+
MINUTES=$((TOTAL_DURATION / 60))
281+
SECONDS=$((TOTAL_DURATION % 60))
282+
283+
END_SPACE=$(df -h --output=used /var/lib/docker | tail -1)
284+
END_SIZES=($(get_docker_sizes))
285+
END_IMAGES_SIZE=${END_SIZES[0]}
286+
END_CONTAINERS_SIZE=${END_SIZES[1]}
287+
END_VOLUMES_SIZE=${END_SIZES[2]}
288+
289+
echo "============ SETUP COMPLETED ============"
290+
echo "Total setup time: ${MINUTES}m ${SECONDS}s"
291+
echo ""
292+
echo "============ FINAL DISK USAGE ============"
293+
echo "Docker directory usage: $END_SPACE"
294+
echo "Images size: $END_IMAGES_SIZE"
295+
echo "Containers size: $END_CONTAINERS_SIZE"
296+
echo "Volumes size: $END_VOLUMES_SIZE"
297+
echo "==========================================="
298+
echo ""
299+
echo "============ SERVICES RUNNING ============"
300+
echo "✓ Indexer Service: http://localhost:7601"
301+
echo "✓ TAP Agent: http://localhost:7300/metrics"
302+
echo "✓ Gateway: http://localhost:7700"
303+
echo "✓ Indexer CLI: Ready (container: indexer-cli)"
304+
echo " Use: docker exec indexer-cli graph-indexer indexer --help"
305+
echo "=========================================="
306+
307+
# go back to root dir indexer-rs/
308+
# and execute pg_admin.sh if requested
309+
# this scripts deploys a docker container with pgAdmin which can be used to inspect/modify
310+
# graphtally database tables like tap_horizon_ravs/tap_horizon_receipts and so on
311+
cd ..
312+
313+
# Optional: Start pgAdmin for database inspection
314+
if [ "$START_PGADMIN" = "true" ]; then
315+
echo "Starting pgAdmin for database inspection..."
316+
./pg_admin.sh
317+
fi

0 commit comments

Comments
 (0)