|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +BOOTNODE_URL="${BOOTNODE_URL:-https://chains.base.org}" |
| 5 | +NETWORK="" |
| 6 | +EXECUTION_RPC="${EXECUTION_RPC:-http://localhost:8545}" |
| 7 | +CONSENSUS_RPC="${CONSENSUS_RPC:-http://localhost:7545}" |
| 8 | +EXECUTION_ONLY=false |
| 9 | +CONSENSUS_ONLY=false |
| 10 | +LOOP_INTERVAL=0 |
| 11 | + |
| 12 | +show_usage() { |
| 13 | + cat <<EOF |
| 14 | +Usage: $(basename "$0") --network <network> [options] |
| 15 | +
|
| 16 | +Fetches public node records from chains.base.org and adds them as peers. |
| 17 | +
|
| 18 | +Required: |
| 19 | + --network <name> Network name (base-mainnet, base-sepolia, base-zeronet) |
| 20 | +
|
| 21 | +Options: |
| 22 | + --execution-rpc <url> Execution layer RPC endpoint (default: http://localhost:8545) |
| 23 | + --consensus-rpc <url> Consensus layer RPC endpoint (default: http://localhost:7545) |
| 24 | + --bootnode-url <url> Bootnode server URL (default: https://chains.base.org) |
| 25 | + --execution-only Only add execution layer peers |
| 26 | + --consensus-only Only add consensus layer peers |
| 27 | + --loop <seconds> Poll interval in seconds (0 = run once, default: 0) |
| 28 | + --help Show this help message |
| 29 | +
|
| 30 | +Prerequisites: |
| 31 | + - curl and jq must be installed |
| 32 | + - Execution client must have admin namespace enabled (--http.api includes admin) |
| 33 | + - Consensus client must have admin RPC enabled (--rpc.enable-admin or BASE_NODE_RPC_ENABLE_ADMIN=true) |
| 34 | +
|
| 35 | +Examples: |
| 36 | + $(basename "$0") --network base-sepolia |
| 37 | + $(basename "$0") --network base-sepolia --execution-rpc http://localhost:8545 --loop 300 |
| 38 | + $(basename "$0") --network base-mainnet --consensus-only |
| 39 | +EOF |
| 40 | + exit 0 |
| 41 | +} |
| 42 | + |
| 43 | +while [[ $# -gt 0 ]]; do |
| 44 | + case "$1" in |
| 45 | + --network) NETWORK="$2"; shift 2 ;; |
| 46 | + --execution-rpc) EXECUTION_RPC="$2"; shift 2 ;; |
| 47 | + --consensus-rpc) CONSENSUS_RPC="$2"; shift 2 ;; |
| 48 | + --bootnode-url) BOOTNODE_URL="$2"; shift 2 ;; |
| 49 | + --execution-only) EXECUTION_ONLY=true; shift ;; |
| 50 | + --consensus-only) CONSENSUS_ONLY=true; shift ;; |
| 51 | + --loop) LOOP_INTERVAL="$2"; shift 2 ;; |
| 52 | + --help) show_usage ;; |
| 53 | + *) echo "Unknown option: $1"; show_usage ;; |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +if [[ -z "$NETWORK" ]]; then |
| 58 | + echo "Error: --network is required" |
| 59 | + show_usage |
| 60 | +fi |
| 61 | + |
| 62 | +for cmd in curl jq; do |
| 63 | + if ! command -v "$cmd" &>/dev/null; then |
| 64 | + echo "Error: $cmd is required but not installed" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +done |
| 68 | + |
| 69 | +add_execution_peers() { |
| 70 | + local nodes |
| 71 | + nodes=$(echo "$1" | jq -r '.execution[]? // empty' 2>/dev/null) |
| 72 | + if [[ -z "$nodes" ]]; then |
| 73 | + echo "[$(date -Iseconds)] No execution peers found for $NETWORK" |
| 74 | + return |
| 75 | + fi |
| 76 | + |
| 77 | + local count=0 |
| 78 | + while IFS= read -r enode; do |
| 79 | + result=$(curl -s -X POST "$EXECUTION_RPC" \ |
| 80 | + -H "Content-Type: application/json" \ |
| 81 | + -d "{\"jsonrpc\":\"2.0\",\"method\":\"admin_addPeer\",\"params\":[\"$enode\"],\"id\":1}" 2>/dev/null) |
| 82 | + success=$(echo "$result" | jq -r '.result // false' 2>/dev/null) |
| 83 | + if [[ "$success" == "true" ]]; then |
| 84 | + count=$((count + 1)) |
| 85 | + else |
| 86 | + echo "[$(date -Iseconds)] Failed to add execution peer: $enode" |
| 87 | + fi |
| 88 | + done <<< "$nodes" |
| 89 | + |
| 90 | + echo "[$(date -Iseconds)] Added $count execution peers for $NETWORK" |
| 91 | +} |
| 92 | + |
| 93 | +add_consensus_peers() { |
| 94 | + local nodes |
| 95 | + nodes=$(echo "$1" | jq -r '.consensus[]? // empty' 2>/dev/null) |
| 96 | + if [[ -z "$nodes" ]]; then |
| 97 | + echo "[$(date -Iseconds)] No consensus peers found for $NETWORK" |
| 98 | + return |
| 99 | + fi |
| 100 | + |
| 101 | + local count=0 |
| 102 | + while IFS= read -r addr; do |
| 103 | + result=$(curl -s -X POST "$CONSENSUS_RPC" \ |
| 104 | + -H "Content-Type: application/json" \ |
| 105 | + -d "{\"jsonrpc\":\"2.0\",\"method\":\"opp2p_connectPeer\",\"params\":[\"$addr\"],\"id\":1}" 2>/dev/null) |
| 106 | + error=$(echo "$result" | jq -r '.error // empty' 2>/dev/null) |
| 107 | + if [[ -z "$error" ]]; then |
| 108 | + count=$((count + 1)) |
| 109 | + else |
| 110 | + echo "[$(date -Iseconds)] Failed to add consensus peer: $addr ($error)" |
| 111 | + fi |
| 112 | + done <<< "$nodes" |
| 113 | + |
| 114 | + echo "[$(date -Iseconds)] Added $count consensus peers for $NETWORK" |
| 115 | +} |
| 116 | + |
| 117 | +run_once() { |
| 118 | + echo "[$(date -Iseconds)] Fetching peers from $BOOTNODE_URL for $NETWORK..." |
| 119 | + |
| 120 | + response=$(curl -sf "$BOOTNODE_URL/$NETWORK/peers" 2>/dev/null) |
| 121 | + if [[ -z "$response" ]]; then |
| 122 | + echo "[$(date -Iseconds)] Error: failed to fetch from $BOOTNODE_URL/$NETWORK/peers" |
| 123 | + return 1 |
| 124 | + fi |
| 125 | + |
| 126 | + if ! echo "$response" | jq -e '.execution' &>/dev/null; then |
| 127 | + echo "[$(date -Iseconds)] Error: invalid response for network $NETWORK" |
| 128 | + return 1 |
| 129 | + fi |
| 130 | + |
| 131 | + if [[ "$CONSENSUS_ONLY" != "true" ]]; then |
| 132 | + add_execution_peers "$response" |
| 133 | + fi |
| 134 | + |
| 135 | + if [[ "$EXECUTION_ONLY" != "true" ]]; then |
| 136 | + add_consensus_peers "$response" |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +if [[ "$LOOP_INTERVAL" -gt 0 ]]; then |
| 141 | + echo "[$(date -Iseconds)] Running in loop mode (interval: ${LOOP_INTERVAL}s)" |
| 142 | + while true; do |
| 143 | + run_once || true |
| 144 | + sleep "$LOOP_INTERVAL" |
| 145 | + done |
| 146 | +else |
| 147 | + run_once |
| 148 | +fi |
0 commit comments