Skip to content

Commit 303ef36

Browse files
committed
feat(node): add peer fetching to all entrypoints
1 parent 3bac950 commit 303ef36

9 files changed

Lines changed: 165 additions & 0 deletions

File tree

add-peers.sh

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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

base-consensus-entrypoint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export BASE_NODE_P2P_ADVERTISE_IP=$PUBLIC_IP
4444

4545
echo "$BASE_NODE_L2_ENGINE_AUTH_RAW" > "$BASE_NODE_L2_ENGINE_AUTH"
4646

47+
./add-peers.sh --consensus-only --network "$BASE_NODE_NETWORK" --loop 300 &
48+
4749
if [[ -n "${BASE_NODE_SOURCE_L2_RPC:-}" ]]; then
4850
echo "Running base-consensus in follow mode because BASE_NODE_SOURCE_L2_RPC is set"
4951
exec ./base-consensus follow

geth/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
4040
COPY geth/geth-entrypoint ./execution-entrypoint
4141
COPY op-node-entrypoint .
4242
COPY consensus-entrypoint .
43+
COPY add-peers.sh .
4344

4445
CMD ["/usr/bin/supervisord"]

geth/geth-entrypoint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ if [ "${HOST_IP:+x}" = x ]; then
5050
ADDITIONAL_ARGS="$ADDITIONAL_ARGS --nat=extip:$HOST_IP"
5151
fi
5252

53+
./add-peers.sh --execution-only --network "$OP_NODE_NETWORK" --execution-rpc "http://localhost:${RPC_PORT}" --loop 300 &
54+
5355
exec ./geth \
5456
--datadir="$GETH_DATA_DIR" \
5557
--verbosity="$VERBOSITY" \

nethermind/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
4747
COPY nethermind/nethermind-entrypoint ./execution-entrypoint
4848
COPY op-node-entrypoint .
4949
COPY consensus-entrypoint .
50+
COPY add-peers.sh .
5051

5152
CMD ["/usr/bin/supervisord"]

nethermind/nethermind-entrypoint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ if [[ -n "${OP_NETHERMIND_ETHSTATS_ENDPOINT:-}" ]]; then
4343
fi
4444

4545
# Execute Nethermind
46+
./add-peers.sh --execution-only --network "$OP_NODE_NETWORK" --execution-rpc "http://localhost:${RPC_PORT}" --loop 300 &
47+
4648
exec ./nethermind \
4749
--config="$OP_NODE_NETWORK" \
4850
--datadir="$NETHERMIND_DATA_DIR" \

op-node-entrypoint

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ export OP_NODE_P2P_ADVERTISE_IP=$PUBLIC_IP
4646
echo "$BASE_NODE_L2_ENGINE_AUTH_RAW" > "$BASE_NODE_L2_ENGINE_AUTH"
4747
export OP_NODE_L2_ENGINE_AUTH=$BASE_NODE_L2_ENGINE_AUTH
4848

49+
if [[ -n "${OP_NODE_NETWORK:-}" ]]; then
50+
./add-peers.sh --consensus-only --network "$OP_NODE_NETWORK" --loop 300 &
51+
fi
52+
4953
exec ./op-node

reth/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ COPY ./reth/reth-entrypoint ./execution-entrypoint
7171
COPY op-node-entrypoint .
7272
COPY base-consensus-entrypoint .
7373
COPY consensus-entrypoint .
74+
COPY add-peers.sh .
7475

7576
CMD ["/usr/bin/supervisord"]

reth/reth-entrypoint

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ mkdir -p "$RETH_DATA_DIR"
130130
echo "Starting reth with additional args: $ADDITIONAL_ARGS"
131131
echo "$BASE_NODE_L2_ENGINE_AUTH_RAW" > "$BASE_NODE_L2_ENGINE_AUTH"
132132

133+
if [[ -n "${RETH_NETWORK:-}" ]]; then
134+
./add-peers.sh --execution-only --network "$RETH_NETWORK" --execution-rpc "http://localhost:${RPC_PORT}" --loop 300 &
135+
fi
136+
133137
exec "$BINARY" node \
134138
-$LOG_LEVEL \
135139
--datadir="$RETH_DATA_DIR" \

0 commit comments

Comments
 (0)