From ff1781de7d51800b5f71ffed82ab4e36f59c5cc7 Mon Sep 17 00:00:00 2001 From: Pablo Estrada Date: Mon, 28 Oct 2024 20:42:06 -0600 Subject: [PATCH] feat: expose port to local host and improve debug logs --- scripts/devnet/entrypoint.sh | 72 +++++++++++++++++++++--------------- scripts/devnet/setup.sh | 54 ++++++++++++++------------- 2 files changed, 71 insertions(+), 55 deletions(-) diff --git a/scripts/devnet/entrypoint.sh b/scripts/devnet/entrypoint.sh index b67ff7717c..be3a613272 100644 --- a/scripts/devnet/entrypoint.sh +++ b/scripts/devnet/entrypoint.sh @@ -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" @@ -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" @@ -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" diff --git a/scripts/devnet/setup.sh b/scripts/devnet/setup.sh index 8809d5c271..cf5f864880 100755 --- a/scripts/devnet/setup.sh +++ b/scripts/devnet/setup.sh @@ -6,18 +6,18 @@ 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" @@ -25,7 +25,7 @@ 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") @@ -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') @@ -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 < 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 <> docker-compose.yaml $NODE: @@ -82,8 +82,9 @@ 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 '-' '_') @@ -91,14 +92,17 @@ $(for j in $(seq 1 "$NODE_COUNT"); do 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 @@ -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