Skip to content

Commit

Permalink
feat: expose port to local host and improve debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Estrada committed Oct 29, 2024
1 parent fe5d0bd commit ff1781d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
72 changes: 42 additions & 30 deletions scripts/devnet/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@ mkdir -p "$GENTX_DIR"
NODE_COUNT="${NODE_COUNT:-3}"
NODE_NAMES=($(for i in $(seq 1 "$NODE_COUNT"); do echo "regen-node$i"; done))

# Fetch ports from environment variables
P2P_PORT=${P2P_PORT:-26656}
RPC_PORT=${RPC_PORT:-26657}

configure_rpc_and_p2p() {
CONFIG_FILE="$HOME_DIR/config/config.toml"
APP_FILE="$HOME_DIR/config/app.toml"

log "$INFO" "🔍 Verifying configuration of RPC, P2P, and gRPC for ${NODE_NAME}..."

# Configure RPC in the [rpc] section of config.toml
if [ -f "$CONFIG_FILE" ]; then
log "$INFO" "⚙️ Configuring RPC and P2P in $CONFIG_FILE..."

# Update RPC address only in the [rpc] section
sed -i "/\[rpc\]/,/^\[.*\]/ s|^laddr *=.*|laddr = \"tcp://0.0.0.0:$RPC_PORT\"|" "$CONFIG_FILE"

# Update P2P address only in the [p2p] section
sed -i "/\[p2p\]/,/^\[.*\]/ s|^laddr *=.*|laddr = \"tcp://0.0.0.0:$P2P_PORT\"|" "$CONFIG_FILE"
sed -i "/\[p2p\]/,/^\[.*\]/ s|^external_address *=.*|external_address = \"tcp://0.0.0.0:$P2P_PORT\"|" "$CONFIG_FILE"

log "$SUCCESS" "✅ Configured RPC on $RPC_PORT and P2P on $P2P_PORT in $CONFIG_FILE."
else
log "$INFO" "⚠️ $CONFIG_FILE not found. Skipping P2P and RPC configuration."
fi

# Configure gRPC and API in app.toml
if [ -f "$APP_FILE" ]; then
log "$INFO" "⚙️ Configuring gRPC and API in $APP_FILE..."

# Update gRPC address in the [grpc] section
sed -i "/\[grpc\]/,/^\[.*\]/ s|^address *=.*|address = \"localhost:$GRPC_PORT\"|" "$APP_FILE"


log "$SUCCESS" "✅ Configured gRPC on $GRPC_PORT and API enabled in $APP_FILE."
else
log "$INFO" "⚠️ $APP_FILE not found. Skipping gRPC and API configuration."
fi
}


