Skip to content

Commit

Permalink
feat: enhance logs and reorganize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Mar 2, 2025
1 parent 637001e commit 584a44a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
94 changes: 49 additions & 45 deletions test/bats/helpers/lxly-bridge.bash
Original file line number Diff line number Diff line change
Expand Up @@ -89,48 +89,6 @@ function bridge_asset() {
fi
}

function get_bridge() {
local network_id="$1"
local expected_tx_hash="$2"
local max_attempts="$3"
local poll_frequency="$4"

local attempt=0

while true; do
((attempt++))
log "Attempt $attempt: fetching bridges from the RPC..."

# Fetch bridges from the RPC
bridges_result=$(cast rpc --rpc-url "$aggkit_node_url" "bridge_getBridges" "$network_id")

log "------ bridges_result ------"
log "$bridges_result"
log "------ bridges_result ------"

# Extract the elements of the 'bridges' array one by one
for row in $(echo "$bridges_result" | jq -c '.bridges[]'); do
# Parse out the tx_hash from each element
tx_hash=$(echo "$row" | jq -r '.tx_hash')

if [[ "$tx_hash" == "$expected_tx_hash" ]]; then
log "Found expected bridge with tx hash: $tx_hash"
echo "$row"
return 0
fi
done

# Fail test if max attempts are reached
if [[ "$attempt" -ge "$max_attempts" ]]; then
echo "Error: Reached max attempts ($max_attempts) without finding expected bridge with tx hash." >&2
return 1
fi

# Sleep before the next attempt
sleep "$poll_frequency"
done
}

# This function is used to claim a bridge using concrete tx hash
# params:
# - timeout - timeout in seconds
Expand Down Expand Up @@ -366,6 +324,48 @@ function wait_for_expected_token() {
done
}

function get_bridge() {
local network_id="$1"
local expected_tx_hash="$2"
local max_attempts="$3"
local poll_frequency="$4"

local attempt=0

log "🔍 Searching for bridge with tx_hash: "$expected_tx_hash" (bridge indexer RPC: "$aggkit_node_url")..."

while true; do
((attempt++))
log "🔍 Attempt $attempt"

# Fetch bridges from the RPC
bridges_result=$(cast rpc --rpc-url "$aggkit_node_url" "bridge_getBridges" "$network_id")

# Extract the elements of the 'bridges' array one by one
for row in $(echo "$bridges_result" | jq -c '.bridges[]'); do
# Parse out the tx_hash from each element
tx_hash=$(echo "$row" | jq -r '.tx_hash')

if [[ "$tx_hash" == "$expected_tx_hash" ]]; then
log "🎉 Found expected bridge with tx hash: $tx_hash"
echo "$row"
return 0
fi
done

# Fail test if max attempts are reached
if [[ "$attempt" -ge "$max_attempts" ]]; then
log "🔍 Bridges result:"
log "$bridges_result"
echo "❌ Error: Reached max attempts ($max_attempts) without finding expected bridge with tx hash." >&2
return 1
fi

# Sleep before the next attempt
sleep "$poll_frequency"
done
}

function get_claim() {
local network_id="$1"
local expected_global_index="$2"
Expand All @@ -379,8 +379,6 @@ function get_claim() {
((attempt++))
log "🔍 Attempt $attempt"
claims_result=$(cast rpc --rpc-url "$aggkit_node_url" "bridge_getClaims" "$network_id")
log "------ claims_result ------"
log "$claims_result"

for row in $(echo "$claims_result" | jq -c '.claims[]'); do
global_index=$(jq -r '.global_index' <<<"$row")
Expand All @@ -403,8 +401,11 @@ function get_claim() {
for field in "${required_fields[@]}"; do
value=$(jq -r --arg fld "$field" '.[$fld]' <<<"$row")
if [ "$value" = "null" ] || [ -z "$value" ]; then
log "🔍 Claims result:"
log "$claims_result"

echo "❌ Error: Assertion failed missing or null '$field' in the claim object." >&2
exit 1
return 1
fi
done

Expand All @@ -415,6 +416,9 @@ function get_claim() {

# Fail test if max attempts are reached
if [[ "$attempt" -ge "$max_attempts" ]]; then
log "🔍 Claims result:"
log "$claims_result"

echo "❌ Error: Reached max attempts ($max_attempts) without finding expected claim with global index ($expected_global_index)." >&2
return 1
fi
Expand Down
13 changes: 5 additions & 8 deletions test/bats/pp/bridge-e2e.bats
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,15 @@ setup() {
assert_success
local bridge_tx_hash=$output

echo "------- bridge_getBridges API testcase"
echo "=== Retrieve bridge from bridge_getBridges API" >&3
run get_bridge "$l1_rpc_network_id" "$bridge_tx_hash" 10 3
assert_success
local bridge
bridge="$output"
local origin_network
origin_network="$(echo "$bridge" | jq -r '.origin_network')"
local destination_network
destination_network="$(echo "$bridge" | jq -r '.destination_network')"

local bridge="$output"
local origin_network="$(echo "$bridge" | jq -r '.origin_network')"
local destination_network="$(echo "$bridge" | jq -r '.destination_network')"
assert_equal "$l1_rpc_network_id" "$origin_network"
assert_equal "$l2_rpc_network_id" "$destination_network"
echo "------- bridge_getBridges API testcase passed"

echo "=== Running LxLy claim on L2" >&3
timeout="180"
Expand Down

0 comments on commit 584a44a

Please sign in to comment.