use mulitple reference rpc

This commit is contained in:
squidbear
2025-03-23 11:27:14 +01:00
parent 271d9041ce
commit 0e85af9526
4 changed files with 122 additions and 11 deletions

View File

@@ -1,7 +1,23 @@
#!/bin/bash
RPC_URL="$1"
ref="$2"
BASEPATH="$(dirname "$0")"
if [ $# -lt 2 ]; then
echo "Error: At least two parameters are required."
exit 1
fi
RPC_URL=$1
REF=""
for url in "${@:2}"; do
REF+="--url $url "
done
# Optional: You can remove the trailing space if needed
ref=${REF% }
# echo "ref: $ref"
timeout=5 # seconds
@@ -26,7 +42,7 @@ if [ $? -eq 0 ]; then
sleep 3 # to give the reference node more time to import the block if it is very current
http_status_code2=$(curl --ipv4 -m $timeout -s -X POST -w "%{http_code}" -o "$response_file2" -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"$latest_block_number\", false],\"id\":1}" $ref)
http_status_code2=$($BASEPATH/multicurl.sh --ipv4 -m $timeout -s -X POST -w "%{http_code}" -o "$response_file2" -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"$latest_block_number\", false],\"id\":1}" $ref)
curl_code2=$?
@@ -41,7 +57,7 @@ if [ $? -eq 0 ]; then
response_file3=$(mktemp)
status_file3=$(mktemp)
{
curl --ipv4 -m $timeout -s -X POST -w "%{http_code} %{time_total}" -o "$response_file3" -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"latest\", false],\"id\":1}" $ref > "$status_file3"
$BASEPATH/multicurl.sh --ipv4 -m $timeout -s -X POST -w "%{http_code} %{time_total}" -o "$response_file3" -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"latest\", false],\"id\":1}" $ref > "$status_file3"
} &
pid3=$!

69
multicurl.sh Executable file
View File

@@ -0,0 +1,69 @@
#!/bin/bash
# echo "$@"
urls=()
options=()
while [[ $# -gt 0 ]]; do
case "$1" in
--url)
urls+=("$2")
shift 2
;;
*)
options+=("$1")
shift 1
;;
esac
done
if [[ ${#urls[@]} -eq 0 ]]; then
echo "No URLs provided"
exit 1
fi
original_output=""
temp_file=""
for i in "${!options[@]}"; do
if [[ "${options[i]}" == "-o" ]]; then
output_file="${options[i+1]}"
temp_file=$(mktemp)
options[i+1]="$temp_file"
original_output="$output_file"
break
fi
done
output=""
for url in "${urls[@]}"; do
#echo "curl -s ${options[@]} $url"
output=$(eval "curl -s ${options[@]@Q} '$url'")
if [[ $? -eq 0 ]]; then
if cat "$temp_file" | jq -e 'has("error")' > /dev/null 2>&1; then
continue # Try the next URL
fi
if [ -n "$original_output" ]; then
#echo "$(cat $temp_file)"
cat "$temp_file" > "$original_output"
fi
echo "$output"
exit 0
else
continue
fi
done
# Write the final output to the original output file if specified
if [ -n "$original_output" ]; then
cat "$temp_file" > "$original_output"
fi
# Print the output to stdout
echo "$output"
exit 1

View File

@@ -1,35 +1,61 @@
#!/bin/bash
# Check if more than two arguments are provided
if [ $# -gt 2 ]; then
echo "Usage: $0 <chainid> [<index>]"
exit 1
fi
id="$1"
index="${2:-all}"
if [[ $id == 0x* ]]; then
# Convert hex id to decimal if necessary
if [[ "$id" == "0x"* ]]; then
id=$(printf "%d" "$id")
fi
index="${2:-0}"
json_file="/root/rpc/reference-rpc-endpoint.json"
# Check if JSON file exists
if [ ! -f "$json_file" ]; then
echo "JSON file not found: $json_file"
exit 1
fi
# Find the object with matching id
object=$(jq --arg id "$id" '.[] | select(.id == ($id | tonumber))' "$json_file")
# If object not found, exit
if [ -z "$object" ]; then
echo "Chain ID not found: $id"
exit 1
fi
# Extract URLs from the object
urls=$(echo "$object" | jq -r '.urls')
num_urls=$(echo "$urls" | jq -r 'length')
if [ "$index" -ge "$num_urls" ]; then
# If index is set to 'all', return all URLs separated by whitespace
if [ "$index" = "all" ]; then
echo "$urls" | jq -r '.[]' | tr '\n' ' '
exit
fi
# Otherwise, treat index as numeric
# Validate that index is a number
if ! [[ "$index" =~ ^[0-9]+$ ]]; then
echo "Invalid index: $index"
exit 1
fi
url=$(echo "$urls" | jq -r ".[$index]")
# Get the number of URLs
num_urls=$(echo "$urls" | jq -r 'length')
# Check if index is within bounds
if [ "$index" -ge "$num_urls" ]; then
echo "Index out of bounds: $index (max $(($num_urls - 1)))"
exit 1
fi
# Get and print the URL at the specified index
url=$(echo "$urls" | jq -r ".[$index]")
echo "$url"

View File

@@ -54,7 +54,7 @@ for path in $pathlist; do
fi
# Call the health check script with RPC_URL and ref
$BASEPATH/check-health.sh "$RPC_URL" "$ref"
$BASEPATH/check-health.sh "$RPC_URL" $ref
exit $?
fi
done