fetch_environment_variables() {
NODE_ENV_NAME=$(echo "${NODE_NAME^^}" | tr '-' '_')
VALIDATOR_MNEMONIC_VAR="${NODE_ENV_NAME}_VALIDATOR_MNEMONIC"
Expand Down Expand Up @@ -115,41 +156,14 @@ collect_and_finalize_genesis() {
log "$SUCCESS" "Finalized genesis.json saved."
}

configure_peers() {
log "$INFO" "Configuring persistent peers..."
PERSISTENT_PEERS=""
for NODE in "${NODE_NAMES[@]}"; do
if [ "$NODE" != "$NODE_NAME" ]; then
PEER_ID=$(cat "$SHARED_DIR/${NODE}_id")
PERSISTENT_PEERS+="$PEER_ID@$NODE:26656,"
fi
done
PERSISTENT_PEERS=${PERSISTENT_PEERS%,}
sed -i "s/^persistent_peers = .*/persistent_peers = \"$PERSISTENT_PEERS\"/" "$HOME_DIR/config/config.toml"
log "$SUCCESS" "Peers configured."
}

# Main Setup Logic
log "$INFO" "Starting setup for ${NODE_NAME}..."

configure_rpc() {
CONFIG_FILE="$HOME_DIR/config/config.toml"
log "$INFO" "Configuring RPC endpoint..."

# Update RPC listener address in config.toml
sed -i 's/^laddr *=.*/laddr = "tcp:\/\/0.0.0.0:26657"/' "$CONFIG_FILE"

# Ensure other RPC parameters are correct
sed -i 's/^grpc_laddr *=.*/grpc_laddr = ""/' "$CONFIG_FILE"
sed -i 's/^rpc_servers *=.*/rpc_servers = ""/' "$CONFIG_FILE"

log "$SUCCESS" "RPC endpoint configured."
}


fetch_environment_variables
initialize_node
save_node_id
configure_rpc_and_p2p

if [ "$NODE_NAME" == "regen-node1" ]; then
jq '.app_state.staking.params.bond_denom = "uregen"' "$HOME_DIR/config/genesis.json" > "$HOME_DIR/config/genesis_tmp.json"
Expand All @@ -171,8 +185,6 @@ else
wait_for_final_genesis
fi

configure_peers
configure_rpc

log "$INFO" "Starting ${NODE_NAME}..."
exec regen start --home "$HOME_DIR" --minimum-gas-prices="0.025uregen"
54 changes: 29 additions & 25 deletions scripts/devnet/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ set -e
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# 🚀 Starting the setup
# 🚀 Start the setup
echo -e "${GREEN}🚀 Starting the Regen network setup...${NC}"

# Check if node count is provided, default to 3
# Check if node count is provided; default to 3
NODE_COUNT=${1:-3}
echo -e "🔢 Setting up $NODE_COUNT nodes."

# 🧹 Clean up existing data directories
echo -e "🧹 Cleaning up existing data directories..."
# 🧹 Clean up existing directories
echo -e "🧹 Cleaning up existing directories..."
rm -rf ./shared ./node*_data .env docker-compose.yaml

# 🛠 Create a temporary directory for key generation
# Temporary directory for key generation
TEMP_DIR=$(mktemp -d)
echo -e "🔑 Generating validator keys in temporary directory: $TEMP_DIR"

NODE_NAMES=()
NODE_ADDRESSES=()
NODE_MNEMONICS=()

# 📦 Generate keys for each node dynamically
# 📦 Generate keys for each node
for i in $(seq 1 "$NODE_COUNT"); do
NODE="regen-node$i"
NODE_NAMES+=("$NODE")
Expand All @@ -35,7 +35,6 @@ for i in $(seq 1 "$NODE_COUNT"); do
mkdir -p "$NODE_HOME"
KEY_OUTPUT=$(regen keys add my_validator --keyring-backend test --home "$NODE_HOME" --output json)

# Extract address and mnemonic
ADDRESS=$(echo "$KEY_OUTPUT" | jq -r '.address')
MNEMONIC=$(echo "$KEY_OUTPUT" | jq -r '.mnemonic')

Expand All @@ -45,33 +44,34 @@ for i in $(seq 1 "$NODE_COUNT"); do
echo -e "📬 Address for ${GREEN}$NODE${NC}: ${ADDRESS}"
done

# 📝 Write the .env file dynamically
# 📝 Write the .env file
echo -e "📝 Writing ${GREEN}.env${NC} file..."
rm -f .env
for i in "${!NODE_NAMES[@]}"; do
NODE="${NODE_NAMES[$i]}"
ADDRESS="${NODE_ADDRESSES[$i]}"
MNEMONIC="${NODE_MNEMONICS[$i]}"

# Replace hyphens with underscores for valid environment variable names
NODE_ENV_NAME=$(echo "${NODE^^}" | tr '-' '_')

echo "${NODE_ENV_NAME}_VALIDATOR_ADDRESS=${ADDRESS}" >> .env
echo "${NODE_ENV_NAME}_VALIDATOR_MNEMONIC=\"${MNEMONIC}\"" >> .env
done

# 📝 Generate the `docker-compose.yaml` file dynamically
# 📝 Generate `docker-compose.yaml`
echo -e "📝 Generating ${GREEN}docker-compose.yaml${NC} file..."
cat <<EOF > docker-compose.yaml
version: '3.8'
services:
EOF

for i in $(seq 1 "$NODE_COUNT"); do
RPC_PORT=$((26657 + (i - 1) * 10))
P2P_PORT=$((26656 + (i - 1) * 10))
NODE="regen-node$i"
# ⚙️ Assign non-overlapping ports for each node
BASE_PORT=26000 # Start from a clean base to avoid collisions
for i in $(seq 0 $((NODE_COUNT - 1))); do
P2P_PORT=$((BASE_PORT + i * 3)) # P2P port
RPC_PORT=$((BASE_PORT + i * 3 + 1)) # RPC port
GRPC_PORT=$((BASE_PORT + i * 3 + 2)) # gRPC port


NODE="regen-node$((i + 1))"

cat <<EOF >> docker-compose.yaml
$NODE:
Expand All @@ -82,23 +82,27 @@ for i in $(seq 1 "$NODE_COUNT"); do
environment:
- NODE_NAME=$NODE
- NODE_COUNT=$NODE_COUNT
- RPC_PORT=$RPC_PORT
- P2P_PORT=$P2P_PORT
- RPC_PORT=$RPC_PORT
- GRPC_PORT=$GRPC_PORT
$(for j in $(seq 1 "$NODE_COUNT"); do
PEER_NODE="regen-node$j"
PEER_ENV=$(echo "${PEER_NODE^^}" | tr '-' '_')
echo " - ${PEER_ENV}_VALIDATOR_ADDRESS=\${${PEER_ENV}_VALIDATOR_ADDRESS}"
echo " - ${PEER_ENV}_VALIDATOR_MNEMONIC=\${${PEER_ENV}_VALIDATOR_MNEMONIC}"
done)
volumes:
- ./shared:/mnt/nvme/shared
- ./shared/node:/mnt/nvme/shared
- ./shared/node$i-conf:/mnt/nvme/.regen
- ./entrypoint.sh:/entrypoint.sh
ports:
- "$P2P_PORT:$P2P_PORT"
- "$RPC_PORT:$RPC_PORT"
entrypoint: ["/bin/bash", "/entrypoint.sh"]
networks:
- regen-network
ports:
- "${RPC_PORT}:${RPC_PORT}"
- ":${P2P_PORT}:${P2P_PORT}"
- ":${P2P_PORT}:${P2P_PORT}"
entrypoint: ["/bin/bash", "/entrypoint.sh"]
EOF
done

Expand All @@ -108,9 +112,9 @@ networks:
driver: bridge
EOF

echo -e "${GREEN}✅ docker-compose.yaml${NC} file generated."
echo -e "${GREEN}✅ docker-compose.yaml${NC} generated."

# 🐳 Start Docker Compose
# 🐳 Start the Docker containers
echo -e "${GREEN}🐳 Starting the Regen network with Docker Compose...${NC}"
docker compose up --build

Expand Down

0 comments on commit ff1781d

Please sign in to comment.